ARMEBS4  revision-26.06.2015
stm32_gpio.c
Go to the documentation of this file.
1 /**
2  * \file heivs/stm32_gpio.c
3  */
4 
5 #include "heivs/stm32_gpio.h"
6 #include "stm32/stm32f4xx_gpio.h"
7 #include "stm32/stm32f4xx_rcc.h"
8 
9 status_e gpio_setup(const struct gpio_t *gpio)
10 {
11  /* Handle the GPIO_NC case */
12  if (gpio->ctrl == GPIO_NC_CTRL)
13  {
14  return NO_ERROR;
15  }
16  RCC_AHB1PeriphClockCmd(1 << gpio->ctrl, ENABLE);
17 
19  uint32_t mode = gpio->mode;
20  uint32_t type = (mode >> _GPIO_TYPE_SHIFT) & _GPIO_TYPE_MASK;
21  uint32_t out_value = (mode >> _GPIO_OUT_SHIFT) & _GPIO_OUT_MASK;
22  uint32_t drive = (mode >> _GPIO_DRIVE_SHIFT) & _GPIO_DRIVE_MASK;
23  uint32_t speed = (mode >> _GPIO_SPEED_SHIFT) & _GPIO_SPEED_MASK;
24  uint32_t pull = (mode >> _GPIO_PULL_SHIFT) & _GPIO_PULL_MASK;
25  uint32_t af_nr = (mode >> _GPIO_AF_NR_SHIFT) & _GPIO_AF_NR_MASK;
26 
27  gpio_set(gpio, out_value);
28 
29  GPIOx->AFR[gpio->nr >> 0x03] &=~ (0xF << ((gpio->nr & 0x07) * 4));
30  GPIOx->AFR[gpio->nr >> 0x03] |= (af_nr << ((gpio->nr & 0x07) * 4));
31 
32  GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (gpio->nr * 2));
33  GPIOx->MODER |= type << (gpio->nr * 2);
34 
35  GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (gpio->nr * 2));
36  GPIOx->OSPEEDR |= speed << (gpio->nr * 2);
37 
38  GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)gpio->nr)) ;
39  GPIOx->OTYPER |= drive << gpio->nr;
40 
41  GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)gpio->nr * 2));
42  GPIOx->PUPDR |= pull << (gpio->nr * 2);
43 
44  return NO_ERROR;
45 }
46 
47 status_e gpio_setup_list(const struct gpio_t gpio[], size_t len)
48 {
49  size_t i;
50  status_e status;
51 
52  for (i = 0 ; i < len ; i++)
53  {
54  status = gpio_setup(&gpio[i]);
55  if (status != NO_ERROR)
56  {
57  return status;
58  }
59  }
60 
61  return NO_ERROR;
62 }
63 
This file contains all the functions prototypes for the RCC firmware library.
This file contains all the functions prototypes for the GPIO firmware library.
__IO uint32_t MODER
Definition: stm32f4xx.h:645
uint8_t nr
Pin number (0 for GPIOx0, ... , 3 for GPIOx3, ...)
Definition: stm32_gpio.h:99
uint8_t ctrl
Controller number (0 for GPIOA, 1 for GPIOB, ...)
Definition: stm32_gpio.h:98
__IO uint32_t OTYPER
Definition: stm32f4xx.h:646
enum gpio_mode_e mode
mode for instance GPIO_INPUT | GPIO_PULLDOWN
Definition: stm32_gpio.h:101
__IO uint32_t OSPEEDR
Definition: stm32f4xx.h:647
static void gpio_set(const struct gpio_t *gpio, uint32_t value)
Set a gpio.
Definition: stm32_gpio.h:260
static GPIO_TypeDef * _internal_gpio_get_ctrl(const struct gpio_t *gpio)
Get the gpio controller given the gpio struct.
Definition: stm32_gpio.h:175
General Purpose I/O.
Definition: stm32f4xx.h:643
__IO uint32_t PUPDR
Definition: stm32f4xx.h:648
__IO uint32_t AFR[2]
Definition: stm32f4xx.h:654
board specific defines
GPIO control structure.
Definition: stm32_gpio.h:96
status_e gpio_setup_list(const struct gpio_t gpio[], size_t len)
Setup an array of gpio.
Definition: stm32_gpio.c:47
status_e gpio_setup(const struct gpio_t *gpio)
Setup a gpio.
Definition: stm32_gpio.c:9
status_e
Known errors.
Definition: error.h:21
No error.
Definition: error.h:28