🛠 C SDK : cyw43_arch, lwIP threadbackground vs poll, et verrous d'arch
Objectives
Switch to C for full control: in the Pico SDK, the CYW43 driver is integrated into the SDK itself (src/rp2_common/pico_cyw43_driver, pico_cyw43_arch) and lwIP has been moved from pico-extras (pico_lwip, pico_lwip_arch). The hardware chain is remarkable: cyw43_arch_init powers the CYW43439 via GP23, loads its firmware from your QSPI flash, and starts an SPI bus driven by a PIO state machine at ~33 MHz. This tutorial compares the two arch modes — pico_cyw43_arch_lwip_threadbackground (lwIP in a thread) vs pico_cyw43_arch_poll (explicit loop) — and their locking rules, the number one source of network bugs in C.
Hardware
- 1 × Raspberry Pi Pico W + micro-USB data cable
- Pico SDK 2.x,
PICO_BOARD=pico_w, gcc-arm-none-eabi toolchain + CMake - A TCP test server on the LAN (e.g.,
nc -l -p 4242) - Serial console at 115200 baud
Complete Code
// picow_tcp_client.c — squelette basé sur l'exemple officiel
// pico_w/wifi/tcp_client (BSD-3, © Raspberry Pi (Trading) Ltd.)
// CMake : pico_stdlib pico_cyw43_arch_lwip_threadbackground
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h" // Wi-Fi + glue lwIP, désormais dans le SDK
#include "lwip/tcp.h"
#define WIFI_SSID "myssid"
#define WIFI_PASS "mypass"
#define TEST_TCP_SERVER_IP "192.168.1.50"
#define TCP_PORT 4242
int main(void) {
stdio_init_all;
// Alimente le CYW43 via GP23, charge le firmware, démarre le SPI PIO 33 MHz
if (cyw43_arch_init) { printf("failed to initialise\n"); return 1; }
cyw43_arch_enable_sta_mode;
if (cyw43_arch_wifi_connect_timeout_ms(
WIFI_SSID, WIFI_PASS, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
printf("failed to connect.\n"); return 1;
}
printf("Connected.\n");
// Règle d'or (variante threadbackground) : tout appel lwIP depuis TON
// code est entouré du verrou d'arch ; depuis un callback lwIP, inutile :
// cyw43_arch_lwip_begin;
// err_t err = tcp_connect(pcb, &remote, TCP_PORT, cb);
// cyw43_arch_lwip_end;
// Avec pico_cyw43_arch_poll à la place : appeler cyw43_arch_poll
// régulièrement dans la boucle principale, sinon rien ne bouge.
// ... tcp_new_ip_type / tcp_connect / tcp_write ...
cyw43_arch_disable_sta_mode;
cyw43_arch_deinit;
return 0;
}
Expected Verification
Connected. on the console, and the TCP test server receives the connection. To decide between the two variants: with threadbackground, your main can block (sleep_ms) and lwIP still runs in its thread; with _poll, if you forget cyw43_arch_poll in a computation loop, TCP callbacks freeze — symptom: the connection degrades as soon as the CPU is busy.
Common Errors
- Calling lwIP without locking in threadbackground: state corruption, asserts, deadlocks —
cyw43_arch_lwip_begin/endis mandatory outside callbacks. - Choosing
_pollthen writing a loop withoutcyw43_arch_poll: the network stack never runs; choosing_threadbackgroundinitially is the advice from official examples. - Network
printfwithoutstdio_init_allfirst: the serial console is silent and you look for a Wi-Fi bug that doesn't exist. - Confusing arch variants with
pico_cyw43_arch_none: this one only brings up the chip (LED!), not the network. - Linking multiple arch variants together: duplicate symbols at link time — only one of the three per target.
- Forgetting that the CYW43 firmware is stored in your flash (2 MB): a bloated binary can saturate the space — network examples are already quite large.
Other tutorials for this board
- Faire clignoter la LED embarquée (elle est derrière la puce Wi-Fi !)
- Se connecter à un réseau Wi-Fi (station, WPA2/WPA3)
- Bouton et potentiomètre sans marcher sur le sans-fil (GPIO libres & ADC)
- Servir une page web depuis la Pico W (socket TCP + HTTP)
- Client HTTP : consommer une API depuis la Pico W
- Heure exacte via NTP : l'horloge temps réel des projets connectés
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython