Skip to content

Commit e25d382

Browse files
Support WiFi/BT/BLT with RP2350 CYW43 boards (#2616)
Using pico-sdk develop branch, add in support for CYW43-based WiFi/BT/BLE boards on the RP2350 such as the SparkFun Thing Plus RP2350 or the Pimoroni Pico Plus 2W. Fixes #2608 Rolls in dynamic SPI divider #2600 * Support LED digitalWrite on RP2350+CYW Also move "special GPIO" to 64 since the Pimoroni Pico 2W uses the RP2350B with 48 GPIOs. * Enable CYW43_PIN_WL_DYNAMIC in IDE and P.IO Allows calling `cyw43_set_pins_wl(cyw43_pin_array);` to redefine the CYW43 hookup in the variant initialization.
1 parent bda12cd commit e25d382

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1120
-238
lines changed

.github/workflows/build-libpico.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ jobs:
2929
# add to PATH
3030
echo "$GITHUB_WORKSPACE/system/riscv32-unknown-elf/bin" >> "$GITHUB_PATH"
3131
echo "$GITHUB_WORKSPACE/system/arm-none-eabi/bin" >> "$GITHUB_PATH"
32-
# Needed until we switch to Pico-SDK 2.0.1.
33-
- name: Patch Pico-SDK (Fix Assembly for newer GCC)
34-
run: |
35-
cd pico-sdk
36-
wget https://patch-diff.githubusercontent.com/raw/raspberrypi/pico-sdk/pull/2000.patch
37-
patch -p1 < 2000.patch
3832
- name: Build libpico
3933
run: |
4034
cd tools/libpico

README.md

Lines changed: 1 addition & 0 deletions

boards.txt

Lines changed: 546 additions & 0 deletions
Large diffs are not rendered by default.

cores/rp2040/RP2040Support.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ class RP2040 {
465465
}
466466

467467
bool isPicoW() {
468-
#if !defined(ARDUINO_RASPBERRY_PI_PICO_W)
468+
#if !defined(PICO_CYW43_SUPPORTED)
469469
return false;
470470
#else
471471
extern bool __isPicoW;

cores/rp2040/cyw43_wrappers.cpp

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
20+
#if defined(PICO_CYW43_SUPPORTED)
2121

2222
#include <lwip/netif.h>
2323
extern "C" {
2424
#include <cyw43.h>
2525
#include <cyw43_stats.h>
2626
}
2727
#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>
2834
#include <Arduino.h>
2935

3036
// From cyw43_ctrl.c
@@ -101,4 +107,100 @@ extern "C" void __wrap_cyw43_cb_tcpip_deinit(cyw43_t *self, int itf) {
101107
(void) itf;
102108
}
103109

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+
104206
#endif

cores/rp2040/cyw43_wrappers.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
CYW43 TCP/Ethernet wrappers
3+
Copyright (c) 2023 Earle F. Philhower, III <earlephilhower@yahoo.com>
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#pragma once
21+
22+
#include <Arduino.h>
23+
#include <pico/cyw43_driver.h>
24+
25+
extern bool __isPicoW;
26+
extern "C" {
27+
void init_cyw43_wifi();
28+
void __lockBluetooth();
29+
void __unlockBluetooth();
30+
void cyw43_pinMode(pin_size_t pin, PinMode mode);
31+
void cyw43_digitalWrite(pin_size_t pin, PinStatus val);
32+
PinStatus cyw43_digitalRead(pin_size_t pin);
33+
}

cores/rp2040/lwip_wrap.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
#include <lwip/dns.h>
2727
#include <lwip/raw.h>
2828
#include <lwip/timeouts.h>
29-
#ifdef PICO_RP2040
3029
#include <pico/cyw43_arch.h>
31-
#endif
3230
#include <pico/mutex.h>
3331
#include <sys/lock.h>
3432
#include "_xoshiro.h"
@@ -46,7 +44,7 @@ class LWIPMutex {
4644
if (ethernet_arch_lwip_gpio_mask) {
4745
ethernet_arch_lwip_gpio_mask();
4846
}
49-
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
47+
#if defined(PICO_CYW43_SUPPORTED)
5048
if (rp2040.isPicoW()) {
5149
cyw43_arch_lwip_begin();
5250
return;
@@ -60,7 +58,7 @@ class LWIPMutex {
6058
}
6159

6260
~LWIPMutex() {
63-
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
61+
#if defined(PICO_CYW43_SUPPORTED)
6462
if (rp2040.isPicoW()) {
6563
cyw43_arch_lwip_end();
6664
} else {
@@ -70,7 +68,7 @@ class LWIPMutex {
7068
} else {
7169
recursive_mutex_exit(&__lwipMutex);
7270
}
73-
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
71+
#if defined(PICO_CYW43_SUPPORTED)
7472
}
7573
#endif
7674
if (ethernet_arch_lwip_gpio_unmask) {

include/lwipopts.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ extern unsigned long __lwip_rand(void);
2929
#define MEM_SIZE (__LWIP_MEMMULT * 16384)
3030
#define MEMP_NUM_TCP_SEG (32)
3131
#define MEMP_NUM_ARP_QUEUE (10)
32-
//#define MEMP_NUM_SYS_TIMEOUT (LWIP_NUM_SYS_TIMEOUT_INTERNAL + 4)
3332
#define PBUF_POOL_SIZE (__LWIP_MEMMULT > 1 ? 32 : 24)
3433
#define LWIP_ARP 5
3534
#define LWIP_ETHERNET 1

include/rp2040/pico_base/pico/version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#define PICO_SDK_VERSION_MAJOR 2
1515
#define PICO_SDK_VERSION_MINOR 0
16-
#define PICO_SDK_VERSION_REVISION 0
17-
#define PICO_SDK_VERSION_STRING "2.0.0"
16+
#define PICO_SDK_VERSION_REVISION 1
17+
#define PICO_SDK_VERSION_STRING "2.0.1-develop"
1818

1919
#endif

include/rp2350/pico_base/pico/version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#define PICO_SDK_VERSION_MAJOR 2
1515
#define PICO_SDK_VERSION_MINOR 0
16-
#define PICO_SDK_VERSION_REVISION 0
17-
#define PICO_SDK_VERSION_STRING "2.0.0"
16+
#define PICO_SDK_VERSION_REVISION 1
17+
#define PICO_SDK_VERSION_STRING "2.0.1-develop"
1818

1919
#endif

0 commit comments

Comments
 (0)