Skip to content

Commit b7912f1

Browse files
Add isPicoW call to RP2040 object (#1204)
Use the RPi code to get a best-guess as to whether the board is a Pico or PicoW. Fixes #849
1 parent 6db0ac8 commit b7912f1

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

cores/rp2040/RP2040Support.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,16 @@ class RP2040 {
363363
return (uint32_t)rr;
364364
}
365365

366+
bool isPicoW() {
367+
#if !defined(ARDUINO_RASPBERRY_PI_PICO_W)
368+
return false;
369+
#else
370+
extern bool __isPicoW;
371+
return __isPicoW;
372+
#endif
373+
}
374+
375+
366376
private:
367377
static void _SystickHandler() {
368378
rp2040._epoch += 1LL << 24;

docs/rp2040.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ Returns the total heap that was available to this program at compile time (i.e.
7575
the Pico RAM size minus things like the ``.data`` and ``.bss`` sections and other
7676
overhead).
7777

78+
Hardware Identification
79+
-----------------------
80+
81+
bool isPicoW()
82+
~~~~~~~~~~~~~~
83+
Returns the core's best guess if this code is running on a Raspberry Pi Pico W.
84+
This would let you, possibly, use the same UF2 for Pico and PicoW by simply not
85+
doing any WiFi calls.
86+
7887
Bootloader
7988
----------
8089

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ restartCore1 KEYWORD2
4444
reboot KEYWORD2
4545
restart KEYWORD2
4646

47+
isPicoW KEYWORD2
48+
4749
getChipID KEYWORD2
4850

4951
hwrand32 KEYWORD2

variants/rpipicow/picow_init.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,48 @@
1919
*/
2020

2121
#include <pico/cyw43_arch.h>
22+
#include "hardware/resets.h"
23+
#include "hardware/gpio.h"
24+
#include "hardware/adc.h"
2225

2326
#ifndef WIFICC
2427
#define WIFICC CYW43_COUNTRY_WORLDWIDE
2528
#endif
2629

30+
// Taken from https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf
31+
// also discussion in https://github.com/earlephilhower/arduino-pico/issues/849
32+
static bool CheckPicoW() {
33+
adc_init();
34+
auto dir = gpio_get_dir(29);
35+
auto fnc = gpio_get_function(29);
36+
adc_gpio_init(29);
37+
adc_select_input(3);
38+
auto adc29 = adc_read();
39+
gpio_set_function(29, fnc);
40+
gpio_set_dir(29, dir);
41+
42+
dir = gpio_get_dir(25);
43+
fnc = gpio_get_function(25);
44+
gpio_init(25);
45+
gpio_set_dir(25, GPIO_IN);
46+
auto gp25 = gpio_get(25);
47+
gpio_set_function(25, fnc);
48+
gpio_set_dir(25, dir);
49+
50+
if (gp25) {
51+
return true; // Can't tell, so assume yes
52+
} else if (adc29 < 200) {
53+
return true; // PicoW
54+
} else {
55+
return false;
56+
}
57+
}
58+
59+
bool __isPicoW = true;
60+
2761
extern "C" void initVariant() {
28-
cyw43_arch_init_with_country(WIFICC);
62+
__isPicoW = CheckPicoW();
63+
if (__isPicoW) {
64+
cyw43_arch_init_with_country(WIFICC);
65+
}
2966
}

variants/rpipicow/pins_arduino.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
// Pin definitions taken from:
44
// https://datasheets.raspberrypi.org/pico/pico-datasheet.pdf
55

6+
extern bool __isPicoW;
67

78
// LEDs
8-
#define PIN_LED (32u)
9+
#define PIN_LED (__isPicoW ? 32u : 25u)
910

1011
// Serial
1112
#define PIN_SERIAL1_TX (0u)

0 commit comments

Comments
 (0)