]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Maxim/TARGET_MAX32610/gpio_irq_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Maxim / TARGET_MAX32610 / gpio_irq_api.c
1 /*******************************************************************************
2  * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
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.
21  *
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.
25  *
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
30  * ownership rights.
31  *******************************************************************************
32  */
33  
34 #include <stddef.h>
35 #include "cmsis.h"
36 #include "gpio_irq_api.h"
37 #include "mbed_error.h"
38
39 #define NUM_PORTS           3
40 #define NUM_PINS_PER_PORT   8
41
42 static uint32_t ids[NUM_PORTS][NUM_PINS_PER_PORT] = {{0}};
43 static gpio_irq_handler irq_handler;
44
45 static void handle_irq(unsigned int port)
46 {
47     uint32_t intfl, in_val;
48     uint32_t mask;
49     unsigned int pin;
50
51     /* Read pin state */
52     in_val = MXC_GPIO->in_val[port];
53
54     /* Read interrupts */
55     intfl = MXC_GPIO->intfl[port] & MXC_GPIO->inten[port];
56     
57     mask = 1;
58
59     for (pin = 0; pin < NUM_PINS_PER_PORT; pin++) {
60         if (intfl & mask) {
61             if (ids[port][pin]) {
62                 if (in_val & mask) {
63                     irq_handler(ids[port][pin], IRQ_RISE);
64                 } else {
65                     irq_handler(ids[port][pin], IRQ_FALL);
66                 }
67             }
68             MXC_GPIO->intfl[port] = mask;    /* clear interrupt */
69         }
70         mask <<= 1;
71     }
72 }
73
74 void gpio_irq_0(void)
75 {
76     handle_irq(0);
77 }
78
79 void gpio_irq_1(void)
80 {
81     handle_irq(1);
82 }
83
84 void gpio_irq_2(void)
85 {
86     handle_irq(2);
87 }
88
89 int gpio_irq_init(gpio_irq_t *obj, PinName name, gpio_irq_handler handler, uint32_t id)
90 {
91     if (name == NC)
92         return -1;
93
94     uint8_t port = PINNAME_TO_PORT(name);
95     uint8_t pin = PINNAME_TO_PIN(name);
96
97     if ((port > NUM_PORTS) || (pin > NUM_PINS_PER_PORT)) {
98         return 1;
99     }
100
101     obj->port = port;
102     obj->pin = pin;
103
104     irq_handler = handler;
105
106     ids[port][pin] = id;
107
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);
112
113     /* disable the interrupt locally */
114     MXC_GPIO->int_mode[port] &= ~(0xF << (pin*4));
115
116     /* clear a pending request */
117     MXC_GPIO->intfl[port] = 1 << pin;
118
119     /* enable the requested interrupt */
120     MXC_GPIO->inten[port] |= (1 << pin);
121     NVIC_EnableIRQ((IRQn_Type)((uint32_t)GPIO_P0_IRQn + port));
122
123     return 0;
124 }
125
126 void gpio_irq_free(gpio_irq_t *obj)
127 {
128     /* disable interrupt */
129     MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);
130     MXC_GPIO->int_mode[obj->port] &= ~(0xF << (obj->pin*4));
131
132     ids[obj->port][obj->pin] = 0;
133 }
134
135 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
136 {
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 */
139
140     uint32_t new_mode = curr_mode;
141     if (event == IRQ_FALL) {
142         if (enable) {
143             new_mode |= 0x1;
144         } else {
145             new_mode &= ~0x1;
146         }
147     } else if (event == IRQ_RISE) {
148         if (enable) {
149             new_mode |= 0x2;
150         } else {
151             new_mode &= ~0x2;
152         }
153     }
154
155     int_mode &= ~(0xF << (obj->pin*4));
156     int_mode |= (new_mode << (obj->pin*4));
157     MXC_GPIO->int_mode[obj->port] = int_mode;
158 }
159
160 void gpio_irq_enable(gpio_irq_t *obj)
161 {
162     MXC_GPIO->inten[obj->port] |= (1 << obj->pin);
163 }
164
165 void gpio_irq_disable(gpio_irq_t *obj)
166 {
167     MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);
168 }