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 #include "pico/binary_info.h"
14 
15 static gpio_irq_callback_t _callbacks[NUM_CORES];
16 
17 // Get the raw value from the pin, bypassing any muxing or overrides.
gpio_get_pad(uint gpio)18 int gpio_get_pad(uint gpio) {
19     invalid_params_if(GPIO, gpio >= NUM_BANK0_GPIOS);
20     hw_set_bits(&padsbank0_hw->io[gpio], PADS_BANK0_GPIO0_IE_BITS);
21     return (iobank0_hw->io[gpio].status & IO_BANK0_GPIO0_STATUS_INFROMPAD_BITS)
22             >> IO_BANK0_GPIO0_STATUS_INFROMPAD_LSB;
23 }
24 
25 /// \tag::gpio_set_function[]
26 // Select function for this GPIO, and ensure input/output are enabled at the pad.
27 // This also clears the input/output/irq override bits.
gpio_set_function(uint gpio,enum gpio_function fn)28 void gpio_set_function(uint gpio, enum gpio_function fn) {
29     invalid_params_if(GPIO, gpio >= NUM_BANK0_GPIOS);
30     invalid_params_if(GPIO, fn << IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB & ~IO_BANK0_GPIO0_CTRL_FUNCSEL_BITS);
31     // Set input enable on, output disable off
32     hw_write_masked(&padsbank0_hw->io[gpio],
33                    PADS_BANK0_GPIO0_IE_BITS,
34                    PADS_BANK0_GPIO0_IE_BITS | PADS_BANK0_GPIO0_OD_BITS
35     );
36     // Zero all fields apart from fsel; we want this IO to do what the peripheral tells it.
37     // This doesn't affect e.g. pullup/pulldown, as these are in pad controls.
38     iobank0_hw->io[gpio].ctrl = fn << IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB;
39 }
40 /// \end::gpio_set_function[]
41 
gpio_get_function(uint gpio)42 enum gpio_function gpio_get_function(uint gpio) {
43     invalid_params_if(GPIO, gpio >= NUM_BANK0_GPIOS);
44     return (enum gpio_function) ((iobank0_hw->io[gpio].ctrl & IO_BANK0_GPIO0_CTRL_FUNCSEL_BITS) >> IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB);
45 }
46 
47 // Note that, on RP2040, setting both pulls enables a "bus keep" function,
48 // i.e. weak pull to whatever is current high/low state of GPIO.
gpio_set_pulls(uint gpio,bool up,bool down)49 void gpio_set_pulls(uint gpio, bool up, bool down) {
50     invalid_params_if(GPIO, gpio >= NUM_BANK0_GPIOS);
51     hw_write_masked(
52             &padsbank0_hw->io[gpio],
53             (!!up << PADS_BANK0_GPIO0_PUE_LSB) | (!!down << PADS_BANK0_GPIO0_PDE_LSB),
54             PADS_BANK0_GPIO0_PUE_BITS | PADS_BANK0_GPIO0_PDE_BITS
55     );
56 }
57 
58 // Direct overrides for pad controls
gpio_set_inover(uint gpio,uint value)59 void gpio_set_inover(uint gpio, uint value) {
60     invalid_params_if(GPIO, gpio >= NUM_BANK0_GPIOS);
61     hw_write_masked(&iobank0_hw->io[gpio].ctrl,
62                    value << IO_BANK0_GPIO0_CTRL_INOVER_LSB,
63                    IO_BANK0_GPIO0_CTRL_INOVER_BITS
64     );
65 }
66 
gpio_set_outover(uint gpio,uint value)67 void gpio_set_outover(uint gpio, uint value) {
68     invalid_params_if(GPIO, gpio >= NUM_BANK0_GPIOS);
69     hw_write_masked(&iobank0_hw->io[gpio].ctrl,
70                    value << IO_BANK0_GPIO0_CTRL_OUTOVER_LSB,
71                    IO_BANK0_GPIO0_CTRL_OUTOVER_BITS
72     );
73 }
74 
gpio_set_oeover(uint gpio,uint value)75 void gpio_set_oeover(uint gpio, uint value) {
76     invalid_params_if(GPIO, gpio >= NUM_BANK0_GPIOS);
77     hw_write_masked(&iobank0_hw->io[gpio].ctrl,
78                    value << IO_BANK0_GPIO0_CTRL_OEOVER_LSB,
79                    IO_BANK0_GPIO0_CTRL_OEOVER_BITS
80     );
81 }
82 
gpio_irq_handler(void)83 static void gpio_irq_handler(void) {
84     io_irq_ctrl_hw_t *irq_ctrl_base = get_core_num() ?
85                                            &iobank0_hw->proc1_irq_ctrl : &iobank0_hw->proc0_irq_ctrl;
86     for (uint gpio = 0; gpio < NUM_BANK0_GPIOS; gpio++) {
87         io_rw_32 *status_reg = &irq_ctrl_base->ints[gpio / 8];
88         uint events = (*status_reg >> 4 * (gpio % 8)) & 0xf;
89         if (events) {
90             // TODO: If both cores care about this event then the second core won't get the irq?
91             gpio_acknowledge_irq(gpio, events);
92             gpio_irq_callback_t callback = _callbacks[get_core_num()];
93             if (callback) {
94                 callback(gpio, events);
95             }
96         }
97     }
98 }
99 
_gpio_set_irq_enabled(uint gpio,uint32_t events,bool enabled,io_irq_ctrl_hw_t * irq_ctrl_base)100 static void _gpio_set_irq_enabled(uint gpio, uint32_t events, bool enabled, io_irq_ctrl_hw_t *irq_ctrl_base) {
101     // Clear stale events which might cause immediate spurious handler entry
102     gpio_acknowledge_irq(gpio, events);
103 
104     io_rw_32 *en_reg = &irq_ctrl_base->inte[gpio / 8];
105     events <<= 4 * (gpio % 8);
106 
107     if (enabled)
108         hw_set_bits(en_reg, events);
109     else
110         hw_clear_bits(en_reg, events);
111 }
112 
gpio_set_irq_enabled(uint gpio,uint32_t events,bool enabled)113 void gpio_set_irq_enabled(uint gpio, uint32_t events, bool enabled) {
114     // Separate mask/force/status per-core, so check which core called, and
115     // set the relevant IRQ controls.
116     io_irq_ctrl_hw_t *irq_ctrl_base = get_core_num() ?
117                                            &iobank0_hw->proc1_irq_ctrl : &iobank0_hw->proc0_irq_ctrl;
118     _gpio_set_irq_enabled(gpio, events, enabled, irq_ctrl_base);
119 }
120 
gpio_set_irq_enabled_with_callback(uint gpio,uint32_t events,bool enabled,gpio_irq_callback_t callback)121 void gpio_set_irq_enabled_with_callback(uint gpio, uint32_t events, bool enabled, gpio_irq_callback_t callback) {
122     gpio_set_irq_enabled(gpio, events, enabled);
123 
124     // TODO: Do we want to support a callback per GPIO pin?
125     // Install IRQ handler
126     _callbacks[get_core_num()] = callback;
127     irq_set_exclusive_handler(IO_IRQ_BANK0, gpio_irq_handler);
128     irq_set_enabled(IO_IRQ_BANK0, true);
129 }
130 
gpio_set_dormant_irq_enabled(uint gpio,uint32_t events,bool enabled)131 void gpio_set_dormant_irq_enabled(uint gpio, uint32_t events, bool enabled) {
132     io_irq_ctrl_hw_t *irq_ctrl_base = &iobank0_hw->dormant_wake_irq_ctrl;
133     _gpio_set_irq_enabled(gpio, events, enabled, irq_ctrl_base);
134 }
135 
gpio_acknowledge_irq(uint gpio,uint32_t events)136 void gpio_acknowledge_irq(uint gpio, uint32_t events) {
137     iobank0_hw->intr[gpio / 8] = events << 4 * (gpio % 8);
138 }
139 
140 #define DEBUG_PIN_MASK (((1u << PICO_DEBUG_PIN_COUNT)-1) << PICO_DEBUG_PIN_BASE)
gpio_debug_pins_init()141 void gpio_debug_pins_init() {
142     gpio_init_mask(DEBUG_PIN_MASK);
143     gpio_set_dir_masked(DEBUG_PIN_MASK, DEBUG_PIN_MASK);
144     bi_decl_if_func_used(bi_pin_mask_with_names(DEBUG_PIN_MASK, "Debug"));
145 }
146 
gpio_set_input_enabled(uint gpio,bool enabled)147 void gpio_set_input_enabled(uint gpio, bool enabled) {
148     if (enabled)
149         hw_set_bits(&padsbank0_hw->io[gpio], PADS_BANK0_GPIO0_IE_BITS);
150     else
151         hw_clear_bits(&padsbank0_hw->io[gpio], PADS_BANK0_GPIO0_IE_BITS);
152 }
153 
gpio_init(uint gpio)154 void gpio_init(uint gpio) {
155     sio_hw->gpio_oe_clr = 1ul << gpio;
156     sio_hw->gpio_clr = 1ul << gpio;
157     gpio_set_function(gpio, GPIO_FUNC_SIO);
158 }
159 
gpio_init_mask(uint gpio_mask)160 void gpio_init_mask(uint gpio_mask) {
161     for(uint i=0;i<32;i++) {
162         if (gpio_mask & 1) {
163             gpio_init(i);
164         }
165         gpio_mask >>= 1;
166     }
167 }
168 
169