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