Skip to content

Commit 05699d6

Browse files
authored
bsp: ab32vg1 board support gpio interrupt (#10280)
1 parent 9c3ed20 commit 05699d6

File tree

3 files changed

+239
-8
lines changed

3 files changed

+239
-8
lines changed

bsp/bluetrum/libraries/hal_drivers/drv_gpio.c

Lines changed: 213 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2020-11-19 greedyhao first version
9+
* 2025-05-15 kurisaW support gpio interrupt
910
*/
1011

1112
#include "drv_gpio.h"
@@ -40,6 +41,9 @@ static const hal_sfr_t port_sfr[] =
4041
GPIOF_BASE,
4142
};
4243

44+
static struct ab32_pin_irq pin_irq_table[8] = {0}; // For WAKEUP_CRICUIT_0 to _7
45+
static rt_mq_t gpio_irq_mq = RT_NULL;
46+
4347
static rt_uint8_t _pin_port(rt_uint32_t pin)
4448
{
4549
rt_uint8_t port = 0;
@@ -99,7 +103,7 @@ static rt_base_t ab32_pin_get(const char *name)
99103
return pin;
100104
}
101105

102-
static void ab32_pin_write(rt_device_t dev, rt_base_t pin, rt_uint8_t value)
106+
static void ab32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
103107
{
104108
rt_uint8_t port = PIN_PORT(pin);
105109
rt_uint8_t gpio_pin = pin - port_table[port].total_pin;
@@ -147,22 +151,223 @@ static void ab32_pin_mode(rt_device_t dev, rt_base_t pin, rt_uint8_t mode)
147151
hal_gpio_init(PORT_SFR(port), &gpio_init);
148152
}
149153

150-
static rt_err_t ab32_pin_attach_irq(struct rt_device *device, rt_base_t pin,
151-
rt_uint8_t mode, void (*hdr)(void *args), void *args)
154+
static rt_err_t get_port_pin(rt_base_t pin, rt_uint8_t *port, rt_uint8_t *hw_pin)
155+
{
156+
rt_uint8_t i;
157+
for (i = 0; i < sizeof(port_table) / sizeof(port_table[0]); i++)
158+
{
159+
if (pin >= port_table[i].total_pin &&
160+
pin < port_table[i].total_pin + port_table[i].delta_pin)
161+
{
162+
*port = i;
163+
*hw_pin = pin - port_table[i].total_pin + port_table[i].start_pin;
164+
return RT_EOK;
165+
}
166+
}
167+
return -RT_EINVAL;
168+
}
169+
170+
static rt_int32_t get_wakeup_circuit(rt_base_t pin)
171+
{
172+
/* Special interrupt pins */
173+
if (pin == PIN_NUM(0, 7)) return WAKEUP_CRICUIT_0; // PA7
174+
if (pin == PIN_NUM(1, 1)) return WAKEUP_CRICUIT_1; // PB1
175+
if (pin == PIN_NUM(1, 2)) return WAKEUP_CRICUIT_2; // PB2
176+
if (pin == PIN_NUM(1, 3)) return WAKEUP_CRICUIT_3; // PB3
177+
if (pin == PIN_NUM(1, 4)) return WAKEUP_CRICUIT_4; // PB4
178+
/* WAKEUP_CRICUIT_5 is for RTC (WKO), assuming not a GPIO pin */
179+
/* Other pins use WAKEUP_CRICUIT_6 (falling) or WAKEUP_CRICUIT_7 (rising) */
180+
return -1; // Will be handled in attach_irq based on mode
181+
}
182+
183+
static rt_uint32_t get_edge_select_bit(rt_uint8_t circuit)
184+
{
185+
switch (circuit)
186+
{
187+
case WAKEUP_CRICUIT_0: return WAKEUP_EDGE_SELECT_0;
188+
case WAKEUP_CRICUIT_1: return WAKEUP_EDGE_SELECT_1;
189+
case WAKEUP_CRICUIT_2: return WAKEUP_EDGE_SELECT_2;
190+
case WAKEUP_CRICUIT_3: return WAKEUP_EDGE_SELECT_3;
191+
case WAKEUP_CRICUIT_4: return WAKEUP_EDGE_SELECT_4;
192+
case WAKEUP_CRICUIT_5: return WAKEUP_EDGE_SELECT_5;
193+
case WAKEUP_CRICUIT_6: return WAKEUP_EDGE_SELECT_6;
194+
case WAKEUP_CRICUIT_7: return WAKEUP_EDGE_SELECT_7;
195+
default: return 0;
196+
}
197+
}
198+
199+
void __attribute__((section(".irq.gpio"))) ab32_pin_irq_handler(void *args)
200+
{
201+
rt_interrupt_enter();
202+
rt_uint32_t pending = WKUPEDG;
203+
rt_uint8_t circuit = 0;
204+
205+
for (circuit = 0; circuit <= WAKEUP_CRICUIT_7; circuit++)
206+
{
207+
if (pending & (BIT(circuit) << WAKEUP_INT_ENABLE))
208+
{
209+
/* clear pending interrupt */
210+
WKUPCPND = (BIT(circuit) << WAKEUP_INT_ENABLE);
211+
rt_mq_send(gpio_irq_mq, &circuit, sizeof(circuit));
212+
}
213+
}
214+
rt_interrupt_leave();
215+
}
216+
217+
static void ab32_pin_irq_thread(void *parameter)
152218
{
153-
return -RT_ERROR;
219+
rt_uint8_t circuit;
220+
while (1)
221+
{
222+
if (rt_mq_recv(gpio_irq_mq, &circuit, sizeof(circuit), RT_WAITING_FOREVER) == RT_EOK)
223+
{
224+
if (circuit <= WAKEUP_CRICUIT_7 && pin_irq_table[circuit].hdr)
225+
{
226+
pin_irq_table[circuit].hdr(pin_irq_table[circuit].args);
227+
}
228+
}
229+
}
154230
}
155231

156-
static rt_err_t ab32_pin_dettach_irq(struct rt_device *device, rt_base_t pin)
232+
static rt_err_t ab32_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
233+
rt_uint32_t mode, void (*hdr)(void *args), void *args)
157234
{
158-
return -RT_ERROR;
235+
rt_uint8_t port, hw_pin;
236+
rt_int32_t circuit;
237+
uint8_t pin_mode;
238+
239+
if (get_port_pin(pin, &port, &hw_pin) != RT_EOK)
240+
{
241+
return -RT_EINVAL;
242+
}
243+
244+
circuit = get_wakeup_circuit(pin);
245+
if(circuit == -1)
246+
{
247+
circuit = (mode == PIN_IRQ_MODE_FALLING) ? WAKEUP_CRICUIT_6 : WAKEUP_CRICUIT_7;
248+
}
249+
250+
/* store handler and arguments */
251+
pin_irq_table[circuit].hdr = hdr;
252+
pin_irq_table[circuit].args = args;
253+
254+
/* The interrupt source in the port is: Port_intsrc = {PG[4:0], PF[5:0], PE[7:0], PB[4:0], PA[7:0]}. */
255+
/* Such as:the interrupt source number of PA0 is 0, interrupt source number of PG4 is 31. */
256+
PORTINTEN |= BIT(pin); // Enable interrupt
257+
PORTINTEDG |= BIT(pin); // Edge trigger
258+
259+
return RT_EOK;
260+
}
261+
262+
static rt_err_t ab32_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
263+
{
264+
rt_uint8_t port, hw_pin;
265+
rt_int32_t circuit;
266+
267+
if (get_port_pin(pin, &port, &hw_pin) != RT_EOK)
268+
{
269+
return -RT_EINVAL;
270+
}
271+
272+
circuit = get_wakeup_circuit(pin);
273+
if (circuit < 0)
274+
{
275+
/* assume previously assigned to WAKEUP_CRICUIT_6 or _7 */
276+
/* check both circuits for handler */
277+
if (pin_irq_table[WAKEUP_CRICUIT_6].hdr)
278+
circuit = WAKEUP_CRICUIT_6;
279+
else if (pin_irq_table[WAKEUP_CRICUIT_7].hdr)
280+
circuit = WAKEUP_CRICUIT_7;
281+
else
282+
return RT_EOK;
283+
}
284+
285+
PORTINTEN &= ~BIT(circuit);
286+
WKUPCON &= ~BIT(circuit);
287+
WKUPCPND = BIT(get_edge_select_bit(circuit));
288+
289+
/* clear handler */
290+
pin_irq_table[circuit].hdr = RT_NULL;
291+
pin_irq_table[circuit].args = RT_NULL;
292+
293+
return RT_EOK;
159294
}
160295

161296
static rt_err_t ab32_pin_irq_enable(struct rt_device *device, rt_base_t pin,
162-
rt_uint8_t enabled)
297+
rt_uint32_t enabled)
163298
{
164-
return -RT_ERROR;
299+
rt_uint8_t port, hw_pin;
300+
rt_int32_t circuit;
301+
302+
if (get_port_pin(pin, &port, &hw_pin) != RT_EOK)
303+
{
304+
return -RT_EINVAL;
305+
}
306+
307+
circuit = get_wakeup_circuit(pin);
308+
if (circuit < 0)
309+
{
310+
if (pin_irq_table[WAKEUP_CRICUIT_6].hdr)
311+
{
312+
circuit = WAKEUP_CRICUIT_6;
313+
}
314+
else if (pin_irq_table[WAKEUP_CRICUIT_7].hdr)
315+
{
316+
circuit = WAKEUP_CRICUIT_7;
317+
}
318+
else
319+
return -RT_EINVAL;
320+
}
321+
322+
rt_uint32_t edge_bit = get_edge_select_bit(circuit);
323+
324+
if (enabled == RT_TRUE)
325+
{
326+
WKUPEDG = BIT(circuit) | BIT(edge_bit);
327+
WKUPCON = BIT(circuit) | BIT(WAKEUP_INT_ENABLE);
328+
329+
WKUPCPND = BIT(circuit);
330+
331+
/* install interrupt handler */
332+
rt_hw_interrupt_install(IRQ_GPIO_IRQ, ab32_pin_irq_handler, RT_NULL, "gpio_isr");
333+
rt_hw_irq_enable(IRQ_GPIO_IRQ);
334+
}
335+
else
336+
{
337+
/* disable interrupt */
338+
WKUPCON &= ~BIT(circuit);
339+
}
340+
341+
return RT_EOK;
342+
}
343+
344+
static int ab32_pin_irq_init(void)
345+
{
346+
gpio_irq_mq = rt_mq_create("gpio_irq", sizeof(rt_uint8_t), 128, RT_IPC_FLAG_FIFO);
347+
if (gpio_irq_mq == RT_NULL)
348+
{
349+
return -RT_ENOMEM;
350+
}
351+
352+
rt_thread_t tid = rt_thread_create("gpio_irq",
353+
ab32_pin_irq_thread,
354+
RT_NULL,
355+
512,
356+
15,
357+
5);
358+
if (tid != RT_NULL)
359+
{
360+
rt_thread_startup(tid);
361+
}
362+
else
363+
{
364+
rt_mq_delete(gpio_irq_mq);
365+
return -RT_ENOMEM;
366+
}
367+
368+
return RT_EOK;
165369
}
370+
INIT_PREV_EXPORT(ab32_pin_irq_init);
166371

167372
const static struct rt_pin_ops _ab32_pin_ops =
168373
{

bsp/bluetrum/libraries/hal_drivers/drv_gpio.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@
2020
#define __AB32_GET_PIN_E(PIN) 13 + PIN
2121
#define __AB32_GET_PIN_F(PIN) 21 + PIN
2222

23+
#define WAKEUP_INT_ENABLE 16
24+
#define WAKEUP_CRICUIT_0 0 // PA7
25+
#define WAKEUP_CRICUIT_1 1 // PB1
26+
#define WAKEUP_CRICUIT_2 2 // PB2
27+
#define WAKEUP_CRICUIT_3 3 // PB3
28+
#define WAKEUP_CRICUIT_4 4 // PB4
29+
#define WAKEUP_CRICUIT_5 5 // WKO (RTC)
30+
#define WAKEUP_CRICUIT_6 6 // Falling edge for other GPIOs
31+
#define WAKEUP_CRICUIT_7 7 // Rising edge for other GPIOs
32+
#define WAKEUP_EDGE_SELECT_0 16
33+
#define WAKEUP_EDGE_SELECT_1 17
34+
#define WAKEUP_EDGE_SELECT_2 18
35+
#define WAKEUP_EDGE_SELECT_3 19
36+
#define WAKEUP_EDGE_SELECT_4 20
37+
#define WAKEUP_EDGE_SELECT_5 21
38+
#define WAKEUP_EDGE_SELECT_6 22
39+
#define WAKEUP_EDGE_SELECT_7 23
40+
41+
/* structure to store IRQ handler and arguments per pin */
42+
struct ab32_pin_irq
43+
{
44+
void (*hdr)(void *args);
45+
void *args;
46+
};
47+
2348
int rt_hw_pin_init(void);
2449

2550
#endif // DRV_GPIO_H__

bsp/bluetrum/libraries/hal_libraries/bmsis/include/ab32vg1.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef enum
3030
IRQ_HSUART_VECTOR = 15,
3131
IRQ_RTC_VECTOR = 16, /*!< RTC, LVD and WDT Interrupt */
3232
IRQ_I2S_VECTOR = 17,
33+
IRQ_GPIO_IRQ = 18,
3334
IRQ_TOTAL_NUM = 23,
3435
} irq_type;
3536
#endif // __ASSEMBLER__

0 commit comments

Comments
 (0)