GameOpener / mbed-src

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Oct 16 15:00:10 2014 +0100
Revision:
354:e67efb2aab0e
Parent:
331:098575c6d2c8
Child:
413:ee28a0bed4ad
Synchronized with git revision 36a8882a54cbf25645fa6e11af937c8b8048e184

Full URL: https://github.com/mbedmicro/mbed/commit/36a8882a54cbf25645fa6e11af937c8b8048e184/

Targets: NUCLEO_L152RE - Migration to STM32Cube driver (CMSIS and HAL)

Who changed what in which revision?

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