blob: 6e4261fcb19de8740dfd03fe477a542a388723c3 [file] [log] [blame]
Austin Schuh208337d2022-01-01 14:29:11 -08001/*
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
Ravago Jonesd208ae72023-02-13 02:24:07 -080017static gpio_irq_callback_t callbacks[NUM_CORES];
18// a 1 bit means the IRQ is handled by a raw IRQ handler
19static uint32_t raw_irq_mask[NUM_CORES];
Austin Schuh208337d2022-01-01 14:29:11 -080020
21// Get the raw value from the pin, bypassing any muxing or overrides.
22int gpio_get_pad(uint gpio) {
Ravago Jonesd208ae72023-02-13 02:24:07 -080023 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -080024 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.
32void gpio_set_function(uint gpio, enum gpio_function fn) {
Ravago Jonesd208ae72023-02-13 02:24:07 -080033 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -080034 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
46enum gpio_function gpio_get_function(uint gpio) {
Ravago Jonesd208ae72023-02-13 02:24:07 -080047 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -080048 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.
53void gpio_set_pulls(uint gpio, bool up, bool down) {
Ravago Jonesd208ae72023-02-13 02:24:07 -080054 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -080055 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
63void gpio_set_irqover(uint gpio, uint value) {
Ravago Jonesd208ae72023-02-13 02:24:07 -080064 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -080065 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
72void gpio_set_inover(uint gpio, uint value) {
Ravago Jonesd208ae72023-02-13 02:24:07 -080073 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -080074 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
80void gpio_set_outover(uint gpio, uint value) {
Ravago Jonesd208ae72023-02-13 02:24:07 -080081 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -080082 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
88void gpio_set_oeover(uint gpio, uint value) {
Ravago Jonesd208ae72023-02-13 02:24:07 -080089 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -080090 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
96void gpio_set_input_hysteresis_enabled(uint gpio, bool enabled) {
Ravago Jonesd208ae72023-02-13 02:24:07 -080097 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -080098 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
105bool gpio_is_input_hysteresis_enabled(uint gpio) {
Ravago Jonesd208ae72023-02-13 02:24:07 -0800106 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -0800107 return (padsbank0_hw->io[gpio] & PADS_BANK0_GPIO0_SCHMITT_BITS) != 0;
108}
109
110void gpio_set_slew_rate(uint gpio, enum gpio_slew_rate slew) {
Ravago Jonesd208ae72023-02-13 02:24:07 -0800111 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -0800112 hw_write_masked(&padsbank0_hw->io[gpio],
113 (uint)slew << PADS_BANK0_GPIO0_SLEWFAST_LSB,
114 PADS_BANK0_GPIO0_SLEWFAST_BITS
115 );
116}
117
118enum gpio_slew_rate gpio_get_slew_rate(uint gpio) {
Ravago Jonesd208ae72023-02-13 02:24:07 -0800119 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -0800120 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
127static_assert(PADS_BANK0_GPIO0_DRIVE_VALUE_8MA == GPIO_DRIVE_STRENGTH_8MA, "");
128void gpio_set_drive_strength(uint gpio, enum gpio_drive_strength drive) {
Ravago Jonesd208ae72023-02-13 02:24:07 -0800129 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -0800130 hw_write_masked(&padsbank0_hw->io[gpio],
131 (uint)drive << PADS_BANK0_GPIO0_DRIVE_LSB,
132 PADS_BANK0_GPIO0_DRIVE_BITS
133 );
134}
135
136enum gpio_drive_strength gpio_get_drive_strength(uint gpio) {
Ravago Jonesd208ae72023-02-13 02:24:07 -0800137 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -0800138 return (enum gpio_drive_strength)((padsbank0_hw->io[gpio]
139 & PADS_BANK0_GPIO0_DRIVE_BITS)
140 >> PADS_BANK0_GPIO0_DRIVE_LSB);
141}
142
Ravago Jonesd208ae72023-02-13 02:24:07 -0800143static 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 }
Austin Schuh208337d2022-01-01 14:29:11 -0800157 }
Ravago Jonesd208ae72023-02-13 02:24:07 -0800158 events8 >>= 4;
Austin Schuh208337d2022-01-01 14:29:11 -0800159 }
160 }
161}
162
163static 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
176void 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
184void 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);
Ravago Jonesd208ae72023-02-13 02:24:07 -0800186 gpio_set_irq_callback(callback);
187 if (enabled) irq_set_enabled(IO_IRQ_BANK0, true);
188}
Austin Schuh208337d2022-01-01 14:29:11 -0800189
Ravago Jonesd208ae72023-02-13 02:24:07 -0800190void 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
203void 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
209void 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
213void 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;
Austin Schuh208337d2022-01-01 14:29:11 -0800217}
218
219void gpio_set_dormant_irq_enabled(uint gpio, uint32_t events, bool enabled) {
Ravago Jonesd208ae72023-02-13 02:24:07 -0800220 check_gpio_param(gpio);
Austin Schuh208337d2022-01-01 14:29:11 -0800221 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
225void gpio_acknowledge_irq(uint gpio, uint32_t events) {
Ravago Jonesd208ae72023-02-13 02:24:07 -0800226 check_gpio_param(gpio);
227 iobank0_hw->intr[gpio / 8] = events << (4 * (gpio % 8));
Austin Schuh208337d2022-01-01 14:29:11 -0800228}
229
230#define DEBUG_PIN_MASK (((1u << PICO_DEBUG_PIN_COUNT)-1) << PICO_DEBUG_PIN_BASE)
231void 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
239void 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
246void 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
Ravago Jonesd208ae72023-02-13 02:24:07 -0800252void gpio_deinit(uint gpio) {
253 gpio_set_function(gpio, GPIO_FUNC_NULL);
254}
255
Austin Schuh208337d2022-01-01 14:29:11 -0800256void gpio_init_mask(uint gpio_mask) {
Ravago Jonesd208ae72023-02-13 02:24:07 -0800257 for(uint i=0;i<NUM_BANK0_GPIOS;i++) {
Austin Schuh208337d2022-01-01 14:29:11 -0800258 if (gpio_mask & 1) {
259 gpio_init(i);
260 }
261 gpio_mask >>= 1;
262 }
263}
264