mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jul 08 11:15:08 2014 +0100
Revision:
250:a49055e7a707
Parent:
174:8bb9f3a33240
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?

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