mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Aug 15 16:30:08 2014 +0100
Revision:
285:31249416b6f9
Parent:
251:de9a1e4ffd79
Synchronized with git revision 601712595f49bbd2a2771c52cf3e84c9c7a591af

Full URL: https://github.com/mbedmicro/mbed/commit/601712595f49bbd2a2771c52cf3e84c9c7a591af/

Who changed what in which revision?

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