gpio_irq_api.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* mbed Microcontroller Library
  2. *******************************************************************************
  3. * Copyright (c) 2014, STMicroelectronics
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *******************************************************************************
  29. */
  30. #include <stddef.h>
  31. #include "cmsis.h"
  32. #include "gpio_irq_api.h"
  33. #include "pinmap.h"
  34. #include "mbed_error.h"
  35. #include "gpio_irq_device.h"
  36. #define EDGE_NONE (0)
  37. #define EDGE_RISE (1)
  38. #define EDGE_FALL (2)
  39. #define EDGE_BOTH (3)
  40. typedef struct gpio_channel {
  41. uint32_t pin_mask; // bitmask representing which pins are configured for receiving interrupts
  42. uint32_t channel_ids[MAX_PIN_LINE]; // mbed "gpio_irq_t gpio_irq" field of instance
  43. GPIO_TypeDef *channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
  44. uint32_t channel_pin[MAX_PIN_LINE]; // pin number in port group
  45. } gpio_channel_t;
  46. static gpio_irq_handler irq_handler;
  47. static gpio_channel_t channels[CHANNEL_NUM] = {
  48. #ifdef EXTI_IRQ0_NUM_LINES
  49. {.pin_mask = 0},
  50. #endif
  51. #ifdef EXTI_IRQ1_NUM_LINES
  52. {.pin_mask = 0},
  53. #endif
  54. #ifdef EXTI_IRQ2_NUM_LINES
  55. {.pin_mask = 0},
  56. #endif
  57. #ifdef EXTI_IRQ3_NUM_LINES
  58. {.pin_mask = 0},
  59. #endif
  60. #ifdef EXTI_IRQ4_NUM_LINES
  61. {.pin_mask = 0},
  62. #endif
  63. #ifdef EXTI_IRQ5_NUM_LINES
  64. {.pin_mask = 0},
  65. #endif
  66. #ifdef EXTI_IRQ6_NUM_LINES
  67. {.pin_mask = 0}
  68. #endif
  69. };
  70. static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
  71. {
  72. gpio_channel_t *gpio_channel = &channels[irq_index];
  73. uint32_t gpio_idx;
  74. for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
  75. uint32_t current_mask = (1 << gpio_idx);
  76. if (gpio_channel->pin_mask & current_mask) {
  77. // Retrieve the gpio and pin that generate the irq
  78. GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
  79. uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
  80. // Clear interrupt flag
  81. if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
  82. __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
  83. if (gpio_channel->channel_ids[gpio_idx] == 0) {
  84. continue;
  85. }
  86. // Trying to discern which edge caused the IRQ
  87. gpio_irq_event event = IRQ_NONE;
  88. if (LL_EXTI_IsEnabledFallingTrig_0_31(pin) && !LL_EXTI_IsEnabledRisingTrig_0_31(pin)) {
  89. // Only the fall handler is active, so this must be a falling edge
  90. event = IRQ_FALL;
  91. } else if (LL_EXTI_IsEnabledRisingTrig_0_31(pin) && !LL_EXTI_IsEnabledFallingTrig_0_31(pin)) {
  92. // Only the rise handler is active, so this must be a rising edge
  93. event = IRQ_RISE;
  94. } else {
  95. // Ambiguous as to which edge caused the IRQ
  96. //
  97. // The state of the pin could/should indicate which edge
  98. // has occurred but this can go wrong if the IRQ caused a
  99. // transition from a low power mode. In some circumstances
  100. // only the trailing edge callback will be called.
  101. if ((gpio->IDR & pin) == 0) {
  102. event = IRQ_FALL;
  103. } else {
  104. event = IRQ_RISE;
  105. }
  106. }
  107. irq_handler(gpio_channel->channel_ids[gpio_idx], event);
  108. return;
  109. }
  110. }
  111. }
  112. error("Unexpected Spurious interrupt, index %d\r\n", irq_index);
  113. }
  114. #ifdef EXTI_IRQ0_NUM_LINES
  115. // EXTI line 0
  116. static void gpio_irq0(void)
  117. {
  118. handle_interrupt_in(0, EXTI_IRQ0_NUM_LINES);
  119. }
  120. #endif
  121. #ifdef EXTI_IRQ1_NUM_LINES
  122. // EXTI line 1
  123. static void gpio_irq1(void)
  124. {
  125. handle_interrupt_in(1, EXTI_IRQ1_NUM_LINES);
  126. }
  127. #endif
  128. #ifdef EXTI_IRQ2_NUM_LINES
  129. // EXTI line 2
  130. static void gpio_irq2(void)
  131. {
  132. handle_interrupt_in(2, EXTI_IRQ2_NUM_LINES);
  133. }
  134. #endif
  135. #ifdef EXTI_IRQ3_NUM_LINES
  136. // EXTI line 3
  137. static void gpio_irq3(void)
  138. {
  139. handle_interrupt_in(3, EXTI_IRQ3_NUM_LINES);
  140. }
  141. #endif
  142. #ifdef EXTI_IRQ4_NUM_LINES
  143. // EXTI line 4
  144. static void gpio_irq4(void)
  145. {
  146. handle_interrupt_in(4, EXTI_IRQ4_NUM_LINES);
  147. }
  148. #endif
  149. #ifdef EXTI_IRQ5_NUM_LINES
  150. // EXTI lines 5 to 9
  151. static void gpio_irq5(void)
  152. {
  153. handle_interrupt_in(5, EXTI_IRQ5_NUM_LINES);
  154. }
  155. #endif
  156. #ifdef EXTI_IRQ6_NUM_LINES
  157. // EXTI lines 10 to 15
  158. static void gpio_irq6(void)
  159. {
  160. handle_interrupt_in(6, EXTI_IRQ6_NUM_LINES);
  161. }
  162. #endif
  163. extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
  164. extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode);
  165. int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
  166. {
  167. uint32_t vector = 0;
  168. uint32_t irq_index;
  169. gpio_channel_t *gpio_channel;
  170. uint32_t gpio_idx;
  171. if (pin == NC) {
  172. return -1;
  173. }
  174. /* Enable SYSCFG Clock */
  175. __HAL_RCC_SYSCFG_CLK_ENABLE();
  176. uint32_t port_index = STM_PORT(pin);
  177. uint32_t pin_index = STM_PIN(pin);
  178. irq_index = pin_lines_desc[pin_index].irq_index;
  179. switch (irq_index) {
  180. #ifdef EXTI_IRQ0_NUM_LINES
  181. case 0:
  182. vector = (uint32_t)&gpio_irq0;
  183. break;
  184. #endif
  185. #ifdef EXTI_IRQ1_NUM_LINES
  186. case 1:
  187. vector = (uint32_t)&gpio_irq1;
  188. break;
  189. #endif
  190. #ifdef EXTI_IRQ2_NUM_LINES
  191. case 2:
  192. vector = (uint32_t)&gpio_irq2;
  193. break;
  194. #endif
  195. #ifdef EXTI_IRQ3_NUM_LINES
  196. case 3:
  197. vector = (uint32_t)&gpio_irq3;
  198. break;
  199. #endif
  200. #ifdef EXTI_IRQ4_NUM_LINES
  201. case 4:
  202. vector = (uint32_t)&gpio_irq4;
  203. break;
  204. #endif
  205. #ifdef EXTI_IRQ5_NUM_LINES
  206. case 5:
  207. vector = (uint32_t)&gpio_irq5;
  208. break;
  209. #endif
  210. #ifdef EXTI_IRQ6_NUM_LINES
  211. case 6:
  212. vector = (uint32_t)&gpio_irq6;
  213. break;
  214. #endif
  215. default:
  216. error("InterruptIn error: pin not supported.\n");
  217. return -1;
  218. }
  219. // Enable GPIO clock
  220. GPIO_TypeDef *gpio_add = Set_GPIO_Clock(port_index);
  221. // Save informations for future use
  222. obj->irq_n = pin_lines_desc[pin_index].irq_n;
  223. obj->irq_index = pin_lines_desc[pin_index].irq_index;
  224. obj->event = EDGE_NONE;
  225. obj->pin = pin;
  226. gpio_channel = &channels[irq_index];
  227. gpio_idx = pin_lines_desc[pin_index].gpio_idx;
  228. gpio_channel->pin_mask |= (1 << gpio_idx);
  229. gpio_channel->channel_ids[gpio_idx] = id;
  230. gpio_channel->channel_gpio[gpio_idx] = gpio_add;
  231. gpio_channel->channel_pin[gpio_idx] = pin_index;
  232. irq_handler = handler;
  233. // Enable EXTI interrupt
  234. NVIC_SetVector(obj->irq_n, vector);
  235. gpio_irq_enable(obj);
  236. return 0;
  237. }
  238. void gpio_irq_free(gpio_irq_t *obj)
  239. {
  240. uint32_t gpio_idx = pin_lines_desc[STM_PIN(obj->pin)].gpio_idx;
  241. gpio_channel_t *gpio_channel = &channels[obj->irq_index];
  242. gpio_irq_disable(obj);
  243. gpio_channel->pin_mask &= ~(1 << gpio_idx);
  244. gpio_channel->channel_ids[gpio_idx] = 0;
  245. gpio_channel->channel_gpio[gpio_idx] = 0;
  246. gpio_channel->channel_pin[gpio_idx] = 0;
  247. }
  248. void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
  249. {
  250. /* Enable / Disable Edge triggered interrupt and store event */
  251. if (event == IRQ_RISE) {
  252. if (enable) {
  253. LL_EXTI_EnableRisingTrig_0_31(1 << STM_PIN(obj->pin));
  254. obj->event |= IRQ_RISE;
  255. } else {
  256. LL_EXTI_DisableRisingTrig_0_31(1 << STM_PIN(obj->pin));
  257. obj->event &= ~IRQ_RISE;
  258. }
  259. }
  260. if (event == IRQ_FALL) {
  261. if (enable) {
  262. LL_EXTI_EnableFallingTrig_0_31(1 << STM_PIN(obj->pin));
  263. obj->event |= IRQ_FALL;
  264. } else {
  265. LL_EXTI_DisableFallingTrig_0_31(1 << STM_PIN(obj->pin));
  266. obj->event &= ~IRQ_FALL;
  267. }
  268. }
  269. }
  270. void gpio_irq_enable(gpio_irq_t *obj)
  271. {
  272. uint32_t temp = 0;
  273. uint32_t port_index = STM_PORT(obj->pin);
  274. uint32_t pin_index = STM_PIN(obj->pin);
  275. /* Select Source */
  276. temp = SYSCFG->EXTICR[pin_index >> 2];
  277. CLEAR_BIT(temp, (0x0FU) << (4U * (pin_index & 0x03U)));
  278. SET_BIT(temp, port_index << (4U * (pin_index & 0x03U)));
  279. SYSCFG->EXTICR[pin_index >> 2] = temp;
  280. LL_EXTI_EnableIT_0_31(1 << pin_index);
  281. /* Restore previous edge interrupt configuration if applicable */
  282. if (obj->event & IRQ_RISE) {
  283. LL_EXTI_EnableRisingTrig_0_31(1 << STM_PIN(obj->pin));
  284. }
  285. if (obj->event & IRQ_FALL) {
  286. LL_EXTI_EnableFallingTrig_0_31(1 << STM_PIN(obj->pin));
  287. }
  288. NVIC_EnableIRQ(obj->irq_n);
  289. }
  290. void gpio_irq_disable(gpio_irq_t *obj)
  291. {
  292. /* Clear EXTI line configuration */
  293. LL_EXTI_DisableRisingTrig_0_31(1 << STM_PIN(obj->pin));
  294. LL_EXTI_DisableFallingTrig_0_31(1 << STM_PIN(obj->pin));
  295. LL_EXTI_DisableIT_0_31(1 << STM_PIN(obj->pin));
  296. NVIC_DisableIRQ(obj->irq_n);
  297. NVIC_ClearPendingIRQ(obj->irq_n);
  298. }