]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_irq_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / gpio_irq_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <stddef.h>
17 #include "cmsis.h"
18
19 #include "gpio_irq_api.h"
20
21 #if DEVICE_INTERRUPTIN
22
23 #include "gpio_api.h"
24 #include "fsl_gpio_hal.h"
25 #include "fsl_port_hal.h"
26 #include "mbed_error.h"
27
28 #define CHANNEL_NUM 160
29
30 static uint32_t channel_ids[CHANNEL_NUM] = {0};
31 static gpio_irq_handler irq_handler;
32
33 #define IRQ_DISABLED (0)
34 #define IRQ_RAISING_EDGE (9)
35 #define IRQ_FALLING_EDGE (10)
36 #define IRQ_EITHER_EDGE (11)
37
38 static void handle_interrupt_in(PortName port, int ch_base) {
39 uint32_t i;
40 uint32_t port_addrs[] = PORT_BASE_ADDRS;
41
42 for (i = 0; i < 32; i++) {
43 if (PORT_HAL_IsPinIntPending(port_addrs[port], i)) {
44 uint32_t id = channel_ids[ch_base + i];
45 if (id == 0) {
46 continue;
47 }
48
49 gpio_irq_event event = IRQ_NONE;
50 uint32_t gpio_addrs[] = GPIO_BASE_ADDRS;
51 switch (BR_PORT_PCRn_IRQC(port_addrs[port], i)) {
52 case IRQ_RAISING_EDGE:
53 event = IRQ_RISE;
54 break;
55
56 case IRQ_FALLING_EDGE:
57 event = IRQ_FALL;
58 break;
59
60 case IRQ_EITHER_EDGE:
61 event = (GPIO_HAL_ReadPinInput(gpio_addrs[port], i)) ? (IRQ_RISE) : (IRQ_FALL);
62 break;
63 }
64 if (event != IRQ_NONE) {
65 irq_handler(id, event);
66 }
67 }
68 }
69 PORT_HAL_ClearPortIntFlag(port_addrs[port]);
70 }
71
72 void gpio_irqA(void) {handle_interrupt_in(PortA, 0);}
73 void gpio_irqB(void) {handle_interrupt_in(PortB, 32);}
74 void gpio_irqC(void) {handle_interrupt_in(PortC, 64);}
75 void gpio_irqD(void) {handle_interrupt_in(PortD, 96);}
76 void gpio_irqE(void) {handle_interrupt_in(PortE, 128);}
77
78 int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) {
79 if (pin == NC) {
80 return -1;
81 }
82
83 irq_handler = handler;
84 obj->port = pin >> GPIO_PORT_SHIFT;
85 obj->pin = pin & 0x7F;
86
87 uint32_t ch_base = 0;
88 uint32_t vector = (uint32_t)gpio_irqA;
89 IRQn_Type irq_n = PORTA_IRQn;
90 switch (obj->port) {
91 case PortA:
92 ch_base = 0;
93 irq_n = PORTA_IRQn;
94 vector = (uint32_t)gpio_irqA;
95 break;
96 case PortB:
97 ch_base = 32;
98 irq_n = PORTB_IRQn;
99 vector = (uint32_t)gpio_irqB;
100 break;
101 case PortC:
102 ch_base = 64;
103 irq_n = PORTC_IRQn;
104 vector = (uint32_t)gpio_irqC;
105 break;
106 case PortD:
107 ch_base = 96;
108 irq_n = PORTD_IRQn;
109 vector = (uint32_t)gpio_irqD;
110 break;
111 case PortE:
112 ch_base = 128;
113 irq_n = PORTE_IRQn;
114 vector = (uint32_t)gpio_irqE;
115 break;
116
117 default:
118 error("gpio_irq only supported on port A-E.");
119 break;
120 }
121 NVIC_SetVector(irq_n, vector);
122 NVIC_EnableIRQ(irq_n);
123
124 obj->ch = ch_base + obj->pin;
125 channel_ids[obj->ch] = id;
126
127 return 0;
128 }
129
130 void gpio_irq_free(gpio_irq_t *obj) {
131 channel_ids[obj->ch] = 0;
132 }
133
134 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
135 uint32_t port_addrs[] = PORT_BASE_ADDRS;
136 port_interrupt_config_t irq_settings = kPortIntDisabled;
137
138 switch (BR_PORT_PCRn_IRQC(port_addrs[obj->port], obj->pin)) {
139 case IRQ_DISABLED:
140 if (enable)
141 irq_settings = (event == IRQ_RISE) ? (kPortIntRisingEdge) : (kPortIntFallingEdge);
142 break;
143
144 case IRQ_RAISING_EDGE:
145 if (enable) {
146 irq_settings = (event == IRQ_RISE) ? (kPortIntRisingEdge) : (kPortIntEitherEdge);
147 } else {
148 if (event == IRQ_FALL)
149 irq_settings = kPortIntRisingEdge;
150 }
151 break;
152
153 case IRQ_FALLING_EDGE:
154 if (enable) {
155 irq_settings = (event == IRQ_FALL) ? (kPortIntFallingEdge) : (kPortIntEitherEdge);
156 } else {
157 if (event == IRQ_RISE)
158 irq_settings = kPortIntFallingEdge;
159 }
160 break;
161
162 case IRQ_EITHER_EDGE:
163 if (enable) {
164 irq_settings = kPortIntEitherEdge;
165 } else {
166 irq_settings = (event == IRQ_RISE) ? (kPortIntFallingEdge) : (kPortIntRisingEdge);
167 }
168 break;
169 }
170
171 PORT_HAL_SetPinIntMode(port_addrs[obj->port], obj->pin, irq_settings);
172 PORT_HAL_ClearPinIntFlag(port_addrs[obj->port], obj->pin);
173 }
174
175 void gpio_irq_enable(gpio_irq_t *obj) {
176 switch (obj->port) {
177 case PortA:
178 NVIC_EnableIRQ(PORTA_IRQn);
179 break;
180 case PortB:
181 NVIC_EnableIRQ(PORTB_IRQn);
182 break;
183 case PortC:
184 NVIC_EnableIRQ(PORTC_IRQn);
185 break;
186 case PortD:
187 NVIC_EnableIRQ(PORTD_IRQn);
188 break;
189 case PortE:
190 NVIC_EnableIRQ(PORTE_IRQn);
191 break;
192 }
193 }
194
195 void gpio_irq_disable(gpio_irq_t *obj) {
196 switch (obj->port) {
197 case PortA:
198 NVIC_DisableIRQ(PORTA_IRQn);
199 break;
200 case PortB:
201 NVIC_DisableIRQ(PORTB_IRQn);
202 break;
203 case PortC:
204 NVIC_DisableIRQ(PORTC_IRQn);
205 break;
206 case PortD:
207 NVIC_DisableIRQ(PORTD_IRQn);
208 break;
209 case PortE:
210 NVIC_DisableIRQ(PORTE_IRQn);
211 break;
212 }
213 }
214
215 #endif
Imprint / Impressum