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 Sep 11 09:30:09 2015 +0100
Revision:
621:9c82b0f79f3d
Parent:
515:7467ef1f4ad8
Synchronized with git revision 6c1d63e069ab9bd86de92e8296ca783681257538

Full URL: https://github.com/mbedmicro/mbed/commit/6c1d63e069ab9bd86de92e8296ca783681257538/

ignore target files not supported by the yotta module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 514:7668256dbe61 1 /*******************************************************************************
mbed_official 514:7668256dbe61 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
mbed_official 514:7668256dbe61 3 *
mbed_official 514:7668256dbe61 4 * Permission is hereby granted, free of charge, to any person obtaining a
mbed_official 514:7668256dbe61 5 * copy of this software and associated documentation files (the "Software"),
mbed_official 514:7668256dbe61 6 * to deal in the Software without restriction, including without limitation
mbed_official 514:7668256dbe61 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
mbed_official 514:7668256dbe61 8 * and/or sell copies of the Software, and to permit persons to whom the
mbed_official 514:7668256dbe61 9 * Software is furnished to do so, subject to the following conditions:
mbed_official 514:7668256dbe61 10 *
mbed_official 514:7668256dbe61 11 * The above copyright notice and this permission notice shall be included
mbed_official 514:7668256dbe61 12 * in all copies or substantial portions of the Software.
mbed_official 514:7668256dbe61 13 *
mbed_official 514:7668256dbe61 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
mbed_official 514:7668256dbe61 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
mbed_official 514:7668256dbe61 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
mbed_official 514:7668256dbe61 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
mbed_official 514:7668256dbe61 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
mbed_official 514:7668256dbe61 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
mbed_official 514:7668256dbe61 20 * OTHER DEALINGS IN THE SOFTWARE.
mbed_official 514:7668256dbe61 21 *
mbed_official 514:7668256dbe61 22 * Except as contained in this notice, the name of Maxim Integrated
mbed_official 514:7668256dbe61 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
mbed_official 514:7668256dbe61 24 * Products, Inc. Branding Policy.
mbed_official 514:7668256dbe61 25 *
mbed_official 514:7668256dbe61 26 * The mere transfer of this software does not imply any licenses
mbed_official 514:7668256dbe61 27 * of trade secrets, proprietary technology, copyrights, patents,
mbed_official 514:7668256dbe61 28 * trademarks, maskwork rights, or any other form of intellectual
mbed_official 514:7668256dbe61 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
mbed_official 514:7668256dbe61 30 * ownership rights.
mbed_official 514:7668256dbe61 31 *******************************************************************************
mbed_official 514:7668256dbe61 32 */
mbed_official 514:7668256dbe61 33
mbed_official 514:7668256dbe61 34 #include <stddef.h>
mbed_official 514:7668256dbe61 35 #include "cmsis.h"
mbed_official 514:7668256dbe61 36 #include "gpio_irq_api.h"
mbed_official 514:7668256dbe61 37 #include "mbed_error.h"
mbed_official 514:7668256dbe61 38
mbed_official 514:7668256dbe61 39 #define NUM_PORTS 8
mbed_official 514:7668256dbe61 40 #define NUM_PINS_PER_PORT 8
mbed_official 514:7668256dbe61 41
mbed_official 514:7668256dbe61 42 static uint32_t ids[NUM_PORTS][NUM_PINS_PER_PORT] = {{0}};
mbed_official 514:7668256dbe61 43 static gpio_irq_handler irq_handler;
mbed_official 514:7668256dbe61 44
mbed_official 514:7668256dbe61 45 static void handle_irq(unsigned int port)
mbed_official 514:7668256dbe61 46 {
mbed_official 514:7668256dbe61 47 uint32_t intfl, in_val;
mbed_official 514:7668256dbe61 48 uint32_t mask;
mbed_official 514:7668256dbe61 49 unsigned int pin;
mbed_official 514:7668256dbe61 50
mbed_official 514:7668256dbe61 51 /* Read pin state */
mbed_official 514:7668256dbe61 52 in_val = MXC_GPIO->in_val[port];
mbed_official 514:7668256dbe61 53
mbed_official 514:7668256dbe61 54 /* Read interrupts */
mbed_official 514:7668256dbe61 55 intfl = MXC_GPIO->intfl[port] & MXC_GPIO->inten[port];
mbed_official 514:7668256dbe61 56
mbed_official 514:7668256dbe61 57 mask = 1;
mbed_official 514:7668256dbe61 58
mbed_official 514:7668256dbe61 59 for (pin = 0; pin < NUM_PINS_PER_PORT; pin++) {
mbed_official 514:7668256dbe61 60 if (intfl & mask) {
mbed_official 514:7668256dbe61 61 if (ids[port][pin]) {
mbed_official 514:7668256dbe61 62 if (in_val & mask) {
mbed_official 514:7668256dbe61 63 irq_handler(ids[port][pin], IRQ_RISE);
mbed_official 514:7668256dbe61 64 } else {
mbed_official 514:7668256dbe61 65 irq_handler(ids[port][pin], IRQ_FALL);
mbed_official 514:7668256dbe61 66 }
mbed_official 514:7668256dbe61 67 }
mbed_official 514:7668256dbe61 68 MXC_GPIO->intfl[port] = mask; /* clear interrupt */
mbed_official 514:7668256dbe61 69 }
mbed_official 514:7668256dbe61 70 mask <<= 1;
mbed_official 514:7668256dbe61 71 }
mbed_official 514:7668256dbe61 72 }
mbed_official 514:7668256dbe61 73
mbed_official 514:7668256dbe61 74 void gpio_irq_0(void) { handle_irq(0); }
mbed_official 514:7668256dbe61 75 void gpio_irq_1(void) { handle_irq(1); }
mbed_official 514:7668256dbe61 76 void gpio_irq_2(void) { handle_irq(2); }
mbed_official 514:7668256dbe61 77 void gpio_irq_3(void) { handle_irq(3); }
mbed_official 514:7668256dbe61 78 void gpio_irq_4(void) { handle_irq(4); }
mbed_official 514:7668256dbe61 79 void gpio_irq_5(void) { handle_irq(5); }
mbed_official 514:7668256dbe61 80 void gpio_irq_6(void) { handle_irq(6); }
mbed_official 514:7668256dbe61 81 void gpio_irq_7(void) { handle_irq(7); }
mbed_official 514:7668256dbe61 82
mbed_official 514:7668256dbe61 83 int gpio_irq_init(gpio_irq_t *obj, PinName name, gpio_irq_handler handler, uint32_t id)
mbed_official 514:7668256dbe61 84 {
mbed_official 514:7668256dbe61 85 if (name == NC)
mbed_official 514:7668256dbe61 86 return -1;
mbed_official 514:7668256dbe61 87
mbed_official 514:7668256dbe61 88 uint8_t port = PINNAME_TO_PORT(name);
mbed_official 514:7668256dbe61 89 uint8_t pin = PINNAME_TO_PIN(name);
mbed_official 514:7668256dbe61 90
mbed_official 514:7668256dbe61 91 if ((port > NUM_PORTS) || (pin > NUM_PINS_PER_PORT)) {
mbed_official 514:7668256dbe61 92 return 1;
mbed_official 514:7668256dbe61 93 }
mbed_official 514:7668256dbe61 94
mbed_official 514:7668256dbe61 95 obj->port = port;
mbed_official 514:7668256dbe61 96 obj->pin = pin;
mbed_official 514:7668256dbe61 97
mbed_official 514:7668256dbe61 98 irq_handler = handler;
mbed_official 514:7668256dbe61 99
mbed_official 514:7668256dbe61 100 ids[port][pin] = id;
mbed_official 514:7668256dbe61 101
mbed_official 514:7668256dbe61 102 /* register handlers */
mbed_official 514:7668256dbe61 103 NVIC_SetVector(GPIO_P0_IRQn, (uint32_t)gpio_irq_0);
mbed_official 514:7668256dbe61 104 NVIC_SetVector(GPIO_P1_IRQn, (uint32_t)gpio_irq_1);
mbed_official 514:7668256dbe61 105 NVIC_SetVector(GPIO_P2_IRQn, (uint32_t)gpio_irq_2);
mbed_official 514:7668256dbe61 106 NVIC_SetVector(GPIO_P3_IRQn, (uint32_t)gpio_irq_3);
mbed_official 514:7668256dbe61 107 NVIC_SetVector(GPIO_P4_IRQn, (uint32_t)gpio_irq_4);
mbed_official 514:7668256dbe61 108 NVIC_SetVector(GPIO_P5_IRQn, (uint32_t)gpio_irq_5);
mbed_official 514:7668256dbe61 109 NVIC_SetVector(GPIO_P6_IRQn, (uint32_t)gpio_irq_6);
mbed_official 514:7668256dbe61 110 NVIC_SetVector(GPIO_P7_IRQn, (uint32_t)gpio_irq_7);
mbed_official 514:7668256dbe61 111
mbed_official 514:7668256dbe61 112 /* disable the interrupt locally */
mbed_official 514:7668256dbe61 113 MXC_GPIO->int_mode[port] &= ~(0xF << (pin*4));
mbed_official 514:7668256dbe61 114
mbed_official 514:7668256dbe61 115 /* clear a pending request */
mbed_official 514:7668256dbe61 116 MXC_GPIO->intfl[port] = 1 << pin;
mbed_official 514:7668256dbe61 117
mbed_official 514:7668256dbe61 118 /* enable the requested interrupt */
mbed_official 514:7668256dbe61 119 MXC_GPIO->inten[port] |= (1 << pin);
mbed_official 515:7467ef1f4ad8 120 NVIC_EnableIRQ((IRQn_Type)((uint32_t)GPIO_P0_IRQn + port));
mbed_official 514:7668256dbe61 121
mbed_official 514:7668256dbe61 122 return 0;
mbed_official 514:7668256dbe61 123 }
mbed_official 514:7668256dbe61 124
mbed_official 514:7668256dbe61 125 void gpio_irq_free(gpio_irq_t *obj)
mbed_official 514:7668256dbe61 126 {
mbed_official 514:7668256dbe61 127 /* disable interrupt */
mbed_official 514:7668256dbe61 128 MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);
mbed_official 514:7668256dbe61 129 MXC_GPIO->int_mode[obj->port] &= ~(0xF << (obj->pin*4));
mbed_official 514:7668256dbe61 130
mbed_official 514:7668256dbe61 131 ids[obj->port][obj->pin] = 0;
mbed_official 514:7668256dbe61 132 }
mbed_official 514:7668256dbe61 133
mbed_official 514:7668256dbe61 134 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
mbed_official 514:7668256dbe61 135 {
mbed_official 514:7668256dbe61 136 uint32_t int_mode = MXC_GPIO->int_mode[obj->port];
mbed_official 514:7668256dbe61 137 uint32_t curr_mode = (int_mode >> (obj->pin*4)) & 0x3; /* only supporting edge interrupts */
mbed_official 514:7668256dbe61 138
mbed_official 514:7668256dbe61 139 uint32_t new_mode = curr_mode;
mbed_official 514:7668256dbe61 140 if (event == IRQ_FALL) {
mbed_official 514:7668256dbe61 141 if (enable) {
mbed_official 514:7668256dbe61 142 new_mode |= 0x1;
mbed_official 514:7668256dbe61 143 } else {
mbed_official 514:7668256dbe61 144 new_mode &= ~0x1;
mbed_official 514:7668256dbe61 145 }
mbed_official 514:7668256dbe61 146 } else if (event == IRQ_RISE) {
mbed_official 514:7668256dbe61 147 if (enable) {
mbed_official 514:7668256dbe61 148 new_mode |= 0x2;
mbed_official 514:7668256dbe61 149 } else {
mbed_official 514:7668256dbe61 150 new_mode &= ~0x2;
mbed_official 514:7668256dbe61 151 }
mbed_official 514:7668256dbe61 152 }
mbed_official 514:7668256dbe61 153
mbed_official 514:7668256dbe61 154 int_mode &= ~(0xF << (obj->pin*4));
mbed_official 514:7668256dbe61 155 int_mode |= (new_mode << (obj->pin*4));
mbed_official 514:7668256dbe61 156 MXC_GPIO->int_mode[obj->port] = int_mode;
mbed_official 514:7668256dbe61 157 }
mbed_official 514:7668256dbe61 158
mbed_official 514:7668256dbe61 159 void gpio_irq_enable(gpio_irq_t *obj)
mbed_official 514:7668256dbe61 160 {
mbed_official 514:7668256dbe61 161 MXC_GPIO->inten[obj->port] |= (1 << obj->pin);
mbed_official 514:7668256dbe61 162 }
mbed_official 514:7668256dbe61 163
mbed_official 514:7668256dbe61 164 void gpio_irq_disable(gpio_irq_t *obj)
mbed_official 514:7668256dbe61 165 {
mbed_official 514:7668256dbe61 166 MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);
mbed_official 514:7668256dbe61 167 }