Permalink
Browse files

New GPIO drivers

Implemented GPIO drivers for STM32F2xx and STM32F4xx.
Did several cosmetic changes to driver for STM32F10x.
  • Loading branch information...
1 parent 67bfc8f commit 28b208c05609af58f8ec6cc37ccc135415adb822 @burjui burjui committed Mar 4, 2012
Showing with 301 additions and 9 deletions.
  1. +9 −9 drivers/hd44780_stm32f10x.c
  2. +109 −0 drivers/hd44780_stm32f2xx.c
  3. +37 −0 drivers/hd44780_stm32f2xx.h
  4. +109 −0 drivers/hd44780_stm32f4xx.c
  5. +37 −0 drivers/hd44780_stm32f4xx.h
@@ -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;
@@ -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;
}
@@ -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;
}
View
@@ -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
+};
@@ -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_ */
View
@@ -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
+};
@@ -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.