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