|
17 | 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18 | 18 | */
|
19 | 19 |
|
20 |
| -#if defined(ARDUINO_RASPBERRY_PI_PICO_W) |
| 20 | +#if defined(PICO_CYW43_SUPPORTED) |
21 | 21 |
|
22 | 22 | #include <lwip/netif.h>
|
23 | 23 | extern "C" {
|
24 | 24 | #include <cyw43.h>
|
25 | 25 | #include <cyw43_stats.h>
|
26 | 26 | }
|
27 | 27 | #include <pico/cyw43_arch.h>
|
| 28 | +#include <pico/cyw43_driver.h> |
| 29 | +#include <pico/lwip_nosys.h> |
| 30 | +#include <hardware/resets.h> |
| 31 | +#include <hardware/gpio.h> |
| 32 | +#include <hardware/adc.h> |
| 33 | +#include <hardware/clocks.h> |
28 | 34 | #include <Arduino.h>
|
29 | 35 |
|
30 | 36 | // From cyw43_ctrl.c
|
@@ -101,4 +107,100 @@ extern "C" void __wrap_cyw43_cb_tcpip_deinit(cyw43_t *self, int itf) {
|
101 | 107 | (void) itf;
|
102 | 108 | }
|
103 | 109 |
|
| 110 | +#ifndef WIFICC |
| 111 | +#define WIFICC CYW43_COUNTRY_WORLDWIDE |
| 112 | +#endif |
| 113 | + |
| 114 | +// Taken from https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf |
| 115 | +// also discussion in https://github.com/earlephilhower/arduino-pico/issues/849 |
| 116 | +static bool CheckPicoW() { |
| 117 | +#ifdef PICO_RP2040 |
| 118 | + adc_init(); |
| 119 | + auto dir = gpio_get_dir(29); |
| 120 | + auto fnc = gpio_get_function(29); |
| 121 | + adc_gpio_init(29); |
| 122 | + adc_select_input(3); |
| 123 | + auto adc29 = adc_read(); |
| 124 | + gpio_set_function(29, fnc); |
| 125 | + gpio_set_dir(29, dir); |
| 126 | + |
| 127 | + dir = gpio_get_dir(25); |
| 128 | + fnc = gpio_get_function(25); |
| 129 | + gpio_init(25); |
| 130 | + gpio_set_dir(25, GPIO_IN); |
| 131 | + auto gp25 = gpio_get(25); |
| 132 | + gpio_set_function(25, fnc); |
| 133 | + gpio_set_dir(25, dir); |
| 134 | + |
| 135 | + if (gp25) { |
| 136 | + return true; // Can't tell, so assume yes |
| 137 | + } else if (adc29 < 200) { |
| 138 | + return true; // PicoW |
| 139 | + } else { |
| 140 | + return false; |
| 141 | + } |
| 142 | +#else |
| 143 | + return true; |
| 144 | +#endif |
| 145 | +} |
| 146 | + |
| 147 | +bool __isPicoW = true; |
| 148 | + |
| 149 | +extern "C" void init_cyw43_wifi() { |
| 150 | + __isPicoW = CheckPicoW(); |
| 151 | + if (__isPicoW) { |
| 152 | + // Fix for overclocked CPU: SPI communication breaks down with default "div by 2" speed |
| 153 | + // So, divide clock by 4 for anything including and above 250MHz CPU frequency. |
| 154 | + if (clock_get_hz(clk_sys) >= 250000000) { |
| 155 | + cyw43_set_pio_clock_divisor(4, 0); // div by 4.0 |
| 156 | + } |
| 157 | + cyw43_arch_init_with_country(WIFICC); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +extern "C" void __lockBluetooth() { |
| 162 | + async_context_acquire_lock_blocking(cyw43_arch_async_context()); |
| 163 | +} |
| 164 | + |
| 165 | +extern "C" void __unlockBluetooth() { |
| 166 | + async_context_release_lock(cyw43_arch_async_context()); |
| 167 | +} |
| 168 | + |
| 169 | +extern "C" void __pinMode(pin_size_t pin, PinMode mode); |
| 170 | +extern "C" void __digitalWrite(pin_size_t pin, PinStatus val); |
| 171 | +extern "C" PinStatus __digitalRead(pin_size_t pin); |
| 172 | + |
| 173 | +extern "C" void cyw43_pinMode(pin_size_t pin, PinMode mode) { |
| 174 | + if (!__isPicoW && (pin == PIN_LED)) { |
| 175 | + pin = 25; // Silently swap in the Pico's LED |
| 176 | + } |
| 177 | + if (pin < 64) { |
| 178 | + __pinMode(pin, mode); |
| 179 | + } else { |
| 180 | + // TBD - There is no GPIO direction control in the driver |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +extern "C" void cyw43_digitalWrite(pin_size_t pin, PinStatus val) { |
| 185 | + if (!__isPicoW && (pin == PIN_LED)) { |
| 186 | + pin = 25; // Silently swap in the Pico's LED |
| 187 | + } |
| 188 | + if (pin < 64) { |
| 189 | + __digitalWrite(pin, val); |
| 190 | + } else { |
| 191 | + cyw43_arch_gpio_put(pin - 64, val == HIGH ? 1 : 0); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +extern "C" PinStatus cyw43_digitalRead(pin_size_t pin) { |
| 196 | + if (!__isPicoW && (pin == PIN_LED)) { |
| 197 | + pin = 25; // Silently swap in the Pico's LED |
| 198 | + } |
| 199 | + if (pin < 64) { |
| 200 | + return __digitalRead(pin); |
| 201 | + } else { |
| 202 | + return cyw43_arch_gpio_get(pin - 64) ? HIGH : LOW; |
| 203 | + } |
| 204 | +} |
| 205 | + |
104 | 206 | #endif
|
0 commit comments