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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -32,7 +32,6 @@
|
||||
|
||||
#include "uart.h"
|
||||
#include "misc.h"
|
||||
#include "led.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
|
||||
Reference in New Issue
Block a user