|
| 1 | +/* |
| 2 | + Initialize the Pico W WiFi driver |
| 3 | +
|
| 4 | + Copyright (c) 2022 Earle F. Philhower, III <[email protected]> |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2.1 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + This library is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public |
| 17 | + License along with this library; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <pico/cyw43_arch.h> |
| 22 | +#include <pico/lwip_nosys.h> |
| 23 | +#include "hardware/resets.h" |
| 24 | +#include "hardware/gpio.h" |
| 25 | +#include "hardware/adc.h" |
| 26 | + |
| 27 | +#ifndef WIFICC |
| 28 | +#define WIFICC CYW43_COUNTRY_WORLDWIDE |
| 29 | +#endif |
| 30 | + |
| 31 | +// Taken from https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf |
| 32 | +// also discussion in https://github.com/earlephilhower/arduino-pico/issues/849 |
| 33 | +static bool CheckPicoW() { |
| 34 | + adc_init(); |
| 35 | + auto dir = gpio_get_dir(29); |
| 36 | + auto fnc = gpio_get_function(29); |
| 37 | + adc_gpio_init(29); |
| 38 | + adc_select_input(3); |
| 39 | + auto adc29 = adc_read(); |
| 40 | + gpio_set_function(29, fnc); |
| 41 | + gpio_set_dir(29, dir); |
| 42 | + |
| 43 | + dir = gpio_get_dir(25); |
| 44 | + fnc = gpio_get_function(25); |
| 45 | + gpio_init(25); |
| 46 | + gpio_set_dir(25, GPIO_IN); |
| 47 | + auto gp25 = gpio_get(25); |
| 48 | + gpio_set_function(25, fnc); |
| 49 | + gpio_set_dir(25, dir); |
| 50 | + |
| 51 | + if (gp25) { |
| 52 | + return true; // Can't tell, so assume yes |
| 53 | + } else if (adc29 < 200) { |
| 54 | + return true; // PicoW |
| 55 | + } else { |
| 56 | + return false; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +bool __isPicoW = true; |
| 61 | + |
| 62 | +extern "C" void initVariant() { |
| 63 | + __isPicoW = CheckPicoW(); |
| 64 | + if (__isPicoW) { |
| 65 | + cyw43_arch_init_with_country(WIFICC); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +extern "C" void __lockBluetooth() { |
| 70 | + async_context_acquire_lock_blocking(cyw43_arch_async_context()); |
| 71 | +} |
| 72 | + |
| 73 | +extern "C" void __unlockBluetooth() { |
| 74 | + async_context_release_lock(cyw43_arch_async_context()); |
| 75 | +} |
0 commit comments