Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 | |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 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]; |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 20 | |
| 21 | // Get the raw value from the pin, bypassing any muxing or overrides. |
| 22 | int gpio_get_pad(uint gpio) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 23 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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. |
| 32 | void gpio_set_function(uint gpio, enum gpio_function fn) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 33 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 | |
| 46 | enum gpio_function gpio_get_function(uint gpio) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 47 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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. |
| 53 | void gpio_set_pulls(uint gpio, bool up, bool down) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 54 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 |
| 63 | void gpio_set_irqover(uint gpio, uint value) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 64 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 |
| 72 | void gpio_set_inover(uint gpio, uint value) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 73 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 | |
| 80 | void gpio_set_outover(uint gpio, uint value) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 81 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 | |
| 88 | void gpio_set_oeover(uint gpio, uint value) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 89 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 | |
| 96 | void gpio_set_input_hysteresis_enabled(uint gpio, bool enabled) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 97 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 | |
| 105 | bool gpio_is_input_hysteresis_enabled(uint gpio) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 106 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 107 | return (padsbank0_hw->io[gpio] & PADS_BANK0_GPIO0_SCHMITT_BITS) != 0; |
| 108 | } |
| 109 | |
| 110 | void gpio_set_slew_rate(uint gpio, enum gpio_slew_rate slew) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 111 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 | |
| 118 | enum gpio_slew_rate gpio_get_slew_rate(uint gpio) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 119 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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, ""); |
| 128 | void gpio_set_drive_strength(uint gpio, enum gpio_drive_strength drive) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 129 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 | |
| 136 | enum gpio_drive_strength gpio_get_drive_strength(uint gpio) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 137 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 138 | return (enum gpio_drive_strength)((padsbank0_hw->io[gpio] |
| 139 | & PADS_BANK0_GPIO0_DRIVE_BITS) |
| 140 | >> PADS_BANK0_GPIO0_DRIVE_LSB); |
| 141 | } |
| 142 | |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 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 | } |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 157 | } |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 158 | events8 >>= 4; |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 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 | |
| 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 | |
| 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); |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 186 | gpio_set_irq_callback(callback); |
| 187 | if (enabled) irq_set_enabled(IO_IRQ_BANK0, true); |
| 188 | } |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 189 | |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 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 | |
| 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 | |
| 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 | |
| 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; |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void gpio_set_dormant_irq_enabled(uint gpio, uint32_t events, bool enabled) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 220 | check_gpio_param(gpio); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 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 | |
| 225 | void gpio_acknowledge_irq(uint gpio, uint32_t events) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 226 | check_gpio_param(gpio); |
| 227 | iobank0_hw->intr[gpio / 8] = events << (4 * (gpio % 8)); |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | #define DEBUG_PIN_MASK (((1u << PICO_DEBUG_PIN_COUNT)-1) << PICO_DEBUG_PIN_BASE) |
| 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 | |
| 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 | |
| 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 | |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 252 | void gpio_deinit(uint gpio) { |
| 253 | gpio_set_function(gpio, GPIO_FUNC_NULL); |
| 254 | } |
| 255 | |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 256 | void gpio_init_mask(uint gpio_mask) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 257 | for(uint i=0;i<NUM_BANK0_GPIOS;i++) { |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 258 | if (gpio_mask & 1) { |
| 259 | gpio_init(i); |
| 260 | } |
| 261 | gpio_mask >>= 1; |
| 262 | } |
| 263 | } |
| 264 | |