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