mbed library sources

Dependents:   bare

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Mar 19 10:15:22 2014 +0000
Revision:
125:23cc3068a9e4
Synchronized with git revision ace35dfba3748c7cdc102eb38ec6b9e1067c3252

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

[NUCLEO_F302R8] Add cmsis and hal files + change F401RE clock to 84MHz

Who changed what in which revision?

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