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