C-ITS Sniffer v2: ST7735-Display, SPI2-Fix, Single-Core-Fix, 5-min-Zähler

- ST7735 SPI-Treiber (80x160, 5x7-Font) mit Debug-Panel (5MIN/FRM/DROP/SRC/FREE/CH)
- SPI2_HOST statt SPI1_HOST (SPI1 = Flash-reserviert auf ESP32-C5)
- xTaskCreate statt xTaskCreatePinnedToCore (C5 ist Single-Core)
- SD-Karte (SPI 1-bit, 32 GB, FAT32) + Flash-storage-Fallback
- Serial-REPL: stats, seron, seroff, reboot
- APA102 RGB-Status-LED (blau=boot, gruen=armed, rot=fehler)
- Build: ESP-IDF v6.1-dev, RISC-V, ~920 KB

Replaced old block1-4 prototype with full modular firmware.
This commit is contained in:
clawdia committed 2026-11-10 10:21:23 +01:00
1 parent b852826f5c
commit d1b3d1bae6
24 files changed
+1552 -698

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
idf_component_register(
SRCS
"main.c"
"cits_rx.c"
"cits_parse.c"
"cits_store.c"
"led.c"
"st7735.c"
INCLUDE_DIRS
"."
PRIV_REQUIRES
esp_wifi
esp_phy
esp_netif
esp_event
esp_timer
heap
esp_driver_gpio
esp_driver_spi
esp_driver_sdspi
esp_driver_sdmmc
esp_rom
esp_partition
console
nvs_flash
fatfs
)
+106
View File
@@ -0,0 +1,106 @@
#include "cits_parse.h"
#include <string.h>
esp_err_t cits_parse_80211(const uint8_t *f, uint16_t len, ieee80211_hdr_t *o)
{
if (!f || len < 14) {
return ESP_ERR_INVALID_SIZE;
}
memset(o, 0, sizeof(*o));
uint16_t fc = (uint16_t)(f[0] | (f[1] << 8));
o->type = (uint8_t)((fc >> 2) & 0x03);
o->subtype = (uint8_t)((fc >> 4) & 0x0F);
o->to_ds = (fc >> 8) & 1;
o->from_ds = (fc >> 9) & 1;
o->more_frag = (fc >> 10) & 1;
o->retry = (fc >> 11) & 1;
o->protected_ = (fc >> 14) & 1;
o->order = (fc >> 15) & 1;
/* Address count: data -> 3 (4 if WDS to_ds&&from_ds), mgmt -> 3, ctrl -> 2 */
int naddr;
if (o->type == 2) {
naddr = (o->to_ds && o->from_ds) ? 4 : 3;
} else if (o->type == 0) {
naddr = 3;
} else {
naddr = 2;
}
int off = 4;
if ((uint32_t)off + (uint32_t)naddr * 6 > len) {
return ESP_ERR_INVALID_SIZE;
}
memcpy(o->da, f + off, 6); off += 6;
memcpy(o->sa, f + off, 6); off += 6;
memcpy(o->bssid, f + off, 6); off += 6;
o->addr_count = naddr;
if (o->type == 2) {
off += 4; /* seq control + frame-control word */
}
o->is_data = (o->type == 2);
if ((uint32_t)off <= len) {
o->payload = f + off;
o->payload_len = (uint16_t)(len - off);
} else {
o->payload = NULL;
o->payload_len = 0;
}
return ESP_OK;
}
esp_err_t cits_parse_asdu(const uint8_t *p, uint16_t len, cits_msg_t *o)
{
if (!p || len < 7) {
return ESP_ERR_INVALID_SIZE;
}
memset(o, 0, sizeof(*o));
o->asdu_id = p[0];
o->asdu_version = p[1];
o->asdu_len = (uint16_t)(p[2] | (p[3] << 8)); /* ASdu body length (BE) */
switch (o->asdu_id) {
case 0x01: o->type = CITS_SPM; break;
case 0x02: o->type = CITS_SPT; break;
case 0x03: o->type = CITS_SPS; break;
case 0x04: o->type = CITS_SPA; break;
case 0x05: o->type = CITS_MAP; break;
case 0x06: o->type = CITS_CAM; break;
case 0x07: o->type = CITS_DENM; break;
default: o->type = CITS_UNKNOWN; break;
}
/* ASdu body begins after: id(1) ver(1) len(2) protov(1) len2(2) = 7 bytes */
int b = 7;
if (o->type == CITS_SPM && (uint16_t)(b + 6) <= len) {
o->site_id = (uint32_t)(p[b] << 16) | (p[b + 1] << 8) | p[b + 2];
o->num_phases = p[b + 5];
/* first SignalPhase: phaseId(1) stateId(2) ... -> state is bytes 6..7 */
if (o->num_phases >= 1 && (uint16_t)(b + 8) <= len) {
o->phase0_state = p[b + 7]; /* ASN.1 INTEGER state value (0-5) */
}
} else if (o->type == CITS_CAM && (uint16_t)(b + 5) <= len) {
/* msgIssueID(2) then stationID(21): take leading 3 bytes as a hint */
o->station_id = (uint32_t)(p[b + 2] << 16) | (p[b + 3] << 8) | p[b + 4];
}
return ESP_OK;
}
const char *cits_type_name(cits_msg_type_t t)
{
switch (t) {
case CITS_SPM: return "SpMS";
case CITS_SPT: return "SpTM";
case CITS_SPS: return "SpSM";
case CITS_SPA: return "SpAM";
case CITS_MAP: return "MAPM";
case CITS_CAM: return "CAM";
case CITS_DENM: return "DENM";
default: return "raw";
}
}
+56
View File
@@ -0,0 +1,56 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
/* Parsed 802.11 MAC header. */
typedef struct {
uint8_t type; /* 0=Mgmt, 1=Ctrl, 2=Data */
uint8_t subtype;
bool to_ds, from_ds, more_frag, retry, protected_, order;
uint8_t da[6];
uint8_t sa[6];
uint8_t bssid[6];
int addr_count;
const uint8_t *payload;
uint16_t payload_len;
bool is_data;
} ieee80211_hdr_t;
/* C-ITS ASdu classification.
*
* ASdu id values follow the European C-ITS profile (ETSI EN 303 617 / TS 102 913).
* The mapping and the per-message field offsets below are a best-effort decode;
* the authoritative capture is the raw hex we always log, so any mis-decode is
* recoverable offline. */
typedef enum {
CITS_UNKNOWN = 0,
CITS_SPM, /* 0x01 SpMS - Signal Phase & Timing (traffic light) */
CITS_SPT, /* 0x02 SpTM - Signal Timing Message */
CITS_SPS, /* 0x03 SpSM - Signal State Message */
CITS_SPA, /* 0x04 SpAM - Signal Acknowledgment Message */
CITS_MAP, /* 0x05 MAPM - MAP Message */
CITS_CAM, /* 0x06 CAM - Cooperative Awareness Message (vehicle) */
CITS_DENM, /* 0x07 DENM - Decentralised Environmental Notification */
} cits_msg_type_t;
typedef struct {
cits_msg_type_t type;
uint8_t asdu_id;
uint8_t asdu_version;
uint16_t asdu_len;
uint32_t site_id; /* SpMS: 3-byte traffic-light site id */
uint8_t num_phases; /* SpMS: number of signal phases */
uint8_t phase0_state; /* SpMS: first phase state (0 off,1 red,2 amber,3 green,4 flash) */
uint32_t station_id; /* CAM/other: leading station-id bytes (best-effort) */
} cits_msg_t;
/* Parse the 802.11 MAC header; fills address fields and points 'payload' at the
* frame body (after any seq/fcs word). */
esp_err_t cits_parse_80211(const uint8_t *frame, uint16_t len, ieee80211_hdr_t *out);
/* Parse the C-ITS ASdu payload (the body of an 802.11 data frame) and
* best-effort classify/extract fields. */
esp_err_t cits_parse_asdu(const uint8_t *payload, uint16_t len, cits_msg_t *out);
const char *cits_type_name(cits_msg_type_t t);
+232
View File
@@ -0,0 +1,232 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "esp_heap_caps.h"
#include "esp_timer.h"
#include "esp_err.h"
#include "phy_11p.h"
#include "cits_parse.h"
#include "cits_store.h"
#include "cits_rx.h"
static const char *TAG = "cits_rx";
#define FRAME_MAX 2400
#define Q_DEPTH 16
typedef struct {
uint16_t len;
uint8_t data[FRAME_MAX];
} frame_t;
static QueueHandle_t s_q = NULL;
static volatile uint64_t s_rx = 0;
static volatile uint64_t s_drop = 0;
static volatile uint64_t s_cits = 0;
static volatile bool s_serial = true;
/* 5-minute sliding window of C-ITS ASdu counts (10 buckets x 30s). */
#define WB_N 10
#define WB_SEC 30
static uint32_t wb_cnt[WB_N] = {0};
static uint32_t wb_pid[WB_N] = {0};
static void cits_rx_event(void)
{
uint32_t pid = (uint32_t)(esp_timer_get_time() / 1000000ULL) / WB_SEC;
int idx = pid % WB_N;
if (wb_pid[idx] != pid) { wb_pid[idx] = pid; wb_cnt[idx] = 1; }
else { wb_cnt[idx]++; }
}
uint32_t cits_rx_count_5min(void)
{
uint32_t cur = (uint32_t)(esp_timer_get_time() / 1000000ULL) / WB_SEC;
uint32_t sum = 0;
for (int i = 0; i < WB_N; i++) {
if (wb_pid[i] != 0 && wb_pid[i] >= cur - (WB_N - 1)) {
sum += wb_cnt[i];
}
}
return sum;
}
/* Promiscuous RX callback (driver context) - copy fast, defer parsing. */
static void on_rx(void *buf, wifi_promiscuous_pkt_type_t type)
{
if (type != WIFI_PKT_MGMT && type != WIFI_PKT_CTRL && type != WIFI_PKT_DATA) {
return; /* WIFI_PKT_MISC: not a real frame */
}
wifi_promiscuous_pkt_t *p = (wifi_promiscuous_pkt_t *)buf;
if (p->rx_ctrl.rx_state != 0) {
return; /* errored frame */
}
uint16_t len = (uint16_t)p->rx_ctrl.sig_len; /* includes FCS */
if (len > FRAME_MAX) {
len = FRAME_MAX;
}
frame_t *f = (frame_t *)heap_caps_malloc(sizeof(frame_t), MALLOC_CAP_DEFAULT);
if (!f) {
s_drop++;
return;
}
f->len = len;
memcpy(f->data, p->payload, len);
if (xQueueSend(s_q, f, 0) != pdPASS) {
heap_caps_free(f);
s_drop++;
} else {
s_rx++;
}
}
static void hexn(const uint8_t *d, int n, char *out, int outsz)
{
static const char *H = "0123456789abcdef";
int i;
for (i = 0; i < n && (2 * i + 2) <= outsz; i++) {
out[2 * i] = H[d[i] >> 4];
out[2 * i + 1] = H[d[i] & 0x0F];
}
out[2 * i] = '\0';
}
static void process_task(void *arg)
{
(void)arg;
for (;;) {
frame_t *f = NULL;
if (xQueueReceive(s_q, &f, portMAX_DELAY) != pdPASS) {
continue;
}
if (!f) {
continue;
}
ieee80211_hdr_t h;
bool ok = (cits_parse_80211(f->data, f->len, &h) == ESP_OK);
cits_msg_t m;
bool have = false;
if (ok && h.is_data && h.payload && h.payload_len >= 7) {
if (cits_parse_asdu(h.payload, h.payload_len, &m) == ESP_OK && m.type != CITS_UNKNOWN) {
have = true;
s_cits++;
cits_rx_event();
}
}
uint64_t now_ms = (uint64_t)(esp_timer_get_time() / 1000);
if (s_serial) {
if (have) {
ESP_LOGI(TAG, "%-5s id=0x%02x site=%lu ph=%u st0=%u len=%u sa=%02x:%02x:%02x:%02x:%02x:%02x",
cits_type_name(m.type), m.asdu_id, (unsigned long)m.site_id,
m.num_phases, m.phase0_state, h.payload_len,
h.sa[0], h.sa[1], h.sa[2], h.sa[3], h.sa[4], h.sa[5]);
} else if (ok) {
ESP_LOGI(TAG, "80211 t=%u st=%u len=%u sa=%02x:%02x:%02x:%02x:%02x:%02x",
h.type, h.subtype, f->len,
h.sa[0], h.sa[1], h.sa[2], h.sa[3], h.sa[4], h.sa[5]);
}
}
if (cits_store_ready()) {
char hex[2402];
int hn = (int)f->len;
if (hn > 1200) {
hn = 1200;
}
hexn(f->data, hn, hex, sizeof(hex));
char rec[2600];
if (have) {
snprintf(rec, sizeof(rec), "%llu,%s,0x%02x,%u,%lu,%u,%u,%lu,%s",
(unsigned long long)now_ms, cits_type_name(m.type), m.asdu_id,
m.asdu_version, (unsigned long)m.site_id, m.num_phases, m.phase0_state,
(unsigned long)m.station_id, hex);
} else {
const char *cls = ok ? (h.is_data ? "data" : "mgmt") : "raw";
snprintf(rec, sizeof(rec), "%llu,%s,0x00,0,0,0,0,0,%s",
(unsigned long long)now_ms, cls, hex);
}
cits_store_log(rec);
}
heap_caps_free(f);
}
}
esp_err_t cits_rx_start(void)
{
s_q = xQueueCreate(Q_DEPTH, sizeof(frame_t));
if (!s_q) {
return ESP_ERR_NO_MEM;
}
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_err_t e = esp_wifi_init(&cfg);
if (e != ESP_OK && e != ESP_ERR_INVALID_STATE) {
return e;
}
e = esp_wifi_set_storage(WIFI_STORAGE_RAM);
if (e != ESP_OK) {
return e;
}
e = esp_wifi_set_mode(WIFI_MODE_NULL);
if (e != ESP_OK) {
return e;
}
e = esp_wifi_start();
if (e != ESP_OK) {
return e;
}
wifi_promiscuous_filter_t filt;
filt.filter_mask = WIFI_PROMIS_FILTER_MASK_ALL;
esp_wifi_set_promiscuous_filter(&filt);
e = esp_wifi_set_promiscuous_rx_cb(on_rx);
if (e != ESP_OK) {
return e;
}
/* --- The 5.9 GHz / 802.11p "secret command" sequence ---
* (undocumented libphy.a APIs: phy_11p_set + phy_change_channel). */
e = esp_wifi_set_promiscuous(true);
if (e != ESP_OK) {
return e;
}
phy_11p_set(1, 0);
e = esp_wifi_set_channel(140, WIFI_SECOND_CHAN_NONE);
if (e != ESP_OK) {
return e;
}
phy_change_channel(5900, 1, 0, 0);
/* ESP32-C5 is single-core: use xTaskCreate (no core pinning). */
if (xTaskCreate(process_task, "cits_proc", 8192, NULL, 5, NULL) != pdPASS) {
return ESP_FAIL;
}
ESP_LOGI(TAG, "armed: 5.9 GHz C-ITS promiscuous capture (ch 140 / 5900 MHz)");
return ESP_OK;
}
void cits_rx_set_serial(bool on)
{
s_serial = on;
}
uint64_t cits_rx_frames(void)
{
return s_rx;
}
uint64_t cits_rx_cits(void)
{
return s_cits;
}
uint64_t cits_rx_drops(void)
{
return s_drop;
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "esp_err.h"
#include <stdint.h>
#include <stdbool.h>
/* Start the 5.9 GHz / 802.11p C-ITS promiscuous capture (the "secret command"
* sequence from the opentrafficmap ITS-G5 receiver) and spawn the processing
* task that parses frames and logs them to CSV (SD/flash) + serial. */
esp_err_t cits_rx_start(void);
/* Enable/disable live serial logging of each frame (CSV logging is always on). */
void cits_rx_set_serial(bool on);
/* Total 802.11 frames seen (valid rx_state) since start. */
uint64_t cits_rx_frames(void);
/* Frames whose payload classified as a known C-ITS ASdu. */
uint64_t cits_rx_cits(void);
/* Frames dropped (queue full / out of memory). */
uint64_t cits_rx_drops(void);
/* C-ITS ASdu count in the last 5 minutes (sliding window). */
uint32_t cits_rx_count_5min(void);
+186
View File
@@ -0,0 +1,186 @@
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "esp_partition.h"
#include "esp_vfs_fat.h"
#include "wear_levelling.h"
#include "sdmmc_cmd.h"
#include "driver/sdmmc_host.h"
#include "driver/sdspi_host.h"
#include "driver/spi_common.h"
#include "cits_store.h"
static const char *TAG = "cits_store";
/* T-Dongle C5 microSD reader (SPI, 1-bit) — Lilygo official pinout.
* Note: SD shares the SPI1 data lines with the optional LCD; we do not drive
* the LCD, and CS (23) is SD-exclusive, so there is no conflict. */
#define SD_PIN_MOSI 2 /* SD_CMD */
#define SD_PIN_MISO 7 /* SD_D0 */
#define SD_PIN_CLK 6 /* SD_CLK */
#define SD_PIN_CS 23 /* SD_CS */
#define MOUNT_POINT "/sd"
#define ROTATE_US (600ULL * 1000000ULL) /* 10 minutes */
#define FLUSH_LINES 64 /* flush to media every N lines */
static FILE *s_file = NULL;
static int64_t s_open_us = 0;
static uint64_t s_lines = 0;
static bool s_mounted = false;
static char s_src[12] = "none";
static char s_path[96];
static wl_handle_t s_wl = -1;
static void open_current(void)
{
if (s_file) {
fflush(s_file);
fclose(s_file);
s_file = NULL;
}
uint64_t boot_ms = (uint64_t)(esp_timer_get_time() / 1000);
snprintf(s_path, sizeof(s_path), "cits_%010llu.csv", (unsigned long long)boot_ms);
char full[160];
snprintf(full, sizeof(full), MOUNT_POINT "/" "%s", s_path);
s_file = fopen(full, "a");
if (!s_file) {
ESP_LOGE(TAG, "fopen failed: %s", full);
return;
}
/* 16 KiB userspace buffer -> far fewer physical media writes */
setvbuf(s_file, NULL, _IOFBF, 16 * 1024);
fputs("boot_ms,cls,asdu_id,ver,site_id,phases,phase0_state,station_id,hex\n", s_file);
s_open_us = esp_timer_get_time();
ESP_LOGI(TAG, "CSV writing: %s", full);
}
static bool mount_sd(void)
{
spi_bus_config_t bus = {
.mosi_io_num = SD_PIN_MOSI,
.miso_io_num = SD_PIN_MISO,
.sclk_io_num = SD_PIN_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4096,
};
esp_err_t e = spi_bus_initialize(SPI2_HOST, &bus, SPI_DMA_CH_AUTO);
if (e != ESP_OK) {
ESP_LOGW(TAG, "spi_bus_initialize: %s", esp_err_to_name(e));
return false;
}
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
sdspi_device_config_t slot = SDSPI_DEVICE_CONFIG_DEFAULT();
slot.gpio_cs = SD_PIN_CS;
slot.host_id = host.slot;
esp_vfs_fat_sdmmc_mount_config_t mc = {
.format_if_mount_failed = true,
.max_files = 4,
.allocation_unit_size = 16 * 1024,
};
sdmmc_card_t *card = NULL;
e = esp_vfs_fat_sdspi_mount(MOUNT_POINT, &host, &slot, &mc, &card);
if (e == ESP_OK) {
snprintf(s_src, sizeof(s_src), "sd-card");
return true;
}
ESP_LOGW(TAG, "SD mount failed (%s) - no card or bad card", esp_err_to_name(e));
return false;
}
static bool mount_flash(void)
{
const esp_partition_t *part = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage");
if (!part) {
ESP_LOGW(TAG, "no flash 'storage' partition found");
return false;
}
esp_vfs_fat_mount_config_t mc = {
.format_if_mount_failed = true,
.max_files = 4,
.allocation_unit_size = 16 * 1024,
};
esp_err_t e = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_POINT, "storage", &mc, &s_wl);
if (e == ESP_OK) {
snprintf(s_src, sizeof(s_src), "flash");
return true;
}
ESP_LOGW(TAG, "flash mount failed (%s)", esp_err_to_name(e));
return false;
}
esp_err_t cits_store_init(void)
{
if (s_mounted) {
return ESP_OK;
}
if (!mount_sd()) {
if (!mount_flash()) {
ESP_LOGE(TAG, "NO STORAGE (SD + flash both failed) - serial-only logging");
return ESP_FAIL;
}
}
s_mounted = true;
ESP_LOGI(TAG, "storage backend: %s", s_src);
open_current();
return (s_file != NULL) ? ESP_OK : ESP_FAIL;
}
esp_err_t cits_store_log(const char *record)
{
if (!s_file) {
return ESP_FAIL;
}
if (esp_timer_get_time() - s_open_us >= ROTATE_US) {
open_current();
if (!s_file) {
return ESP_FAIL;
}
}
fputs(record, s_file);
fputc('\n', s_file);
s_lines++;
if ((s_lines & (FLUSH_LINES - 1)) == 0) {
fflush(s_file);
}
return ESP_OK;
}
void cits_store_rotate(void)
{
if (!s_mounted) {
return;
}
open_current();
}
bool cits_store_ready(void)
{
return s_mounted && (s_file != NULL);
}
const char *cits_store_source(void)
{
return s_src;
}
const char *cits_store_path(void)
{
char full[160];
snprintf(full, sizeof(full), MOUNT_POINT "/" "%s", s_path);
/* return a stable static copy */
static char p[160];
snprintf(p, sizeof(p), MOUNT_POINT "/" "%s", s_path);
(void)full;
return p;
}
uint64_t cits_store_count(void)
{
return s_lines;
}
+38
View File
@@ -0,0 +1,38 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
/* Storage backend for C-ITS capture.
*
* Primary : SPI SD card reader (T-Dongle C5 pins). Removable -> pull the card
* out and read the CSV on a PC.
* Fallback: built-in flash 'storage' FAT partition (works with no card).
*
* CSV files rotate every 10 minutes; a header line is written on open.
*/
/* Mount a backend (SD first, flash fallback) and open the first CSV file.
* Returns ESP_OK if a usable filesystem is available, ESP_FAIL otherwise
* (in which case capture continues to serial only). */
esp_err_t cits_store_init(void);
/* Append one CSV record (no newline needed — one is added) and flush
* periodically. Safe to call from a single processing task. */
esp_err_t cits_store_log(const char *record);
/* Close/flush the current file and start a new one (also called automatically
* on the 10-minute boundary). */
void cits_store_rotate(void);
/* True if a filesystem (SD or flash) is mounted and logging to it. */
bool cits_store_ready(void);
/* Human-readable backend in use ("sd-card" | "flash" | "none"). */
const char *cits_store_source(void);
/* Absolute path of the file currently being written. */
const char *cits_store_path(void);
/* Total CSV records written since boot. */
uint64_t cits_store_count(void);
+7
View File
@@ -0,0 +1,7 @@
# No managed (3rd-party) components needed — this project uses core ESP-IDF
# components only (esp_wifi, esp_phy, fatfs, esp_console, ...).
# Keeping it free of mqtt/w5500/ethernet deps keeps the image small and the
# boot path short (perf priority).
dependencies:
idf:
version: ">=5.0"
+64
View File
@@ -0,0 +1,64 @@
#include "freertos/FreeRTOS.h"
#include "driver/gpio.h"
#include "esp_rom_sys.h"
#include "led.h"
/* APA102 on the T-Dongle C5 (Lilygo pinout): DI=GPIO5, CI=GPIO4. */
#define LED_DI 5
#define LED_CI 4
static bool s_init = false;
static void apa_bit(int val)
{
gpio_set_level(LED_CI, 0);
gpio_set_level(LED_DI, val ? 1 : 0);
esp_rom_delay_us(1);
gpio_set_level(LED_CI, 1);
esp_rom_delay_us(1);
gpio_set_level(LED_CI, 0);
}
void led_init(void)
{
if (s_init) {
return;
}
gpio_config_t g = {0};
g.pin_bit_mask = (1ULL << LED_DI) | (1ULL << LED_CI);
g.mode = GPIO_MODE_OUTPUT;
g.pull_up_en = GPIO_PULLUP_DISABLE;
g.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpio_set_level(LED_DI, 0);
gpio_set_level(LED_CI, 0);
gpio_config(&g);
s_init = true;
}
/* 12-bit channel: 1 flag bit (must be 1) + 11 brightness bits (all on/off). */
static void apa12(int on)
{
apa_bit(1);
int i;
for (i = 0; i < 11; i++) {
apa_bit(on ? 1 : 0);
}
}
void led_set(uint8_t r, uint8_t g, uint8_t b)
{
if (!s_init) {
led_init();
}
int i;
for (i = 0; i < 32; i++) apa_bit(0); /* 32-bit start frame (all low) */
apa12(g ? 1 : 0); /* channel order is G, R, B */
apa12(r ? 1 : 0);
apa12(b ? 1 : 0);
for (i = 0; i < 32; i++) apa_bit(1); /* 32-bit end frame (all high) */
}
void led_clear(void)
{
led_set(0, 0, 0);
}
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include <stdint.h>
/* APA102 RGB status LED on the T-Dongle C5 (DI=5, CI=4), bit-banged.
* Brightness values here are full-on/full-off per channel (0 = off, else on). */
void led_init(void);
void led_set(uint8_t r, uint8_t g, uint8_t b);
void led_clear(void);
+186
View File
@@ -0,0 +1,186 @@
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_event.h"
#include "esp_timer.h"
#include "esp_err.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_console.h"
#include "nvs_flash.h"
#include "led.h"
#include "cits_store.h"
#include "cits_rx.h"
#include "st7735.h"
#include <sys/statvfs.h>
static const char *TAG = "cits_main";
static int cmd_stats(int argc, char **argv)
{
(void)argc; (void)argv;
printf("rx=%llu cits=%llu drops=%llu\n",
(unsigned long long)cits_rx_frames(),
(unsigned long long)cits_rx_cits(),
(unsigned long long)cits_rx_drops());
printf("storage=%s ready=%d count=%llu path=%s\n",
cits_store_source(), (int)cits_store_ready(),
(unsigned long long)cits_store_count(), cits_store_path());
return 0;
}
static int cmd_seron(int argc, char **argv)
{
(void)argc; (void)argv;
cits_rx_set_serial(true);
printf("live serial logging ON\n");
return 0;
}
static int cmd_seroff(int argc, char **argv)
{
(void)argc; (void)argv;
cits_rx_set_serial(false);
printf("live serial logging OFF (CSV continues)\n");
return 0;
}
static int cmd_reboot(int argc, char **argv)
{
(void)argc; (void)argv;
esp_restart();
return 0;
}
static esp_err_t reg_cmd(const char *name, const char *help, esp_console_cmd_func_t func)
{
esp_console_cmd_t c;
c.command = name;
c.help = help;
c.hint = NULL;
c.func = func;
return esp_console_cmd_register(&c);
}
/* ---- ST7735 debug panel (80x160) ---- */
static void render_panel(void)
{
if (!st7735_ready()) return;
char line[24];
st7735_set_fg(0x07E0); /* green */
st7735_set_bg(0x0000); /* black */
st7735_clear(0x0000);
st7735_text(0, 0, "C-ITS SNIFFER");
snprintf(line, sizeof(line), "5MIN: %lu", (unsigned long)cits_rx_count_5min());
st7735_text(0, 10, line);
snprintf(line, sizeof(line), "FRM: %llu", (unsigned long long)cits_rx_frames());
st7735_text(0, 20, line);
snprintf(line, sizeof(line), "DROP: %llu", (unsigned long long)cits_rx_drops());
st7735_text(0, 30, line);
st7735_text(0, 40, "SRC: ");
st7735_text(26, 40, cits_store_source());
const char *src = cits_store_source();
if (cits_store_ready() && src && !strcmp(src, "sd-card")) {
struct statvfs sv;
if (statvfs("/sd", &sv) == 0) {
uint64_t freeb = (uint64_t)sv.f_bavail * (uint64_t)sv.f_bsize;
uint64_t totb = (uint64_t)sv.f_blocks * (uint64_t)sv.f_bsize;
int pct = totb ? (int)(100ULL * freeb / totb) : 0;
snprintf(line, sizeof(line), "FREE: %d%%", pct);
st7735_text(0, 50, line);
} else {
st7735_text(0, 50, "FREE: n/a");
}
} else {
st7735_text(0, 50, "FREE: flash");
}
st7735_text(0, 60, "CH: 140 / 5.9G");
}
static void display_task(void *arg)
{
(void)arg;
for (;;) {
render_panel();
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void app_main(void)
{
esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("sdmmc_host", ESP_LOG_WARN);
esp_log_level_set("sdmmc", ESP_LOG_WARN);
esp_log_level_set("fatfs", ESP_LOG_WARN);
esp_log_level_set("sdspi", ESP_LOG_WARN);
esp_log_level_set("spi", ESP_LOG_WARN);
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_timer_init();
ESP_LOGI(TAG, "==========================================");
ESP_LOGI(TAG, " C-ITS Sniffer - ESP32-C5 (Lilygo T-Dongle C5)");
ESP_LOGI(TAG, " 5.9 GHz / 802.11p capture -> SD card + serial");
ESP_LOGI(TAG, "==========================================");
led_init();
led_set(0, 0, 60); /* blue: booting */
/* NVS (required by the Wi-Fi driver) */
esp_err_t e = nvs_flash_init();
if (e == ESP_ERR_NVS_NO_FREE_PAGES || e == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
e = nvs_flash_init();
}
if (e != ESP_OK) {
ESP_LOGW(TAG, "nvs_flash_init: %s (continuing)", esp_err_to_name(e));
}
/* Storage: SD card primary, flash 'storage' partition as fallback. */
if (cits_store_init() == ESP_OK) {
ESP_LOGI(TAG, "storage backend: %s", cits_store_source());
led_set(0, 60, 0);
} else {
ESP_LOGE(TAG, "no storage (no SD card, no flash) - serial-only logging");
led_set(60, 0, 0);
}
/* Arm the 5.9 GHz C-ITS receiver. */
e = cits_rx_start();
if (e != ESP_OK) {
ESP_LOGE(TAG, "cits_rx_start failed: %s", esp_err_to_name(e));
led_set(60, 0, 0);
} else {
ESP_LOGI(TAG, "armed - listening for C-ITS on 5.9 GHz");
led_set(0, 60, 0);
}
/* ST7735 debug display (shares SPI2_HOST bus with the SD card). */
if (st7735_init() == ESP_OK) {
ESP_LOGI(TAG, "display ready (ST7735 80x160)");
xTaskCreate(display_task, "disp", 8192, NULL, 3, NULL);
} else {
ESP_LOGW(TAG, "display init failed (continuing)");
}
/* Console REPL (USB-JTAG primary; UART fallback) */
esp_console_repl_config_t repl_cfg = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
repl_cfg.prompt = "cits> ";
esp_console_repl_t *repl = NULL;
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
esp_console_dev_usb_serial_jtag_config_t usbj = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&usbj, &repl_cfg, &repl));
#elif CONFIG_ESP_CONSOLE_UART
esp_console_dev_uart_config_t uart = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart, &repl_cfg, &repl));
#endif
reg_cmd("stats", "show rx + storage stats", cmd_stats);
reg_cmd("seron", "enable live serial logging", cmd_seron);
reg_cmd("seroff", "disable live serial logging (CSV continues)", cmd_seroff);
reg_cmd("reboot", "reboot the device", cmd_reboot);
ESP_ERROR_CHECK(esp_console_start_repl(repl));
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* Undocumented private PHY APIs, provided by esp_phy (libphy.a) in the
* 6.x-series ESP-IDF builds. They expose the C-ITS / 802.11p radio mode on
* the 5.9 GHz band that the public WiFi API does not reach.
*
* phy_11p_set(1, 0) -> enable the 11p / C-ITS PHY path
* phy_change_channel(5900,1,0,0) -> centre-frequency to 5.9 GHz
*
* Both resolve from esp_phy's C5 libphy.a at link time (phy_feature.o /
* phy_api.o). Not present in stable IDF 5.5 — build with a 6.x-series IDF.
*/
void phy_11p_set(int enable, int arg0);
void phy_change_channel(int freq_mhz, int arg0, int arg1, int arg2);
#ifdef __cplusplus
}
#endif
+272
View File
@@ -0,0 +1,272 @@
#include <string.h>
#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "st7735.h"
static const char *TAG = "st7735";
#define LCD_W 80
#define LCD_H 160
/* T-Dongle C5: LCD shares SPI data lines with the SD card (MOSI=2,MISO=7,SCK=6)
* but has its own CS. RS/D-C, backlight, reset are GPIO-only. */
#define LCD_PIN_CS 10
#define LCD_PIN_RS 3 /* D/C: 0=command, 1=data */
#define LCD_PIN_BL 0 /* backlight (active high) */
#define LCD_HOST SPI2_HOST
#define LCD_FG_DEF 0x07E0 /* green */
#define LCD_BG_DEF 0x0000 /* black */
static spi_device_handle_t s_dev = NULL;
static uint16_t s_fg = LCD_FG_DEF;
static uint16_t s_bg = LCD_BG_DEF;
static uint16_t s_buf[1024];
static void rs(bool data) { gpio_set_level(LCD_PIN_RS, data ? 1 : 0); }
/* Transmit n bytes (data phase). RS already set by caller. */
static esp_err_t tx_bytes(const uint8_t *d, int n)
{
if (n <= 0) return ESP_OK;
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.flags = 0;
t.length = (uint32_t)(n * 8);
t.tx_buffer = (void *)d;
return spi_device_polling_transmit(s_dev, &t);
}
static void cmd(uint8_t c) { rs(false); tx_bytes(&c, 1); }
static void dat(uint8_t v) { rs(true); tx_bytes(&v, 1); }
/* Render one 5x7 glyph (5 columns x 7 rows, LSB=top) at (x, y). */
static void draw_glyph(int x, int y, const uint8_t g[5])
{
if (x >= LCD_W || y >= LCD_H) return;
for (int r = 0; r < 7; r++) {
for (int c = 0; c < 5; c++) {
int px = x + c, py = y + r;
if (px < 0 || py < 0 || px >= LCD_W || py >= LCD_H) continue;
uint16_t col = (g[c] >> r) & 1 ? s_fg : s_bg;
st7735_fill_rect(px, py, 1, 1, col);
}
}
}
/* 5x7 font: indexed by (c - 0x20); each glyph = 5 columns, LSB=top. */
static const uint8_t FONT[][5] = {
/* 0x20 space */ {0x00,0x00,0x00,0x00,0x00},
/* 0x21 '!' */ {0x00,0x00,0x5F,0x00,0x00},
/* 0x22 '"' */ {0x00,0x07,0x00,0x07,0x00},
/* 0x23 '#' */ {0x14,0x7F,0x14,0x7F,0x14},
/* 0x24 '$' */ {0x24,0x2A,0x7F,0x2A,0x12},
/* 0x25 '%' */ {0x23,0x13,0x08,0x64,0x62},
/* 0x26 '&' */ {0x36,0x49,0x55,0x22,0x50},
/* 0x27 ''' */ {0x00,0x05,0x03,0x00,0x00},
/* 0x28 '(' */ {0x00,0x1C,0x22,0x41,0x00},
/* 0x29 ')' */ {0x00,0x41,0x22,0x1C,0x00},
/* 0x2A '*' */ {0x14,0x08,0x3E,0x08,0x14},
/* 0x2B '+' */ {0x08,0x08,0x3E,0x08,0x08},
/* 0x2C ',' */ {0x00,0x50,0x30,0x00,0x00},
/* 0x2D '-' */ {0x08,0x08,0x08,0x08,0x08},
/* 0x2E '.' */ {0x00,0x60,0x60,0x00,0x00},
/* 0x2F '/' */ {0x20,0x10,0x08,0x04,0x02},
/* 0x30 '0' */ {0x3E,0x51,0x49,0x45,0x3E},
/* 0x31 '1' */ {0x00,0x42,0x7F,0x40,0x00},
/* 0x32 '2' */ {0x42,0x61,0x51,0x49,0x46},
/* 0x33 '3' */ {0x21,0x41,0x45,0x4B,0x31},
/* 0x34 '4' */ {0x18,0x14,0x12,0x7F,0x10},
/* 0x35 '5' */ {0x27,0x45,0x45,0x45,0x39},
/* 0x36 '6' */ {0x3C,0x4A,0x49,0x49,0x30},
/* 0x37 '7' */ {0x01,0x71,0x09,0x05,0x03},
/* 0x38 '8' */ {0x36,0x49,0x49,0x49,0x36},
/* 0x39 '9' */ {0x06,0x49,0x49,0x29,0x1E},
/* 0x3A ':' */ {0x00,0x36,0x36,0x00,0x00},
/* 0x3B ';' */ {0x00,0x56,0x36,0x00,0x00},
/* 0x3C '<' */ {0x00,0x08,0x14,0x22,0x41},
/* 0x3D '=' */ {0x14,0x14,0x14,0x14,0x14},
/* 0x3E '>' */ {0x41,0x22,0x14,0x08,0x00},
/* 0x3F '?' */ {0x02,0x01,0x51,0x09,0x06},
/* 0x40 '@' */ {0x32,0x49,0x79,0x41,0x3E},
/* 0x41 'A' */ {0x7C,0x12,0x11,0x12,0x7C},
/* 0x42 'B' */ {0x7F,0x49,0x49,0x49,0x36},
/* 0x43 'C' */ {0x3E,0x41,0x41,0x41,0x22},
/* 0x44 'D' */ {0x7F,0x41,0x41,0x22,0x1C},
/* 0x45 'E' */ {0x7F,0x49,0x49,0x49,0x41},
/* 0x46 'F' */ {0x7F,0x09,0x09,0x09,0x01},
/* 0x47 'G' */ {0x3E,0x41,0x41,0x51,0x32},
/* 0x48 'H' */ {0x7F,0x08,0x08,0x08,0x7F},
/* 0x49 'I' */ {0x00,0x41,0x7F,0x41,0x00},
/* 0x4A 'J' */ {0x20,0x40,0x41,0x3F,0x01},
/* 0x4B 'K' */ {0x7F,0x08,0x14,0x22,0x41},
/* 0x4C 'L' */ {0x7F,0x40,0x40,0x40,0x40},
/* 0x4D 'M' */ {0x7F,0x02,0x04,0x02,0x7F},
/* 0x4E 'N' */ {0x7F,0x04,0x08,0x10,0x7F},
/* 0x4F 'O' */ {0x3E,0x41,0x41,0x41,0x3E},
/* 0x50 'P' */ {0x7F,0x09,0x09,0x09,0x06},
/* 0x51 'Q' */ {0x3E,0x41,0x51,0x21,0x5E},
/* 0x52 'R' */ {0x7F,0x09,0x19,0x29,0x46},
/* 0x53 'S' */ {0x46,0x49,0x49,0x49,0x31},
/* 0x54 'T' */ {0x01,0x01,0x7F,0x01,0x01},
/* 0x55 'U' */ {0x3F,0x40,0x40,0x40,0x3F},
/* 0x56 'V' */ {0x1F,0x20,0x40,0x20,0x1F},
/* 0x57 'W' */ {0x7F,0x20,0x18,0x20,0x7F},
/* 0x58 'X' */ {0x63,0x14,0x08,0x14,0x63},
/* 0x59 'Y' */ {0x03,0x04,0x78,0x04,0x03},
/* 0x5A 'Z' */ {0x61,0x51,0x49,0x45,0x43},
/* 0x5B '[' */ {0x00,0x00,0x7F,0x41,0x41},
/* 0x5C '\' */ {0x02,0x04,0x08,0x10,0x20},
/* 0x5D ']' */ {0x41,0x41,0x7F,0x00,0x00},
/* 0x5E '^' */ {0x04,0x02,0x01,0x02,0x04},
/* 0x5F '_' */ {0x40,0x40,0x40,0x40,0x40},
/* 0x60 '`' */ {0x00,0x01,0x02,0x04,0x00},
/* 0x61 'a' */ {0x20,0x54,0x54,0x54,0x78},
/* 0x62 'b' */ {0x7F,0x44,0x44,0x44,0x3C},
/* 0x63 'c' */ {0x38,0x44,0x44,0x44,0x28},
/* 0x64 'd' */ {0x7C,0x54,0x54,0x54,0x18},
/* 0x65 'e' */ {0x38,0x54,0x54,0x54,0x18},
/* 0x66 'f' */ {0x08,0x7E,0x09,0x01,0x02},
/* 0x67 'g' */ {0x0C,0x54,0x54,0x54,0x38},
/* 0x68 'h' */ {0x7F,0x08,0x04,0x04,0x78},
/* 0x69 'i' */ {0x00,0x44,0x7D,0x40,0x00},
/* 0x6A 'j' */ {0x20,0x40,0x44,0x3D,0x00},
/* 0x6B 'k' */ {0x7F,0x10,0x28,0x44,0x00},
/* 0x6C 'l' */ {0x00,0x41,0x7F,0x40,0x00},
/* 0x6D 'm' */ {0x7C,0x04,0x18,0x04,0x78},
/* 0x6E 'n' */ {0x7C,0x08,0x04,0x04,0x78},
/* 0x6F 'o' */ {0x38,0x44,0x44,0x44,0x38},
/* 0x70 'p' */ {0x7C,0x14,0x14,0x14,0x08},
/* 0x71 'q' */ {0x08,0x14,0x14,0x14,0x7C},
/* 0x72 'r' */ {0x7C,0x08,0x04,0x04,0x08},
/* 0x73 's' */ {0x48,0x54,0x54,0x54,0x20},
/* 0x74 't' */ {0x04,0x3F,0x44,0x40,0x20},
/* 0x75 'u' */ {0x3C,0x40,0x40,0x20,0x7C},
/* 0x76 'v' */ {0x1C,0x20,0x40,0x20,0x1C},
/* 0x77 'w' */ {0x3C,0x40,0x30,0x40,0x3C},
/* 0x78 'x' */ {0x44,0x28,0x10,0x28,0x44},
/* 0x79 'y' */ {0x0C,0x50,0x50,0x50,0x3C},
/* 0x7A 'z' */ {0x44,0x64,0x54,0x4C,0x44},
/* 0x7B '{' */ {0x00,0x08,0x36,0x41,0x00},
/* 0x7C '|' */ {0x00,0x00,0x7F,0x00,0x00},
/* 0x7D '}' */ {0x00,0x41,0x36,0x08,0x00},
/* 0x7E '~' */ {0x08,0x04,0x08,0x10,0x08},
};
#define FONT_N (sizeof(FONT) / sizeof(FONT[0]))
uint16_t st7735_rgb565(int r, int g, int b)
{
r >>= 3; g >>= 2; b >>= 3;
return (uint16_t)((r << 11) | (g << 5) | b);
}
static void write_area_pixels(int x0, int y0, int x1, int y1, const uint16_t *px)
{
/* px is (x1-x0+1)*(y1-y0+1) 16-bit pixels, row-major from (x0,y0). */
cmd(0x2A); dat((x0 >> 8) & 0xFF); dat(x0 & 0xFF); dat((x1 >> 8) & 0xFF); dat(x1 & 0xFF);
cmd(0x2B); dat((y0 >> 8) & 0xFF); dat(y0 & 0xFF); dat((y1 >> 8) & 0xFF); dat(y1 & 0xFF);
cmd(0x2C);
rs(true);
int n = (x1 - x0 + 1) * (y1 - y0 + 1);
/* chunk to respect the shared-bus max_transfer_sz */
int off = 0;
while (off < n) {
int chunk = n - off;
if (chunk > 512) chunk = 512;
tx_bytes((const uint8_t *)(px + off), chunk * 2);
off += chunk;
}
}
void st7735_fill_rect(int x, int y, int w, int h, uint16_t color)
{
if (x < 0) { w += x; x = 0; }
if (y < 0) { h += y; y = 0; }
if (x >= LCD_W || y >= LCD_H || w <= 0 || h <= 0) return;
if (x + w > LCD_W) w = LCD_W - x;
if (y + h > LCD_H) h = LCD_H - y;
int n = w * h;
if (n > (int)sizeof(s_buf) / 2) n = (int)sizeof(s_buf) / 2;
for (int i = 0; i < n; i++) s_buf[i] = color;
write_area_pixels(x, y, x + w - 1, y + h - 1, s_buf);
}
void st7735_clear(uint16_t color)
{
st7735_fill_rect(0, 0, LCD_W, LCD_H, color);
}
void st7735_set_fg(uint16_t c) { s_fg = c; }
void st7735_set_bg(uint16_t c) { s_bg = c; }
void st7735_text(int x, int y, const char *s)
{
for (; *s; s++) {
uint8_t c = (uint8_t)*s;
if (c >= 0x20 && (c - 0x20) < FONT_N) {
draw_glyph(x, y, FONT[c - 0x20]);
x += 6;
} else if (c == '\n') {
x = 0; y += 8;
} else {
x += 6;
}
}
}
bool st7735_ready(void) { return s_dev != NULL; }
esp_err_t st7735_init(void)
{
/* GPIO: RS + backlight (CS is SPI-managed via spics_io_num). */
gpio_set_direction(LCD_PIN_RS, GPIO_MODE_OUTPUT);
gpio_set_level(LCD_PIN_RS, 0);
gpio_set_direction(LCD_PIN_BL, GPIO_MODE_OUTPUT);
gpio_set_level(LCD_PIN_BL, 1); /* backlight on */
/* Ensure the shared SPI2_HOST bus is initialized (cits_store may have done
* it already; tolerate ESP_ERR_INVALID_STATE). */
spi_bus_config_t bus = {
.mosi_io_num = 2,
.miso_io_num = 7,
.sclk_io_num = 6,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4096,
};
esp_err_t e = spi_bus_initialize(LCD_HOST, &bus, SPI_DMA_CH_AUTO);
if (e != ESP_OK && e != ESP_ERR_INVALID_STATE) {
ESP_LOGE(TAG, "spi_bus_initialize: %s", esp_err_to_name(e));
return e;
}
spi_device_interface_config_t cfg = {
.clock_speed_hz = 24 * 1000 * 1000,
.mode = 0,
.spics_io_num = LCD_PIN_CS, /* SPI-managed CS (GPIO10) */
.flags = SPI_DEVICE_NO_DUMMY,
.command_bits = 0,
.address_bits = 0,
};
e = spi_bus_add_device(LCD_HOST, &cfg, &s_dev);
if (e != ESP_OK) {
ESP_LOGE(TAG, "spi_device_add_transmit: %s", esp_err_to_name(e));
return e;
}
vTaskDelay(pdMS_TO_TICKS(120)); /* power-on */
cmd(0x12); vTaskDelay(pdMS_TO_TICKS(50)); /* SWRESET */
cmd(0x11); vTaskDelay(pdMS_TO_TICKS(120)); /* SLPOUT */
cmd(0x36); dat(0x08); /* MADCTL */
cmd(0x3A); dat(0x05); /* COLMOD 16-bit */
cmd(0x2A); dat(0x00); dat(0x00); dat(0x00); dat(0x4F); /* CASET 0-79 */
cmd(0x2B); dat(0x00); dat(0x00); dat(0x00); dat(0x9F); /* RASET 0-159 */
cmd(0x2C); /* RAMWR */
cmd(0x13); /* normal display on */
cmd(0x29); /* DISPON */
st7735_clear(LCD_BG_DEF);
ESP_LOGI(TAG, "ST7735 80x160 ready");
return ESP_OK;
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h"
/* ST7735 0.96" 80x160 SPI display (T-Dongle C5).
* Shares the SPI2_HOST bus with the microSD card (different CS).
* Manual RS (D/C) pin; SPI-managed CS. */
esp_err_t st7735_init(void);
bool st7735_ready(void);
void st7735_clear(uint16_t color);
void st7735_fill_rect(int x, int y, int w, int h, uint16_t color);
void st7735_set_fg(uint16_t color);
void st7735_set_bg(uint16_t color);
/* Render a C string at (x, y) using a 5x7 font (1x scale, 6px advance). */
void st7735_text(int x, int y, const char *s);
/* RGB565 helpers */
uint16_t st7735_rgb565(int r, int g, int b);