From cf0435c273c9e0d949c93342a903fdbf2d96fff2 Mon Sep 17 00:00:00 2001 From: mmi Date: Wed, 4 Oct 2017 14:18:33 +0000 Subject: [PATCH] Replaced LED with more generic class GPIO git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@233 05563f52-14a8-4384-a975-3d1654cca0fa --- .../0 - Code/Platform/Makefile | 2 +- .../0 - Code/Platform/inc/{led.h => gpio.h} | 77 +++++++---- .../0 - Code/Platform/inc/platform.h | 4 +- .../0 - Code/Platform/src/{led.c => gpio.c} | 126 +++++++++++++----- .../0 - Code/Platform/src/oli_stm32_h107.c | 34 ++--- .../0 - Code/Platform/src/uart.c | 1 - .../0 - Code/hsb-mrts/src/FreeRTOSFixes.c | 2 - .../0 - Code/hsb-mrts/src/main.c | 18 +-- .../0 - Code/hsb-mrts/src/stm32f10x_it.c | 1 - 9 files changed, 172 insertions(+), 93 deletions(-) rename S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/{led.h => gpio.h} (51%) rename S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/{led.c => gpio.c} (53%) diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/Makefile b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/Makefile index 11c7e5d..64bf919 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/Makefile +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/Makefile @@ -25,9 +25,9 @@ ARFLAGS = rs OBJECTS = \ stm32f10x_it.o \ adc.o \ +gpio.o \ IODevice.o \ keypadMatrix.o \ -led.o \ oli_stm32_h107.o \ PCBA.o \ rtc.o \ diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/led.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/gpio.h similarity index 51% rename from S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/led.h rename to S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/gpio.h index a3a9fba..fb34888 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/led.h +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/gpio.h @@ -1,6 +1,5 @@ - // ----------------------------------------------------------------------------- -/// @file led.h +/// @file gpio.h /// @brief File description // ----------------------------------------------------------------------------- // Micro-Key bv @@ -15,28 +14,26 @@ /// $Revision$ /// $Author$ /// $Date$ -// (c) 2017 Micro-Key bv +// (c) 2015 Micro-Key bv // ----------------------------------------------------------------------------- /// @defgroup {group_name} {group_description} /// Description -/// @file led.h +/// @file gpio.h /// @ingroup {group_name} - -#ifndef LED_INC_LED_H_ -#define LED_INC_LED_H_ +#ifndef INC_GPIO_H_ +#define INC_GPIO_H_ // ----------------------------------------------------------------------------- // Include files // ----------------------------------------------------------------------------- -#include +#include #include "platform.h" #include "IODevice.h" - // ----------------------------------------------------------------------------- // Constant and macro definitions // ----------------------------------------------------------------------------- @@ -44,13 +41,20 @@ // ----------------------------------------------------------------------------- -// Type definitions. +// Type definitions. // ----------------------------------------------------------------------------- -struct Led +typedef enum +{ + INPUT = 0, + OUTPUT = !INPUT +}GpioDirection; + +struct Gpio { struct IODevice device; - T_PL_GPIO ledGpio; + T_PL_GPIO gpio; + GpioDirection direction; bool status; }; @@ -59,34 +63,57 @@ struct Led // ----------------------------------------------------------------------------- -ErrorStatus LED_construct (struct Led* self); - /** ---------------------------------------------------------------------------- - * LED_turnOn - * Turns on the LED identified with the ID + * GPIO_construct + * Constructs a GPIO as IODevice * - * @param ledID Unique identifier of the LED + * @param self The GPIO instance + * @param direction Direction of the GPIO + * - INPUT or OUTPUT + * @param io The Input/Output to use * - * @return ErrorStatus SUCCESS if init was successful + * @return ErrorStatus SUCCESS if construction was successful * ERROR otherwise * * @todo * ----------------------------------------------------------------------------- */ -extern ErrorStatus LED_turnOn(struct Led* led); +extern ErrorStatus GPIO_construct(struct Gpio* self, GpioDirection direction, T_PL_GPIO io); + /** ---------------------------------------------------------------------------- - * LED_turnOff - * Turns off the LED identified with the ID + * GPIO_setValue + * Sets value to GPIO self * - * @param ledID Unique identifier of the LED + * @param self The GPIO instance + * @param value the value to use + * 0 => Output LOW + * 1 => Output HIGH * - * @return ErrorStatus SUCCESS if init was successful + * @return ErrorStatus SUCCESS if construction was successful * ERROR otherwise * * @todo * ----------------------------------------------------------------------------- */ -extern ErrorStatus LED_turnOff(struct Led* led); +extern ErrorStatus GPIO_setValue(struct Gpio* self, bool value); -#endif /* LED_INC_LED_H_ */ + +/** ---------------------------------------------------------------------------- + * GPIO_getValue + * Gets value from GPIO self + * + * @param self The GPIO instance + * @param value the value that has been read + * 0 => Output LOW + * 1 => Output HIGH + * + * @return ErrorStatus SUCCESS if construction was successful + * ERROR otherwise + * + * @todo + * ----------------------------------------------------------------------------- + */ +extern ErrorStatus GPIO_getValue(struct Gpio* self, bool* value); + +#endif /* INC_GPIO_H_ */ diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/platform.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/platform.h index 3268a6a..bc14a38 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/platform.h +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/platform.h @@ -64,8 +64,8 @@ typedef struct extern struct Pcba* pcba; // Export of LEDs -extern struct Led* const ledGreen; -extern struct Led* const ledOrange; +extern struct Gpio* const ledGreen; +extern struct Gpio* const ledOrange; // Export of ADCs extern struct Adc* const adc1; // Export of the rtc diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/led.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/gpio.c similarity index 53% rename from S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/led.c rename to S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/gpio.c index a742cef..70d5af9 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/led.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/gpio.c @@ -1,5 +1,5 @@ // ----------------------------------------------------------------------------- -/// @file led.c +/// @file gpio.c /// @brief Description // ----------------------------------------------------------------------------- // Micro-Key bv @@ -17,7 +17,7 @@ // (c) 2017 Micro-Key bv // ----------------------------------------------------------------------------- -/// @file led.c +/// @file gpio.c /// @ingroup {group_name} @@ -25,8 +25,10 @@ // Include files // ----------------------------------------------------------------------------- -#include "led.h" +#include "stm32f10x_gpio.h" +#include "gpio.h" +#include "Logger.h" // ----------------------------------------------------------------------------- // Constant and macro definitions @@ -39,7 +41,6 @@ // ----------------------------------------------------------------------------- - // ----------------------------------------------------------------------------- // File-scope variables // ----------------------------------------------------------------------------- @@ -58,11 +59,83 @@ static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length // ----------------------------------------------------------------------------- -ErrorStatus LED_construct (struct Led* self) +ErrorStatus GPIO_construct(struct Gpio* self, GpioDirection direction, T_PL_GPIO io) { ErrorStatus returnValue = SUCCESS; - IODevice_construct(&self->device, read, write); + + returnValue = IODevice_construct(&self->device, read, write); + self->direction = direction; + self->gpio = io; + + + return returnValue; +} + + +ErrorStatus GPIO_setValue(struct Gpio* self, bool value) +{ + ErrorStatus returnValue = SUCCESS; + + if (self->direction == OUTPUT) + { + // Writing to output is valid + if (value) + { + GPIO_SetBits(self->gpio.GPIO_Typedef, self->gpio.GPIO_InitStruct.GPIO_Pin); + self->status = true; + } + else + { + { + GPIO_ResetBits(self->gpio.GPIO_Typedef, self->gpio.GPIO_InitStruct.GPIO_Pin); + self->status = false; + } + } + } + else + { + // Writing to input is invalid + returnValue = ERROR; + } + + return returnValue; +} + + +ErrorStatus GPIO_getValue(struct Gpio* self, bool* value) +{ + ErrorStatus returnValue = SUCCESS; + + if (self->direction == OUTPUT) + { + // Reading an output is impossible - but returning its current status is valid + if(GPIO_ReadOutputDataBit(self->gpio.GPIO_Typedef, self->gpio.GPIO_InitStruct.GPIO_Pin) != 0) + { + *value = true; + self->status = true; + } + else + { + *value = false; + self->status = false; + } + } + else + { + // Read value on input + if(GPIO_ReadInputDataBit(self->gpio.GPIO_Typedef, self->gpio.GPIO_InitStruct.GPIO_Pin) != 0) + { + + *value = true; + self->status = true; + } + else + { + *value = false; + self->status = false; + } + } return returnValue; } @@ -70,46 +143,29 @@ ErrorStatus LED_construct (struct Led* self) static ErrorStatus write(const struct IODevice* self, const char* buffer, size_t length) { - (void)length; - + ErrorStatus returnValue = SUCCESS; if (buffer[0] != 0) { - return LED_turnOn((struct Led*)self); + returnValue = GPIO_setValue((struct Gpio*)self, true); } else { - return LED_turnOff((struct Led*)self); + returnValue = GPIO_setValue((struct Gpio*)self, false); } + + return returnValue; } + + static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength) { - struct Led* led = (struct Led*)self; + ErrorStatus returnValue = SUCCESS; + bool value; + (void)length; *actualLength = 1; - *buffer = (char)led->status; - return SUCCESS; -} - - -ErrorStatus LED_turnOn(struct Led* led) -{ - ErrorStatus returnValue = SUCCESS; - - GPIO_SetBits(led->ledGpio.GPIO_Typedef, led->ledGpio.GPIO_InitStruct.GPIO_Pin); - led->status = true; + returnValue = GPIO_getValue((struct Gpio*)self, &value); + *buffer = (char)value; return returnValue; } - - -ErrorStatus LED_turnOff(struct Led* const led) -{ - ErrorStatus returnValue = SUCCESS; - - GPIO_ResetBits(led->ledGpio.GPIO_Typedef, led->ledGpio.GPIO_InitStruct.GPIO_Pin); - led->status = false; - - return returnValue; -} - - diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/oli_stm32_h107.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/oli_stm32_h107.c index d3e48a7..b37c270 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/oli_stm32_h107.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/oli_stm32_h107.c @@ -36,7 +36,7 @@ #include "platform.h" #include "adc.h" -#include "led.h" +#include "gpio.h" #include "PCBA.h" #include "rtc.h" #include "spi.h" @@ -87,8 +87,8 @@ // PCBA information // LEDs -static struct Led _ledGreen; -static struct Led _ledOrange; +static struct Gpio _ledGreen; +static struct Gpio _ledOrange; // ADC static struct Adc _adc1; @@ -121,8 +121,8 @@ static struct KeypadParameters _keypadParameters; // Note that the pointer content is marked "const" struct Pcba* pcba; -struct Led* const ledGreen = &_ledGreen; -struct Led* const ledOrange = &_ledOrange; +struct Gpio* const ledGreen = &_ledGreen; +struct Gpio* const ledOrange = &_ledOrange; struct Adc* const adc1 = &_adc1; struct AdcParameters* adc1Parameters = &_adc1Parameters; @@ -187,8 +187,8 @@ ErrorStatus initPlatform(void) /* --------------------------------------------------------------------*/ /* LEDs */ /* --------------------------------------------------------------------*/ - LED_construct(ledGreen); - LED_construct(ledOrange); + GPIO_construct(ledGreen, OUTPUT, ledGreen->gpio); + GPIO_construct(ledOrange, OUTPUT, ledOrange->gpio); /* --------------------------------------------------------------------*/ /* DMA1 - Channel 1 - For use with ADC1 */ @@ -437,18 +437,18 @@ static ErrorStatus initIO (void) /*LED IO initialisation --------------------------------------------------*/ // Init LED Green - ledGreen->ledGpio.GPIO_Typedef = GPIOC; - ledGreen->ledGpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; - ledGreen->ledGpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; - ledGreen->ledGpio.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(ledGreen->ledGpio.GPIO_Typedef, &ledGreen->ledGpio.GPIO_InitStruct); + ledGreen->gpio.GPIO_Typedef = GPIOC; + ledGreen->gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; + ledGreen->gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; + ledGreen->gpio.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(ledGreen->gpio.GPIO_Typedef, &ledGreen->gpio.GPIO_InitStruct); // Init LED Orange - ledOrange->ledGpio.GPIO_Typedef = GPIOC; - ledOrange->ledGpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; - ledOrange->ledGpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7; - ledOrange->ledGpio.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(ledOrange->ledGpio.GPIO_Typedef, &ledOrange->ledGpio.GPIO_InitStruct); + ledOrange->gpio.GPIO_Typedef = GPIOC; + ledOrange->gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; + ledOrange->gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7; + ledOrange->gpio.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(ledOrange->gpio.GPIO_Typedef, &ledOrange->gpio.GPIO_InitStruct); /* ADC1 initialisation ---------------------------------------------------*/ // Channel 0 - PA0 diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/uart.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/uart.c index 10df3de..c7ebbde 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/uart.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/uart.c @@ -32,7 +32,6 @@ #include "uart.h" #include "misc.h" -#include "led.h" // ----------------------------------------------------------------------------- // Constant and macro definitions diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/FreeRTOSFixes.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/FreeRTOSFixes.c index a5de711..2bbc94d 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/FreeRTOSFixes.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/FreeRTOSFixes.c @@ -31,7 +31,6 @@ #include #include "Logger.h" -#include "led.h" // ----------------------------------------------------------------------------- // Constant and macro definitions @@ -95,7 +94,6 @@ caddr_t _sbrk(int incr) // Stack overflow hook void vApplicationStackOverflowHook(xTaskHandle xTask, signed portCHAR* pcTaskName) { - LED_turnOn(ledGreen); LOGGER_ERROR("STACK OVERFLOW IN TASK %s", pcTaskName); } diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c index 18416aa..9971d95 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c @@ -45,9 +45,9 @@ #include "platform.h" #include "adc.h" +#include "gpio.h" #include "IODevice.h" #include "keypadMatrix.h" -#include "led.h" #include "PCBA.h" #include "uart.h" #include "spi.h" @@ -68,7 +68,7 @@ tick hook. */ struct LedTaskArguments { - struct Led* led; + struct Gpio* led; int frequency; }; @@ -158,8 +158,8 @@ static ErrorStatus systeminfoCommandHandler(void) OS_logTaskInfo(ledTaskHandle); vTaskDelay(100); OS_logTaskInfo(sysTaskHandle); - vTaskDelay(100); - OS_logTaskInfo(display->taskHandle); +// vTaskDelay(100); +// OS_logTaskInfo(display->taskHandle); return errorStatus; } @@ -168,9 +168,9 @@ static void initTask(void* parameters) { initPlatform(); - xTaskCreate(ledBlinkTask, (const char* const)"ledTask", 40, &ledTaskArguments, 0, &ledTaskHandle); + xTaskCreate(ledBlinkTask, (const char* const)"ledTask", 50, &ledTaskArguments, 0, &ledTaskHandle); - Logger_construct(&uart3->device); + Logger_construct(&uart1->device); NHD0420_construct(&nhd0420, &spiDisplay->device); @@ -203,13 +203,13 @@ static void ledBlinkTask (void* parameters) char high = 1; char low = 0; struct LedTaskArguments* arguments = (struct LedTaskArguments*) parameters; - struct Led* led = arguments->led; + struct Gpio* gpio = arguments->led; int frequency = arguments->frequency; while (1) { - IODevice_write(&led->device, &high, 1); + IODevice_write(&gpio->device, &high, 1); vTaskDelay(configTICK_RATE_HZ / (frequency * 2)); - IODevice_write(&led->device, &low, 1); + IODevice_write(&gpio->device, &low, 1); vTaskDelay(configTICK_RATE_HZ / (frequency * 2)); } } diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/stm32f10x_it.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/stm32f10x_it.c index 2f64919..ae2aa6e 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/stm32f10x_it.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/stm32f10x_it.c @@ -39,7 +39,6 @@ #include "Display.h" #include "Logger.h" -#include "led.h" #include "platform.h" #include "rtc.h" #include "spi.h"