🛠 C SDK : cyw43_arch, lwIP threadbackground vs poll, et verrous d'arch

Raspberry Pi Pico W · avance · Schéma : IMG-pico-w-avance-2-picow-sdk-cyw43-lwip.svg — architecture logicielle Pico W : application, pile lwIP, couche cyw43_arch, pilote SPI PIO vers la puce CYW43439, deux variantes filees (thread arriere-plan vs boucle poll) cote a cote

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials