Initial commit

This commit is contained in:
2026-02-17 23:54:13 +01:00
commit c02ec01f75
14 changed files with 935 additions and 0 deletions

26
basic-udp/CMakeLists.txt Normal file
View File

@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.13)
set(PICO_BOARD pico_w)
include(pico_sdk_import.cmake)
project(pico_w_udp_client C CXX ASM)
set(CMAKE_C_STANDARD 11)
pico_sdk_init()
add_executable(pico_w_udp_client
main.c
)
target_include_directories(pico_w_udp_client PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/../secrets
)
target_link_libraries(pico_w_udp_client
pico_stdlib
pico_cyw43_arch_lwip_poll
pico_lwip_nosys
)
pico_add_extra_outputs(pico_w_udp_client)

37
basic-udp/lwipopts.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef LWIPOPTS_H
#define LWIPOPTS_H
/* Platform */
#define NO_SYS 1
#define LWIP_SOCKET 0
#define LWIP_NETCONN 0
#define LWIP_TIMEVAL_PRIVATE 0
/* Memory */
#define MEM_ALIGNMENT 4
#define MEM_SIZE (8 * 1024)
#define MEMP_NUM_PBUF 16
#define MEMP_NUM_UDP_PCB 4
#define MEMP_NUM_TCP_PCB 4
#define MEMP_NUM_TCP_PCB_LISTEN 4
#define MEMP_NUM_SYS_TIMEOUT 8
/* Pbufs */
#define PBUF_POOL_SIZE 16
#define PBUF_POOL_BUFSIZE 1520
/* IP */
#define LWIP_IPV4 1
#define LWIP_IPV6 0
#define LWIP_ICMP 1
#define LWIP_DHCP 1
#define LWIP_UDP 1
#define LWIP_TCP 1
/* Checksums */
#define CHECKSUM_BY_HARDWARE 1
/* Debug */
#define LWIP_DEBUG 0
#endif /* LWIPOPTS_H */

81
basic-udp/main.c Normal file
View File

@@ -0,0 +1,81 @@
#include <string.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "lwip/netif.h"
#include "lwip/udp.h"
#include "lwip/pbuf.h"
#include "lwip/ip_addr.h"
#include "lwip/dhcp.h"
#include "wifi-settings.h"
static struct udp_pcb *udp;
static ip_addr_t dest_ip;
extern struct netif *netif_default;
static void send_hello(void) {
const char msg[] = "hello world\n";
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, sizeof(msg) - 1, PBUF_RAM);
if (!p) {
return;
}
memcpy(p->payload, msg, sizeof(msg) - 1);
udp_sendto(udp, p, &dest_ip, 5505);
pbuf_free(p);
}
int main() {
stdio_init_all();
if (cyw43_arch_init()) {
return -1;
}
cyw43_arch_enable_sta_mode();
if (cyw43_arch_wifi_connect_timeout_ms(
WIFI_SSID,
WIFI_PASS,
CYW43_AUTH_WPA2_AES_PSK,
30000)) {
return -1;
}
/* Raw lwIP setup */
udp = udp_new_ip_type(IPADDR_TYPE_V4);
if (!udp) {
return -1;
}
ipaddr_aton("192.168.2.99", &dest_ip);
absolute_time_t next = get_absolute_time();
bool dhcp_done = false;
while (true) {
cyw43_arch_poll();
if (!dhcp_done) {
if (netif_default &&
netif_is_up(netif_default) &&
!ip4_addr_isany_val(*netif_ip4_addr(netif_default))) {
dhcp_done = true;
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
}
}
if (absolute_time_diff_us(get_absolute_time(), next) <= 0) {
send_hello();
next = delayed_by_ms(next, 1000);
}
sleep_ms(1);
}
}

View File

@@ -0,0 +1,121 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()
# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
endif ()
if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
endif()
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
)
if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
# GIT_SUBMODULES_RECURSE was added in 3.17
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
FetchContent_Populate(
pico_sdk
QUIET
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
GIT_SUBMODULES_RECURSE FALSE
SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
)
else ()
FetchContent_Populate(
pico_sdk
QUIET
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
)
endif ()
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
include(${PICO_SDK_INIT_CMAKE_FILE})

128
basic-udp/readme.md Normal file
View File

@@ -0,0 +1,128 @@
# Raspberry Pi Pico W Minimal Wi-Fi UDP Example (Arch Linux)
## Authorship
This document was generated using ChatGPT (OpenAI), version 5.2.
It has not been independently reviewed or validated yet.
## Purpose
Minimal Pico W Wi-Fi example using the Arch Linux packaged Pico SDK.
Goal: clean build, no hidden assumptions, raw lwIP usage.
---
## Environment
* Arch Linux
* `pico-sdk` from `/usr/share/pico-sdk`
* Board: Pico W
* Toolchain: `arm-none-eabi-gcc`
* lwIP in `NO_SYS` mode
* Driver: `pico_cyw43_arch_lwip_poll`
---
Add the following sections.
---
## Build
```sh
mkdir build
cd build
cmake ..
make -j
```
Explanation:
* `cmake ..` configures the project and generates build files.
* `make -j` builds using all available CPU cores.
* After configuration, normal source changes require only `make -j`.
* If `CMakeLists.txt` changes, delete the `build/` directory and run `cmake` again.
The resulting firmware file:
```
build/pico_w_udp_client.uf2
```
---
## Flash
```sh
picotool load -f pico_w_udp_client.uf2
```
This writes the UF2 image to the Pico W over USB (device must be in BOOTSEL mode or rebooted via picotool).
---
## Runtime Behavior
After flashing and reset:
1. The Pico W connects to the configured Wi-Fi network.
2. Once DHCP assigns an IP address, the onboard LED turns on.
3. The device begins sending periodic `hello world` UDP messages to the configured destination IP address.
## CMake Configuration
Board is selected in `CMakeLists.txt`:
```cmake
set(PICO_BOARD pico_w)
include(pico_sdk_import.cmake)
```
Wi-Fi credentials are kept outside the main source tree:
```cmake
target_include_directories(pico_w_udp_client PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/../secrets
)
```
Example usage:
```c
#include "secrets/fra.h"
```
This keeps credentials separate from firmware logic.
---
## Networking Model
* lwIP raw API (UDP)
* `NO_SYS = 1`
* Explicit polling via `cyw43_arch_poll()`
* Project-local `lwipopts.h`
No sockets, no netconn, no `sys_arch`, no background threads.
---
## Key Points
* Arch SDK requires providing your own `lwipopts.h`.
* Sockets require `NO_SYS = 0` and a `sys_arch` implementation — not used here.
* Raw API is the simplest and fully self-contained option.
* TCP/UDP framing must be handled at application level.
---
## Result
✔ Builds cleanly
✔ Pico W connects to Wi-Fi
✔ UDP packets transmitted
✔ Deterministic configuration

14
basic-udp/udp-dump.py Normal file
View File

@@ -0,0 +1,14 @@
import socket, time
UDP_IP = "192.168.2.99"
UDP_PORT = 5505
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind((UDP_IP, UDP_PORT))
print(f"Listening on UDP port {UDP_PORT}...")
while True:
data, addr = sock.recvfrom(1024)
print(f"{time.ctime()} [{addr[0]}:{addr[1]}] {data.decode('utf-8', errors='replace')}")