mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jul 08 14:15:07 2014 +0100
Revision:
251:de9a1e4ffd79
Parent:
250:a49055e7a707
Child:
285:31249416b6f9
Synchronized with git revision b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63

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

Revert "error.h -> mbed_error.h"

Who changed what in which revision?

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