Initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
secrets/
|
||||
build/
|
||||
26
basic-udp/CMakeLists.txt
Normal file
26
basic-udp/CMakeLists.txt
Normal 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
37
basic-udp/lwipopts.h
Normal 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
81
basic-udp/main.c
Normal 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);
|
||||
}
|
||||
}
|
||||
121
basic-udp/pico_sdk_import.cmake
Normal file
121
basic-udp/pico_sdk_import.cmake
Normal 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
128
basic-udp/readme.md
Normal 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
14
basic-udp/udp-dump.py
Normal 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')}")
|
||||
39
i2c-over-tcp/plans.md
Normal file
39
i2c-over-tcp/plans.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# TCP → I2C bridge
|
||||
|
||||
## Authorship
|
||||
|
||||
This document was generated using ChatGPT (OpenAI), version 5.2.
|
||||
It has not been independently reviewed or validated yet.
|
||||
|
||||
# Translation units
|
||||
|
||||
## `i2c_controller.c`
|
||||
|
||||
* `i2c_handle_frame(cmd, payload, len, reply_buf, *reply_len)`
|
||||
* No transport knowledge.
|
||||
* Pure logic + hardware calls.
|
||||
|
||||
|
||||
## `usb_transport.c`
|
||||
|
||||
* Parses frames from CDC.
|
||||
* Calls `i2c_handle_frame()`.
|
||||
* Writes reply back over USB.
|
||||
|
||||
## `tcp_transport.c`
|
||||
|
||||
* Same framing.
|
||||
* Calls same `i2c_handle_frame()`.
|
||||
* Writes reply over TCP.
|
||||
|
||||
|
||||
# Notes
|
||||
|
||||
* The framing + transport layer is swappable.
|
||||
* The I²C logic is isolated.
|
||||
|
||||
# Result
|
||||
|
||||
* USB for development/debug.
|
||||
* TCP for deployment.
|
||||
* Zero duplication of protocol logic.
|
||||
26
tcp-command-frame/CMakeLists.txt
Normal file
26
tcp-command-frame/CMakeLists.txt
Normal file
@@ -0,0 +1,26 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
set(PICO_BOARD pico_w)
|
||||
include(pico_sdk_import.cmake)
|
||||
|
||||
project(pico_w_tcp_command_framing C CXX ASM)
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
pico_sdk_init()
|
||||
|
||||
add_executable(pico_w_tcp_command_framing
|
||||
main.c
|
||||
)
|
||||
|
||||
target_include_directories(pico_w_tcp_command_framing PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${CMAKE_CURRENT_LIST_DIR}/../secrets
|
||||
)
|
||||
|
||||
target_link_libraries(pico_w_tcp_command_framing
|
||||
pico_stdlib
|
||||
pico_cyw43_arch_lwip_poll
|
||||
pico_lwip_nosys
|
||||
)
|
||||
|
||||
pico_add_extra_outputs(pico_w_tcp_command_framing)
|
||||
58
tcp-command-frame/client.mjs
Normal file
58
tcp-command-frame/client.mjs
Normal file
@@ -0,0 +1,58 @@
|
||||
// client.mjs
|
||||
import net from 'node:net';
|
||||
|
||||
const HOST = '192.168.2.25'; // <-- set Pico IP
|
||||
const PORT = 5505;
|
||||
|
||||
function buildFrame(cmd, payload) {
|
||||
const len = payload.length;
|
||||
const frame = Buffer.alloc(3 + len);
|
||||
|
||||
frame[0] = cmd;
|
||||
frame[1] = len & 0xff; // len_lo
|
||||
frame[2] = (len >> 8) & 0xff; // len_hi
|
||||
payload.copy(frame, 3);
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
function parseFrame(buffer) {
|
||||
if (buffer.length < 3) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cmd = buffer[0];
|
||||
const len = buffer[1] | (buffer[2] << 8);
|
||||
|
||||
if (buffer.length < 3 + len) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const payload = buffer.subarray(3, 3 + len);
|
||||
return { cmd, payload };
|
||||
}
|
||||
|
||||
const socket = net.createConnection({ host: HOST, port: PORT });
|
||||
|
||||
let rxBuffer = Buffer.alloc(0);
|
||||
|
||||
socket.on('connect', () => {
|
||||
const payload = Buffer.from('Test');
|
||||
const frame = buildFrame(0x10, payload);
|
||||
socket.write(frame);
|
||||
});
|
||||
|
||||
socket.on('data', (chunk) => {
|
||||
rxBuffer = Buffer.concat([rxBuffer, chunk]);
|
||||
|
||||
const frame = parseFrame(rxBuffer);
|
||||
if (frame) {
|
||||
console.log('CMD:', frame.cmd);
|
||||
console.log('Payload:', frame.payload.toString());
|
||||
socket.end();
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('error', (err) => {
|
||||
console.error(err);
|
||||
});
|
||||
37
tcp-command-frame/lwipopts.h
Normal file
37
tcp-command-frame/lwipopts.h
Normal 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 */
|
||||
185
tcp-command-frame/main.c
Normal file
185
tcp-command-frame/main.c
Normal file
@@ -0,0 +1,185 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/ip4_addr.h"
|
||||
|
||||
#include "wifi-settings.h"
|
||||
|
||||
#define MAX_PAYLOAD 256
|
||||
#define TCP_PORT 5505
|
||||
|
||||
extern struct netif *netif_default;
|
||||
|
||||
enum {
|
||||
READ_CMD,
|
||||
READ_LEN0,
|
||||
READ_LEN1,
|
||||
READ_PAYLOAD
|
||||
};
|
||||
|
||||
struct conn_state {
|
||||
uint8_t state;
|
||||
uint8_t cmd;
|
||||
uint16_t len;
|
||||
uint16_t received;
|
||||
uint8_t buf[MAX_PAYLOAD];
|
||||
};
|
||||
|
||||
static void handle_frame(struct tcp_pcb *pcb,
|
||||
uint8_t cmd,
|
||||
uint8_t *payload,
|
||||
uint16_t len)
|
||||
{
|
||||
(void)cmd;
|
||||
(void)payload;
|
||||
(void)len;
|
||||
|
||||
const char reply_payload[] = "Hello";
|
||||
const uint16_t reply_len = 5;
|
||||
|
||||
uint8_t frame[3 + reply_len];
|
||||
|
||||
frame[0] = 0x01; // ACK command
|
||||
frame[1] = (uint8_t)(reply_len & 0xFF);
|
||||
frame[2] = (uint8_t)(reply_len >> 8);
|
||||
memcpy(&frame[3], reply_payload, reply_len);
|
||||
|
||||
tcp_write(pcb, frame, sizeof(frame), TCP_WRITE_FLAG_COPY);
|
||||
tcp_output(pcb);
|
||||
}
|
||||
|
||||
static err_t on_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
|
||||
(void)err;
|
||||
|
||||
struct conn_state *st = (struct conn_state *)arg;
|
||||
|
||||
if (!p) {
|
||||
free(st);
|
||||
tcp_close(pcb);
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
struct pbuf *q = p;
|
||||
|
||||
while (q) {
|
||||
uint8_t *data = (uint8_t *)q->payload;
|
||||
uint16_t i = 0;
|
||||
|
||||
while (i < q->len) {
|
||||
uint8_t b = data[i++];
|
||||
|
||||
if (st->state == READ_CMD) {
|
||||
st->cmd = b;
|
||||
st->len = 0;
|
||||
st->received = 0;
|
||||
st->state = READ_LEN0;
|
||||
}
|
||||
else if (st->state == READ_LEN0) {
|
||||
st->len = (uint16_t)b;
|
||||
st->state = READ_LEN1;
|
||||
}
|
||||
else if (st->state == READ_LEN1) {
|
||||
st->len |= ((uint16_t)b << 8);
|
||||
|
||||
if (st->len > MAX_PAYLOAD) {
|
||||
pbuf_free(p);
|
||||
free(st);
|
||||
tcp_abort(pcb);
|
||||
return ERR_ABRT;
|
||||
}
|
||||
|
||||
st->received = 0;
|
||||
st->state = (st->len == 0) ? READ_CMD : READ_PAYLOAD;
|
||||
|
||||
if (st->len == 0) {
|
||||
handle_frame(pcb, st->cmd, st->buf, 0);
|
||||
}
|
||||
}
|
||||
else { /* READ_PAYLOAD */
|
||||
st->buf[st->received++] = b;
|
||||
|
||||
if (st->received == st->len) {
|
||||
handle_frame(pcb, st->cmd, st->buf, st->len);
|
||||
st->state = READ_CMD;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
q = q->next;
|
||||
}
|
||||
|
||||
tcp_recved(pcb, p->tot_len);
|
||||
pbuf_free(p);
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
static err_t on_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
|
||||
(void)arg;
|
||||
(void)err;
|
||||
|
||||
struct conn_state *st = (struct conn_state *)calloc(1, sizeof(struct conn_state));
|
||||
if (!st) {
|
||||
tcp_abort(newpcb);
|
||||
return ERR_ABRT;
|
||||
}
|
||||
|
||||
st->state = READ_CMD;
|
||||
|
||||
tcp_arg(newpcb, st);
|
||||
tcp_recv(newpcb, on_recv);
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
struct tcp_pcb *listen_pcb = tcp_new_ip_type(IPADDR_TYPE_V4);
|
||||
if (!listen_pcb) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tcp_bind(listen_pcb, IP_ADDR_ANY, TCP_PORT) != ERR_OK) {
|
||||
tcp_close(listen_pcb);
|
||||
return -1;
|
||||
}
|
||||
|
||||
listen_pcb = tcp_listen(listen_pcb);
|
||||
tcp_accept(listen_pcb, on_accept);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
sleep_ms(1);
|
||||
}
|
||||
}
|
||||
121
tcp-command-frame/pico_sdk_import.cmake
Normal file
121
tcp-command-frame/pico_sdk_import.cmake
Normal 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})
|
||||
60
tcp-command-frame/readme.md
Normal file
60
tcp-command-frame/readme.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# TCP Command Frame
|
||||
|
||||
## Authorship
|
||||
|
||||
This document was generated using ChatGPT (OpenAI), version 5.2.
|
||||
It has not been independently reviewed or validated yet.
|
||||
|
||||
# Planned design
|
||||
|
||||
## Transport
|
||||
|
||||
* TCP (lwIP raw API).
|
||||
* Single synchronous request/response per connection.
|
||||
* TCP flow control is the only queue.
|
||||
|
||||
## Future goals
|
||||
|
||||
* HMAC with preshared key
|
||||
|
||||
## Framing
|
||||
|
||||
|
||||
```
|
||||
cmd (1 byte)
|
||||
len (2 bytes, uint16)
|
||||
payload (len bytes)
|
||||
```
|
||||
|
||||
## Limits
|
||||
|
||||
* `MAX_PAYLOAD` compile-time constant.
|
||||
* Fixed local frame buffer: `uint8_t frame[MAX_PAYLOAD]`.
|
||||
* No dynamic allocation based on `len`.
|
||||
|
||||
## Parser
|
||||
|
||||
* Stateful stream parser (READ_CMD → READ_LEN → READ_PAYLOAD).
|
||||
* Accumulate until full frame received.
|
||||
* When `len > MAX_PAYLOAD` → `tcp_abort()` immediately.
|
||||
* On complete frame → execute command → send response → reset parser.
|
||||
|
||||
## Memory safety
|
||||
|
||||
* Only copy up to `MAX_PAYLOAD`.
|
||||
* Call `tcp_recved()` only for consumed bytes.
|
||||
* `pbuf_free()` after processing.
|
||||
* No resynchronization logic.
|
||||
* Protocol violation ⇒ connection closed.
|
||||
|
||||
## Commands (initial)
|
||||
|
||||
* `SETUP_I2C`
|
||||
* `I2C_XFER` (write + read with bounded `wlen`/`rlen`)
|
||||
|
||||
## Result
|
||||
|
||||
* Deterministic RAM usage.
|
||||
* Clear failure behavior.
|
||||
* Minimal parser complexity.
|
||||
* No reliance on TCP message boundaries.
|
||||
Reference in New Issue
Block a user