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