1 /*******************************************************************************
2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name of Maxim Integrated
23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
24 * Products, Inc. Branding Policy.
26 * The mere transfer of this software does not imply any licenses
27 * of trade secrets, proprietary technology, copyrights, patents,
28 * trademarks, maskwork rights, or any other form of intellectual
29 * property whatsoever. Maxim Integrated Products, Inc. retains all
31 *******************************************************************************
36 #include "gpio_irq_api.h"
37 #include "mbed_error.h"
40 #define NUM_PINS_PER_PORT 8
42 static uint32_t ids[NUM_PORTS][NUM_PINS_PER_PORT] = {{0}};
43 static gpio_irq_handler irq_handler;
45 static void handle_irq(unsigned int port)
47 uint32_t intfl, in_val;
52 in_val = MXC_GPIO->in_val[port];
55 intfl = MXC_GPIO->intfl[port] & MXC_GPIO->inten[port];
59 for (pin = 0; pin < NUM_PINS_PER_PORT; pin++) {
63 irq_handler(ids[port][pin], IRQ_RISE);
65 irq_handler(ids[port][pin], IRQ_FALL);
68 MXC_GPIO->intfl[port] = mask; /* clear interrupt */
89 int gpio_irq_init(gpio_irq_t *obj, PinName name, gpio_irq_handler handler, uint32_t id)
94 uint8_t port = PINNAME_TO_PORT(name);
95 uint8_t pin = PINNAME_TO_PIN(name);
97 if ((port > NUM_PORTS) || (pin > NUM_PINS_PER_PORT)) {
104 irq_handler = handler;
108 /* register handlers */
109 NVIC_SetVector(GPIO_P0_IRQn, (uint32_t)gpio_irq_0);
110 NVIC_SetVector(GPIO_P1_IRQn, (uint32_t)gpio_irq_1);
111 NVIC_SetVector(GPIO_P2_IRQn, (uint32_t)gpio_irq_2);
113 /* disable the interrupt locally */
114 MXC_GPIO->int_mode[port] &= ~(0xF << (pin*4));
116 /* clear a pending request */
117 MXC_GPIO->intfl[port] = 1 << pin;
119 /* enable the requested interrupt */
120 MXC_GPIO->inten[port] |= (1 << pin);
121 NVIC_EnableIRQ((IRQn_Type)((uint32_t)GPIO_P0_IRQn + port));
126 void gpio_irq_free(gpio_irq_t *obj)
128 /* disable interrupt */
129 MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);
130 MXC_GPIO->int_mode[obj->port] &= ~(0xF << (obj->pin*4));
132 ids[obj->port][obj->pin] = 0;
135 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
137 uint32_t int_mode = MXC_GPIO->int_mode[obj->port];
138 uint32_t curr_mode = (int_mode >> (obj->pin*4)) & 0x3; /* only supporting edge interrupts */
140 uint32_t new_mode = curr_mode;
141 if (event == IRQ_FALL) {
147 } else if (event == IRQ_RISE) {
155 int_mode &= ~(0xF << (obj->pin*4));
156 int_mode |= (new_mode << (obj->pin*4));
157 MXC_GPIO->int_mode[obj->port] = int_mode;
160 void gpio_irq_enable(gpio_irq_t *obj)
162 MXC_GPIO->inten[obj->port] |= (1 << obj->pin);
165 void gpio_irq_disable(gpio_irq_t *obj)
167 MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);