Skip to content

Commit

Permalink
New GPIO drivers
Browse files Browse the repository at this point in the history
Implemented GPIO drivers for STM32F2xx and STM32F4xx.
Did several cosmetic changes to driver for STM32F10x.
  • Loading branch information
burjui committed Mar 4, 2012
1 parent 67bfc8f commit 28b208c
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 9 deletions.
18 changes: 9 additions & 9 deletions drivers/hd44780_stm32f10x.c
Expand Up @@ -52,7 +52,9 @@ static HD44780_Result stm32f10x_default_pin_configure(HD44780_GPIO_Interface *dr
gpio_config.GPIO_Mode = GPIO_Mode_IN_FLOATING;
break;

default: HD44780_STM32F10X_ASSERT(0);
default:
HD44780_STM32F10X_ASSERT(0);
break;
}

gpio_config.GPIO_Pin = stm32f10x_pin->pinmask;
Expand All @@ -74,7 +76,10 @@ static HD44780_Result stm32f10x_default_pin_write(HD44780_GPIO_Interface *driver
HD44780_STM32F10X_RETURN_ASSERT(stm32f10x_pin->gpio != NULL, HD44780_RESULT_ERROR);

if (stm32f10x_pin->gpio != NULL)
GPIO_WriteBit(stm32f10x_pin->gpio, stm32f10x_pin->pinmask, (value == HD44780_PINSTATE_LOW ? RESET : SET));
{
GPIO_WriteBit(stm32f10x_pin->gpio, stm32f10x_pin->pinmask,
(value == HD44780_PINSTATE_LOW ? Bit_RESET : Bit_SET));
}

return HD44780_RESULT_OK;
}
Expand All @@ -90,13 +95,8 @@ static HD44780_Result stm32f10x_default_pin_read(HD44780_GPIO_Interface *driver,

HD44780_STM32F10X_RETURN_ASSERT(stm32f10x_pin->gpio != NULL, HD44780_RESULT_ERROR);

if (stm32f10x_pin->gpio != NULL)
{
*value = (GPIO_ReadInputDataBit(stm32f10x_pin->gpio, stm32f10x_pin->pinmask) == RESET ?
HD44780_PINSTATE_LOW : HD44780_PINSTATE_HIGH);
}
else
*value = HD44780_PINSTATE_LOW;
uint8_t out_bit = GPIO_ReadInputDataBit(stm32f10x_pin->gpio, stm32f10x_pin->pinmask);
*value = (out_bit == Bit_RESET ? HD44780_PINSTATE_LOW : HD44780_PINSTATE_HIGH);

return HD44780_RESULT_OK;
}
Expand Down
109 changes: 109 additions & 0 deletions drivers/hd44780_stm32f2xx.c
@@ -0,0 +1,109 @@
#include <stm32f2xx_rcc.h>
#include <stm32f2xx_gpio.h>
#include <stdint.h>
#include <stdlib.h>
#include "hd44780_stm32f2xx.h"


#ifndef NDEBUG
#define HD44780_STM32F2XX_ASSERT(x) \
{ \
if (!(x)) \
{ \
HD44780_STM32F2xx_GPIO_Driver *stm32f2xx_driver = ((HD44780_STM32F2xx_GPIO_Driver*)driver); \
if (stm32f2xx_driver->assert_failure_handler != NULL) \
stm32f2xx_driver->assert_failure_handler(__FILE__, __LINE__); \
} \
}

#define HD44780_STM32F2XX_RETURN_ASSERT(x,ret) \
do { \
int condition = (x); \
HD44780_STM32F2XX_ASSERT(condition) \
if (!condition) \
return (ret); \
} while (0)
#else
#define HD44780_STM32F2XX_ASSERT(x)
#define HD44780_STM32F2XX_RETURN_ASSERT(x,ret)
#endif


static HD44780_Result stm32f2xx_default_pin_configure(HD44780_GPIO_Interface *driver,
HD44780_Pin pin, HD44780_PinMode mode)
{
HD44780_STM32F2XX_RETURN_ASSERT(driver != NULL, HD44780_RESULT_ERROR);

HD44780_STM32F2xx_GPIO_Driver *stm32f2xx_pindriver = (HD44780_STM32F2xx_GPIO_Driver*)driver;
HD44780_STM32F2xx_Pin *stm32f2xx_pin = &stm32f2xx_pindriver->pinout.pins[pin];

HD44780_STM32F2XX_RETURN_ASSERT(stm32f2xx_pin->gpio != NULL, HD44780_RESULT_ERROR);

GPIO_InitTypeDef gpio_config;
GPIO_StructInit(&gpio_config);

switch (mode)
{
case HD44780_PINMODE_OUTPUT:
gpio_config.GPIO_Mode = GPIO_Mode_OUT;
break;

case HD44780_PINMODE_INPUT:
gpio_config.GPIO_Mode = GPIO_Mode_IN;
break;

default:
HD44780_STM32F2XX_ASSERT(0);
break;
}

gpio_config.GPIO_Pin = stm32f2xx_pin->pinmask;

if (stm32f2xx_pin->gpio != NULL)
GPIO_Init(stm32f2xx_pin->gpio, &gpio_config);

return HD44780_RESULT_OK;
}

static HD44780_Result stm32f2xx_default_pin_write(HD44780_GPIO_Interface *driver,
HD44780_Pin pin, HD44780_PinState value)
{
HD44780_STM32F2XX_RETURN_ASSERT(driver != NULL, HD44780_RESULT_ERROR);

HD44780_STM32F2xx_GPIO_Driver *stm32f2xx_pindriver = (HD44780_STM32F2xx_GPIO_Driver*)driver;
HD44780_STM32F2xx_Pin *stm32f2xx_pin = &stm32f2xx_pindriver->pinout.pins[pin];

HD44780_STM32F2XX_RETURN_ASSERT(stm32f2xx_pin->gpio != NULL, HD44780_RESULT_ERROR);

if (stm32f2xx_pin->gpio != NULL)
{
GPIO_WriteBit(stm32f2xx_pin->gpio, stm32f2xx_pin->pinmask,
(value == HD44780_PINSTATE_LOW ? Bit_RESET : Bit_SET));
}

return HD44780_RESULT_OK;
}

static HD44780_Result stm32f2xx_default_pin_read(HD44780_GPIO_Interface *driver,
HD44780_Pin pin, HD44780_PinState *value)
{
HD44780_STM32F2XX_RETURN_ASSERT(driver != NULL, HD44780_RESULT_ERROR);
HD44780_STM32F2XX_RETURN_ASSERT(value != NULL, HD44780_RESULT_ERROR);

HD44780_STM32F2xx_GPIO_Driver *stm32f2xx_pindriver = (HD44780_STM32F2xx_GPIO_Driver*)driver;
HD44780_STM32F2xx_Pin *stm32f2xx_pin = &stm32f2xx_pindriver->pinout.pins[pin];

HD44780_STM32F2XX_RETURN_ASSERT(stm32f2xx_pin->gpio != NULL, HD44780_RESULT_ERROR);

uint8_t out_bit = GPIO_ReadInputDataBit(stm32f2xx_pin->gpio, stm32f2xx_pin->pinmask);
*value = (out_bit == Bit_RESET ? HD44780_PINSTATE_LOW : HD44780_PINSTATE_HIGH);

return HD44780_RESULT_OK;
}

const HD44780_GPIO_Interface HD44780_STM32F2XX_PINDRIVER_INTERFACE =
{
stm32f2xx_default_pin_configure,
stm32f2xx_default_pin_write,
stm32f2xx_default_pin_read
};
37 changes: 37 additions & 0 deletions drivers/hd44780_stm32f2xx.h
@@ -0,0 +1,37 @@
#ifndef HD44780_STM32F2XX_H_
#define HD44780_STM32F2XX_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stm32f2xx_rcc.h>
#include <stm32f2xx_gpio.h>
#include <stdint.h>
#include "hd44780.h"

typedef struct
{
GPIO_TypeDef *gpio;
uint16_t pinmask;
} HD44780_STM32F2xx_Pin;

typedef struct
{
HD44780_STM32F2xx_Pin pins[HD44780_PINS_AMOUNT];
} HD44780_STM32F2xx_Pinout;

typedef struct
{
HD44780_GPIO_Interface interface;
HD44780_STM32F2xx_Pinout pinout;
HD44780_AssertFn assert_failure_handler;
} HD44780_STM32F2xx_GPIO_Driver;

extern const HD44780_GPIO_Interface HD44780_STM32F2XX_PINDRIVER_INTERFACE;

#ifdef __cplusplus
}
#endif

#endif /* HD44780_STM32F2XX_H_ */
109 changes: 109 additions & 0 deletions drivers/hd44780_stm32f4xx.c
@@ -0,0 +1,109 @@
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stdint.h>
#include <stdlib.h>
#include "hd44780_stm32f4xx.h"


#ifndef NDEBUG
#define HD44780_STM32F4XX_ASSERT(x) \
{ \
if (!(x)) \
{ \
HD44780_STM32F4xx_GPIO_Driver *stm32f4xx_driver = ((HD44780_STM32F4xx_GPIO_Driver*)driver); \
if (stm32f4xx_driver->assert_failure_handler != NULL) \
stm32f4xx_driver->assert_failure_handler(__FILE__, __LINE__); \
} \
}

#define HD44780_STM32F4XX_RETURN_ASSERT(x,ret) \
do { \
int condition = (x); \
HD44780_STM32F4XX_ASSERT(condition) \
if (!condition) \
return (ret); \
} while (0)
#else
#define HD44780_STM32F4XX_ASSERT(x)
#define HD44780_STM32F4XX_RETURN_ASSERT(x,ret)
#endif


static HD44780_Result stm32f4xx_default_pin_configure(HD44780_GPIO_Interface *driver,
HD44780_Pin pin, HD44780_PinMode mode)
{
HD44780_STM32F4XX_RETURN_ASSERT(driver != NULL, HD44780_RESULT_ERROR);

HD44780_STM32F4xx_GPIO_Driver *stm32f4xx_pindriver = (HD44780_STM32F4xx_GPIO_Driver*)driver;
HD44780_STM32F4xx_Pin *stm32f4xx_pin = &stm32f4xx_pindriver->pinout.pins[pin];

HD44780_STM32F4XX_RETURN_ASSERT(stm32f4xx_pin->gpio != NULL, HD44780_RESULT_ERROR);

GPIO_InitTypeDef gpio_config;
GPIO_StructInit(&gpio_config);

switch (mode)
{
case HD44780_PINMODE_OUTPUT:
gpio_config.GPIO_Mode = GPIO_Mode_OUT;
break;

case HD44780_PINMODE_INPUT:
gpio_config.GPIO_Mode = GPIO_Mode_IN;
break;

default:
HD44780_STM32F4XX_ASSERT(0);
break;
}

gpio_config.GPIO_Pin = stm32f4xx_pin->pinmask;

if (stm32f4xx_pin->gpio != NULL)
GPIO_Init(stm32f4xx_pin->gpio, &gpio_config);

return HD44780_RESULT_OK;
}

static HD44780_Result stm32f4xx_default_pin_write(HD44780_GPIO_Interface *driver,
HD44780_Pin pin, HD44780_PinState value)
{
HD44780_STM32F4XX_RETURN_ASSERT(driver != NULL, HD44780_RESULT_ERROR);

HD44780_STM32F4xx_GPIO_Driver *stm32f4xx_pindriver = (HD44780_STM32F4xx_GPIO_Driver*)driver;
HD44780_STM32F4xx_Pin *stm32f4xx_pin = &stm32f4xx_pindriver->pinout.pins[pin];

HD44780_STM32F4XX_RETURN_ASSERT(stm32f4xx_pin->gpio != NULL, HD44780_RESULT_ERROR);

if (stm32f4xx_pin->gpio != NULL)
{
GPIO_WriteBit(stm32f4xx_pin->gpio, stm32f4xx_pin->pinmask,
(value == HD44780_PINSTATE_LOW ? Bit_RESET : Bit_SET));
}

return HD44780_RESULT_OK;
}

static HD44780_Result stm32f4xx_default_pin_read(HD44780_GPIO_Interface *driver,
HD44780_Pin pin, HD44780_PinState *value)
{
HD44780_STM32F4XX_RETURN_ASSERT(driver != NULL, HD44780_RESULT_ERROR);
HD44780_STM32F4XX_RETURN_ASSERT(value != NULL, HD44780_RESULT_ERROR);

HD44780_STM32F4xx_GPIO_Driver *stm32f4xx_pindriver = (HD44780_STM32F4xx_GPIO_Driver*)driver;
HD44780_STM32F4xx_Pin *stm32f4xx_pin = &stm32f4xx_pindriver->pinout.pins[pin];

HD44780_STM32F4XX_RETURN_ASSERT(stm32f4xx_pin->gpio != NULL, HD44780_RESULT_ERROR);

uint8_t out_bit = GPIO_ReadInputDataBit(stm32f4xx_pin->gpio, stm32f4xx_pin->pinmask);
*value = (out_bit == Bit_RESET ? HD44780_PINSTATE_LOW : HD44780_PINSTATE_HIGH);

return HD44780_RESULT_OK;
}

const HD44780_GPIO_Interface HD44780_STM32F4XX_PINDRIVER_INTERFACE =
{
stm32f4xx_default_pin_configure,
stm32f4xx_default_pin_write,
stm32f4xx_default_pin_read
};
37 changes: 37 additions & 0 deletions drivers/hd44780_stm32f4xx.h
@@ -0,0 +1,37 @@
#ifndef HD44780_STM32F4XX_H_
#define HD44780_STM32F4XX_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stdint.h>
#include "hd44780.h"

typedef struct
{
GPIO_TypeDef *gpio;
uint16_t pinmask;
} HD44780_STM32F4xx_Pin;

typedef struct
{
HD44780_STM32F4xx_Pin pins[HD44780_PINS_AMOUNT];
} HD44780_STM32F4xx_Pinout;

typedef struct
{
HD44780_GPIO_Interface interface;
HD44780_STM32F4xx_Pinout pinout;
HD44780_AssertFn assert_failure_handler;
} HD44780_STM32F4xx_GPIO_Driver;

extern const HD44780_GPIO_Interface HD44780_STM32F4XX_PINDRIVER_INTERFACE;

#ifdef __cplusplus
}
#endif

#endif /* HD44780_STM32F4XX_H_ */

0 comments on commit 28b208c

Please sign in to comment.