Skip to content

Commit

Permalink
WIP - Support CYW43 boards on RP2350
Browse files Browse the repository at this point in the history
Using pico-sdk develop branch with minor patch, add in support for the
CYW43-based WiFi boards on the RP2350.

Fixes #2608

Rolls in dynamic SPI divider #2600
  • Loading branch information
earlephilhower committed Nov 15, 2024
1 parent ca30518 commit 1780acd
Show file tree
Hide file tree
Showing 63 changed files with 996 additions and 189 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/build-libpico.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ jobs:
# add to PATH
echo "$GITHUB_WORKSPACE/system/riscv32-unknown-elf/bin" >> "$GITHUB_PATH"
echo "$GITHUB_WORKSPACE/system/arm-none-eabi/bin" >> "$GITHUB_PATH"
# Needed until we switch to Pico-SDK 2.0.1.
- name: Patch Pico-SDK (Fix Assembly for newer GCC)
run: |
cd pico-sdk
wget https://patch-diff.githubusercontent.com/raw/raspberrypi/pico-sdk/pull/2000.patch
patch -p1 < 2000.patch
- name: Build libpico
run: |
cd tools/libpico
Expand Down
546 changes: 546 additions & 0 deletions boards.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cores/rp2040/RP2040Support.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class RP2040 {
}

bool isPicoW() {
#if !defined(ARDUINO_RASPBERRY_PI_PICO_W)
#if !defined(PICO_CYW43_SUPPORTED)
return false;
#else
extern bool __isPicoW;
Expand Down
67 changes: 66 additions & 1 deletion cores/rp2040/cyw43_wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
#if defined(PICO_CYW43_SUPPORTED)

#include <lwip/netif.h>
extern "C" {
#include <cyw43.h>
#include <cyw43_stats.h>
}
#include <pico/cyw43_arch.h>
#include <pico/cyw43_driver.h>
#include <pico/lwip_nosys.h>
#include <hardware/resets.h>
#include <hardware/gpio.h>
#include <hardware/adc.h>
#include <hardware/clocks.h>
#include <Arduino.h>

// From cyw43_ctrl.c
Expand Down Expand Up @@ -101,4 +107,63 @@ extern "C" void __wrap_cyw43_cb_tcpip_deinit(cyw43_t *self, int itf) {
(void) itf;
}

#ifndef WIFICC
#define WIFICC CYW43_COUNTRY_WORLDWIDE
#endif

// Taken from https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf
// also discussion in https://github.com/earlephilhower/arduino-pico/issues/849
static bool CheckPicoW() {
#ifdef PICO_RP2040
adc_init();
auto dir = gpio_get_dir(29);
auto fnc = gpio_get_function(29);
adc_gpio_init(29);
adc_select_input(3);
auto adc29 = adc_read();
gpio_set_function(29, fnc);
gpio_set_dir(29, dir);

dir = gpio_get_dir(25);
fnc = gpio_get_function(25);
gpio_init(25);
gpio_set_dir(25, GPIO_IN);
auto gp25 = gpio_get(25);
gpio_set_function(25, fnc);
gpio_set_dir(25, dir);

if (gp25) {
return true; // Can't tell, so assume yes
} else if (adc29 < 200) {
return true; // PicoW
} else {
return false;
}
#else
return true;
#endif
}

bool __isPicoW = true;

extern "C" void init_cyw43_wifi() {
__isPicoW = CheckPicoW();
if (__isPicoW) {
// Fix for overclocked CPU: SPI communication breaks down with default "div by 2" speed
// So, divide clock by 4 for anything including and above 250MHz CPU frequency.
if (clock_get_hz(clk_sys) >= 250000000) {
cyw43_set_pio_clock_divisor(4, 0); // div by 4.0
}
cyw43_arch_init_with_country(WIFICC);
}
}

extern "C" void __lockBluetooth() {
async_context_acquire_lock_blocking(cyw43_arch_async_context());
}

extern "C" void __unlockBluetooth() {
async_context_release_lock(cyw43_arch_async_context());
}

#endif
25 changes: 25 additions & 0 deletions cores/rp2040/cyw43_wrappers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
CYW43 TCP/Ethernet wrappers
Copyright (c) 2023 Earle F. Philhower, III <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#pragma once

extern bool __isPicoW;
extern "C" void init_cyw43_wifi();
extern "C" void __lockBluetooth();
extern "C" void __unlockBluetooth();
8 changes: 3 additions & 5 deletions cores/rp2040/lwip_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
#include <lwip/dns.h>
#include <lwip/raw.h>
#include <lwip/timeouts.h>
#ifdef PICO_RP2040
#include <pico/cyw43_arch.h>
#endif
#include <pico/mutex.h>
#include <sys/lock.h>
#include "_xoshiro.h"
Expand All @@ -46,7 +44,7 @@ class LWIPMutex {
if (ethernet_arch_lwip_gpio_mask) {
ethernet_arch_lwip_gpio_mask();
}
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
#if defined(PICO_CYW43_SUPPORTED)
if (rp2040.isPicoW()) {
cyw43_arch_lwip_begin();
return;
Expand All @@ -60,7 +58,7 @@ class LWIPMutex {
}

~LWIPMutex() {
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
#if defined(PICO_CYW43_SUPPORTED)
if (rp2040.isPicoW()) {
cyw43_arch_lwip_end();
} else {
Expand All @@ -70,7 +68,7 @@ class LWIPMutex {
} else {
recursive_mutex_exit(&__lwipMutex);
}
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
#if defined(PICO_CYW43_SUPPORTED)
}
#endif
if (ethernet_arch_lwip_gpio_unmask) {
Expand Down
1 change: 0 additions & 1 deletion include/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ extern unsigned long __lwip_rand(void);
#define MEM_SIZE (__LWIP_MEMMULT * 16384)
#define MEMP_NUM_TCP_SEG (32)
#define MEMP_NUM_ARP_QUEUE (10)
//#define MEMP_NUM_SYS_TIMEOUT (LWIP_NUM_SYS_TIMEOUT_INTERNAL + 4)
#define PBUF_POOL_SIZE (__LWIP_MEMMULT > 1 ? 32 : 24)
#define LWIP_ARP 5
#define LWIP_ETHERNET 1
Expand Down
4 changes: 2 additions & 2 deletions include/rp2040/pico_base/pico/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#define PICO_SDK_VERSION_MAJOR 2
#define PICO_SDK_VERSION_MINOR 0
#define PICO_SDK_VERSION_REVISION 0
#define PICO_SDK_VERSION_STRING "2.0.0"
#define PICO_SDK_VERSION_REVISION 1
#define PICO_SDK_VERSION_STRING "2.0.1-develop"

#endif
4 changes: 2 additions & 2 deletions include/rp2350/pico_base/pico/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#define PICO_SDK_VERSION_MAJOR 2
#define PICO_SDK_VERSION_MINOR 0
#define PICO_SDK_VERSION_REVISION 0
#define PICO_SDK_VERSION_STRING "2.0.0"
#define PICO_SDK_VERSION_REVISION 1
#define PICO_SDK_VERSION_STRING "2.0.1-develop"

#endif
2 changes: 2 additions & 0 deletions lib/core_inc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-iwithprefixbefore/pico-sdk/src/common/pico_util/include
-iwithprefixbefore/pico-sdk/src/common/pico_stdlib_headers/include
-iwithprefixbefore/pico-sdk/src/common/pico_usb_reset_interface_headers/include
-iwithprefixbefore/pico-sdk/src/rp2_common/boot_bootrom_headers/include
-iwithprefixbefore/pico-sdk/src/rp2_common/cmsis/include
-iwithprefixbefore/pico-sdk/src/rp2_common/cmsis/stub/CMSIS/Core/Include
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_adc/include
Expand Down Expand Up @@ -47,6 +48,7 @@
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_bootrom/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_double/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_fix/rp2040_usb_device_enumeration/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_flash/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_float/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_int64_ops/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_lwip/include
Expand Down
Binary file modified lib/rp2040/libipv4-big.a
Binary file not shown.
Binary file modified lib/rp2040/libipv4-bt-big.a
Binary file not shown.
Binary file modified lib/rp2040/libipv4-bt.a
Binary file not shown.
Binary file modified lib/rp2040/libipv4-ipv6-big.a
Binary file not shown.
Binary file modified lib/rp2040/libipv4-ipv6-bt-big.a
Binary file not shown.
Binary file modified lib/rp2040/libipv4-ipv6-bt.a
Binary file not shown.
Binary file modified lib/rp2040/libipv4-ipv6.a
Binary file not shown.
Binary file modified lib/rp2040/libipv4.a
Binary file not shown.
Binary file modified lib/rp2040/libpico.a
Binary file not shown.
1 change: 1 addition & 0 deletions lib/rp2040/platform_def.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1
-DCYW43_LWIP=1
-DCYW43_PIO_CLOCK_DIV_DYNAMIC=1
-DCFG_TUSB_MCU=OPT_MCU_RP2040
-DARM_MATH_CM0_FAMILY
-DARM_MATH_CM0_PLUS
Expand Down
Binary file modified lib/rp2350-riscv/libipv4-big.a
Binary file not shown.
Binary file added lib/rp2350-riscv/libipv4-bt-big.a
Binary file not shown.
Binary file added lib/rp2350-riscv/libipv4-bt.a
Binary file not shown.
Binary file modified lib/rp2350-riscv/libipv4-ipv6-big.a
Binary file not shown.
Binary file added lib/rp2350-riscv/libipv4-ipv6-bt-big.a
Binary file not shown.
Binary file added lib/rp2350-riscv/libipv4-ipv6-bt.a
Binary file not shown.
Binary file modified lib/rp2350-riscv/libipv4-ipv6.a
Binary file not shown.
Binary file modified lib/rp2350-riscv/libipv4.a
Binary file not shown.
Binary file modified lib/rp2350-riscv/libpico.a
Binary file not shown.
3 changes: 3 additions & 0 deletions lib/rp2350-riscv/platform_def.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1
-DCYW43_LWIP=1
-DCYW43_PIO_CLOCK_DIV_DYNAMIC=1
-DCFG_TUSB_MCU=OPT_MCU_RP2040
-DCFG_TUSB_OS=OPT_OS_PICO
-DLIB_BOOT_STAGE2_HEADERS=1
Expand Down
8 changes: 8 additions & 0 deletions lib/rp2350-riscv/platform_inc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_hazard3/include
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_sha256/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_sha256/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_btstack/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_cyw43_arch/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_cyw43_driver/include
-iwithprefixbefore/pico-sdk/lib/cyw43-driver/src
-iwithprefixbefore/pico-sdk/lib/btstack/src
-iwithprefixbefore/pico-sdk/lib/btstack/3rd-party/bluedroid/decoder/include
-iwithprefixbefore/pico-sdk/lib/btstack/3rd-party/bluedroid/encoder/include
-iwithprefixbefore/pico-sdk/lib/btstack/platform/embedded
Binary file modified lib/rp2350/libipv4-big.a
Binary file not shown.
Binary file added lib/rp2350/libipv4-bt-big.a
Binary file not shown.
Binary file added lib/rp2350/libipv4-bt.a
Binary file not shown.
Binary file modified lib/rp2350/libipv4-ipv6-big.a
Binary file not shown.
Binary file added lib/rp2350/libipv4-ipv6-bt-big.a
Binary file not shown.
Binary file added lib/rp2350/libipv4-ipv6-bt.a
Binary file not shown.
Binary file modified lib/rp2350/libipv4-ipv6.a
Binary file not shown.
Binary file modified lib/rp2350/libipv4.a
Binary file not shown.
Binary file modified lib/rp2350/libpico.a
Binary file not shown.
3 changes: 3 additions & 0 deletions lib/rp2350/platform_def.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1
-DCYW43_LWIP=1
-DCYW43_PIO_CLOCK_DIV_DYNAMIC=1
-DCFG_TUSB_MCU=OPT_MCU_RP2040
-DCFG_TUSB_OS=OPT_OS_PICO
-DLIB_BOOT_STAGE2_HEADERS=1
Expand Down
8 changes: 8 additions & 0 deletions lib/rp2350/platform_inc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@
-iwithprefixbefore/pico-sdk/src/rp2_common/cmsis/stub/CMSIS/Device/RP2350/Include
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_sha256/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_sha256/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_btstack/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_cyw43_arch/include
-iwithprefixbefore/pico-sdk/src/rp2_common/pico_cyw43_driver/include
-iwithprefixbefore/pico-sdk/lib/cyw43-driver/src
-iwithprefixbefore/pico-sdk/lib/btstack/src
-iwithprefixbefore/pico-sdk/lib/btstack/3rd-party/bluedroid/decoder/include
-iwithprefixbefore/pico-sdk/lib/btstack/3rd-party/bluedroid/encoder/include
-iwithprefixbefore/pico-sdk/lib/btstack/platform/embedded
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void setup() {
TaskHandle_t blinkTask;
Serial.begin(115200);
xTaskCreate(blink, "BLINK", 256, nullptr, 1, &blinkTask);
#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
// The PicoW WiFi chip controls the LED, and only core 0 can make calls to it safely
vTaskCoreAffinitySet(blinkTask, 1 << 0);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ void setup() {
xSemaphore = xSemaphoreCreateMutexStatic( &xMutexBuffer );

ledOnTask = xTaskCreateStatic(led_ON, "led_ON", STACK_SIZE, NULL, configMAX_PRIORITIES - 1, xStack_A, &xTaskBuffer_A);
#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
// The PicoW WiFi chip controls the LED, and only core 0 can make calls to it safely
vTaskCoreAffinitySet(ledOnTask, 1 << 0);
#endif
ledOffTask = xTaskCreateStatic(led_OFF, "led_OFF", STACK_SIZE, NULL, configMAX_PRIORITIES - 1, xStack_B, &xTaskBuffer_B);
#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
// The PicoW WiFi chip controls the LED, and only core 0 can make calls to it safely
vTaskCoreAffinitySet(ledOffTask, 1 << 0);
#endif
Expand Down
10 changes: 5 additions & 5 deletions libraries/WiFi/src/WiFiClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

// This is the real WiFi network object, we just tickle it to do our magic
#include <LwipEthernet.h>
#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
#include <pico/cyw43_arch.h>
static CYW43lwIP _wifi(1);
#elif defined(ESPHOSTSPI)
Expand Down Expand Up @@ -204,7 +204,7 @@ uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase) {
return WL_CONNECTED;
}

#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
uint8_t WiFiClass::softAPgetStationNum() {
if (!_apMode || !_wifiHWInitted) {
return 0;
Expand Down Expand Up @@ -324,7 +324,7 @@ void WiFiClass::end(void) {
return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
*/
uint8_t* WiFiClass::macAddress(uint8_t* mac) {
#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
if (!_wifiHWInitted) {
_apMode = false;
cyw43_wifi_set_up(&cyw43_state, _apMode ? 1 : 0, true, CYW43_COUNTRY_WORLDWIDE);
Expand Down Expand Up @@ -423,7 +423,7 @@ uint8_t WiFiClass::encryptionType() {
int8_t WiFiClass::scanNetworks(bool async) {
if (!_wifiHWInitted) {
_apMode = false;
#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
cyw43_arch_enable_sta_mode();
#endif
_wifiHWInitted = true;
Expand Down Expand Up @@ -519,7 +519,7 @@ unsigned long WiFiClass::getTime() {
return millis();
}

#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
void WiFiClass::aggressiveLowPowerMode() {
cyw43_wifi_pm(&cyw43_state, CYW43_AGGRESSIVE_PM);
}
Expand Down
6 changes: 3 additions & 3 deletions libraries/WiFi/src/WiFiClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#pragma once

#include <Arduino.h>
#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
#include <lwIP_CYW43.h>
#elif defined(ESPHOSTSPI)
#include <lwIP_ESPHost.h>
Expand Down Expand Up @@ -158,7 +158,7 @@ class WiFiClass {
return true;
}

#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
uint8_t softAPgetStationNum();
#endif

Expand Down Expand Up @@ -405,7 +405,7 @@ class WiFiClass {

unsigned long getTime();

#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#if defined(PICO_CYW43_SUPPORTED)
void aggressiveLowPowerMode();
void defaultLowPowerMode();
#endif
Expand Down
Loading

0 comments on commit 1780acd

Please sign in to comment.