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:
Tue Dec 16 08:15:08 2014 +0000
Revision:
440:8a0b45cd594f
Parent:
413:ee28a0bed4ad
Synchronized with git revision 67fbbf0b635d0c0d93fbe433306c537c2ad206aa

Full URL: https://github.com/mbedmicro/mbed/commit/67fbbf0b635d0c0d93fbe433306c537c2ad206aa/

Targets: nrf51 - updating app_timer.c from Norid'c SDKv7.1.0

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 413:ee28a0bed4ad 41 // Number of EXTI irq vectors (EXTI0_1, EXTI2_3, EXTI4_15)
mbed_official 219:993c9b0acbcc 42 #define CHANNEL_NUM (3)
mbed_official 219:993c9b0acbcc 43
mbed_official 413:ee28a0bed4ad 44 // Max pins for one line (max with EXTI4_15)
mbed_official 413:ee28a0bed4ad 45 #define MAX_PIN_LINE (12)
mbed_official 413:ee28a0bed4ad 46
mbed_official 413:ee28a0bed4ad 47 typedef struct gpio_channel {
mbed_official 413:ee28a0bed4ad 48 uint32_t pin_mask; // bitmask representing which pins are configured for receiving interrupts
mbed_official 413:ee28a0bed4ad 49 uint32_t channel_ids[MAX_PIN_LINE]; // mbed "gpio_irq_t gpio_irq" field of instance
mbed_official 413:ee28a0bed4ad 50 uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group
mbed_official 413:ee28a0bed4ad 51 uint32_t channel_pin[MAX_PIN_LINE]; // pin number in port group
mbed_official 413:ee28a0bed4ad 52 } gpio_channel_t;
mbed_official 413:ee28a0bed4ad 53
mbed_official 413:ee28a0bed4ad 54 static gpio_channel_t channels[CHANNEL_NUM] = {
mbed_official 413:ee28a0bed4ad 55 {.pin_mask = 0},
mbed_official 413:ee28a0bed4ad 56 {.pin_mask = 0},
mbed_official 413:ee28a0bed4ad 57 {.pin_mask = 0}
mbed_official 413:ee28a0bed4ad 58 };
mbed_official 413:ee28a0bed4ad 59
mbed_official 413:ee28a0bed4ad 60 // Used to return the index for channels array.
mbed_official 413:ee28a0bed4ad 61 static uint32_t pin_base_nr[16] = {
mbed_official 413:ee28a0bed4ad 62 // EXTI0_1
mbed_official 413:ee28a0bed4ad 63 0, // pin 0
mbed_official 413:ee28a0bed4ad 64 1, // pin 1
mbed_official 413:ee28a0bed4ad 65 // EXTI2_3
mbed_official 413:ee28a0bed4ad 66 0, // pin 2
mbed_official 413:ee28a0bed4ad 67 1, // pin 3
mbed_official 413:ee28a0bed4ad 68 // EXTI4_15
mbed_official 413:ee28a0bed4ad 69 0, // pin 4
mbed_official 413:ee28a0bed4ad 70 1, // pin 5
mbed_official 413:ee28a0bed4ad 71 2, // pin 6
mbed_official 413:ee28a0bed4ad 72 3, // pin 7
mbed_official 413:ee28a0bed4ad 73 4, // pin 8
mbed_official 413:ee28a0bed4ad 74 5, // pin 9
mbed_official 413:ee28a0bed4ad 75 6, // pin 10
mbed_official 413:ee28a0bed4ad 76 7, // pin 11
mbed_official 413:ee28a0bed4ad 77 8, // pin 12
mbed_official 413:ee28a0bed4ad 78 9, // pin 13
mbed_official 413:ee28a0bed4ad 79 10, // pin 14
mbed_official 413:ee28a0bed4ad 80 11 // pin 15
mbed_official 413:ee28a0bed4ad 81 };
mbed_official 219:993c9b0acbcc 82
mbed_official 219:993c9b0acbcc 83 static gpio_irq_handler irq_handler;
mbed_official 219:993c9b0acbcc 84
mbed_official 413:ee28a0bed4ad 85 static void handle_interrupt_in(uint32_t irq_index, uint32_t max_num_pin_line)
mbed_official 402:09075a3b15e3 86 {
mbed_official 413:ee28a0bed4ad 87 gpio_channel_t *gpio_channel = &channels[irq_index];
mbed_official 413:ee28a0bed4ad 88 uint32_t gpio_idx;
mbed_official 413:ee28a0bed4ad 89
mbed_official 413:ee28a0bed4ad 90 for (gpio_idx = 0; gpio_idx < max_num_pin_line; gpio_idx++) {
mbed_official 413:ee28a0bed4ad 91 uint32_t current_mask = (1 << gpio_idx);
mbed_official 413:ee28a0bed4ad 92
mbed_official 413:ee28a0bed4ad 93 if (gpio_channel->pin_mask & current_mask) {
mbed_official 413:ee28a0bed4ad 94 // Retrieve the gpio and pin that generate the irq
mbed_official 413:ee28a0bed4ad 95 GPIO_TypeDef *gpio = (GPIO_TypeDef *)(gpio_channel->channel_gpio[gpio_idx]);
mbed_official 413:ee28a0bed4ad 96 uint32_t pin = (uint32_t)(1 << (gpio_channel->channel_pin[gpio_idx]));
mbed_official 219:993c9b0acbcc 97
mbed_official 413:ee28a0bed4ad 98 // Clear interrupt flag
mbed_official 413:ee28a0bed4ad 99 if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
mbed_official 413:ee28a0bed4ad 100 __HAL_GPIO_EXTI_CLEAR_FLAG(pin);
mbed_official 413:ee28a0bed4ad 101
mbed_official 413:ee28a0bed4ad 102 if (gpio_channel->channel_ids[gpio_idx] == 0) continue;
mbed_official 219:993c9b0acbcc 103
mbed_official 413:ee28a0bed4ad 104 // Check which edge has generated the irq
mbed_official 413:ee28a0bed4ad 105 if ((gpio->IDR & pin) == 0) {
mbed_official 413:ee28a0bed4ad 106 irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_FALL);
mbed_official 413:ee28a0bed4ad 107 } else {
mbed_official 413:ee28a0bed4ad 108 irq_handler(gpio_channel->channel_ids[gpio_idx], IRQ_RISE);
mbed_official 413:ee28a0bed4ad 109 }
mbed_official 413:ee28a0bed4ad 110 }
mbed_official 413:ee28a0bed4ad 111 }
mbed_official 219:993c9b0acbcc 112 }
mbed_official 219:993c9b0acbcc 113 }
mbed_official 219:993c9b0acbcc 114
mbed_official 219:993c9b0acbcc 115 // EXTI lines 0 to 1
mbed_official 402:09075a3b15e3 116 static void gpio_irq0(void)
mbed_official 402:09075a3b15e3 117 {
mbed_official 413:ee28a0bed4ad 118 handle_interrupt_in(0, 2);
mbed_official 219:993c9b0acbcc 119 }
mbed_official 413:ee28a0bed4ad 120
mbed_official 219:993c9b0acbcc 121 // EXTI lines 2 to 3
mbed_official 402:09075a3b15e3 122 static void gpio_irq1(void)
mbed_official 402:09075a3b15e3 123 {
mbed_official 413:ee28a0bed4ad 124 handle_interrupt_in(1, 2);
mbed_official 219:993c9b0acbcc 125 }
mbed_official 413:ee28a0bed4ad 126
mbed_official 219:993c9b0acbcc 127 // EXTI lines 4 to 15
mbed_official 402:09075a3b15e3 128 static void gpio_irq2(void)
mbed_official 402:09075a3b15e3 129 {
mbed_official 413:ee28a0bed4ad 130 handle_interrupt_in(2, 12);
mbed_official 219:993c9b0acbcc 131 }
mbed_official 219:993c9b0acbcc 132
mbed_official 219:993c9b0acbcc 133 extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
mbed_official 219:993c9b0acbcc 134
mbed_official 402:09075a3b15e3 135 int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
mbed_official 402:09075a3b15e3 136 {
mbed_official 219:993c9b0acbcc 137 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 219:993c9b0acbcc 138 uint32_t vector = 0;
mbed_official 219:993c9b0acbcc 139 uint32_t irq_index;
mbed_official 413:ee28a0bed4ad 140 gpio_channel_t *gpio_channel;
mbed_official 413:ee28a0bed4ad 141 uint32_t gpio_idx;
mbed_official 219:993c9b0acbcc 142
mbed_official 219:993c9b0acbcc 143 if (pin == NC) return -1;
mbed_official 219:993c9b0acbcc 144
mbed_official 219:993c9b0acbcc 145 uint32_t port_index = STM_PORT(pin);
mbed_official 219:993c9b0acbcc 146 uint32_t pin_index = STM_PIN(pin);
mbed_official 219:993c9b0acbcc 147
mbed_official 219:993c9b0acbcc 148 // Select irq number and interrupt routine
mbed_official 219:993c9b0acbcc 149 if ((pin_index == 0) || (pin_index == 1)) {
mbed_official 219:993c9b0acbcc 150 irq_n = EXTI0_1_IRQn;
mbed_official 219:993c9b0acbcc 151 vector = (uint32_t)&gpio_irq0;
mbed_official 219:993c9b0acbcc 152 irq_index = 0;
mbed_official 219:993c9b0acbcc 153 } else if ((pin_index == 2) || (pin_index == 3)) {
mbed_official 219:993c9b0acbcc 154 irq_n = EXTI2_3_IRQn;
mbed_official 219:993c9b0acbcc 155 vector = (uint32_t)&gpio_irq1;
mbed_official 219:993c9b0acbcc 156 irq_index = 1;
mbed_official 219:993c9b0acbcc 157 } else if ((pin_index > 3) && (pin_index < 16)) {
mbed_official 219:993c9b0acbcc 158 irq_n = EXTI4_15_IRQn;
mbed_official 219:993c9b0acbcc 159 vector = (uint32_t)&gpio_irq2;
mbed_official 219:993c9b0acbcc 160 irq_index = 2;
mbed_official 219:993c9b0acbcc 161 } else {
mbed_official 219:993c9b0acbcc 162 error("InterruptIn error: pin not supported.\n");
mbed_official 219:993c9b0acbcc 163 return -1;
mbed_official 219:993c9b0acbcc 164 }
mbed_official 219:993c9b0acbcc 165
mbed_official 219:993c9b0acbcc 166 // Enable GPIO clock
mbed_official 219:993c9b0acbcc 167 uint32_t gpio_add = Set_GPIO_Clock(port_index);
mbed_official 219:993c9b0acbcc 168
mbed_official 219:993c9b0acbcc 169 // Configure GPIO
mbed_official 219:993c9b0acbcc 170 pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0));
mbed_official 219:993c9b0acbcc 171
mbed_official 219:993c9b0acbcc 172 // Enable EXTI interrupt
mbed_official 219:993c9b0acbcc 173 NVIC_SetVector(irq_n, vector);
mbed_official 219:993c9b0acbcc 174 NVIC_EnableIRQ(irq_n);
mbed_official 219:993c9b0acbcc 175
mbed_official 219:993c9b0acbcc 176 // Save informations for future use
mbed_official 219:993c9b0acbcc 177 obj->irq_n = irq_n;
mbed_official 219:993c9b0acbcc 178 obj->irq_index = irq_index;
mbed_official 219:993c9b0acbcc 179 obj->event = EDGE_NONE;
mbed_official 219:993c9b0acbcc 180 obj->pin = pin;
mbed_official 413:ee28a0bed4ad 181
mbed_official 413:ee28a0bed4ad 182 gpio_channel = &channels[irq_index];
mbed_official 413:ee28a0bed4ad 183 gpio_idx = pin_base_nr[pin_index];
mbed_official 413:ee28a0bed4ad 184 gpio_channel->pin_mask |= (1 << gpio_idx);
mbed_official 413:ee28a0bed4ad 185 gpio_channel->channel_ids[gpio_idx] = id;
mbed_official 413:ee28a0bed4ad 186 gpio_channel->channel_gpio[gpio_idx] = gpio_add;
mbed_official 413:ee28a0bed4ad 187 gpio_channel->channel_pin[gpio_idx] = pin_index;
mbed_official 219:993c9b0acbcc 188
mbed_official 219:993c9b0acbcc 189 irq_handler = handler;
mbed_official 219:993c9b0acbcc 190
mbed_official 219:993c9b0acbcc 191 return 0;
mbed_official 219:993c9b0acbcc 192 }
mbed_official 219:993c9b0acbcc 193
mbed_official 402:09075a3b15e3 194 void gpio_irq_free(gpio_irq_t *obj)
mbed_official 402:09075a3b15e3 195 {
mbed_official 413:ee28a0bed4ad 196 gpio_channel_t *gpio_channel = &channels[obj->irq_index];
mbed_official 413:ee28a0bed4ad 197 uint32_t pin_index = STM_PIN(obj->pin);
mbed_official 413:ee28a0bed4ad 198 uint32_t gpio_idx = pin_base_nr[pin_index];
mbed_official 413:ee28a0bed4ad 199
mbed_official 413:ee28a0bed4ad 200 gpio_channel->pin_mask &= ~(1 << gpio_idx);
mbed_official 413:ee28a0bed4ad 201 gpio_channel->channel_ids[gpio_idx] = 0;
mbed_official 413:ee28a0bed4ad 202 gpio_channel->channel_gpio[gpio_idx] = 0;
mbed_official 413:ee28a0bed4ad 203 gpio_channel->channel_pin[gpio_idx] = 0;
mbed_official 413:ee28a0bed4ad 204
mbed_official 219:993c9b0acbcc 205 // Disable EXTI line
mbed_official 219:993c9b0acbcc 206 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 219:993c9b0acbcc 207 obj->event = EDGE_NONE;
mbed_official 219:993c9b0acbcc 208 }
mbed_official 219:993c9b0acbcc 209
mbed_official 331:098575c6d2c8 210 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
mbed_official 331:098575c6d2c8 211 {
mbed_official 331:098575c6d2c8 212 uint32_t mode = STM_MODE_IT_EVT_RESET;
mbed_official 219:993c9b0acbcc 213 uint32_t pull = GPIO_NOPULL;
mbed_official 219:993c9b0acbcc 214
mbed_official 219:993c9b0acbcc 215 if (enable) {
mbed_official 219:993c9b0acbcc 216 if (event == IRQ_RISE) {
mbed_official 219:993c9b0acbcc 217 if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
mbed_official 219:993c9b0acbcc 218 mode = STM_MODE_IT_RISING_FALLING;
mbed_official 219:993c9b0acbcc 219 obj->event = EDGE_BOTH;
mbed_official 219:993c9b0acbcc 220 } else { // NONE or RISE
mbed_official 219:993c9b0acbcc 221 mode = STM_MODE_IT_RISING;
mbed_official 219:993c9b0acbcc 222 obj->event = EDGE_RISE;
mbed_official 219:993c9b0acbcc 223 }
mbed_official 219:993c9b0acbcc 224 }
mbed_official 219:993c9b0acbcc 225 if (event == IRQ_FALL) {
mbed_official 219:993c9b0acbcc 226 if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
mbed_official 219:993c9b0acbcc 227 mode = STM_MODE_IT_RISING_FALLING;
mbed_official 219:993c9b0acbcc 228 obj->event = EDGE_BOTH;
mbed_official 219:993c9b0acbcc 229 } else { // NONE or FALL
mbed_official 219:993c9b0acbcc 230 mode = STM_MODE_IT_FALLING;
mbed_official 219:993c9b0acbcc 231 obj->event = EDGE_FALL;
mbed_official 219:993c9b0acbcc 232 }
mbed_official 219:993c9b0acbcc 233 }
mbed_official 331:098575c6d2c8 234 } else { // Disable
mbed_official 331:098575c6d2c8 235 if (event == IRQ_RISE) {
mbed_official 331:098575c6d2c8 236 if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
mbed_official 331:098575c6d2c8 237 mode = STM_MODE_IT_FALLING;
mbed_official 331:098575c6d2c8 238 obj->event = EDGE_FALL;
mbed_official 331:098575c6d2c8 239 } else { // NONE or RISE
mbed_official 331:098575c6d2c8 240 mode = STM_MODE_IT_EVT_RESET;
mbed_official 331:098575c6d2c8 241 obj->event = EDGE_NONE;
mbed_official 331:098575c6d2c8 242 }
mbed_official 331:098575c6d2c8 243 }
mbed_official 331:098575c6d2c8 244 if (event == IRQ_FALL) {
mbed_official 331:098575c6d2c8 245 if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
mbed_official 331:098575c6d2c8 246 mode = STM_MODE_IT_RISING;
mbed_official 331:098575c6d2c8 247 obj->event = EDGE_RISE;
mbed_official 331:098575c6d2c8 248 } else { // NONE or FALL
mbed_official 331:098575c6d2c8 249 mode = STM_MODE_IT_EVT_RESET;
mbed_official 331:098575c6d2c8 250 obj->event = EDGE_NONE;
mbed_official 331:098575c6d2c8 251 }
mbed_official 331:098575c6d2c8 252 }
mbed_official 219:993c9b0acbcc 253 }
mbed_official 219:993c9b0acbcc 254
mbed_official 219:993c9b0acbcc 255 pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0));
mbed_official 219:993c9b0acbcc 256 }
mbed_official 219:993c9b0acbcc 257
mbed_official 402:09075a3b15e3 258 void gpio_irq_enable(gpio_irq_t *obj)
mbed_official 402:09075a3b15e3 259 {
mbed_official 219:993c9b0acbcc 260 NVIC_EnableIRQ(obj->irq_n);
mbed_official 219:993c9b0acbcc 261 }
mbed_official 219:993c9b0acbcc 262
mbed_official 402:09075a3b15e3 263 void gpio_irq_disable(gpio_irq_t *obj)
mbed_official 402:09075a3b15e3 264 {
mbed_official 219:993c9b0acbcc 265 NVIC_DisableIRQ(obj->irq_n);
mbed_official 219:993c9b0acbcc 266 obj->event = EDGE_NONE;
mbed_official 219:993c9b0acbcc 267 }