From: arighi Date: Tue, 4 May 2010 08:55:13 +0000 (+0000) Subject: STM32: add GPIO driver. X-Git-Tag: 2.5.0~302 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=e4f77ad0d0b668029e0400412ea8628bc005a583;p=bertos.git STM32: add GPIO driver. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3606 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/cpu/cortex-m3/drv/gpio_stm32.c b/bertos/cpu/cortex-m3/drv/gpio_stm32.c new file mode 100644 index 00000000..f15145a4 --- /dev/null +++ b/bertos/cpu/cortex-m3/drv/gpio_stm32.c @@ -0,0 +1,107 @@ +/** + * \file + * + * + * \brief STM32 GPIO control interface. + * + * \author Andrea Righi + */ + +#include +#include +#include +#include "gpio_stm32.h" + +/** + * Configure a GPIO pin + * + * \param base Base address of the GPIO port + * \param pins Bit-packed representation of the pin(s) + * \param mode Pin(s) configuration mode + * \param speed Output drive speed + * + * Return 0 on success, otherwise a negative value. + */ +int stm32_gpioPinConfig(struct stm32_gpio *base, + uint16_t pins, uint8_t mode, uint8_t speed) +{ + uint32_t reg_mode = mode & 0x0f; + int i; + + if (mode & 0x10) + reg_mode |= speed; + + if (pins & 0xff) + { + uint32_t reg = base->CRL; + + for (i = 0; i < 8; i++) + { + uint32_t pos = 1 << i; + + if (pins & pos) + { + pos = i << 2; + reg &= ~(0x0f << pos); + reg |= reg_mode << pos; + + if (mode == GPIO_MODE_IPD) + base->BRR = 0x01 << i; + if (mode == GPIO_MODE_IPU) + base->BSRR = 0x01 << i; + } + } + base->CRL = reg; + } + if (pins > 0xff) + { + uint32_t reg = base->CRH; + + for (i = 0; i < 8; i++) + { + uint32_t pos = 1 << (i + 8); + + if (pins & pos) + { + pos = i << 2; + reg &= ~(0x0f << pos); + reg |= reg_mode << pos; + + if (mode == GPIO_MODE_IPD) + base->BRR = 0x01 << (i + 8); + if (mode == GPIO_MODE_IPU) + base->BSRR = 0x01 << (i + 8); + } + } + base->CRH = reg; + } + return 0; +} diff --git a/bertos/cpu/cortex-m3/drv/gpio_stm32.h b/bertos/cpu/cortex-m3/drv/gpio_stm32.h new file mode 100644 index 00000000..4d09db47 --- /dev/null +++ b/bertos/cpu/cortex-m3/drv/gpio_stm32.h @@ -0,0 +1,102 @@ +/** + * \file + * + * + * \brief STM32 GPIO control interface. + */ + +#ifndef GPIO_STM32_H +#define GPIO_STM32_H + +#include + +/* GPIO configuration registers structure */ +struct stm32_gpio +{ + reg32_t CRL; + reg32_t CRH; + reg32_t IDR; + reg32_t ODR; + reg32_t BSRR; + reg32_t BRR; + reg32_t LCKR; +}; + +/** + * GPIO mode + */ +/*\{*/ +enum +{ + GPIO_MODE_AIN = 0x0, + GPIO_MODE_IN_FLOATING = 0x04, + GPIO_MODE_IPD = 0x28, + GPIO_MODE_IPU = 0x48, + GPIO_MODE_OUT_OD = 0x14, + GPIO_MODE_OUT_PP = 0x10, + GPIO_MODE_AF_OD = 0x1C, + GPIO_MODE_AF_PP = 0x18, +}; +/*\}*/ + +/** + * GPIO speed + */ +/*\{*/ +enum +{ + GPIO_SPEED_10MHZ = 1, + GPIO_SPEED_2MHZ, + GPIO_SPEED_50MHZ, +}; +/*\}*/ + +/* Write a value to the specified pin(s) */ +INLINE void +stm32_gpioPinWrite(struct stm32_gpio *base, uint32_t pins, uint8_t val) +{ + if (val) + base->BSRR |= pins; + else + base->BRR |= pins; +} + +/* Read a value from the specified pin(s) */ +INLINE uint8_t stm32_gpioPinRead(struct stm32_gpio *base, uint32_t pins) +{ + return !!(base->IDR & pins); +} + +/* Initialize a GPIO peripheral configuration */ +int stm32_gpioPinConfig(struct stm32_gpio *base, + uint16_t pins, uint8_t mode, uint8_t speed); + +#endif /* GPIO_STM32_H */