mbed SDK library sources

Fork of mbed-src by mbed official

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Feb 03 09:30:05 2014 +0000
Revision:
84:f54042cbc282
Parent:
76:aeb1df146756
Synchronized with git revision bbbd8699601c42149ccf0c37bc42bb6856ccc4c6

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

[NUCLEO_L152RE/F030_R8] SPI, I2C, Ticker, PWM updates

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 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 76:aeb1df146756 54
mbed_official 76:aeb1df146756 55 // Clear interrupt flag
mbed_official 76:aeb1df146756 56 if (EXTI_GetITStatus(pin) != RESET)
mbed_official 60:142c6c6f5949 57 {
mbed_official 76:aeb1df146756 58 EXTI_ClearITPendingBit(pin);
mbed_official 60:142c6c6f5949 59 }
mbed_official 60:142c6c6f5949 60
mbed_official 76:aeb1df146756 61 if (channel_ids[irq_index] == 0) return;
mbed_official 76:aeb1df146756 62
mbed_official 76:aeb1df146756 63 // Check which edge has generated the irq
mbed_official 76:aeb1df146756 64 if ((gpio->IDR & pin) == 0) {
mbed_official 76:aeb1df146756 65 irq_handler(channel_ids[irq_index], IRQ_FALL);
mbed_official 60:142c6c6f5949 66 }
mbed_official 76:aeb1df146756 67 else {
mbed_official 76:aeb1df146756 68 irq_handler(channel_ids[irq_index], IRQ_RISE);
mbed_official 60:142c6c6f5949 69 }
mbed_official 60:142c6c6f5949 70 }
mbed_official 60:142c6c6f5949 71
mbed_official 76:aeb1df146756 72 // The irq_index is passed to the function
mbed_official 60:142c6c6f5949 73 static void gpio_irq0(void) {handle_interrupt_in(0);}
mbed_official 60:142c6c6f5949 74 static void gpio_irq1(void) {handle_interrupt_in(1);}
mbed_official 60:142c6c6f5949 75 static void gpio_irq2(void) {handle_interrupt_in(2);}
mbed_official 60:142c6c6f5949 76 static void gpio_irq3(void) {handle_interrupt_in(3);}
mbed_official 76:aeb1df146756 77
mbed_official 76:aeb1df146756 78 extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
mbed_official 60:142c6c6f5949 79
mbed_official 60:142c6c6f5949 80 int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) {
mbed_official 76:aeb1df146756 81 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 60:142c6c6f5949 82 uint32_t vector = 0;
mbed_official 76:aeb1df146756 83 uint32_t irq_index;
mbed_official 76:aeb1df146756 84
mbed_official 60:142c6c6f5949 85 if (pin == NC) return -1;
mbed_official 60:142c6c6f5949 86
mbed_official 76:aeb1df146756 87 uint32_t port_index = STM_PORT(pin);
mbed_official 76:aeb1df146756 88 uint32_t pin_index = STM_PIN(pin);
mbed_official 76:aeb1df146756 89
mbed_official 76:aeb1df146756 90 // Select irq number and interrupt routine
mbed_official 76:aeb1df146756 91 switch (pin) {
mbed_official 76:aeb1df146756 92 case PC_13: // User button
mbed_official 76:aeb1df146756 93 irq_n = EXTI15_10_IRQn;
mbed_official 60:142c6c6f5949 94 vector = (uint32_t)&gpio_irq0;
mbed_official 76:aeb1df146756 95 irq_index = 0;
mbed_official 60:142c6c6f5949 96 break;
mbed_official 76:aeb1df146756 97 case PB_3:
mbed_official 60:142c6c6f5949 98 irq_n = EXTI3_IRQn;
mbed_official 76:aeb1df146756 99 vector = (uint32_t)&gpio_irq1;
mbed_official 76:aeb1df146756 100 irq_index = 1;
mbed_official 60:142c6c6f5949 101 break;
mbed_official 76:aeb1df146756 102 case PB_4:
mbed_official 76:aeb1df146756 103 irq_n = EXTI4_IRQn;
mbed_official 76:aeb1df146756 104 vector = (uint32_t)&gpio_irq2;
mbed_official 76:aeb1df146756 105 irq_index = 2;
mbed_official 76:aeb1df146756 106 break;
mbed_official 76:aeb1df146756 107 case PB_5:
mbed_official 60:142c6c6f5949 108 irq_n = EXTI9_5_IRQn;
mbed_official 76:aeb1df146756 109 vector = (uint32_t)&gpio_irq3;
mbed_official 76:aeb1df146756 110 irq_index = 3;
mbed_official 60:142c6c6f5949 111 break;
mbed_official 60:142c6c6f5949 112 default:
mbed_official 76:aeb1df146756 113 error("This pin is not supported with InterruptIn.\n");
mbed_official 60:142c6c6f5949 114 return -1;
mbed_official 60:142c6c6f5949 115 }
mbed_official 76:aeb1df146756 116
mbed_official 76:aeb1df146756 117 // Enable GPIO clock
mbed_official 76:aeb1df146756 118 uint32_t gpio_add = Set_GPIO_Clock(port_index);
mbed_official 60:142c6c6f5949 119
mbed_official 76:aeb1df146756 120 // Enable AFIO clock
mbed_official 60:142c6c6f5949 121 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
mbed_official 76:aeb1df146756 122
mbed_official 60:142c6c6f5949 123 // Connect EXTI line to pin
mbed_official 60:142c6c6f5949 124 GPIO_EXTILineConfig(port_index, pin_index);
mbed_official 60:142c6c6f5949 125
mbed_official 60:142c6c6f5949 126 // Configure EXTI line
mbed_official 60:142c6c6f5949 127 EXTI_InitTypeDef EXTI_InitStructure;
mbed_official 60:142c6c6f5949 128 EXTI_InitStructure.EXTI_Line = (uint32_t)(1 << pin_index);
mbed_official 60:142c6c6f5949 129 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
mbed_official 60:142c6c6f5949 130 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
mbed_official 60:142c6c6f5949 131 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
mbed_official 60:142c6c6f5949 132 EXTI_Init(&EXTI_InitStructure);
mbed_official 60:142c6c6f5949 133
mbed_official 60:142c6c6f5949 134 // Enable and set EXTI interrupt to the lowest priority
mbed_official 60:142c6c6f5949 135 NVIC_InitTypeDef NVIC_InitStructure;
mbed_official 60:142c6c6f5949 136 NVIC_InitStructure.NVIC_IRQChannel = irq_n;
mbed_official 60:142c6c6f5949 137 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
mbed_official 60:142c6c6f5949 138 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
mbed_official 60:142c6c6f5949 139 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
mbed_official 60:142c6c6f5949 140 NVIC_Init(&NVIC_InitStructure);
mbed_official 60:142c6c6f5949 141
mbed_official 60:142c6c6f5949 142 NVIC_SetVector(irq_n, vector);
mbed_official 60:142c6c6f5949 143 NVIC_EnableIRQ(irq_n);
mbed_official 60:142c6c6f5949 144
mbed_official 76:aeb1df146756 145 // Save informations for future use
mbed_official 60:142c6c6f5949 146 obj->irq_n = irq_n;
mbed_official 76:aeb1df146756 147 obj->irq_index = irq_index;
mbed_official 60:142c6c6f5949 148 obj->event = EDGE_NONE;
mbed_official 76:aeb1df146756 149 channel_ids[irq_index] = id;
mbed_official 76:aeb1df146756 150 channel_gpio[irq_index] = gpio_add;
mbed_official 76:aeb1df146756 151 channel_pin[irq_index] = pin_index;
mbed_official 60:142c6c6f5949 152
mbed_official 60:142c6c6f5949 153 irq_handler = handler;
mbed_official 60:142c6c6f5949 154
mbed_official 60:142c6c6f5949 155 return 0;
mbed_official 60:142c6c6f5949 156 }
mbed_official 60:142c6c6f5949 157
mbed_official 60:142c6c6f5949 158 void gpio_irq_free(gpio_irq_t *obj) {
mbed_official 76:aeb1df146756 159 channel_ids[obj->irq_index] = 0;
mbed_official 76:aeb1df146756 160 channel_gpio[obj->irq_index] = 0;
mbed_official 76:aeb1df146756 161 channel_pin[obj->irq_index] = 0;
mbed_official 60:142c6c6f5949 162 // Disable EXTI line
mbed_official 60:142c6c6f5949 163 EXTI_InitTypeDef EXTI_InitStructure;
mbed_official 60:142c6c6f5949 164 EXTI_StructInit(&EXTI_InitStructure);
mbed_official 60:142c6c6f5949 165 EXTI_Init(&EXTI_InitStructure);
mbed_official 60:142c6c6f5949 166 obj->event = EDGE_NONE;
mbed_official 60:142c6c6f5949 167 }
mbed_official 60:142c6c6f5949 168
mbed_official 60:142c6c6f5949 169 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
mbed_official 60:142c6c6f5949 170 EXTI_InitTypeDef EXTI_InitStructure;
mbed_official 60:142c6c6f5949 171
mbed_official 76:aeb1df146756 172 uint32_t pin_index = channel_pin[obj->irq_index];
mbed_official 76:aeb1df146756 173
mbed_official 76:aeb1df146756 174 EXTI_InitStructure.EXTI_Line = (uint32_t)(1 << pin_index);
mbed_official 60:142c6c6f5949 175 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
mbed_official 60:142c6c6f5949 176
mbed_official 60:142c6c6f5949 177 if (event == IRQ_RISE) {
mbed_official 60:142c6c6f5949 178 if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
mbed_official 60:142c6c6f5949 179 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
mbed_official 60:142c6c6f5949 180 obj->event = EDGE_BOTH;
mbed_official 60:142c6c6f5949 181 }
mbed_official 60:142c6c6f5949 182 else { // NONE or RISE
mbed_official 60:142c6c6f5949 183 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
mbed_official 60:142c6c6f5949 184 obj->event = EDGE_RISE;
mbed_official 60:142c6c6f5949 185 }
mbed_official 60:142c6c6f5949 186 }
mbed_official 60:142c6c6f5949 187
mbed_official 60:142c6c6f5949 188 if (event == IRQ_FALL) {
mbed_official 60:142c6c6f5949 189 if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
mbed_official 60:142c6c6f5949 190 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
mbed_official 60:142c6c6f5949 191 obj->event = EDGE_BOTH;
mbed_official 60:142c6c6f5949 192 }
mbed_official 60:142c6c6f5949 193 else { // NONE or FALL
mbed_official 60:142c6c6f5949 194 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
mbed_official 60:142c6c6f5949 195 obj->event = EDGE_FALL;
mbed_official 60:142c6c6f5949 196 }
mbed_official 60:142c6c6f5949 197 }
mbed_official 60:142c6c6f5949 198
mbed_official 60:142c6c6f5949 199 if (enable) {
mbed_official 60:142c6c6f5949 200 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
mbed_official 60:142c6c6f5949 201 }
mbed_official 60:142c6c6f5949 202 else {
mbed_official 60:142c6c6f5949 203 EXTI_InitStructure.EXTI_LineCmd = DISABLE;
mbed_official 60:142c6c6f5949 204 }
mbed_official 60:142c6c6f5949 205
mbed_official 60:142c6c6f5949 206 EXTI_Init(&EXTI_InitStructure);
mbed_official 60:142c6c6f5949 207 }
mbed_official 60:142c6c6f5949 208
mbed_official 60:142c6c6f5949 209 void gpio_irq_enable(gpio_irq_t *obj) {
mbed_official 60:142c6c6f5949 210 NVIC_EnableIRQ(obj->irq_n);
mbed_official 60:142c6c6f5949 211 }
mbed_official 60:142c6c6f5949 212
mbed_official 60:142c6c6f5949 213 void gpio_irq_disable(gpio_irq_t *obj) {
mbed_official 60:142c6c6f5949 214 NVIC_DisableIRQ(obj->irq_n);
mbed_official 60:142c6c6f5949 215 obj->event = EDGE_NONE;
mbed_official 60:142c6c6f5949 216 }