Replace custom UDP RGB with DDP on 4048, fix picotool -f ordering for v1.1.2, and chunk test frames under MTU so the W5500 receives them. Co-authored-by: Cursor <cursoragent@cursor.com>
334 lines
8.2 KiB
C
334 lines
8.2 KiB
C
/**
|
|
* Portal panel firmware — W5500 DDP + WS2812 (Pico SDK).
|
|
*
|
|
* DDP (Distributed Display Protocol) on UDP 4048:
|
|
* - 10-byte header (+4 if timecode flag set), then RGB payload
|
|
* - destination ID 1 (or 255): one linear frame for all strips
|
|
* - payload layout: strip0 RGB | strip1 RGB | … | strip7 RGB
|
|
* (equal LED count per strip = total_pixels / MAX_STRIPS)
|
|
* - offset = byte offset into that linear frame (network order)
|
|
* - PUSH flag → split buffer onto strips and show
|
|
*
|
|
* One Pico drives up to MAX_STRIPS displays on separate GPIOs.
|
|
* MAX_LEDS is per-strip capacity.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include "board_config.h"
|
|
#include "wizchip_conf.h"
|
|
#include "dhcp.h"
|
|
#include "socket.h"
|
|
#include "timer.h"
|
|
#include "wizchip_spi.h"
|
|
#include "ws2812_led.h"
|
|
|
|
#include "hardware/gpio.h"
|
|
#include "pico/stdlib.h"
|
|
|
|
#define SOCKET_UDP 1
|
|
#define SOCKET_DHCP 0
|
|
#define ETH_BUF_SIZE 2048
|
|
#define DHCP_RETRY_MAX 10
|
|
|
|
#define DDP_HEADER_LEN 10
|
|
#define DDP_HEADER_LEN_TIME 14
|
|
#define DDP_FLAGS1_VER1 0x40
|
|
#define DDP_FLAGS1_VER_MASK 0xc0
|
|
#define DDP_FLAGS1_TIME 0x10
|
|
#define DDP_FLAGS1_PUSH 0x01
|
|
#define DDP_ID_DEFAULT 1
|
|
#define DDP_ID_ALL 255
|
|
|
|
#define FRAME_RGB_BYTES ((uint32_t)MAX_STRIPS * (uint32_t)LED_RGB_BYTES)
|
|
|
|
static wiz_NetInfo g_net_info = {
|
|
.mac = MAC_ADDR,
|
|
.ip = STATIC_IP,
|
|
.sn = STATIC_SN,
|
|
.gw = STATIC_GW,
|
|
.dns = STATIC_DNS,
|
|
#if USE_DHCP
|
|
.dhcp = NETINFO_DHCP,
|
|
#else
|
|
.dhcp = NETINFO_STATIC,
|
|
#endif
|
|
};
|
|
|
|
static uint8_t g_eth_buf[ETH_BUF_SIZE];
|
|
static uint8_t g_frame_buf[FRAME_RGB_BYTES];
|
|
static uint32_t g_frame_end;
|
|
static uint8_t g_need_show;
|
|
static volatile uint16_t g_ms_tick;
|
|
static uint8_t g_dhcp_ready;
|
|
|
|
static void dhcp_timer_cb(void) {
|
|
g_ms_tick++;
|
|
if (g_ms_tick >= 999) {
|
|
g_ms_tick = 0;
|
|
DHCP_time_handler();
|
|
}
|
|
}
|
|
|
|
static void dhcp_assign(void) {
|
|
getIPfromDHCP(g_net_info.ip);
|
|
getGWfromDHCP(g_net_info.gw);
|
|
getSNfromDHCP(g_net_info.sn);
|
|
getDNSfromDHCP(g_net_info.dns);
|
|
g_net_info.dhcp = NETINFO_DHCP;
|
|
network_initialize(g_net_info);
|
|
print_network_information(g_net_info);
|
|
g_dhcp_ready = 1;
|
|
}
|
|
|
|
static void dhcp_conflict(void) {
|
|
printf("DHCP conflict\n");
|
|
}
|
|
|
|
static int network_bring_up(void) {
|
|
#if USE_DHCP
|
|
uint8_t retries = 0;
|
|
DHCP_init(SOCKET_DHCP, g_eth_buf);
|
|
reg_dhcp_cbfunc(dhcp_assign, dhcp_assign, dhcp_conflict);
|
|
wizchip_1ms_timer_initialize(dhcp_timer_cb);
|
|
|
|
while (!g_dhcp_ready && retries < DHCP_RETRY_MAX) {
|
|
int8_t rv = DHCP_run();
|
|
if (rv == DHCP_IP_LEASED) {
|
|
g_dhcp_ready = 1;
|
|
break;
|
|
}
|
|
if (rv == DHCP_FAILED) {
|
|
retries++;
|
|
}
|
|
wizchip_delay_ms(250);
|
|
}
|
|
if (!g_dhcp_ready) {
|
|
printf("DHCP failed, using static IP\n");
|
|
g_net_info.dhcp = NETINFO_STATIC;
|
|
network_initialize(g_net_info);
|
|
print_network_information(g_net_info);
|
|
}
|
|
#else
|
|
network_initialize(g_net_info);
|
|
print_network_information(g_net_info);
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
static int udp_socket_open(void) {
|
|
int8_t sn = socket(SOCKET_UDP, Sn_MR_UDP, UDP_PORT, 0);
|
|
if (sn != SOCKET_UDP) {
|
|
printf("UDP socket open failed: %d\n", sn);
|
|
return -1;
|
|
}
|
|
printf("DDP listening on UDP %d (1 packet, %d strips)\n", UDP_PORT, MAX_STRIPS);
|
|
return 0;
|
|
}
|
|
|
|
static void led_boot_test(unsigned int strip) {
|
|
if (!ws2812_strip_active(strip)) {
|
|
return;
|
|
}
|
|
uint8_t one[3] = {255, 0, 0};
|
|
ws2812_set_rgb(strip, one, 1);
|
|
ws2812_show(strip);
|
|
sleep_ms(150);
|
|
one[0] = 0;
|
|
one[1] = 255;
|
|
ws2812_set_rgb(strip, one, 1);
|
|
ws2812_show(strip);
|
|
sleep_ms(150);
|
|
one[1] = 0;
|
|
one[2] = 255;
|
|
ws2812_set_rgb(strip, one, 1);
|
|
ws2812_show(strip);
|
|
sleep_ms(150);
|
|
one[2] = 0;
|
|
ws2812_set_rgb(strip, one, 1);
|
|
ws2812_show(strip);
|
|
}
|
|
|
|
static void buffer_frame(uint32_t offset, const uint8_t *rgb, uint16_t nbytes) {
|
|
if (nbytes == 0) {
|
|
return;
|
|
}
|
|
if (offset >= FRAME_RGB_BYTES) {
|
|
return;
|
|
}
|
|
uint32_t room = FRAME_RGB_BYTES - offset;
|
|
if (nbytes > room) {
|
|
nbytes = (uint16_t)room;
|
|
}
|
|
memcpy(g_frame_buf + offset, rgb, nbytes);
|
|
uint32_t end = offset + nbytes;
|
|
if (end > g_frame_end) {
|
|
g_frame_end = end;
|
|
}
|
|
}
|
|
|
|
static void show_frame(void) {
|
|
if (g_frame_end < (uint32_t)MAX_STRIPS * 3u) {
|
|
return;
|
|
}
|
|
|
|
uint32_t total_pixels = g_frame_end / 3u;
|
|
uint16_t per_strip = (uint16_t)(total_pixels / (uint32_t)MAX_STRIPS);
|
|
if (per_strip == 0) {
|
|
return;
|
|
}
|
|
if (per_strip > MAX_LEDS) {
|
|
per_strip = MAX_LEDS;
|
|
}
|
|
|
|
uint32_t strip_bytes = (uint32_t)per_strip * 3u;
|
|
for (unsigned int s = 0; s < MAX_STRIPS; s++) {
|
|
if (!ws2812_strip_active(s)) {
|
|
continue;
|
|
}
|
|
const uint8_t *rgb = g_frame_buf + (uint32_t)s * strip_bytes;
|
|
ws2812_set_rgb(s, rgb, per_strip);
|
|
ws2812_show(s);
|
|
}
|
|
}
|
|
|
|
static void handle_ddp(const uint8_t *data, int16_t len) {
|
|
if (len < DDP_HEADER_LEN) {
|
|
return;
|
|
}
|
|
|
|
uint8_t flags1 = data[0];
|
|
if ((flags1 & DDP_FLAGS1_VER_MASK) != DDP_FLAGS1_VER1) {
|
|
return;
|
|
}
|
|
|
|
uint8_t hdr_len = DDP_HEADER_LEN;
|
|
if (flags1 & DDP_FLAGS1_TIME) {
|
|
hdr_len = DDP_HEADER_LEN_TIME;
|
|
if (len < hdr_len) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
uint8_t id = data[3];
|
|
if (id != DDP_ID_DEFAULT && id != DDP_ID_ALL) {
|
|
return;
|
|
}
|
|
|
|
uint32_t offset = ((uint32_t)data[4] << 24) | ((uint32_t)data[5] << 16) |
|
|
((uint32_t)data[6] << 8) | (uint32_t)data[7];
|
|
uint16_t dlen = ((uint16_t)data[8] << 8) | (uint16_t)data[9];
|
|
|
|
int16_t avail = (int16_t)(len - hdr_len);
|
|
if (avail < 0) {
|
|
return;
|
|
}
|
|
if (dlen > (uint16_t)avail) {
|
|
dlen = (uint16_t)avail;
|
|
}
|
|
|
|
if (dlen > 0) {
|
|
if (offset == 0) {
|
|
g_frame_end = 0;
|
|
}
|
|
buffer_frame(offset, data + hdr_len, dlen);
|
|
}
|
|
|
|
if (flags1 & DDP_FLAGS1_PUSH) {
|
|
g_need_show = 1;
|
|
}
|
|
}
|
|
|
|
static void poll_udp(void) {
|
|
while (1) {
|
|
uint16_t rx = getSn_RX_RSR(SOCKET_UDP);
|
|
if (rx == 0) {
|
|
return;
|
|
}
|
|
if (rx > ETH_BUF_SIZE) {
|
|
rx = ETH_BUF_SIZE;
|
|
}
|
|
|
|
uint8_t src_ip[4];
|
|
uint16_t src_port;
|
|
int16_t n = recvfrom(SOCKET_UDP, g_eth_buf, rx, src_ip, &src_port);
|
|
if (n <= 0) {
|
|
return;
|
|
}
|
|
handle_ddp(g_eth_buf, n);
|
|
}
|
|
}
|
|
|
|
static void print_strip_map(void) {
|
|
printf("strips=%d ddp=1pkt", MAX_STRIPS);
|
|
for (unsigned int s = 0; s < MAX_STRIPS; s++) {
|
|
if (ws2812_strip_active(s)) {
|
|
printf(" [%u]=GP%u", s, ws2812_get_pin(s));
|
|
}
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
int main(void) {
|
|
stdio_init_all();
|
|
sleep_ms(2000);
|
|
|
|
gpio_init(PIN_LED_STATUS);
|
|
gpio_set_dir(PIN_LED_STATUS, GPIO_OUT);
|
|
|
|
printf("portal panel firmware (Pico SDK, DDP)\n");
|
|
printf("max_leds=%d max_strips=%d panel_id=%d mac=02:50:52:54:4C:%02X "
|
|
"ip=%u.%u.%u.%u %s\n",
|
|
MAX_LEDS, MAX_STRIPS, PANEL_ID, (unsigned)PANEL_ID, g_net_info.ip[0],
|
|
g_net_info.ip[1], g_net_info.ip[2], g_net_info.ip[3],
|
|
USE_DHCP ? "dhcp" : "static");
|
|
|
|
wizchip_spi_initialize();
|
|
wizchip_cris_initialize();
|
|
wizchip_reset();
|
|
wizchip_initialize();
|
|
wizchip_check();
|
|
|
|
ws2812_init(MAX_LEDS);
|
|
memset(g_frame_buf, 0, sizeof(g_frame_buf));
|
|
g_frame_end = 0;
|
|
g_need_show = 0;
|
|
|
|
static const unsigned int default_pins[MAX_STRIPS] = DEFAULT_STRIP_PINS;
|
|
for (unsigned int s = 0; s < MAX_STRIPS; s++) {
|
|
if (ws2812_set_pin(s, default_pins[s]) == 0) {
|
|
led_boot_test(s);
|
|
} else {
|
|
printf("default strip %u GP%u failed\n", s, default_pins[s]);
|
|
}
|
|
}
|
|
print_strip_map();
|
|
|
|
network_bring_up();
|
|
if (udp_socket_open() != 0) {
|
|
while (1) {
|
|
gpio_xor_mask(1u << PIN_LED_STATUS);
|
|
sleep_ms(100);
|
|
}
|
|
}
|
|
|
|
uint32_t heartbeat = 0;
|
|
while (1) {
|
|
#if USE_DHCP
|
|
DHCP_run();
|
|
#endif
|
|
poll_udp();
|
|
if (g_need_show) {
|
|
show_frame();
|
|
g_need_show = 0;
|
|
}
|
|
if (++heartbeat >= 500) {
|
|
gpio_xor_mask(1u << PIN_LED_STATUS);
|
|
heartbeat = 0;
|
|
}
|
|
sleep_ms(1);
|
|
}
|
|
}
|