mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

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:
Fri Nov 14 09:00:07 2014 +0000
Revision:
402:09075a3b15e3
Parent:
331:098575c6d2c8
Child:
413:ee28a0bed4ad
Synchronized with git revision dffeedc96e731e30785ca4a7842fb9769412bea1

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

Targets: add USBTX and USBRX pin definitions for targets that don't provide them

Who changed what in which revision?

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