Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: SANFAN_read_analog_value nucleo-wdg Nucleo_sleep_copy
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/gpio_irq_api.c@402:09075a3b15e3, 2014-11-14 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Nov 14 09:00:07 2014 +0000
- Revision:
- 402:09075a3b15e3
- Parent:
- 331:098575c6d2c8
- Child:
- 413:ee28a0bed4ad
Synchronized with git revision dffeedc96e731e30785ca4a7842fb9769412bea1
Full URL: https://github.com/mbedmicro/mbed/commit/dffeedc96e731e30785ca4a7842fb9769412bea1/
Targets: add USBTX and USBRX pin definitions for targets that don't provide them
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 181:a4cbdfbbd2f4 | 1 | /* mbed Microcontroller Library |
| mbed_official | 181:a4cbdfbbd2f4 | 2 | ******************************************************************************* |
| mbed_official | 181:a4cbdfbbd2f4 | 3 | * Copyright (c) 2014, STMicroelectronics |
| mbed_official | 181:a4cbdfbbd2f4 | 4 | * All rights reserved. |
| mbed_official | 181:a4cbdfbbd2f4 | 5 | * |
| mbed_official | 181:a4cbdfbbd2f4 | 6 | * Redistribution and use in source and binary forms, with or without |
| mbed_official | 181:a4cbdfbbd2f4 | 7 | * modification, are permitted provided that the following conditions are met: |
| mbed_official | 181:a4cbdfbbd2f4 | 8 | * |
| mbed_official | 181:a4cbdfbbd2f4 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
| mbed_official | 181:a4cbdfbbd2f4 | 10 | * this list of conditions and the following disclaimer. |
| mbed_official | 181:a4cbdfbbd2f4 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| mbed_official | 181:a4cbdfbbd2f4 | 12 | * this list of conditions and the following disclaimer in the documentation |
| mbed_official | 181:a4cbdfbbd2f4 | 13 | * and/or other materials provided with the distribution. |
| mbed_official | 181:a4cbdfbbd2f4 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| mbed_official | 181:a4cbdfbbd2f4 | 15 | * may be used to endorse or promote products derived from this software |
| mbed_official | 181:a4cbdfbbd2f4 | 16 | * without specific prior written permission. |
| mbed_official | 181:a4cbdfbbd2f4 | 17 | * |
| mbed_official | 181:a4cbdfbbd2f4 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| mbed_official | 181:a4cbdfbbd2f4 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| mbed_official | 181:a4cbdfbbd2f4 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| mbed_official | 181:a4cbdfbbd2f4 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| mbed_official | 181:a4cbdfbbd2f4 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| mbed_official | 181:a4cbdfbbd2f4 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| mbed_official | 181:a4cbdfbbd2f4 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| mbed_official | 181:a4cbdfbbd2f4 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| mbed_official | 181:a4cbdfbbd2f4 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| mbed_official | 181:a4cbdfbbd2f4 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| mbed_official | 181:a4cbdfbbd2f4 | 28 | ******************************************************************************* |
| mbed_official | 181:a4cbdfbbd2f4 | 29 | */ |
| mbed_official | 181:a4cbdfbbd2f4 | 30 | #include <stddef.h> |
| mbed_official | 181:a4cbdfbbd2f4 | 31 | #include "cmsis.h" |
| mbed_official | 181:a4cbdfbbd2f4 | 32 | #include "gpio_irq_api.h" |
| mbed_official | 181:a4cbdfbbd2f4 | 33 | #include "pinmap.h" |
| mbed_official | 285:31249416b6f9 | 34 | #include "mbed_error.h" |
| mbed_official | 181:a4cbdfbbd2f4 | 35 | |
| mbed_official | 181:a4cbdfbbd2f4 | 36 | #define EDGE_NONE (0) |
| mbed_official | 181:a4cbdfbbd2f4 | 37 | #define EDGE_RISE (1) |
| mbed_official | 181:a4cbdfbbd2f4 | 38 | #define EDGE_FALL (2) |
| mbed_official | 181:a4cbdfbbd2f4 | 39 | #define EDGE_BOTH (3) |
| mbed_official | 181:a4cbdfbbd2f4 | 40 | |
| mbed_official | 181:a4cbdfbbd2f4 | 41 | #define CHANNEL_NUM (3) |
| mbed_official | 181:a4cbdfbbd2f4 | 42 | |
| mbed_official | 181:a4cbdfbbd2f4 | 43 | static uint32_t channel_ids[CHANNEL_NUM] = {0, 0, 0}; |
| mbed_official | 181:a4cbdfbbd2f4 | 44 | static uint32_t channel_gpio[CHANNEL_NUM] = {0, 0, 0}; |
| mbed_official | 181:a4cbdfbbd2f4 | 45 | static uint32_t channel_pin[CHANNEL_NUM] = {0, 0, 0}; |
| mbed_official | 181:a4cbdfbbd2f4 | 46 | |
| mbed_official | 181:a4cbdfbbd2f4 | 47 | static gpio_irq_handler irq_handler; |
| mbed_official | 181:a4cbdfbbd2f4 | 48 | |
| mbed_official | 402:09075a3b15e3 | 49 | static void handle_interrupt_in(uint32_t irq_index) |
| mbed_official | 402:09075a3b15e3 | 50 | { |
| mbed_official | 181:a4cbdfbbd2f4 | 51 | // Retrieve the gpio and pin that generate the irq |
| mbed_official | 181:a4cbdfbbd2f4 | 52 | GPIO_TypeDef *gpio = (GPIO_TypeDef *)(channel_gpio[irq_index]); |
| mbed_official | 181:a4cbdfbbd2f4 | 53 | uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]); |
| mbed_official | 181:a4cbdfbbd2f4 | 54 | |
| mbed_official | 181:a4cbdfbbd2f4 | 55 | // Clear interrupt flag |
| mbed_official | 181:a4cbdfbbd2f4 | 56 | if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) { |
| mbed_official | 181:a4cbdfbbd2f4 | 57 | __HAL_GPIO_EXTI_CLEAR_FLAG(pin); |
| mbed_official | 181:a4cbdfbbd2f4 | 58 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 59 | |
| mbed_official | 181:a4cbdfbbd2f4 | 60 | if (channel_ids[irq_index] == 0) return; |
| mbed_official | 181:a4cbdfbbd2f4 | 61 | |
| mbed_official | 181:a4cbdfbbd2f4 | 62 | // Check which edge has generated the irq |
| mbed_official | 181:a4cbdfbbd2f4 | 63 | if ((gpio->IDR & pin) == 0) { |
| mbed_official | 181:a4cbdfbbd2f4 | 64 | irq_handler(channel_ids[irq_index], IRQ_FALL); |
| mbed_official | 181:a4cbdfbbd2f4 | 65 | } else { |
| mbed_official | 181:a4cbdfbbd2f4 | 66 | irq_handler(channel_ids[irq_index], IRQ_RISE); |
| mbed_official | 181:a4cbdfbbd2f4 | 67 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 68 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 69 | |
| mbed_official | 181:a4cbdfbbd2f4 | 70 | // The irq_index is passed to the function |
| mbed_official | 181:a4cbdfbbd2f4 | 71 | // EXTI lines 0 to 1 |
| mbed_official | 402:09075a3b15e3 | 72 | static void gpio_irq0(void) |
| mbed_official | 402:09075a3b15e3 | 73 | { |
| mbed_official | 181:a4cbdfbbd2f4 | 74 | handle_interrupt_in(0); |
| mbed_official | 181:a4cbdfbbd2f4 | 75 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 76 | // EXTI lines 2 to 3 |
| mbed_official | 402:09075a3b15e3 | 77 | static void gpio_irq1(void) |
| mbed_official | 402:09075a3b15e3 | 78 | { |
| mbed_official | 181:a4cbdfbbd2f4 | 79 | handle_interrupt_in(1); |
| mbed_official | 181:a4cbdfbbd2f4 | 80 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 81 | // EXTI lines 4 to 15 |
| mbed_official | 402:09075a3b15e3 | 82 | static void gpio_irq2(void) |
| mbed_official | 402:09075a3b15e3 | 83 | { |
| mbed_official | 181:a4cbdfbbd2f4 | 84 | handle_interrupt_in(2); |
| mbed_official | 181:a4cbdfbbd2f4 | 85 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 86 | |
| mbed_official | 181:a4cbdfbbd2f4 | 87 | extern uint32_t Set_GPIO_Clock(uint32_t port_idx); |
| mbed_official | 181:a4cbdfbbd2f4 | 88 | |
| mbed_official | 402:09075a3b15e3 | 89 | int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) |
| mbed_official | 402:09075a3b15e3 | 90 | { |
| mbed_official | 181:a4cbdfbbd2f4 | 91 | IRQn_Type irq_n = (IRQn_Type)0; |
| mbed_official | 181:a4cbdfbbd2f4 | 92 | uint32_t vector = 0; |
| mbed_official | 181:a4cbdfbbd2f4 | 93 | uint32_t irq_index; |
| mbed_official | 181:a4cbdfbbd2f4 | 94 | |
| mbed_official | 181:a4cbdfbbd2f4 | 95 | if (pin == NC) return -1; |
| mbed_official | 181:a4cbdfbbd2f4 | 96 | |
| mbed_official | 181:a4cbdfbbd2f4 | 97 | uint32_t port_index = STM_PORT(pin); |
| mbed_official | 181:a4cbdfbbd2f4 | 98 | uint32_t pin_index = STM_PIN(pin); |
| mbed_official | 181:a4cbdfbbd2f4 | 99 | |
| mbed_official | 181:a4cbdfbbd2f4 | 100 | // Select irq number and interrupt routine |
| mbed_official | 181:a4cbdfbbd2f4 | 101 | if ((pin_index == 0) || (pin_index == 1)) { |
| mbed_official | 181:a4cbdfbbd2f4 | 102 | irq_n = EXTI0_1_IRQn; |
| mbed_official | 181:a4cbdfbbd2f4 | 103 | vector = (uint32_t)&gpio_irq0; |
| mbed_official | 181:a4cbdfbbd2f4 | 104 | irq_index = 0; |
| mbed_official | 181:a4cbdfbbd2f4 | 105 | } else if ((pin_index == 2) || (pin_index == 3)) { |
| mbed_official | 181:a4cbdfbbd2f4 | 106 | irq_n = EXTI2_3_IRQn; |
| mbed_official | 181:a4cbdfbbd2f4 | 107 | vector = (uint32_t)&gpio_irq1; |
| mbed_official | 181:a4cbdfbbd2f4 | 108 | irq_index = 1; |
| mbed_official | 181:a4cbdfbbd2f4 | 109 | } else if ((pin_index > 3) && (pin_index < 16)) { |
| mbed_official | 181:a4cbdfbbd2f4 | 110 | irq_n = EXTI4_15_IRQn; |
| mbed_official | 181:a4cbdfbbd2f4 | 111 | vector = (uint32_t)&gpio_irq2; |
| mbed_official | 181:a4cbdfbbd2f4 | 112 | irq_index = 2; |
| mbed_official | 181:a4cbdfbbd2f4 | 113 | } else { |
| mbed_official | 181:a4cbdfbbd2f4 | 114 | error("InterruptIn error: pin not supported.\n"); |
| mbed_official | 181:a4cbdfbbd2f4 | 115 | return -1; |
| mbed_official | 181:a4cbdfbbd2f4 | 116 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 117 | |
| mbed_official | 181:a4cbdfbbd2f4 | 118 | // Enable GPIO clock |
| mbed_official | 181:a4cbdfbbd2f4 | 119 | uint32_t gpio_add = Set_GPIO_Clock(port_index); |
| mbed_official | 181:a4cbdfbbd2f4 | 120 | |
| mbed_official | 181:a4cbdfbbd2f4 | 121 | // Configure GPIO |
| mbed_official | 181:a4cbdfbbd2f4 | 122 | pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0)); |
| mbed_official | 181:a4cbdfbbd2f4 | 123 | |
| mbed_official | 181:a4cbdfbbd2f4 | 124 | // Enable EXTI interrupt |
| mbed_official | 181:a4cbdfbbd2f4 | 125 | NVIC_SetVector(irq_n, vector); |
| mbed_official | 181:a4cbdfbbd2f4 | 126 | NVIC_EnableIRQ(irq_n); |
| mbed_official | 181:a4cbdfbbd2f4 | 127 | |
| mbed_official | 181:a4cbdfbbd2f4 | 128 | // Save informations for future use |
| mbed_official | 181:a4cbdfbbd2f4 | 129 | obj->irq_n = irq_n; |
| mbed_official | 181:a4cbdfbbd2f4 | 130 | obj->irq_index = irq_index; |
| mbed_official | 181:a4cbdfbbd2f4 | 131 | obj->event = EDGE_NONE; |
| mbed_official | 181:a4cbdfbbd2f4 | 132 | obj->pin = pin; |
| mbed_official | 181:a4cbdfbbd2f4 | 133 | channel_ids[irq_index] = id; |
| mbed_official | 181:a4cbdfbbd2f4 | 134 | channel_gpio[irq_index] = gpio_add; |
| mbed_official | 181:a4cbdfbbd2f4 | 135 | channel_pin[irq_index] = pin_index; |
| mbed_official | 181:a4cbdfbbd2f4 | 136 | |
| mbed_official | 181:a4cbdfbbd2f4 | 137 | irq_handler = handler; |
| mbed_official | 181:a4cbdfbbd2f4 | 138 | |
| mbed_official | 181:a4cbdfbbd2f4 | 139 | return 0; |
| mbed_official | 181:a4cbdfbbd2f4 | 140 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 141 | |
| mbed_official | 402:09075a3b15e3 | 142 | void gpio_irq_free(gpio_irq_t *obj) |
| mbed_official | 402:09075a3b15e3 | 143 | { |
| mbed_official | 181:a4cbdfbbd2f4 | 144 | channel_ids[obj->irq_index] = 0; |
| mbed_official | 181:a4cbdfbbd2f4 | 145 | channel_gpio[obj->irq_index] = 0; |
| mbed_official | 181:a4cbdfbbd2f4 | 146 | channel_pin[obj->irq_index] = 0; |
| mbed_official | 181:a4cbdfbbd2f4 | 147 | // Disable EXTI line |
| mbed_official | 181:a4cbdfbbd2f4 | 148 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
| mbed_official | 181:a4cbdfbbd2f4 | 149 | obj->event = EDGE_NONE; |
| mbed_official | 181:a4cbdfbbd2f4 | 150 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 151 | |
| mbed_official | 331:098575c6d2c8 | 152 | void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) |
| mbed_official | 331:098575c6d2c8 | 153 | { |
| mbed_official | 331:098575c6d2c8 | 154 | uint32_t mode = STM_MODE_IT_EVT_RESET; |
| mbed_official | 181:a4cbdfbbd2f4 | 155 | uint32_t pull = GPIO_NOPULL; |
| mbed_official | 181:a4cbdfbbd2f4 | 156 | |
| mbed_official | 181:a4cbdfbbd2f4 | 157 | if (enable) { |
| mbed_official | 181:a4cbdfbbd2f4 | 158 | if (event == IRQ_RISE) { |
| mbed_official | 181:a4cbdfbbd2f4 | 159 | if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) { |
| mbed_official | 181:a4cbdfbbd2f4 | 160 | mode = STM_MODE_IT_RISING_FALLING; |
| mbed_official | 181:a4cbdfbbd2f4 | 161 | obj->event = EDGE_BOTH; |
| mbed_official | 181:a4cbdfbbd2f4 | 162 | } else { // NONE or RISE |
| mbed_official | 181:a4cbdfbbd2f4 | 163 | mode = STM_MODE_IT_RISING; |
| mbed_official | 181:a4cbdfbbd2f4 | 164 | obj->event = EDGE_RISE; |
| mbed_official | 181:a4cbdfbbd2f4 | 165 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 166 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 167 | if (event == IRQ_FALL) { |
| mbed_official | 181:a4cbdfbbd2f4 | 168 | if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) { |
| mbed_official | 181:a4cbdfbbd2f4 | 169 | mode = STM_MODE_IT_RISING_FALLING; |
| mbed_official | 181:a4cbdfbbd2f4 | 170 | obj->event = EDGE_BOTH; |
| mbed_official | 181:a4cbdfbbd2f4 | 171 | } else { // NONE or FALL |
| mbed_official | 181:a4cbdfbbd2f4 | 172 | mode = STM_MODE_IT_FALLING; |
| mbed_official | 181:a4cbdfbbd2f4 | 173 | obj->event = EDGE_FALL; |
| mbed_official | 181:a4cbdfbbd2f4 | 174 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 175 | } |
| mbed_official | 331:098575c6d2c8 | 176 | } else { // Disable |
| mbed_official | 331:098575c6d2c8 | 177 | if (event == IRQ_RISE) { |
| mbed_official | 331:098575c6d2c8 | 178 | if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) { |
| mbed_official | 331:098575c6d2c8 | 179 | mode = STM_MODE_IT_FALLING; |
| mbed_official | 331:098575c6d2c8 | 180 | obj->event = EDGE_FALL; |
| mbed_official | 331:098575c6d2c8 | 181 | } else { // NONE or RISE |
| mbed_official | 331:098575c6d2c8 | 182 | mode = STM_MODE_IT_EVT_RESET; |
| mbed_official | 331:098575c6d2c8 | 183 | obj->event = EDGE_NONE; |
| mbed_official | 331:098575c6d2c8 | 184 | } |
| mbed_official | 331:098575c6d2c8 | 185 | } |
| mbed_official | 331:098575c6d2c8 | 186 | if (event == IRQ_FALL) { |
| mbed_official | 331:098575c6d2c8 | 187 | if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) { |
| mbed_official | 331:098575c6d2c8 | 188 | mode = STM_MODE_IT_RISING; |
| mbed_official | 331:098575c6d2c8 | 189 | obj->event = EDGE_RISE; |
| mbed_official | 331:098575c6d2c8 | 190 | } else { // NONE or FALL |
| mbed_official | 331:098575c6d2c8 | 191 | mode = STM_MODE_IT_EVT_RESET; |
| mbed_official | 331:098575c6d2c8 | 192 | obj->event = EDGE_NONE; |
| mbed_official | 331:098575c6d2c8 | 193 | } |
| mbed_official | 331:098575c6d2c8 | 194 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 195 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 196 | |
| mbed_official | 181:a4cbdfbbd2f4 | 197 | pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0)); |
| mbed_official | 181:a4cbdfbbd2f4 | 198 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 199 | |
| mbed_official | 402:09075a3b15e3 | 200 | void gpio_irq_enable(gpio_irq_t *obj) |
| mbed_official | 402:09075a3b15e3 | 201 | { |
| mbed_official | 181:a4cbdfbbd2f4 | 202 | NVIC_EnableIRQ(obj->irq_n); |
| mbed_official | 181:a4cbdfbbd2f4 | 203 | } |
| mbed_official | 181:a4cbdfbbd2f4 | 204 | |
| mbed_official | 402:09075a3b15e3 | 205 | void gpio_irq_disable(gpio_irq_t *obj) |
| mbed_official | 402:09075a3b15e3 | 206 | { |
| mbed_official | 181:a4cbdfbbd2f4 | 207 | NVIC_DisableIRQ(obj->irq_n); |
| mbed_official | 181:a4cbdfbbd2f4 | 208 | obj->event = EDGE_NONE; |
| mbed_official | 181:a4cbdfbbd2f4 | 209 | } |
