git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@242 05563f52-14a8-4384-a975-3d1654cca0fa
192 lines
5.0 KiB
C
192 lines
5.0 KiB
C
// -----------------------------------------------------------------------------
|
|
/// @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;
|
|
if (!self->initialized)
|
|
{
|
|
returnValue = IODevice_construct(&self->device, read, write);
|
|
if (returnValue == SUCCESS)
|
|
{
|
|
self->direction = direction;
|
|
self->gpio = io;
|
|
self->initialized = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
returnValue = ERROR;
|
|
}
|
|
|
|
|
|
return returnValue;
|
|
}
|
|
|
|
|
|
ErrorStatus GPIO_setValue(struct Gpio* self, bool value)
|
|
{
|
|
ErrorStatus returnValue = SUCCESS;
|
|
if (self->initialized)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
returnValue = ERROR;
|
|
}
|
|
return returnValue;
|
|
}
|
|
|
|
|
|
ErrorStatus GPIO_getValue(struct Gpio* self, bool* value)
|
|
{
|
|
ErrorStatus returnValue = SUCCESS;
|
|
if (self->initialized)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
returnValue = ERROR;
|
|
}
|
|
|
|
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;
|
|
}
|