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