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:
Fri Sep 11 09:30:09 2015 +0100
Revision:
621:9c82b0f79f3d
Parent:
285:31249416b6f9
Synchronized with git revision 6c1d63e069ab9bd86de92e8296ca783681257538

Full URL: https://github.com/mbedmicro/mbed/commit/6c1d63e069ab9bd86de92e8296ca783681257538/

ignore target files not supported by the yotta module

Who changed what in which revision?

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