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

@@ -0,0 +1,171 @@
// -----------------------------------------------------------------------------
/// @file gpio.c
/// @brief Description
// -----------------------------------------------------------------------------
// Micro-Key bv
// Industrieweg 28, 9804 TG Noordhorn
// Postbus 92, 9800 AB Zuidhorn
// The Netherlands
// Tel: +31 594 503020
// Fax: +31 594 505825
// Email: support@microkey.nl
// Web: www.microkey.nl
// -----------------------------------------------------------------------------
/// $Revision$
/// $Author$
/// $Date$
// (c) 2017 Micro-Key bv
// -----------------------------------------------------------------------------
/// @file gpio.c
/// @ingroup {group_name}
// -----------------------------------------------------------------------------
// Include files
// -----------------------------------------------------------------------------
#include "stm32f10x_gpio.h"
#include "gpio.h"
#include "Logger.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Type definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// File-scope variables
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
static ErrorStatus write(const struct IODevice* self, const char* buffer, size_t length);
static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength);
// -----------------------------------------------------------------------------
// Function definitions
// -----------------------------------------------------------------------------
ErrorStatus GPIO_construct(struct Gpio* self, GpioDirection direction, T_PL_GPIO io)
{
ErrorStatus returnValue = SUCCESS;
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;
}
static ErrorStatus write(const struct IODevice* self, const char* buffer, size_t length)
{
ErrorStatus returnValue = SUCCESS;
if (buffer[0] != 0)
{
returnValue = GPIO_setValue((struct Gpio*)self, true);
}
else
{
returnValue = GPIO_setValue((struct Gpio*)self, false);
}
return returnValue;
}
static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength)
{
ErrorStatus returnValue = SUCCESS;
bool value;
(void)length;
*actualLength = 1;
returnValue = GPIO_getValue((struct Gpio*)self, &value);
*buffer = (char)value;
return returnValue;
}