1 /*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include "hardware/gpio.h"
8 #include "hardware/sync.h"
9
10 #include "hardware/structs/iobank0.h"
11 #include "hardware/irq.h"
12
13 #if LIB_PICO_BINARY_INFO
14 #include "pico/binary_info.h"
15 #endif
16
17 static gpio_irq_callback_t callbacks[NUM_CORES];
18 // a 1 bit means the IRQ is handled by a raw IRQ handler
19 static uint32_t raw_irq_mask[NUM_CORES];
20
21 // Get the raw value from the pin, bypassing any muxing or overrides.
gpio_get_pad(uint gpio)22 int gpio_get_pad(uint gpio) {
23 check_gpio_param(gpio);
24 hw_set_bits(&padsbank0_hw->io[gpio], PADS_BANK0_GPIO0_IE_BITS);
25 return (iobank0_hw->io[gpio].status & IO_BANK0_GPIO0_STATUS_INFROMPAD_BITS)
26 >> IO_BANK0_GPIO0_STATUS_INFROMPAD_LSB;
27 }
28
29 /// \tag::gpio_set_function[]
30 // Select function for this GPIO, and ensure input/output are enabled at the pad.
31 // This also clears the input/output/irq override bits.
gpio_set_function(uint gpio,enum gpio_function fn)32 void gpio_set_function(uint gpio, enum gpio_function fn) {
33 check_gpio_param(gpio);
34 invalid_params_if(GPIO, ((uint32_t)fn << IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB) & ~IO_BANK0_GPIO0_CTRL_FUNCSEL_BITS);
35 // Set input enable on, output disable off
36 hw_write_masked(&padsbank0_hw->io[gpio],
37 PADS_BANK0_GPIO0_IE_BITS,
38 PADS_BANK0_GPIO0_IE_BITS | PADS_BANK0_GPIO0_OD_BITS
39 );
40 // Zero all fields apart from fsel; we want this IO to do what the peripheral tells it.
41 // This doesn't affect e.g. pullup/pulldown, as these are in pad controls.
42 iobank0_hw->io[gpio].ctrl = fn << IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB;
43 }
44 /// \end::gpio_set_function[]
45
gpio_get_function(uint gpio)46 enum gpio_function gpio_get_function(uint gpio) {
47 check_gpio_param(gpio);
48 return (enum gpio_function) ((iobank0_hw->io[gpio].ctrl & IO_BANK0_GPIO0_CTRL_FUNCSEL_BITS) >> IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB);
49 }
50
51 // Note that, on RP2040, setting both pulls enables a "bus keep" function,
52 // i.e. weak pull to whatever is current high/low state of GPIO.
gpio_set_pulls(uint gpio,bool up,bool down)53 void gpio_set_pulls(uint gpio, bool up, bool down) {
54 check_gpio_param(gpio);
55 hw_write_masked(
56 &padsbank0_hw->io[gpio],
57 (bool_to_bit(up) << PADS_BANK0_GPIO0_PUE_LSB) | (bool_to_bit(down) << PADS_BANK0_GPIO0_PDE_LSB),
58 PADS_BANK0_GPIO0_PUE_BITS | PADS_BANK0_GPIO0_PDE_BITS
59 );
60 }
61
62 // Direct override for per-GPIO IRQ signal
gpio_set_irqover(uint gpio,uint value)63 void gpio_set_irqover(uint gpio, uint value) {
64 check_gpio_param(gpio);
65 hw_write_masked(&iobank0_hw->io[gpio].ctrl,
66 value << IO_BANK0_GPIO0_CTRL_IRQOVER_LSB,
67 IO_BANK0_GPIO0_CTRL_IRQOVER_BITS
68 );
69 }
70
71 // Direct overrides for pad controls
gpio_set_inover(uint gpio,uint value)72 void gpio_set_inover(uint gpio, uint value) {
73 check_gpio_param(gpio);
74 hw_write_masked(&iobank0_hw->io[gpio].ctrl,
75 value << IO_BANK0_GPIO0_CTRL_INOVER_LSB,
76 IO_BANK0_GPIO0_CTRL_INOVER_BITS
77 );
78 }
79
gpio_set_outover(uint gpio,uint value)80 void gpio_set_outover(uint gpio, uint value) {
81 check_gpio_param(gpio);
82 hw_write_masked(&iobank0_hw->io[gpio].ctrl,
83 value << IO_BANK0_GPIO0_CTRL_OUTOVER_LSB,
84 IO_BANK0_GPIO0_CTRL_OUTOVER_BITS
85 );
86 }
87
gpio_set_oeover(uint gpio,uint value)88 void gpio_set_oeover(uint gpio, uint value) {
89 check_gpio_param(gpio);
90 hw_write_masked(&iobank0_hw->io[gpio].ctrl,
91 value << IO_BANK0_GPIO0_CTRL_OEOVER_LSB,
92 IO_BANK0_GPIO0_CTRL_OEOVER_BITS
93 );
94 }
95
gpio_set_input_hysteresis_enabled(uint gpio,bool enabled)96 void gpio_set_input_hysteresis_enabled(uint gpio, bool enabled) {
97 check_gpio_param(gpio);
98 if (enabled)
99 hw_set_bits(&padsbank0_hw->io[gpio], PADS_BANK0_GPIO0_SCHMITT_BITS);
100 else
101 hw_clear_bits(&padsbank0_hw->io[gpio], PADS_BANK0_GPIO0_SCHMITT_BITS);
102 }
103
104
gpio_is_input_hysteresis_enabled(uint gpio)105 bool gpio_is_input_hysteresis_enabled(uint gpio) {
106 check_gpio_param(gpio);
107 return (padsbank0_hw->io[gpio] & PADS_BANK0_GPIO0_SCHMITT_BITS) != 0;
108 }
109
gpio_set_slew_rate(uint gpio,enum gpio_slew_rate slew)110 void gpio_set_slew_rate(uint gpio, enum gpio_slew_rate slew) {
111 check_gpio_param(gpio);
112 hw_write_masked(&padsbank0_hw->io[gpio],
113 (uint)slew << PADS_BANK0_GPIO0_SLEWFAST_LSB,
114 PADS_BANK0_GPIO0_SLEWFAST_BITS
115 );
116 }
117
gpio_get_slew_rate(uint gpio)118 enum gpio_slew_rate gpio_get_slew_rate(uint gpio) {
119 check_gpio_param(gpio);
120 return (enum gpio_slew_rate)((padsbank0_hw->io[gpio]
121 & PADS_BANK0_GPIO0_SLEWFAST_BITS)
122 >> PADS_BANK0_GPIO0_SLEWFAST_LSB);
123 }
124
125
126 // Enum encoding should match hardware encoding on RP2040
127 static_assert(PADS_BANK0_GPIO0_DRIVE_VALUE_8MA == GPIO_DRIVE_STRENGTH_8MA, "");
gpio_set_drive_strength(uint gpio,enum gpio_drive_strength drive)128 void gpio_set_drive_strength(uint gpio, enum gpio_drive_strength drive) {
129 check_gpio_param(gpio);
130 hw_write_masked(&padsbank0_hw->io[gpio],
131 (uint)drive << PADS_BANK0_GPIO0_DRIVE_LSB,
132 PADS_BANK0_GPIO0_DRIVE_BITS
133 );
134 }
135
gpio_get_drive_strength(uint gpio)136 enum gpio_drive_strength gpio_get_drive_strength(uint gpio) {
137 check_gpio_param(gpio);
138 return (enum gpio_drive_strength)((padsbank0_hw->io[gpio]
139 & PADS_BANK0_GPIO0_DRIVE_BITS)
140 >> PADS_BANK0_GPIO0_DRIVE_LSB);
141 }
142
gpio_default_irq_handler(void)143 static void gpio_default_irq_handler(void) {
144 uint core = get_core_num();
145 gpio_irq_callback_t callback = callbacks[core];
146 io_irq_ctrl_hw_t *irq_ctrl_base = core ? &iobank0_hw->proc1_irq_ctrl : &iobank0_hw->proc0_irq_ctrl;
147 for (uint gpio = 0; gpio < NUM_BANK0_GPIOS; gpio+=8) {
148 uint32_t events8 = irq_ctrl_base->ints[gpio >> 3u];
149 // note we assume events8 is 0 for non-existent GPIO
150 for(uint i=gpio;events8 && i<gpio+8;i++) {
151 uint32_t events = events8 & 0xfu;
152 if (events && !(raw_irq_mask[core] & (1u << i))) {
153 gpio_acknowledge_irq(i, events);
154 if (callback) {
155 callback(i, events);
156 }
157 }
158 events8 >>= 4;
159 }
160 }
161 }
162
_gpio_set_irq_enabled(uint gpio,uint32_t events,bool enabled,io_irq_ctrl_hw_t * irq_ctrl_base)163 static void _gpio_set_irq_enabled(uint gpio, uint32_t events, bool enabled, io_irq_ctrl_hw_t *irq_ctrl_base) {
164 // Clear stale events which might cause immediate spurious handler entry
165 gpio_acknowledge_irq(gpio, events);
166
167 io_rw_32 *en_reg = &irq_ctrl_base->inte[gpio / 8];
168 events <<= 4 * (gpio % 8);
169
170 if (enabled)
171 hw_set_bits(en_reg, events);
172 else
173 hw_clear_bits(en_reg, events);
174 }
175
gpio_set_irq_enabled(uint gpio,uint32_t events,bool enabled)176 void gpio_set_irq_enabled(uint gpio, uint32_t events, bool enabled) {
177 // Separate mask/force/status per-core, so check which core called, and
178 // set the relevant IRQ controls.
179 io_irq_ctrl_hw_t *irq_ctrl_base = get_core_num() ?
180 &iobank0_hw->proc1_irq_ctrl : &iobank0_hw->proc0_irq_ctrl;
181 _gpio_set_irq_enabled(gpio, events, enabled, irq_ctrl_base);
182 }
183
gpio_set_irq_enabled_with_callback(uint gpio,uint32_t events,bool enabled,gpio_irq_callback_t callback)184 void gpio_set_irq_enabled_with_callback(uint gpio, uint32_t events, bool enabled, gpio_irq_callback_t callback) {
185 gpio_set_irq_enabled(gpio, events, enabled);
186 gpio_set_irq_callback(callback);
187 if (enabled) irq_set_enabled(IO_IRQ_BANK0, true);
188 }
189
gpio_set_irq_callback(gpio_irq_callback_t callback)190 void gpio_set_irq_callback(gpio_irq_callback_t callback) {
191 uint core = get_core_num();
192 if (callbacks[core]) {
193 if (!callback) {
194 irq_remove_handler(IO_IRQ_BANK0, gpio_default_irq_handler);
195 }
196 callbacks[core] = callback;
197 } else if (callback) {
198 callbacks[core] = callback;
199 irq_add_shared_handler(IO_IRQ_BANK0, gpio_default_irq_handler, GPIO_IRQ_CALLBACK_ORDER_PRIORITY);
200 }
201 }
202
gpio_add_raw_irq_handler_with_order_priority_masked(uint gpio_mask,irq_handler_t handler,uint8_t order_priority)203 void gpio_add_raw_irq_handler_with_order_priority_masked(uint gpio_mask, irq_handler_t handler, uint8_t order_priority) {
204 hard_assert(!(raw_irq_mask[get_core_num()] & gpio_mask)); // should not add multiple handlers for the same event
205 raw_irq_mask[get_core_num()] |= gpio_mask;
206 irq_add_shared_handler(IO_IRQ_BANK0, handler, order_priority);
207 }
208
gpio_add_raw_irq_handler_masked(uint gpio_mask,irq_handler_t handler)209 void gpio_add_raw_irq_handler_masked(uint gpio_mask, irq_handler_t handler) {
210 gpio_add_raw_irq_handler_with_order_priority_masked(gpio_mask, handler, GPIO_RAW_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY);
211 }
212
gpio_remove_raw_irq_handler_masked(uint gpio_mask,irq_handler_t handler)213 void gpio_remove_raw_irq_handler_masked(uint gpio_mask, irq_handler_t handler) {
214 assert(raw_irq_mask[get_core_num()] & gpio_mask); // should not remove handlers that are not added
215 irq_remove_handler(IO_IRQ_BANK0, handler);
216 raw_irq_mask[get_core_num()] &= ~gpio_mask;
217 }
218
gpio_set_dormant_irq_enabled(uint gpio,uint32_t events,bool enabled)219 void gpio_set_dormant_irq_enabled(uint gpio, uint32_t events, bool enabled) {
220 check_gpio_param(gpio);
221 io_irq_ctrl_hw_t *irq_ctrl_base = &iobank0_hw->dormant_wake_irq_ctrl;
222 _gpio_set_irq_enabled(gpio, events, enabled, irq_ctrl_base);
223 }
224
gpio_acknowledge_irq(uint gpio,uint32_t events)225 void gpio_acknowledge_irq(uint gpio, uint32_t events) {
226 check_gpio_param(gpio);
227 iobank0_hw->intr[gpio / 8] = events << (4 * (gpio % 8));
228 }
229
230 #define DEBUG_PIN_MASK (((1u << PICO_DEBUG_PIN_COUNT)-1) << PICO_DEBUG_PIN_BASE)
gpio_debug_pins_init()231 void gpio_debug_pins_init() {
232 gpio_init_mask(DEBUG_PIN_MASK);
233 gpio_set_dir_masked(DEBUG_PIN_MASK, DEBUG_PIN_MASK);
234 #if LIB_PICO_BINARY_INFO
235 bi_decl_if_func_used(bi_pin_mask_with_names(DEBUG_PIN_MASK, "Debug"));
236 #endif
237 }
238
gpio_set_input_enabled(uint gpio,bool enabled)239 void gpio_set_input_enabled(uint gpio, bool enabled) {
240 if (enabled)
241 hw_set_bits(&padsbank0_hw->io[gpio], PADS_BANK0_GPIO0_IE_BITS);
242 else
243 hw_clear_bits(&padsbank0_hw->io[gpio], PADS_BANK0_GPIO0_IE_BITS);
244 }
245
gpio_init(uint gpio)246 void gpio_init(uint gpio) {
247 sio_hw->gpio_oe_clr = 1ul << gpio;
248 sio_hw->gpio_clr = 1ul << gpio;
249 gpio_set_function(gpio, GPIO_FUNC_SIO);
250 }
251
gpio_deinit(uint gpio)252 void gpio_deinit(uint gpio) {
253 gpio_set_function(gpio, GPIO_FUNC_NULL);
254 }
255
gpio_init_mask(uint gpio_mask)256 void gpio_init_mask(uint gpio_mask) {
257 for(uint i=0;i<NUM_BANK0_GPIOS;i++) {
258 if (gpio_mask & 1) {
259 gpio_init(i);
260 }
261 gpio_mask >>= 1;
262 }
263 }
264
265