Skip to content

Commit fcb5f48

Browse files
committed
bsp: ab32vg1 board support gpio interrupt
1 parent 7598581 commit fcb5f48

File tree

3 files changed

+240
-8
lines changed

3 files changed

+240
-8
lines changed

bsp/bluetrum/libraries/hal_drivers/drv_gpio.c

Lines changed: 214 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,224 @@ 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+
RT_SECTION(".irq.gpio")
200+
static void ab32_pin_irq_handler(void)
201+
{
202+
rt_interrupt_enter();
203+
rt_uint32_t pending = WKUPEDG;
204+
rt_uint8_t circuit = 0;
205+
206+
for (circuit = 0; circuit <= WAKEUP_CRICUIT_7; circuit++)
207+
{
208+
if (pending & (BIT(circuit) << WAKEUP_INT_ENABLE))
209+
{
210+
/* clear pending interrupt */
211+
WKUPCPND = (BIT(circuit) << WAKEUP_INT_ENABLE);
212+
rt_mq_send(gpio_irq_mq, &circuit, sizeof(circuit));
213+
}
214+
}
215+
rt_interrupt_leave();
216+
}
217+
218+
static void ab32_pin_irq_thread(void *parameter)
152219
{
153-
return -RT_ERROR;
220+
rt_uint8_t circuit;
221+
while (1)
222+
{
223+
if (rt_mq_recv(gpio_irq_mq, &circuit, sizeof(circuit), RT_WAITING_FOREVER) == RT_EOK)
224+
{
225+
if (circuit <= WAKEUP_CRICUIT_7 && pin_irq_table[circuit].hdr)
226+
{
227+
pin_irq_table[circuit].hdr(pin_irq_table[circuit].args);
228+
}
229+
}
230+
}
154231
}
155232

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

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

167373
const static struct rt_pin_ops _ab32_pin_ops =
168374
{

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)