mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F334R8/gpio_irq_api.c@285:31249416b6f9, 2014-08-15 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 15 16:30:08 2014 +0100
- Revision:
- 285:31249416b6f9
- Parent:
- 251:de9a1e4ffd79
Synchronized with git revision 601712595f49bbd2a2771c52cf3e84c9c7a591af
Full URL: https://github.com/mbedmicro/mbed/commit/601712595f49bbd2a2771c52cf3e84c9c7a591af/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 237:f3da66175598 | 1 | /* mbed Microcontroller Library |
mbed_official | 237:f3da66175598 | 2 | ******************************************************************************* |
mbed_official | 237:f3da66175598 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 237:f3da66175598 | 4 | * All rights reserved. |
mbed_official | 237:f3da66175598 | 5 | * |
mbed_official | 237:f3da66175598 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 237:f3da66175598 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 237:f3da66175598 | 8 | * |
mbed_official | 237:f3da66175598 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 237:f3da66175598 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 237:f3da66175598 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 237:f3da66175598 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 237:f3da66175598 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 237:f3da66175598 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 237:f3da66175598 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 237:f3da66175598 | 16 | * without specific prior written permission. |
mbed_official | 237:f3da66175598 | 17 | * |
mbed_official | 237:f3da66175598 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 237:f3da66175598 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 237:f3da66175598 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 237:f3da66175598 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 237:f3da66175598 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 237:f3da66175598 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 237:f3da66175598 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 237:f3da66175598 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 237:f3da66175598 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 237:f3da66175598 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 237:f3da66175598 | 28 | ******************************************************************************* |
mbed_official | 237:f3da66175598 | 29 | */ |
mbed_official | 237:f3da66175598 | 30 | #include <stddef.h> |
mbed_official | 237:f3da66175598 | 31 | #include "cmsis.h" |
mbed_official | 237:f3da66175598 | 32 | #include "gpio_irq_api.h" |
mbed_official | 237:f3da66175598 | 33 | #include "pinmap.h" |
mbed_official | 285:31249416b6f9 | 34 | #include "mbed_error.h" |
mbed_official | 237:f3da66175598 | 35 | |
mbed_official | 237:f3da66175598 | 36 | #define EDGE_NONE (0) |
mbed_official | 237:f3da66175598 | 37 | #define EDGE_RISE (1) |
mbed_official | 237:f3da66175598 | 38 | #define EDGE_FALL (2) |
mbed_official | 237:f3da66175598 | 39 | #define EDGE_BOTH (3) |
mbed_official | 237:f3da66175598 | 40 | |
mbed_official | 237:f3da66175598 | 41 | #define CHANNEL_NUM (7) |
mbed_official | 237:f3da66175598 | 42 | |
mbed_official | 237:f3da66175598 | 43 | static uint32_t channel_ids[CHANNEL_NUM] = {0, 0, 0, 0, 0, 0, 0}; |
mbed_official | 237:f3da66175598 | 44 | static uint32_t channel_gpio[CHANNEL_NUM] = {0, 0, 0, 0, 0, 0, 0}; |
mbed_official | 237:f3da66175598 | 45 | static uint32_t channel_pin[CHANNEL_NUM] = {0, 0, 0, 0, 0, 0, 0}; |
mbed_official | 237:f3da66175598 | 46 | |
mbed_official | 237:f3da66175598 | 47 | static gpio_irq_handler irq_handler; |
mbed_official | 237:f3da66175598 | 48 | |
mbed_official | 237:f3da66175598 | 49 | static void handle_interrupt_in(uint32_t irq_index) |
mbed_official | 237:f3da66175598 | 50 | { |
mbed_official | 237:f3da66175598 | 51 | // Retrieve the gpio and pin that generate the irq |
mbed_official | 237:f3da66175598 | 52 | GPIO_TypeDef *gpio = (GPIO_TypeDef *)(channel_gpio[irq_index]); |
mbed_official | 237:f3da66175598 | 53 | uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]); |
mbed_official | 237:f3da66175598 | 54 | |
mbed_official | 237:f3da66175598 | 55 | // Clear interrupt flag |
mbed_official | 237:f3da66175598 | 56 | if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) { |
mbed_official | 237:f3da66175598 | 57 | __HAL_GPIO_EXTI_CLEAR_FLAG(pin); |
mbed_official | 237:f3da66175598 | 58 | } |
mbed_official | 237:f3da66175598 | 59 | |
mbed_official | 237:f3da66175598 | 60 | if (channel_ids[irq_index] == 0) return; |
mbed_official | 237:f3da66175598 | 61 | |
mbed_official | 237:f3da66175598 | 62 | // Check which edge has generated the irq |
mbed_official | 237:f3da66175598 | 63 | if ((gpio->IDR & pin) == 0) { |
mbed_official | 237:f3da66175598 | 64 | irq_handler(channel_ids[irq_index], IRQ_FALL); |
mbed_official | 237:f3da66175598 | 65 | } else { |
mbed_official | 237:f3da66175598 | 66 | irq_handler(channel_ids[irq_index], IRQ_RISE); |
mbed_official | 237:f3da66175598 | 67 | } |
mbed_official | 237:f3da66175598 | 68 | } |
mbed_official | 237:f3da66175598 | 69 | |
mbed_official | 237:f3da66175598 | 70 | // The irq_index is passed to the function |
mbed_official | 237:f3da66175598 | 71 | // EXTI line 0 |
mbed_official | 237:f3da66175598 | 72 | static void gpio_irq0(void) |
mbed_official | 237:f3da66175598 | 73 | { |
mbed_official | 237:f3da66175598 | 74 | handle_interrupt_in(0); |
mbed_official | 237:f3da66175598 | 75 | } |
mbed_official | 237:f3da66175598 | 76 | // EXTI line 1 |
mbed_official | 237:f3da66175598 | 77 | static void gpio_irq1(void) |
mbed_official | 237:f3da66175598 | 78 | { |
mbed_official | 237:f3da66175598 | 79 | handle_interrupt_in(1); |
mbed_official | 237:f3da66175598 | 80 | } |
mbed_official | 237:f3da66175598 | 81 | // EXTI line 2 |
mbed_official | 237:f3da66175598 | 82 | static void gpio_irq2(void) |
mbed_official | 237:f3da66175598 | 83 | { |
mbed_official | 237:f3da66175598 | 84 | handle_interrupt_in(2); |
mbed_official | 237:f3da66175598 | 85 | } |
mbed_official | 237:f3da66175598 | 86 | // EXTI line 3 |
mbed_official | 237:f3da66175598 | 87 | static void gpio_irq3(void) |
mbed_official | 237:f3da66175598 | 88 | { |
mbed_official | 237:f3da66175598 | 89 | handle_interrupt_in(3); |
mbed_official | 237:f3da66175598 | 90 | } |
mbed_official | 237:f3da66175598 | 91 | // EXTI line 4 |
mbed_official | 237:f3da66175598 | 92 | static void gpio_irq4(void) |
mbed_official | 237:f3da66175598 | 93 | { |
mbed_official | 237:f3da66175598 | 94 | handle_interrupt_in(4); |
mbed_official | 237:f3da66175598 | 95 | } |
mbed_official | 237:f3da66175598 | 96 | // EXTI lines 5 to 9 |
mbed_official | 237:f3da66175598 | 97 | static void gpio_irq5(void) |
mbed_official | 237:f3da66175598 | 98 | { |
mbed_official | 237:f3da66175598 | 99 | handle_interrupt_in(5); |
mbed_official | 237:f3da66175598 | 100 | } |
mbed_official | 237:f3da66175598 | 101 | // EXTI lines 10 to 15 |
mbed_official | 237:f3da66175598 | 102 | static void gpio_irq6(void) |
mbed_official | 237:f3da66175598 | 103 | { |
mbed_official | 237:f3da66175598 | 104 | handle_interrupt_in(6); |
mbed_official | 237:f3da66175598 | 105 | } |
mbed_official | 237:f3da66175598 | 106 | |
mbed_official | 237:f3da66175598 | 107 | extern uint32_t Set_GPIO_Clock(uint32_t port_idx); |
mbed_official | 237:f3da66175598 | 108 | |
mbed_official | 237:f3da66175598 | 109 | int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) |
mbed_official | 237:f3da66175598 | 110 | { |
mbed_official | 237:f3da66175598 | 111 | IRQn_Type irq_n = (IRQn_Type)0; |
mbed_official | 237:f3da66175598 | 112 | uint32_t vector = 0; |
mbed_official | 237:f3da66175598 | 113 | uint32_t irq_index; |
mbed_official | 237:f3da66175598 | 114 | |
mbed_official | 237:f3da66175598 | 115 | if (pin == NC) return -1; |
mbed_official | 237:f3da66175598 | 116 | |
mbed_official | 237:f3da66175598 | 117 | uint32_t port_index = STM_PORT(pin); |
mbed_official | 237:f3da66175598 | 118 | uint32_t pin_index = STM_PIN(pin); |
mbed_official | 237:f3da66175598 | 119 | |
mbed_official | 237:f3da66175598 | 120 | // Select irq number and interrupt routine |
mbed_official | 237:f3da66175598 | 121 | switch (pin_index) { |
mbed_official | 237:f3da66175598 | 122 | case 0: |
mbed_official | 237:f3da66175598 | 123 | irq_n = EXTI0_IRQn; |
mbed_official | 237:f3da66175598 | 124 | vector = (uint32_t)&gpio_irq0; |
mbed_official | 237:f3da66175598 | 125 | irq_index = 0; |
mbed_official | 237:f3da66175598 | 126 | break; |
mbed_official | 237:f3da66175598 | 127 | case 1: |
mbed_official | 237:f3da66175598 | 128 | irq_n = EXTI1_IRQn; |
mbed_official | 237:f3da66175598 | 129 | vector = (uint32_t)&gpio_irq1; |
mbed_official | 237:f3da66175598 | 130 | irq_index = 1; |
mbed_official | 237:f3da66175598 | 131 | break; |
mbed_official | 237:f3da66175598 | 132 | case 2: |
mbed_official | 237:f3da66175598 | 133 | irq_n = EXTI2_TSC_IRQn; |
mbed_official | 237:f3da66175598 | 134 | vector = (uint32_t)&gpio_irq2; |
mbed_official | 237:f3da66175598 | 135 | irq_index = 2; |
mbed_official | 237:f3da66175598 | 136 | break; |
mbed_official | 237:f3da66175598 | 137 | case 3: |
mbed_official | 237:f3da66175598 | 138 | irq_n = EXTI3_IRQn; |
mbed_official | 237:f3da66175598 | 139 | vector = (uint32_t)&gpio_irq3; |
mbed_official | 237:f3da66175598 | 140 | irq_index = 3; |
mbed_official | 237:f3da66175598 | 141 | break; |
mbed_official | 237:f3da66175598 | 142 | case 4: |
mbed_official | 237:f3da66175598 | 143 | irq_n = EXTI4_IRQn; |
mbed_official | 237:f3da66175598 | 144 | vector = (uint32_t)&gpio_irq4; |
mbed_official | 237:f3da66175598 | 145 | irq_index = 4; |
mbed_official | 237:f3da66175598 | 146 | break; |
mbed_official | 237:f3da66175598 | 147 | case 5: |
mbed_official | 237:f3da66175598 | 148 | case 6: |
mbed_official | 237:f3da66175598 | 149 | case 7: |
mbed_official | 237:f3da66175598 | 150 | case 8: |
mbed_official | 237:f3da66175598 | 151 | case 9: |
mbed_official | 237:f3da66175598 | 152 | irq_n = EXTI9_5_IRQn; |
mbed_official | 237:f3da66175598 | 153 | vector = (uint32_t)&gpio_irq5; |
mbed_official | 237:f3da66175598 | 154 | irq_index = 5; |
mbed_official | 237:f3da66175598 | 155 | break; |
mbed_official | 237:f3da66175598 | 156 | case 10: |
mbed_official | 237:f3da66175598 | 157 | case 11: |
mbed_official | 237:f3da66175598 | 158 | case 12: |
mbed_official | 237:f3da66175598 | 159 | case 13: |
mbed_official | 237:f3da66175598 | 160 | case 14: |
mbed_official | 237:f3da66175598 | 161 | case 15: |
mbed_official | 237:f3da66175598 | 162 | irq_n = EXTI15_10_IRQn; |
mbed_official | 237:f3da66175598 | 163 | vector = (uint32_t)&gpio_irq6; |
mbed_official | 237:f3da66175598 | 164 | irq_index = 6; |
mbed_official | 237:f3da66175598 | 165 | break; |
mbed_official | 237:f3da66175598 | 166 | default: |
mbed_official | 237:f3da66175598 | 167 | error("InterruptIn error: pin not supported.\n"); |
mbed_official | 237:f3da66175598 | 168 | return -1; |
mbed_official | 237:f3da66175598 | 169 | } |
mbed_official | 237:f3da66175598 | 170 | |
mbed_official | 237:f3da66175598 | 171 | // Enable GPIO clock |
mbed_official | 237:f3da66175598 | 172 | uint32_t gpio_add = Set_GPIO_Clock(port_index); |
mbed_official | 237:f3da66175598 | 173 | |
mbed_official | 237:f3da66175598 | 174 | // Configure GPIO |
mbed_official | 237:f3da66175598 | 175 | pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0)); |
mbed_official | 237:f3da66175598 | 176 | |
mbed_official | 237:f3da66175598 | 177 | // Enable EXTI interrupt |
mbed_official | 237:f3da66175598 | 178 | NVIC_SetVector(irq_n, vector); |
mbed_official | 237:f3da66175598 | 179 | NVIC_EnableIRQ(irq_n); |
mbed_official | 237:f3da66175598 | 180 | |
mbed_official | 237:f3da66175598 | 181 | // Save informations for future use |
mbed_official | 237:f3da66175598 | 182 | obj->irq_n = irq_n; |
mbed_official | 237:f3da66175598 | 183 | obj->irq_index = irq_index; |
mbed_official | 237:f3da66175598 | 184 | obj->event = EDGE_NONE; |
mbed_official | 237:f3da66175598 | 185 | obj->pin = pin; |
mbed_official | 237:f3da66175598 | 186 | channel_ids[irq_index] = id; |
mbed_official | 237:f3da66175598 | 187 | channel_gpio[irq_index] = gpio_add; |
mbed_official | 237:f3da66175598 | 188 | channel_pin[irq_index] = pin_index; |
mbed_official | 237:f3da66175598 | 189 | |
mbed_official | 237:f3da66175598 | 190 | irq_handler = handler; |
mbed_official | 237:f3da66175598 | 191 | |
mbed_official | 237:f3da66175598 | 192 | return 0; |
mbed_official | 237:f3da66175598 | 193 | } |
mbed_official | 237:f3da66175598 | 194 | |
mbed_official | 237:f3da66175598 | 195 | void gpio_irq_free(gpio_irq_t *obj) |
mbed_official | 237:f3da66175598 | 196 | { |
mbed_official | 237:f3da66175598 | 197 | channel_ids[obj->irq_index] = 0; |
mbed_official | 237:f3da66175598 | 198 | channel_gpio[obj->irq_index] = 0; |
mbed_official | 237:f3da66175598 | 199 | channel_pin[obj->irq_index] = 0; |
mbed_official | 237:f3da66175598 | 200 | // Disable EXTI line |
mbed_official | 237:f3da66175598 | 201 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 237:f3da66175598 | 202 | obj->event = EDGE_NONE; |
mbed_official | 237:f3da66175598 | 203 | } |
mbed_official | 237:f3da66175598 | 204 | |
mbed_official | 237:f3da66175598 | 205 | void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) |
mbed_official | 237:f3da66175598 | 206 | { |
mbed_official | 237:f3da66175598 | 207 | uint32_t mode = STM_MODE_INPUT; |
mbed_official | 237:f3da66175598 | 208 | uint32_t pull = GPIO_NOPULL; |
mbed_official | 237:f3da66175598 | 209 | |
mbed_official | 237:f3da66175598 | 210 | if (enable) { |
mbed_official | 237:f3da66175598 | 211 | |
mbed_official | 237:f3da66175598 | 212 | pull = GPIO_NOPULL; |
mbed_official | 237:f3da66175598 | 213 | |
mbed_official | 237:f3da66175598 | 214 | if (event == IRQ_RISE) { |
mbed_official | 237:f3da66175598 | 215 | if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) { |
mbed_official | 237:f3da66175598 | 216 | mode = STM_MODE_IT_RISING_FALLING; |
mbed_official | 237:f3da66175598 | 217 | obj->event = EDGE_BOTH; |
mbed_official | 237:f3da66175598 | 218 | } else { // NONE or RISE |
mbed_official | 237:f3da66175598 | 219 | mode = STM_MODE_IT_RISING; |
mbed_official | 237:f3da66175598 | 220 | obj->event = EDGE_RISE; |
mbed_official | 237:f3da66175598 | 221 | } |
mbed_official | 237:f3da66175598 | 222 | } |
mbed_official | 237:f3da66175598 | 223 | |
mbed_official | 237:f3da66175598 | 224 | if (event == IRQ_FALL) { |
mbed_official | 237:f3da66175598 | 225 | if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) { |
mbed_official | 237:f3da66175598 | 226 | mode = STM_MODE_IT_RISING_FALLING; |
mbed_official | 237:f3da66175598 | 227 | obj->event = EDGE_BOTH; |
mbed_official | 237:f3da66175598 | 228 | } else { // NONE or FALL |
mbed_official | 237:f3da66175598 | 229 | mode = STM_MODE_IT_FALLING; |
mbed_official | 237:f3da66175598 | 230 | obj->event = EDGE_FALL; |
mbed_official | 237:f3da66175598 | 231 | } |
mbed_official | 237:f3da66175598 | 232 | } |
mbed_official | 237:f3da66175598 | 233 | } else { |
mbed_official | 237:f3da66175598 | 234 | mode = STM_MODE_INPUT; |
mbed_official | 237:f3da66175598 | 235 | pull = GPIO_NOPULL; |
mbed_official | 237:f3da66175598 | 236 | obj->event = EDGE_NONE; |
mbed_official | 237:f3da66175598 | 237 | } |
mbed_official | 237:f3da66175598 | 238 | |
mbed_official | 237:f3da66175598 | 239 | pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0)); |
mbed_official | 237:f3da66175598 | 240 | } |
mbed_official | 237:f3da66175598 | 241 | |
mbed_official | 237:f3da66175598 | 242 | void gpio_irq_enable(gpio_irq_t *obj) |
mbed_official | 237:f3da66175598 | 243 | { |
mbed_official | 237:f3da66175598 | 244 | NVIC_EnableIRQ(obj->irq_n); |
mbed_official | 237:f3da66175598 | 245 | } |
mbed_official | 237:f3da66175598 | 246 | |
mbed_official | 237:f3da66175598 | 247 | void gpio_irq_disable(gpio_irq_t *obj) |
mbed_official | 237:f3da66175598 | 248 | { |
mbed_official | 237:f3da66175598 | 249 | NVIC_DisableIRQ(obj->irq_n); |
mbed_official | 237:f3da66175598 | 250 | obj->event = EDGE_NONE; |
mbed_official | 237:f3da66175598 | 251 | } |