mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Sep 24 13:15:06 2014 +0100
Revision:
331:098575c6d2c8
Parent:
285:31249416b6f9
Child:
402:09075a3b15e3
Synchronized with git revision b1260b55d48f08b7a25efe2b72e7765686db0a70

Full URL: https://github.com/mbedmicro/mbed/commit/b1260b55d48f08b7a25efe2b72e7765686db0a70/

[HAL] Nucleo boards - Fix issue with InterruptIn edges disable (MBED_A7 test)

Who changed what in which revision?

UserRevisionLine numberNew 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 285:31249416b6f9 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 331:098575c6d2c8 146 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
mbed_official 331:098575c6d2c8 147 {
mbed_official 331:098575c6d2c8 148 uint32_t mode = STM_MODE_IT_EVT_RESET;
mbed_official 219:993c9b0acbcc 149 uint32_t pull = GPIO_NOPULL;
mbed_official 219:993c9b0acbcc 150
mbed_official 219:993c9b0acbcc 151 if (enable) {
mbed_official 219:993c9b0acbcc 152 if (event == IRQ_RISE) {
mbed_official 219:993c9b0acbcc 153 if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
mbed_official 219:993c9b0acbcc 154 mode = STM_MODE_IT_RISING_FALLING;
mbed_official 219:993c9b0acbcc 155 obj->event = EDGE_BOTH;
mbed_official 219:993c9b0acbcc 156 } else { // NONE or RISE
mbed_official 219:993c9b0acbcc 157 mode = STM_MODE_IT_RISING;
mbed_official 219:993c9b0acbcc 158 obj->event = EDGE_RISE;
mbed_official 219:993c9b0acbcc 159 }
mbed_official 219:993c9b0acbcc 160 }
mbed_official 219:993c9b0acbcc 161 if (event == IRQ_FALL) {
mbed_official 219:993c9b0acbcc 162 if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
mbed_official 219:993c9b0acbcc 163 mode = STM_MODE_IT_RISING_FALLING;
mbed_official 219:993c9b0acbcc 164 obj->event = EDGE_BOTH;
mbed_official 219:993c9b0acbcc 165 } else { // NONE or FALL
mbed_official 219:993c9b0acbcc 166 mode = STM_MODE_IT_FALLING;
mbed_official 219:993c9b0acbcc 167 obj->event = EDGE_FALL;
mbed_official 219:993c9b0acbcc 168 }
mbed_official 219:993c9b0acbcc 169 }
mbed_official 331:098575c6d2c8 170 } else { // Disable
mbed_official 331:098575c6d2c8 171 if (event == IRQ_RISE) {
mbed_official 331:098575c6d2c8 172 if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
mbed_official 331:098575c6d2c8 173 mode = STM_MODE_IT_FALLING;
mbed_official 331:098575c6d2c8 174 obj->event = EDGE_FALL;
mbed_official 331:098575c6d2c8 175 } else { // NONE or RISE
mbed_official 331:098575c6d2c8 176 mode = STM_MODE_IT_EVT_RESET;
mbed_official 331:098575c6d2c8 177 obj->event = EDGE_NONE;
mbed_official 331:098575c6d2c8 178 }
mbed_official 331:098575c6d2c8 179 }
mbed_official 331:098575c6d2c8 180 if (event == IRQ_FALL) {
mbed_official 331:098575c6d2c8 181 if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
mbed_official 331:098575c6d2c8 182 mode = STM_MODE_IT_RISING;
mbed_official 331:098575c6d2c8 183 obj->event = EDGE_RISE;
mbed_official 331:098575c6d2c8 184 } else { // NONE or FALL
mbed_official 331:098575c6d2c8 185 mode = STM_MODE_IT_EVT_RESET;
mbed_official 331:098575c6d2c8 186 obj->event = EDGE_NONE;
mbed_official 331:098575c6d2c8 187 }
mbed_official 331:098575c6d2c8 188 }
mbed_official 219:993c9b0acbcc 189 }
mbed_official 219:993c9b0acbcc 190
mbed_official 219:993c9b0acbcc 191 pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0));
mbed_official 219:993c9b0acbcc 192 }
mbed_official 219:993c9b0acbcc 193
mbed_official 219:993c9b0acbcc 194 void gpio_irq_enable(gpio_irq_t *obj) {
mbed_official 219:993c9b0acbcc 195 NVIC_EnableIRQ(obj->irq_n);
mbed_official 219:993c9b0acbcc 196 }
mbed_official 219:993c9b0acbcc 197
mbed_official 219:993c9b0acbcc 198 void gpio_irq_disable(gpio_irq_t *obj) {
mbed_official 219:993c9b0acbcc 199 NVIC_DisableIRQ(obj->irq_n);
mbed_official 219:993c9b0acbcc 200 obj->event = EDGE_NONE;
mbed_official 219:993c9b0acbcc 201 }