Blame view

RIOT/cpu/stm32_common/periph/gpio.c 6.46 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  /*
   * Copyright (C) 2014-2015 Freie Universitรคt Berlin
   *               2015 Hamburg University of Applied Sciences
   *               2017 Inria
   *               2017 OTA keys S.A.
   *
   * This file is subject to the terms and conditions of the GNU Lesser General
   * Public License v2.1. See the file LICENSE in the top level directory for more
   * details.
   */
  
  /**
   * @ingroup     cpu_stm32_common
   * @ingroup     drivers_periph_gpio
   * @{
   *
   * @file
   * @brief       Low-level GPIO driver implementation
   *
   * @author      Hauke Petersen <mail@haukepetersen.de>
   * @author      Fabian Nack <nack@inf.fu-berlin.de>
   * @author      Alexandre Abadie <alexandre.abadie@inria.fr>
   * @author      Katja Kirstein <katja.kirstein@haw-hamburg.de>
   * @author      Vincent Dupont <vincent@otakeys.com>
   *
   * @}
   */
  
  #ifndef CPU_FAM_STM32F1
  
  #include "cpu.h"
  #include "periph/gpio.h"
  #include "periph_conf.h"
  
  /**
   * @brief   The STM32F0 family has 16 external interrupt lines
   */
  #define EXTI_NUMOF          (16U)
  
  /**
   * @brief   Allocate memory for one callback and argument per EXTI channel
   */
  static gpio_isr_ctx_t isr_ctx[EXTI_NUMOF];
  
  /**
   * @brief   Extract the port base address from the given pin identifier
   */
  static inline GPIO_TypeDef *_port(gpio_t pin)
  {
      return (GPIO_TypeDef *)(pin & ~(0x0f));
  }
  
  /**
   * @brief   Extract the port number form the given identifier
   *
   * The port number is extracted by looking at bits 10, 11, 12, 13 of the base
   * register addresses.
   */
  static inline int _port_num(gpio_t pin)
  {
      return ((pin >> 10) & 0x0f);
  }
  
  /**
   * @brief   Extract the pin number from the last 4 bit of the pin identifier
   */
  static inline int _pin_num(gpio_t pin)
  {
      return (pin & 0x0f);
  }
  
  int gpio_init(gpio_t pin, gpio_mode_t mode)
  {
      GPIO_TypeDef *port = _port(pin);
      int pin_num = _pin_num(pin);
  
      /* enable clock */
  #if defined(CPU_FAM_STM32F0) || defined (CPU_FAM_STM32F3) || defined(CPU_FAM_STM32L1)
      periph_clk_en(AHB, (RCC_AHBENR_GPIOAEN << _port_num(pin)));
  #elif defined (CPU_FAM_STM32L0)
      periph_clk_en(IOP, (RCC_IOPENR_GPIOAEN << _port_num(pin)));
  #elif defined (CPU_FAM_STM32L4)
      periph_clk_en(AHB2, (RCC_AHB2ENR_GPIOAEN << _port_num(pin)));
  #else
      periph_clk_en(AHB1, (RCC_AHB1ENR_GPIOAEN << _port_num(pin)));
  #endif
  
      /* set mode */
      port->MODER &= ~(0x3 << (2 * pin_num));
      port->MODER |=  ((mode & 0x3) << (2 * pin_num));
      /* set pull resistor configuration */
      port->PUPDR &= ~(0x3 << (2 * pin_num));
      port->PUPDR |=  (((mode >> 2) & 0x3) << (2 * pin_num));
      /* set output mode */
      port->OTYPER &= ~(1 << pin_num);
      port->OTYPER |=  (((mode >> 4) & 0x1) << pin_num);
      /* set pin speed to maximum */
      port->OSPEEDR |= (3 << (2 * pin_num));
  
      return 0;
  }
  
  int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
                    gpio_cb_t cb, void *arg)
  {
      int pin_num = _pin_num(pin);
      int port_num = _port_num(pin);
  
      /* set callback */
      isr_ctx[pin_num].cb = cb;
      isr_ctx[pin_num].arg = arg;
  
      /* enable clock of the SYSCFG module for EXTI configuration */
  #ifdef CPU_FAN_STM32F0
      periph_clk_en(APB2, RCC_APB2ENR_SYSCFGCOMPEN);
  #else
      periph_clk_en(APB2, RCC_APB2ENR_SYSCFGEN);
  #endif
  
      /* initialize pin as input */
      gpio_init(pin, mode);
  
      /* enable global pin interrupt */
  #if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32L0)
      if (pin_num < 2) {
          NVIC_EnableIRQ(EXTI0_1_IRQn);
      }
      else if (pin_num < 4) {
          NVIC_EnableIRQ(EXTI2_3_IRQn);
      }
      else {
          NVIC_EnableIRQ(EXTI4_15_IRQn);
      }
  #else
      if (pin_num < 5) {
          NVIC_EnableIRQ(EXTI0_IRQn + pin_num);
      }
      else if (pin_num < 10) {
          NVIC_EnableIRQ(EXTI9_5_IRQn);
      }
      else {
          NVIC_EnableIRQ(EXTI15_10_IRQn);
      }
  #endif
      /* configure the active flank */
      EXTI->RTSR &= ~(1 << pin_num);
      EXTI->RTSR |=  ((flank & 0x1) << pin_num);
      EXTI->FTSR &= ~(1 << pin_num);
      EXTI->FTSR |=  ((flank >> 1) << pin_num);
      /* enable specific pin as exti sources */
      SYSCFG->EXTICR[pin_num >> 2] &= ~(0xf << ((pin_num & 0x03) * 4));
      SYSCFG->EXTICR[pin_num >> 2] |= (port_num << ((pin_num & 0x03) * 4));
  
      /* clear any pending requests */
      EXTI->PR = (1 << pin);
      /* unmask the pins interrupt channel */
      EXTI->IMR |= (1 << pin);
  
      return 0;
  }
  
  void gpio_init_af(gpio_t pin, gpio_af_t af)
  {
      GPIO_TypeDef *port = _port(pin);
      uint32_t pin_num = _pin_num(pin);
  
      /* set pin to AF mode */
      port->MODER &= ~(3 << (2 * pin_num));
      port->MODER |= (2 << (2 * pin_num));
      /* set selected function */
      port->AFR[(pin_num > 7) ? 1 : 0] &= ~(0xf << ((pin_num & 0x07) * 4));
      port->AFR[(pin_num > 7) ? 1 : 0] |= (af << ((pin_num & 0x07) * 4));
  }
  
  void gpio_init_analog(gpio_t pin)
  {
      /* enable clock, needed as this function can be used without calling
       * gpio_init first */
  #if defined(CPU_FAM_STM32F0) || defined (CPU_FAM_STM32F3) || defined(CPU_FAM_STM32L1)
      periph_clk_en(AHB, (RCC_AHBENR_GPIOAEN << _port_num(pin)));
  #elif defined (CPU_FAM_STM32L0)
      periph_clk_en(IOP, (RCC_IOPENR_GPIOAEN << _port_num(pin)));
  #elif defined (CPU_FAM_STM32L4)
      periph_clk_en(AHB2, (RCC_AHB2ENR_GPIOAEN << _port_num(pin)));
  #else
      periph_clk_en(AHB1, (RCC_AHB1ENR_GPIOAEN << _port_num(pin)));
  #endif
      /* set to analog mode */
      _port(pin)->MODER |= (0x3 << (2 * _pin_num(pin)));
  }
  
  void gpio_irq_enable(gpio_t pin)
  {
      EXTI->IMR |= (1 << _pin_num(pin));
  }
  
  void gpio_irq_disable(gpio_t pin)
  {
      EXTI->IMR &= ~(1 << _pin_num(pin));
  }
  
  int gpio_read(gpio_t pin)
  {
      if (_port(pin)->MODER & (0x3 << (_pin_num(pin) * 2))) {
          return _port(pin)->ODR & (1 << _pin_num(pin));
      }
      else {
          return _port(pin)->IDR & (1 << _pin_num(pin));
      }
  }
  
  void gpio_set(gpio_t pin)
  {
      _port(pin)->BSRR = (1 << _pin_num(pin));
  }
  
  void gpio_clear(gpio_t pin)
  {
      _port(pin)->BSRR = (1 << (_pin_num(pin) + 16));
  }
  
  void gpio_toggle(gpio_t pin)
  {
      if (gpio_read(pin)) {
          gpio_clear(pin);
      } else {
          gpio_set(pin);
      }
  }
  
  void gpio_write(gpio_t pin, int value)
  {
      if (value) {
          gpio_set(pin);
      } else {
          gpio_clear(pin);
      }
  }
  
  void isr_exti(void)
  {
      /* only generate interrupts against lines which have their IMR set */
      uint32_t pending_isr = (EXTI->PR & EXTI->IMR);
      for (size_t i = 0; i < EXTI_NUMOF; i++) {
          if (pending_isr & (1 << i)) {
              EXTI->PR = (1 << i);        /* clear by writing a 1 */
              isr_ctx[i].cb(isr_ctx[i].arg);
          }
      }
      cortexm_isr_end();
  }
  
  #else
  typedef int dont_be_pedantic;
  #endif