Replaced LED with more generic class GPIO

git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@233 05563f52-14a8-4384-a975-3d1654cca0fa
This commit is contained in:
mmi
2017-10-04 14:18:33 +00:00
parent 8b315602e9
commit cf0435c273
9 changed files with 172 additions and 93 deletions

View File

@@ -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 \

View File

@@ -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 <stdbool.h>
#include <stdbool.h>
#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_ */

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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

View File

@@ -32,7 +32,6 @@
#include "uart.h"
#include "misc.h"
#include "led.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions