mbed library with additional peripherals for ST F401 board

Fork of mbed-src by mbed official

This mbed LIB has additional peripherals for ST F401 board

  • UART2 : PA_3 rx, PA_2 tx
  • UART3 : PC_7 rx, PC_6 tx
  • I2C2 : PB_3 SDA, PB_10 SCL
  • I2C3 : PB_4 SDA, PA_8 SCL
Committer:
mbed_official
Date:
Mon Jan 27 14:30:07 2014 +0000
Revision:
76:aeb1df146756
Parent:
70:c1fbde68b492
Child:
84:f54042cbc282
Synchronized with git revision a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd

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

Add NUCLEO_L152RE

Who changed what in which revision?

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