1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "gpio_irq_api.h"
21 #if DEVICE_INTERRUPTIN
24 #include "fsl_gpio_hal.h"
25 #include "fsl_port_hal.h"
26 #include "mbed_error.h"
28 #define CHANNEL_NUM 160
30 static uint32_t channel_ids
[CHANNEL_NUM
] = {0};
31 static gpio_irq_handler irq_handler
;
33 #define IRQ_DISABLED (0)
34 #define IRQ_RAISING_EDGE (9)
35 #define IRQ_FALLING_EDGE (10)
36 #define IRQ_EITHER_EDGE (11)
38 static void handle_interrupt_in(PortName port
, int ch_base
) {
40 uint32_t port_addrs
[] = PORT_BASE_ADDRS
;
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
];
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
:
56 case IRQ_FALLING_EDGE
:
61 event
= (GPIO_HAL_ReadPinInput(gpio_addrs
[port
], i
)) ? (IRQ_RISE
) : (IRQ_FALL
);
64 if (event
!= IRQ_NONE
) {
65 irq_handler(id
, event
);
69 PORT_HAL_ClearPortIntFlag(port_addrs
[port
]);
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);}
78 int gpio_irq_init(gpio_irq_t
*obj
, PinName pin
, gpio_irq_handler handler
, uint32_t id
) {
83 irq_handler
= handler
;
84 obj
->port
= pin
>> GPIO_PORT_SHIFT
;
85 obj
->pin
= pin
& 0x7F;
88 uint32_t vector
= (uint32_t)gpio_irqA
;
89 IRQn_Type irq_n
= PORTA_IRQn
;
94 vector
= (uint32_t)gpio_irqA
;
99 vector
= (uint32_t)gpio_irqB
;
104 vector
= (uint32_t)gpio_irqC
;
109 vector
= (uint32_t)gpio_irqD
;
114 vector
= (uint32_t)gpio_irqE
;
118 error("gpio_irq only supported on port A-E.");
121 NVIC_SetVector(irq_n
, vector
);
122 NVIC_EnableIRQ(irq_n
);
124 obj
->ch
= ch_base
+ obj
->pin
;
125 channel_ids
[obj
->ch
] = id
;
130 void gpio_irq_free(gpio_irq_t
*obj
) {
131 channel_ids
[obj
->ch
] = 0;
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
;
138 switch (BR_PORT_PCRn_IRQC(port_addrs
[obj
->port
], obj
->pin
)) {
141 irq_settings
= (event
== IRQ_RISE
) ? (kPortIntRisingEdge
) : (kPortIntFallingEdge
);
144 case IRQ_RAISING_EDGE
:
146 irq_settings
= (event
== IRQ_RISE
) ? (kPortIntRisingEdge
) : (kPortIntEitherEdge
);
148 if (event
== IRQ_FALL
)
149 irq_settings
= kPortIntRisingEdge
;
153 case IRQ_FALLING_EDGE
:
155 irq_settings
= (event
== IRQ_FALL
) ? (kPortIntFallingEdge
) : (kPortIntEitherEdge
);
157 if (event
== IRQ_RISE
)
158 irq_settings
= kPortIntFallingEdge
;
162 case IRQ_EITHER_EDGE
:
164 irq_settings
= kPortIntEitherEdge
;
166 irq_settings
= (event
== IRQ_RISE
) ? (kPortIntFallingEdge
) : (kPortIntRisingEdge
);
171 PORT_HAL_SetPinIntMode(port_addrs
[obj
->port
], obj
->pin
, irq_settings
);
172 PORT_HAL_ClearPinIntFlag(port_addrs
[obj
->port
], obj
->pin
);
175 void gpio_irq_enable(gpio_irq_t
*obj
) {
178 NVIC_EnableIRQ(PORTA_IRQn
);
181 NVIC_EnableIRQ(PORTB_IRQn
);
184 NVIC_EnableIRQ(PORTC_IRQn
);
187 NVIC_EnableIRQ(PORTD_IRQn
);
190 NVIC_EnableIRQ(PORTE_IRQn
);
195 void gpio_irq_disable(gpio_irq_t
*obj
) {
198 NVIC_DisableIRQ(PORTA_IRQn
);
201 NVIC_DisableIRQ(PORTB_IRQn
);
204 NVIC_DisableIRQ(PORTC_IRQn
);
207 NVIC_DisableIRQ(PORTD_IRQn
);
210 NVIC_DisableIRQ(PORTE_IRQn
);