]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_irq_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC408X / 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 "gpio_irq_api.h"
18 #include "mbed_error.h"
19 #include "cmsis.h"
20
21 #define CHANNEL_NUM    64
22
23 static uint32_t channel_ids[CHANNEL_NUM] = {0};
24 static gpio_irq_handler irq_handler;
25
26 static void handle_interrupt_in(void) {
27     // Read in all current interrupt registers. We do this once as the
28     // GPIO interrupt registers are on the APB bus, and this is slow.
29     uint32_t rise0 = LPC_GPIOINT->IO0IntStatR;
30     uint32_t fall0 = LPC_GPIOINT->IO0IntStatF;
31     uint32_t rise2 = LPC_GPIOINT->IO2IntStatR;
32     uint32_t fall2 = LPC_GPIOINT->IO2IntStatF;
33
34     uint8_t bitloc;
35
36     // Continue as long as there are interrupts pending
37     while(rise0 > 0) {
38         // CLZ returns number of leading zeros, 31 minus that is location of
39         // first pending interrupt
40         bitloc = 31 - __CLZ(rise0);
41         if (channel_ids[bitloc] != 0)
42             irq_handler(channel_ids[bitloc], IRQ_RISE); //Run that interrupt
43
44         // Both clear the interrupt with clear register, and remove it from
45         // our local copy of the interrupt pending register
46         LPC_GPIOINT->IO0IntClr = 1 << bitloc;
47         rise0 -= 1<<bitloc;
48     }
49
50     // Continue as long as there are interrupts pending
51     while(fall0 > 0) {
52         // CLZ returns number of leading zeros, 31 minus that is location of
53         // first pending interrupt
54         bitloc = 31 - __CLZ(fall0);
55         if (channel_ids[bitloc] != 0)
56             irq_handler(channel_ids[bitloc], IRQ_FALL); //Run that interrupt
57
58         // Both clear the interrupt with clear register, and remove it from
59         // our local copy of the interrupt pending register
60         LPC_GPIOINT->IO0IntClr = 1 << bitloc;
61         fall0 -= 1<<bitloc;
62     }
63
64     // Same for port 2
65
66     // Continue as long as there are interrupts pending
67     while(rise2 > 0) {
68         // CLZ returns number of leading zeros, 31 minus that is location of
69         // first pending interrupt
70         bitloc = 31 - __CLZ(rise2);
71         if (channel_ids[bitloc+32] != 0)
72             irq_handler(channel_ids[bitloc+32], IRQ_RISE); //Run that interrupt
73
74         // Both clear the interrupt with clear register, and remove it from
75         // our local copy of the interrupt pending register
76         LPC_GPIOINT->IO2IntClr = 1 << bitloc;
77         rise2 -= 1<<bitloc;
78     }
79
80     // Continue as long as there are interrupts pending
81     while(fall2 > 0) {
82         // CLZ returns number of leading zeros, 31 minus that is location of
83         // first pending interrupt
84         bitloc = 31 - __CLZ(fall2);
85         if (channel_ids[bitloc+32] != 0)
86             irq_handler(channel_ids[bitloc+32], IRQ_FALL); //Run that interrupt
87
88         // Both clear the interrupt with clear register, and remove it from
89         // our local copy of the interrupt pending register
90         LPC_GPIOINT->IO2IntClr = 1 << bitloc;
91         fall2 -= 1<<bitloc;
92     }
93 }
94
95 int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) {
96     if (pin == NC) return -1;
97     
98     irq_handler = handler;
99     
100     obj->port = ((int)(LPC_GPIO0_BASE+pin) & ~0x1F);
101     obj->pin = (int)pin % 32;
102     
103     // Interrupts available only on GPIO0 and GPIO2
104     if (obj->port != LPC_GPIO0_BASE && obj->port != LPC_GPIO2_BASE) {
105         error("pins on this port cannot generate interrupts");
106     }
107     
108     // put us in the interrupt table
109     int index = (obj->port == LPC_GPIO0_BASE) ? obj->pin : obj->pin + 32;
110     channel_ids[index] = id;
111     obj->ch = index;
112     
113     NVIC_SetVector(GPIO_IRQn, (uint32_t)handle_interrupt_in);
114     NVIC_EnableIRQ(GPIO_IRQn);
115     
116     return 0;
117 }
118
119 void gpio_irq_free(gpio_irq_t *obj) {
120     channel_ids[obj->ch] = 0;
121 }
122
123 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
124     // ensure nothing is pending
125     switch (obj->port) {
126          case LPC_GPIO0_BASE: LPC_GPIOINT->IO0IntClr = 1 << obj->pin; break;
127          case LPC_GPIO2_BASE: LPC_GPIOINT->IO2IntClr = 1 << obj->pin; break;
128     }
129     
130     // enable the pin interrupt
131     if (event == IRQ_RISE) {
132         switch (obj->port) {
133             case LPC_GPIO0_BASE:
134                 if (enable) {
135                     LPC_GPIOINT->IO0IntEnR |= 1 << obj->pin;
136                 } else {
137                     LPC_GPIOINT->IO0IntEnR &= ~(1 << obj->pin);
138                 }
139                 break;
140             case LPC_GPIO2_BASE:
141                 if (enable) {
142                     LPC_GPIOINT->IO2IntEnR |= 1 << obj->pin;
143                 } else {
144                     LPC_GPIOINT->IO2IntEnR &= ~(1 << obj->pin);
145                 }
146                 break;
147         }
148     } else {
149         switch (obj->port) {
150             case LPC_GPIO0_BASE:
151                 if (enable) {
152                     LPC_GPIOINT->IO0IntEnF |= 1 << obj->pin;
153                 } else {
154                     LPC_GPIOINT->IO0IntEnF &= ~(1 << obj->pin);
155                 }
156                 break;
157             case LPC_GPIO2_BASE:
158                 if (enable) {
159                     LPC_GPIOINT->IO2IntEnF |= 1 << obj->pin;
160                 } else {
161                     LPC_GPIOINT->IO2IntEnF &= ~(1 << obj->pin);
162                 }
163                 break;
164         }
165     }
166 }
167
168 void gpio_irq_enable(gpio_irq_t *obj) {
169     NVIC_EnableIRQ(GPIO_IRQn);
170 }
171
172 void gpio_irq_disable(gpio_irq_t *obj) {
173     NVIC_DisableIRQ(GPIO_IRQn);
174 }