forked from arduino/ArduinoCore-mbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0037-RP2040-Add-gpio-interrupt-support.patch
152 lines (145 loc) · 3.47 KB
/
0037-RP2040-Add-gpio-interrupt-support.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
From f1b4a2d92d503d4d2cebbbe297dc36d3406f2fc9 Mon Sep 17 00:00:00 2001
From: giulcioffi <g.cioffi@arduino.cc>
Date: Fri, 5 Mar 2021 11:46:46 +0100
Subject: [PATCH 037/204] RP2040: Add gpio interrupt support
---
.../TARGET_RP2040/gpio_api.c | 105 +++++++++++++++++-
1 file changed, 104 insertions(+), 1 deletion(-)
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/gpio_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/gpio_api.c
index 3d09714881..a10a7d6c89 100644
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/gpio_api.c
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/gpio_api.c
@@ -1,8 +1,15 @@
#include "mbed_assert.h"
#include "hal/gpio_api.h"
+#include "gpio_irq_api.h"
#include "pinmap.h"
#include "mbed_error.h"
+#define GPIO_PIN_COUNT 30
+
+static gpio_irq_handler m_irq_handler;
+static uint32_t m_channel_ids[GPIO_PIN_COUNT] = {0};
+static uint32_t m_pico_events[GPIO_PIN_COUNT] = {0};
+
void gpio_write(gpio_t *obj, int value)
{
gpio_put(obj->pin, value);
@@ -16,16 +23,37 @@ int gpio_read(gpio_t *obj)
void gpio_init(gpio_t *obj, PinName pin)
{
obj->pin = pin;
+
+ if (pin == (PinName)NC) {
+ return;
+ }
+
_gpio_init(obj->pin);
}
+static uint32_t gpio_convert_event(gpio_irq_event event)
+{
+ uint32_t irq_event = 0;
+
+ if (event == IRQ_RISE) {
+ irq_event = GPIO_IRQ_EDGE_RISE;
+ } else if (event == IRQ_FALL) {
+ irq_event = GPIO_IRQ_EDGE_FALL;
+ }
+
+ return irq_event;
+}
+
void gpio_mode(gpio_t *obj, PinMode mode)
{
+ MBED_ASSERT(obj->pin != (PinName)NC);
+
gpio_set_pulls(obj->pin, mode == PullUp, mode == PullDown);
}
void gpio_dir(gpio_t *obj, PinDirection direction)
{
+ MBED_ASSERT(obj->pin != (PinName)NC);
obj->direction = direction;
if (direction == PIN_OUTPUT) {
gpio_set_dir(obj->pin, GPIO_OUT);
@@ -37,4 +65,79 @@ void gpio_dir(gpio_t *obj, PinDirection direction)
int gpio_is_connected(const gpio_t *obj) {
return (obj->pin == NC ? 0 : 1);
-}
\ No newline at end of file
+}
+
+
+/***********
+ GPIO IRQ
+***********/
+
+#if DEVICE_INTERRUPTIN
+
+static void _gpio_irq(uint gpio, uint32_t events)
+{
+ gpio_irq_event ev;
+ if (events == GPIO_IRQ_EDGE_RISE) {
+ ev = IRQ_RISE;
+ } else if (events == GPIO_IRQ_EDGE_FALL) {
+ ev = IRQ_FALL;
+ } else {
+ ev = IRQ_NONE;
+ }
+ m_irq_handler(m_channel_ids[gpio], ev);
+}
+
+int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
+{
+ if (pin == NC) {
+ return -1;
+ }
+ MBED_ASSERT((uint32_t)pin < GPIO_PIN_COUNT);
+
+ m_channel_ids[pin] = id;
+ m_irq_handler = handler;
+
+ obj->irq_n = IO_IRQ_BANK0;
+ obj->pin = pin;
+ obj->irq_index = id;
+
+ return 0;
+}
+
+
+void gpio_irq_free(gpio_irq_t *obj)
+{
+ gpio_irq_disable(obj);
+ obj->irq_n = 0;
+ obj->pin = 0;
+ obj->irq_index = 0;
+}
+
+
+void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
+{
+ uint32_t irq_event = gpio_convert_event(event);
+
+ if (enable) {
+ m_pico_events[obj->pin] |= irq_event;
+ obj->event = irq_event;
+ gpio_irq_enable(obj);
+ }
+}
+
+
+void gpio_irq_enable(gpio_irq_t *obj)
+{
+ gpio_set_irq_enabled_with_callback(obj->pin, obj->event, true, _gpio_irq);
+}
+
+
+void gpio_irq_disable(gpio_irq_t *obj)
+{
+ gpio_set_irq_enabled(obj->pin, m_pico_events[obj->pin], false);
+
+ obj->event = 0;
+ m_pico_events[obj->pin] = 0;
+}
+
+#endif
--
2.39.1