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 */
74 void gpio_irq_0(void) { handle_irq(0); }
75 void gpio_irq_1(void) { handle_irq(1); }
76 void gpio_irq_2(void) { handle_irq(2); }
77 void gpio_irq_3(void) { handle_irq(3); }
78 void gpio_irq_4(void) { handle_irq(4); }
79 void gpio_irq_5(void) { handle_irq(5); }
80 void gpio_irq_6(void) { handle_irq(6); }
81 void gpio_irq_7(void) { handle_irq(7); }
83 int gpio_irq_init(gpio_irq_t *obj, PinName name, gpio_irq_handler handler, uint32_t id)
88 uint8_t port = PINNAME_TO_PORT(name);
89 uint8_t pin = PINNAME_TO_PIN(name);
91 if ((port > NUM_PORTS) || (pin > NUM_PINS_PER_PORT)) {
98 irq_handler = handler;
102 /* register handlers */
103 NVIC_SetVector(GPIO_P0_IRQn, (uint32_t)gpio_irq_0);
104 NVIC_SetVector(GPIO_P1_IRQn, (uint32_t)gpio_irq_1);
105 NVIC_SetVector(GPIO_P2_IRQn, (uint32_t)gpio_irq_2);
106 NVIC_SetVector(GPIO_P3_IRQn, (uint32_t)gpio_irq_3);
107 NVIC_SetVector(GPIO_P4_IRQn, (uint32_t)gpio_irq_4);
108 NVIC_SetVector(GPIO_P5_IRQn, (uint32_t)gpio_irq_5);
109 NVIC_SetVector(GPIO_P6_IRQn, (uint32_t)gpio_irq_6);
110 NVIC_SetVector(GPIO_P7_IRQn, (uint32_t)gpio_irq_7);
112 /* disable the interrupt locally */
113 MXC_GPIO->int_mode[port] &= ~(0xF << (pin*4));
115 /* clear a pending request */
116 MXC_GPIO->intfl[port] = 1 << pin;
118 /* enable the requested interrupt */
119 MXC_GPIO->inten[port] |= (1 << pin);
120 NVIC_EnableIRQ((IRQn_Type)((uint32_t)GPIO_P0_IRQn + port));
125 void gpio_irq_free(gpio_irq_t *obj)
127 /* disable interrupt */
128 MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);
129 MXC_GPIO->int_mode[obj->port] &= ~(0xF << (obj->pin*4));
131 ids[obj->port][obj->pin] = 0;
134 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
136 uint32_t int_mode = MXC_GPIO->int_mode[obj->port];
137 uint32_t curr_mode = (int_mode >> (obj->pin*4)) & 0x3; /* only supporting edge interrupts */
139 uint32_t new_mode = curr_mode;
140 if (event == IRQ_FALL) {
146 } else if (event == IRQ_RISE) {
154 int_mode &= ~(0xF << (obj->pin*4));
155 int_mode |= (new_mode << (obj->pin*4));
156 MXC_GPIO->int_mode[obj->port] = int_mode;
159 void gpio_irq_enable(gpio_irq_t *obj)
161 MXC_GPIO->inten[obj->port] |= (1 << obj->pin);
164 void gpio_irq_disable(gpio_irq_t *obj)
166 MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);