mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Feb 03 09:30:05 2014 +0000
Revision:
84:f54042cbc282
Parent:
76:aeb1df146756
Child:
156:38b9eb24e1d1
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 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
mbed_official 76:aeb1df146756 33 #include "gpio_irq_api.h"
mbed_official 76:aeb1df146756 34 #include "pinmap.h"
mbed_official 76:aeb1df146756 35 #include "error.h"
mbed_official 76:aeb1df146756 36
mbed_official 76:aeb1df146756 37 #define EDGE_NONE (0)
mbed_official 76:aeb1df146756 38 #define EDGE_RISE (1)
mbed_official 76:aeb1df146756 39 #define EDGE_FALL (2)
mbed_official 76:aeb1df146756 40 #define EDGE_BOTH (3)
mbed_official 76:aeb1df146756 41
mbed_official 76:aeb1df146756 42 #define CHANNEL_NUM (4)
mbed_official 76:aeb1df146756 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 76:aeb1df146756 47
mbed_official 76:aeb1df146756 48 static gpio_irq_handler irq_handler;
mbed_official 76:aeb1df146756 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 76:aeb1df146756 57 {
mbed_official 76:aeb1df146756 58 EXTI_ClearITPendingBit(pin);
mbed_official 76:aeb1df146756 59 }
mbed_official 76:aeb1df146756 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 76:aeb1df146756 66 }
mbed_official 76:aeb1df146756 67 else {
mbed_official 76:aeb1df146756 68 irq_handler(channel_ids[irq_index], IRQ_RISE);
mbed_official 76:aeb1df146756 69 }
mbed_official 76:aeb1df146756 70 }
mbed_official 76:aeb1df146756 71
mbed_official 76:aeb1df146756 72 // The irq_index is passed to the function
mbed_official 76:aeb1df146756 73 static void gpio_irq0(void) {handle_interrupt_in(0);}
mbed_official 76:aeb1df146756 74 static void gpio_irq1(void) {handle_interrupt_in(1);}
mbed_official 76:aeb1df146756 75 static void gpio_irq2(void) {handle_interrupt_in(2);}
mbed_official 76:aeb1df146756 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 76:aeb1df146756 79
mbed_official 76:aeb1df146756 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 76:aeb1df146756 82 uint32_t vector = 0;
mbed_official 76:aeb1df146756 83 uint32_t irq_index;
mbed_official 76:aeb1df146756 84
mbed_official 76:aeb1df146756 85 if (pin == NC) return -1;
mbed_official 76:aeb1df146756 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 76:aeb1df146756 94 vector = (uint32_t)&gpio_irq0;
mbed_official 76:aeb1df146756 95 irq_index = 0;
mbed_official 76:aeb1df146756 96 break;
mbed_official 76:aeb1df146756 97 case PB_3:
mbed_official 76:aeb1df146756 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 76:aeb1df146756 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 76:aeb1df146756 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 76:aeb1df146756 111 break;
mbed_official 76:aeb1df146756 112 default:
mbed_official 76:aeb1df146756 113 error("This pin is not supported with InterruptIn.\n");
mbed_official 76:aeb1df146756 114 return -1;
mbed_official 76:aeb1df146756 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 76:aeb1df146756 119
mbed_official 76:aeb1df146756 120 // Enable SYSCFG clock
mbed_official 76:aeb1df146756 121 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
mbed_official 76:aeb1df146756 122
mbed_official 76:aeb1df146756 123 // Connect EXTI line to pin
mbed_official 76:aeb1df146756 124 SYSCFG_EXTILineConfig(port_index, pin_index);
mbed_official 76:aeb1df146756 125
mbed_official 76:aeb1df146756 126 // Configure EXTI line
mbed_official 76:aeb1df146756 127 EXTI_InitTypeDef EXTI_InitStructure;
mbed_official 76:aeb1df146756 128 EXTI_InitStructure.EXTI_Line = (uint32_t)(1 << pin_index);
mbed_official 76:aeb1df146756 129 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
mbed_official 76:aeb1df146756 130 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
mbed_official 76:aeb1df146756 131 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
mbed_official 76:aeb1df146756 132 EXTI_Init(&EXTI_InitStructure);
mbed_official 76:aeb1df146756 133
mbed_official 76:aeb1df146756 134 // Enable and set EXTI interrupt to the lowest priority
mbed_official 76:aeb1df146756 135 NVIC_InitTypeDef NVIC_InitStructure;
mbed_official 76:aeb1df146756 136 NVIC_InitStructure.NVIC_IRQChannel = irq_n;
mbed_official 76:aeb1df146756 137 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
mbed_official 76:aeb1df146756 138 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
mbed_official 76:aeb1df146756 139 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
mbed_official 76:aeb1df146756 140 NVIC_Init(&NVIC_InitStructure);
mbed_official 76:aeb1df146756 141
mbed_official 76:aeb1df146756 142 NVIC_SetVector(irq_n, vector);
mbed_official 76:aeb1df146756 143 NVIC_EnableIRQ(irq_n);
mbed_official 76:aeb1df146756 144
mbed_official 76:aeb1df146756 145 // Save informations for future use
mbed_official 76:aeb1df146756 146 obj->irq_n = irq_n;
mbed_official 76:aeb1df146756 147 obj->irq_index = irq_index;
mbed_official 76:aeb1df146756 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 76:aeb1df146756 152
mbed_official 76:aeb1df146756 153 irq_handler = handler;
mbed_official 76:aeb1df146756 154
mbed_official 76:aeb1df146756 155 return 0;
mbed_official 76:aeb1df146756 156 }
mbed_official 76:aeb1df146756 157
mbed_official 76:aeb1df146756 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 76:aeb1df146756 162 // Disable EXTI line
mbed_official 76:aeb1df146756 163 EXTI_InitTypeDef EXTI_InitStructure;
mbed_official 76:aeb1df146756 164 EXTI_StructInit(&EXTI_InitStructure);
mbed_official 76:aeb1df146756 165 EXTI_Init(&EXTI_InitStructure);
mbed_official 76:aeb1df146756 166 obj->event = EDGE_NONE;
mbed_official 76:aeb1df146756 167 }
mbed_official 76:aeb1df146756 168
mbed_official 76:aeb1df146756 169 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
mbed_official 76:aeb1df146756 170 EXTI_InitTypeDef EXTI_InitStructure;
mbed_official 76:aeb1df146756 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 76:aeb1df146756 175 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
mbed_official 76:aeb1df146756 176
mbed_official 76:aeb1df146756 177 if (event == IRQ_RISE) {
mbed_official 76:aeb1df146756 178 if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
mbed_official 76:aeb1df146756 179 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
mbed_official 76:aeb1df146756 180 obj->event = EDGE_BOTH;
mbed_official 76:aeb1df146756 181 }
mbed_official 76:aeb1df146756 182 else { // NONE or RISE
mbed_official 76:aeb1df146756 183 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
mbed_official 76:aeb1df146756 184 obj->event = EDGE_RISE;
mbed_official 76:aeb1df146756 185 }
mbed_official 76:aeb1df146756 186 }
mbed_official 76:aeb1df146756 187
mbed_official 76:aeb1df146756 188 if (event == IRQ_FALL) {
mbed_official 76:aeb1df146756 189 if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
mbed_official 76:aeb1df146756 190 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
mbed_official 76:aeb1df146756 191 obj->event = EDGE_BOTH;
mbed_official 76:aeb1df146756 192 }
mbed_official 76:aeb1df146756 193 else { // NONE or FALL
mbed_official 76:aeb1df146756 194 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
mbed_official 76:aeb1df146756 195 obj->event = EDGE_FALL;
mbed_official 76:aeb1df146756 196 }
mbed_official 76:aeb1df146756 197 }
mbed_official 76:aeb1df146756 198
mbed_official 76:aeb1df146756 199 if (enable) {
mbed_official 76:aeb1df146756 200 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
mbed_official 76:aeb1df146756 201 }
mbed_official 76:aeb1df146756 202 else {
mbed_official 76:aeb1df146756 203 EXTI_InitStructure.EXTI_LineCmd = DISABLE;
mbed_official 76:aeb1df146756 204 }
mbed_official 76:aeb1df146756 205
mbed_official 76:aeb1df146756 206 EXTI_Init(&EXTI_InitStructure);
mbed_official 76:aeb1df146756 207 }
mbed_official 76:aeb1df146756 208
mbed_official 76:aeb1df146756 209 void gpio_irq_enable(gpio_irq_t *obj) {
mbed_official 76:aeb1df146756 210 NVIC_EnableIRQ(obj->irq_n);
mbed_official 76:aeb1df146756 211 }
mbed_official 76:aeb1df146756 212
mbed_official 76:aeb1df146756 213 void gpio_irq_disable(gpio_irq_t *obj) {
mbed_official 76:aeb1df146756 214 NVIC_DisableIRQ(obj->irq_n);
mbed_official 76:aeb1df146756 215 obj->event = EDGE_NONE;
mbed_official 76:aeb1df146756 216 }