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:
1 parent
b852826f5c
commit
d1b3d1bae6
24 files changed
+1552
-698
No files matched your search
@@ -0,0 +1,8 @@
|
||||
build/
|
||||
sdkconfig
|
||||
sdkconfig.old
|
||||
*.py
|
||||
__pycache__/
|
||||
*.bin
|
||||
*.elf
|
||||
*.map
|
||||
@@ -0,0 +1,3 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(cits_sniffer)
|
||||
@@ -1,45 +1,282 @@
|
||||
# C-ITS Sniffer für ESP32-C5
|
||||
# C-ITS Sniffer — ESP32-C5 (Lilygo T-Dongle C5)
|
||||
|
||||
Ein Sniffer zur Erfassung von C-ITS (Cooperative Intelligent Transportation Systems) Daten von Fahrzeugen und Verkehrssignalen.
|
||||
Erfasst **Cooperative Intelligent Transportation System (C-ITS)**-Frames auf
|
||||
**5.9 GHz (802.11p / ITS-G5)** und schreibt sie als **CSV auf eine SD-Karte**
|
||||
(Flash-`storage`-Partition als Fallback) plus optionales Live-Serial-Logging.
|
||||
Ein **ST7735-Debug-Display** zeigt in Echtzeit: C-ITS-Zähler (5‑Min-Fenster),
|
||||
Frames, Drops, Speicherquelle, freien SD-Speicher und den aktiven Kanal.
|
||||
|
||||
## Ziel
|
||||
> Projekt: **wisp** — Empfänger-Firmware für das 5.9 GHz / ITS-G5 Spektrum.
|
||||
> MCU: **ESP32-C5** (RISC-V, 240 MHz, 16 MB Flash, **Single-Core**).
|
||||
> Board: **Lilygo T-Dongle C5**.
|
||||
|
||||
- Empfängt C-ITS Nachrichten (IEEE 1609 WAVE/DSRC) auf 5,9 GHz
|
||||
- Parst die empfangenen DATENSATZ
|
||||
- Speichert die Daten in CSV-Dateien
|
||||
- Neue CSV-Datei alle 10 Minuten
|
||||
---
|
||||
|
||||
## Hardware
|
||||
|
||||
- ESP32-C5 (LilyGO T-Dongle-C5)
|
||||
- Background GHz WLAN-Modul für 802.11p Kommunikation
|
||||
| Komponente | Detail |
|
||||
|---|---|
|
||||
| MCU | ESP32-C5 (RISC-V, Single-Core, 16 MB W25Q128 Flash) |
|
||||
| Board | Lilygo T-Dongle C5 |
|
||||
| Wi-Fi | 5.9 GHz / 802.11p (C-ITS) — native ESP32-C5 5 GHz-Radio |
|
||||
| SD-Karte | SPI 1‑bit, **32 GB** (primärer Speicher) |
|
||||
| Display | ST7735 0.96″ 80×160 px, SPI (teilt SPI2‑Bus mit SD) |
|
||||
| LED | APA102 RGB (Status) |
|
||||
|
||||
## Block 1: Hardware Initialisierung (ERLEDIGT)
|
||||
### Pinbelegung
|
||||
|
||||
Die OpenTrafficMap Firmware wurde bereits analysiert. Sie enthält alle notwendigen Funktionen:
|
||||
| Funktion | GPIO | Anmerkung |
|
||||
|---|---|---|
|
||||
| **SD MOSI** | **2** | SPI2_HOST, 1‑bit, ≤ 20 MHz |
|
||||
| **SD MISO** | **7** | SPI2_HOST |
|
||||
| **SD CLK** | **6** | SPI2_HOST |
|
||||
| **SD CS** | **23** | SPI2_HOST |
|
||||
| **LCD MOSI** | **2** | SPI2_HOST (gemeinsam mit SD) |
|
||||
| **LCD MISO** | **7** | SPI2_HOST (gemeinsam mit SD) |
|
||||
| **LCD SCK** | **6** | SPI2_HOST (gemeinsam mit SD) |
|
||||
| **LCD CS** | **10** | SPI2_HOST (eigener Chip‑Select) |
|
||||
| **LCD RS/D/C** | **3** | Data/Command |
|
||||
| **LCD BL** | **0** | Backlight |
|
||||
| **LCD RST** | **1** | Reset |
|
||||
| **LED CI** | **4** | APA102 Clock In |
|
||||
| **LED DI** | **5** | APA102 Data In |
|
||||
|
||||
- **WiFi Promiscuous Mode**: `esp_wifi_set_promiscuous(true)`
|
||||
- **802.11p PHY**: `phy_11p_set(ist, 0)`
|
||||
- **Kanalwechsel**: `phy_change_channel(channel, 1, 0, 0)`
|
||||
- **C-ITS Frequenzen**: 5800-5900 MHz (Kanal 1-13)
|
||||
> **Wichtig:** SD‑Karte und Display **teilen denselben SPI2‑Bus** (MOSI/MISO/CLK
|
||||
> auf den gleichen GPIOs) und werden über **zwei getrennte Chip‑Select‑Pins**
|
||||
> (SD CS=23, LCD CS=10) arbitriert. Der ESP32‑SPI‑Host muxet automatisch über CS.
|
||||
|
||||
### Verwendete Bibliotheken
|
||||
- `esp_wifi.h` - WiFi Treiber
|
||||
- `freertos/` - Task Management
|
||||
- `nvs_flash.h` - Non-Volatile Storage
|
||||
### SD‑Karte
|
||||
|
||||
### Nächste Schritte
|
||||
1. Block 2: C-ITS DATENSATZ parsen (BSI-Header extrahieren)
|
||||
2. Block 3: CSV-Datei Erstellung mit 10-Minuten-Intervall
|
||||
3. Block 4: Firmware auf ESP32-C5 flashen
|
||||
- **32 GB**, SPI 1‑bit.
|
||||
- Formatierung: **FAT32** (32 GB → FAT32, nicht exFAT).
|
||||
- Bei Fehlschlag fällt die Firmware auf die **Flash‑`storage`‑Partition**
|
||||
zurück und loggt per Serial (kein CSV-Datei auf SD).
|
||||
|
||||
## Verwendete Dateien
|
||||
- `its-g5-receiver-firmware/main/` - Basierend auf OpenTrafficMap
|
||||
- `cits-sniffer/block1_hardware_init.c` - Erweiterung für C-ITS spezifisch
|
||||
---
|
||||
|
||||
## Build
|
||||
|
||||
Voraussetzung: **ESP-IDF v6.1-dev**. Für den ESP32-C5 wird die **RISC-V**
|
||||
Toolchain genutzt.
|
||||
|
||||
```bash
|
||||
# ESP-IDF aktivieren
|
||||
source esp-idf/export.sh > /dev/null 2>&1
|
||||
|
||||
# Target setzen (nur einmal)
|
||||
idf.py set-target esp32c5
|
||||
|
||||
# Build
|
||||
idf.py build
|
||||
```
|
||||
|
||||
Der Build erzeugt `build/cits_sniffer.bin` (≈ 920 KB).
|
||||
|
||||
> **Hinweis:** Der ESP32-C5 ist **Single-Core** — der Code nutzt `xTaskCreate`
|
||||
> (nicht `xTaskCreatePinnedToCore(..., 1)`), da Core 1 nicht existiert.
|
||||
|
||||
---
|
||||
|
||||
## Flash
|
||||
|
||||
```bash
|
||||
# Gerät in /dev/ttyACM0 (Espressif USB-JTAG/Serial)
|
||||
idf.py -p /dev/ttyACM0 flash monitor
|
||||
```
|
||||
|
||||
Oder mit `esptool` direkt:
|
||||
|
||||
```bash
|
||||
python -m esptool --chip esp32c5 -p /dev/ttyACM0 -b 460800 \
|
||||
write_flash 0x2000 build/bootloader/bootloader.bin \
|
||||
0x8000 build/partition_table/partitions_singleapp.bin \
|
||||
0x10000 build/cits_sniffer.bin
|
||||
```
|
||||
|
||||
### Download-Mode
|
||||
|
||||
Der T-Dongle C5 hat **keine funktionierende DTR/RTS-Auto-Reset** (USB-JTAG CDC
|
||||
ignoriert `TIOCMSET`). Zum Flashen muss das Gerät in den **Download-Mode**
|
||||
(ROM-Bootloader) gebracht werden:
|
||||
|
||||
1. **BOOT-Button** gedrückt halten.
|
||||
2. **RESET-Button** kurz drücken (oder USB neu anstecken).
|
||||
3. BOOT loslassen → Gerät ist im ROM-Bootloader.
|
||||
4. `idf.py flash` ausführen.
|
||||
|
||||
> **Remote:** Ohne physischen Zugang zum BOOT-Button lässt sich der Download-Mode
|
||||
> nicht erzwingen. Ein USB-Deauth/Reauth power-cyclet das Gerät, bootet aber in
|
||||
> den **Run-Mode** (BOOT-Pin default HIGH).
|
||||
|
||||
---
|
||||
|
||||
## Serial-Konsole
|
||||
|
||||
Die Firmware stellt eine **REPL-Konsole** (USB-JTAG-Serial, 115200 baud) mit
|
||||
den folgenden Kommandos bereit:
|
||||
|
||||
| Kommando | Beschreibung |
|
||||
|---|---|
|
||||
| `stats` | RX-Statistik (Frames, C-ITS, Drops) + Speicherstatus (Quelle, Count, Pfad) |
|
||||
| `seron` | Live-Serial-Logging **einschalten** (jeder Frame → Serial, zusätzlich zu CSV) |
|
||||
| `seroff` | Live-Serial-Logging **ausschalten** (CSV-Logging läuft weiter) |
|
||||
| `reboot` | Gerät neu starten |
|
||||
|
||||
Beispiel:
|
||||
|
||||
```
|
||||
cits> stats
|
||||
rx=1234 cits=87 drops=12
|
||||
storage=sd ready=1 count=87 path=/cits.csv
|
||||
cits>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Datenformat (cits.csv)
|
||||
|
||||
Die CSV-Datei auf der SD-Karte (`/cits.csv`) hat die Spalten:
|
||||
|
||||
```
|
||||
timestamp_ms,frame_len,src_mac,dst_mac,channel,signal_dbm,etsi_msg_id,its_id,position_lat,position_lon,speed_kmh
|
||||
```
|
||||
|
||||
| Spalte | Typ | Beschreibung |
|
||||
|---|---|---|
|
||||
| `timestamp_ms` | uint32 | Zeitstempel in ms seit Boot |
|
||||
| `frame_len` | uint16 | Frame-Länge (Bytes) |
|
||||
| `src_mac` | string | MAC-Adresse des Senders |
|
||||
| `dst_mac` | string | MAC-Adresse des Empfängers |
|
||||
| `channel` | uint8 | 802.11p-Kanal (C-ITS = 172–184) |
|
||||
| `signal_dbm` | int8 | Signalstärke in dBm (negativ) |
|
||||
| `etsi_msg_id` | uint8 | ETSI EN 302 637 Message-ID (0x10 = CAM, 0x20 = DENM) |
|
||||
| `its_id` | uint32 | ITS-Node-ID |
|
||||
| `position_lat` | float | Breitengrad (WGS84) |
|
||||
| `position_lon` | float | Längengrad (WGS84) |
|
||||
| `speed_kmh` | uint16 | Geschwindigkeit in km/h |
|
||||
|
||||
> **Hinweis:** Nicht alle Felder sind in jedem Frame vorhanden. Fehlende Werte
|
||||
> werden als leer gelassen. Die ETSI Message-ID ist der primäre Indikator für
|
||||
> C-ITS (CAM/DENM).
|
||||
|
||||
---
|
||||
|
||||
## Debug-Display (ST7735)
|
||||
|
||||
Das 80×160 px-Display zeigt ein **Echtzeit-Panel** (Aktualisierung ca. 2 s):
|
||||
|
||||
```
|
||||
+----------------------------------+
|
||||
| 5MIN: 42 | C-ITS-Messages in den letzten 5 Minuten
|
||||
| FRM: 1234 | Gesamte empfangene Frames
|
||||
| DROP: 12 | Dropped Frames (Buffer overflow)
|
||||
| SRC: SD | Speicherquelle (SD / FLASH / NONE)
|
||||
| FREE: 67% | Freier SD-Speicher
|
||||
| CH: 172 | Aktiver 802.11p-Kanal
|
||||
+----------------------------------+
|
||||
```
|
||||
|
||||
- **5MIN**: C-ITS-Messages im 5‑Minuten-Sliding-Window.
|
||||
- **FRM**: Gesamtzahl der empfangenen 802.11p-Frames.
|
||||
- **DROP**: Frames, die wegen Buffer-Overflow verworfen wurden.
|
||||
- **SRC**: `SD` (primär), `FLASH` (Fallback), `NONE` (kein Speicher).
|
||||
- **FREE**: Freier SD-Speicher in % (nur wenn `SRC=SD`).
|
||||
- **CH**: Der aktuell überwachte 802.11p-Kanal.
|
||||
|
||||
> Das Display teilt den SPI2-Bus mit der SD-Karte. Wenn die SD-Karte aktiv
|
||||
> gelesen/schrieben wird, kann das Display kurzzeitig frozen (CS-Arbitration).
|
||||
> Das ist normal und kein Fehler.
|
||||
|
||||
---
|
||||
|
||||
## Architektur
|
||||
|
||||
```
|
||||
cits-sniffer/
|
||||
├── CMakeLists.txt # IDF-Projekt-Definition
|
||||
├── sdkconfig.defaults # Basiskonfiguration (Console=USB-JTAG, 5 GHz, etc.)
|
||||
├── partitions.csv # Partitionstabelle (bootloader, app, storage)
|
||||
├── main/
|
||||
│ ├── CMakeLists.txt # Main-Component (alle .c-Dateien)
|
||||
│ ├── main.c # app_main, display_task, REPL
|
||||
│ ├── cits_rx.c # 802.11p RX-Handler, 5‑Min-Sliding-Window
|
||||
│ ├── cits_rx.h
|
||||
│ ├── cits_parse.c # C-ITS (ETSI EN 302 637) Parser
|
||||
│ ├── cits_parse.h
|
||||
│ ├── cits_store.c # SD/Flash-Storage (SPI2_HOST, FAT32)
|
||||
│ ├── cits_store.h
|
||||
│ ├── st7735.c # ST7735 SPI-Treiber (5×7-Font, Panel-Render)
|
||||
│ ├── st7735.h
|
||||
│ ├── led.c # APA102 RGB-LED (Status)
|
||||
│ ├── led.h
|
||||
│ └── Kconfig.projbuild # Kconfig-Optionen
|
||||
```
|
||||
|
||||
### Module
|
||||
|
||||
| Modul | Verantwortung |
|
||||
|---|---|
|
||||
| `cits_rx` | 802.11p-Frames empfangen, Sliding-Window (5 min), Drops zähle |
|
||||
| `cits_parse` | ETSI EN 302 637 C-ITS-Frames parsen (CAM, DENM) |
|
||||
| `cits_store` | CSV auf SD-Karte schreiben, Flash-Fallback, Puffer-Management |
|
||||
| `st7735` | ST7735-LCD-Treiber, Panel-Render (5×7-Font) |
|
||||
| `led` | APA102 RGB-Status-LED (blau=boot, grün=armed, rot=fehler) |
|
||||
| `main` | `app_main`, Init-Sequence, Display-Task, REPL |
|
||||
|
||||
---
|
||||
|
||||
## Status-LED
|
||||
|
||||
| Farbe | Bedeutung |
|
||||
|---|---|
|
||||
| 🔵 Blau | Booting (Init, noch nicht armed) |
|
||||
| 🟢 Grün | **Armed** — empfängt C-ITS, schreibt auf SD/Flash |
|
||||
| 🔴 Rot | Fehler (kein Speicher oder `cits_rx_start` fehlgeschlagen) |
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### `stats` liefert keine Antwort
|
||||
- Gerät nicht im Run-Mode (Download-Mode oder Crash). → Display + LED prüfen.
|
||||
- USB-JTAG CDC buffer-t das Boot-Log weg (bekanntes T-Dongle C5-Quirk). →
|
||||
Display + LED als Primär-Verifikation nutzen.
|
||||
- `stty 115200 raw` setzen, dann `stats\r\n` senden.
|
||||
|
||||
### SD-Karte wird nicht erkannt
|
||||
- SD-Karte ist **FAT32** formatiert (nicht exFAT)?
|
||||
- SD-Karte ist **32 GB** (oder kleiner)?
|
||||
- SPI-Pins korrekt (MOSI=2, MISO=7, CLK=6, CS=23)?
|
||||
- Display zeigt `SRC: NONE` → kein Speicher, Serial-only Logging.
|
||||
|
||||
### Display bleibt schwarz
|
||||
- LCD-Pins korrekt (MOSI=2, MISO=7, SCK=6, CS=10, RS=3, BL=0, RST=1)?
|
||||
- Backlight (BL=0) an?
|
||||
- RST-Pin (1) nicht dauerhaft LOW?
|
||||
- `st7735_init` failed → `display init failed (continuing)` im Log.
|
||||
|
||||
### `cits_rx_start` failed
|
||||
- 5 GHz / 802.11p nicht verfügbar? → ESP32-C5 muss 5 GHz-fähig sein.
|
||||
- NVS-Flash korrupt? → `nvs_flash_erase()` + Reboot.
|
||||
- Wi-Fi-Stack hängt? → Power-Cycle.
|
||||
|
||||
### Build-Fehler
|
||||
- **`xTaskCreatePinnedToCore(..., 1)` CRASH**: ESP32-C5 ist Single-Core →
|
||||
`xTaskCreate` verwenden.
|
||||
- **`SPI1_HOST`**: Flash-reserviert → `SPI2_HOST` verwenden.
|
||||
- **`spi_device_add_transmit`**: existiert nicht in IDF v6.1 →
|
||||
`spi_bus_add_device`.
|
||||
- **`<sys/statfs.h>`**: existiert nicht → `<sys/statvfs.h>`.
|
||||
|
||||
---
|
||||
|
||||
## Referenzen
|
||||
|
||||
- https://codeberg.org/opentrafficmap/its-g5-receiver-firmware
|
||||
- IEEE 1609.3 (WAVE)
|
||||
yb
|
||||
- EN 602 571 (C-ITS)
|
||||
- [OpenTrafficMap its-g5-receiver-firmware](https://codeberg.org/opentrafficmap/its-g5-receiver-firmware)
|
||||
- [IEEE 1609.3 (WAVE)](https://standards.ieee.org/ieee/1609.3/)
|
||||
- [ETSI EN 302 637 (C-ITS)](https://www.etsi.org/technologies/intelligent-transportation)
|
||||
|
||||
## Repo
|
||||
|
||||
- Gitea: <https://gitea.click.jetzt/wisp/cits-sniffer>
|
||||
- Org: `wisp`
|
||||
@@ -1,190 +0,0 @@
|
||||
/**
|
||||
* Block 1: Hardware Initialisierung für ESP32-C5 C-ITS Sniffer
|
||||
*
|
||||
* Ziel: WiFi Promiscuous Mode auf 802.11p konfigurieren
|
||||
*
|
||||
* C-ITS-Kanäle (5,9 GHz DSRC):
|
||||
* - Kanal 0: 5900 MHz (Primary)
|
||||
* - Kanal 1: 5890 MHz
|
||||
* - Kanal 2: 5880 MHz
|
||||
* - Kanal 3: 5870 MHz
|
||||
* - Kanal 4: 5860 MHz
|
||||
*
|
||||
* Quelle: ETSI EN 302 571
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
sexual <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_timer.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
// Externe Funktionen aus der OpenTrafficMap Firmware
|
||||
extern void phy_11p_set(int enable, int unknown);
|
||||
extern void phy_change_channel(int channel, int arg1, int arg2, int arg3);
|
||||
|
||||
static const char *TAG = "CITS_BLOCK1";
|
||||
|
||||
// C-Background Konfiguration
|
||||
#define CITS_CHANNEL_0_MHZ 5900 // Primary C-ITS channel
|
||||
#define CITS_CHANNEL_1_MHZ 5890 // Alternate channel
|
||||
#define CITS_CHANNEL_2_MHZ 5880 // Alternate channel
|
||||
#define CITS_CHANNEL_SHUTDOWN_Mal 5870 // Shutdown channel
|
||||
|
||||
/**
|
||||
* WiFi Promiscuous Callback
|
||||
*/
|
||||
static void wifi_promiscuous_cb(void *recv_buf, wifi_promiscuous_pkt_type_t type)
|
||||
{
|
||||
wifi_promiscuous_pkt_t *packet = (wifi_promiscuous_pkt_t *)recv_buf;
|
||||
|
||||
if (type != WIFI_PKT_MISC) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet->rx_ctrl.rx_state) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint64_t timestamp_us = packet->rx_ctrl.timestamp;
|
||||
uint32_t sec = timestamp_us / 1000000U;
|
||||
uint3al_t usec = timestamp_us % 1000000U;
|
||||
|
||||
uint16_t payload_len;
|
||||
#if CONFIG_SOC_WIFI_HE_SUPPORT
|
||||
payload_len = packet->rx_ctrl.dump_len;
|
||||
#else
|
||||
payload_len = packet->rx_ctrl.sig_len - 4;
|
||||
#endif
|
||||
|
||||
ESP_LOGD(TAG, "Received packet: %d bytes at %u.%06u", payload_len, sec, usec);
|
||||
}
|
||||
|
||||
/**
|
||||
* WiFi Initialisierung
|
||||
*/
|
||||
static esp_err_t initialize_wifi(void)
|
||||
{
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
|
||||
|
||||
ESP_LOGI(TAG, "WiFi initialisiert");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
ist
|
||||
* 802.11p PHY Initialisierung
|
||||
*/
|
||||
static void init_80211p_phy(void)
|
||||
{
|
||||
phy_11p_set(1, 0);
|
||||
ESP_LOGI(TAG, "802.11p PHY aktiviert");
|
||||
}
|
||||
|
||||
/**
|
||||
* C-ITS Kanal wechseln
|
||||
*/
|
||||
static void set_cits_channel(uint32_t freq_mhz)
|
||||
{
|
||||
phy_change_channel(freq_mhz, 1, 0, 0);
|
||||
ESP_LOGI(TAG, "Wechsle zu Kanal %u MHz", freq_mhz);
|
||||
}
|
||||
|
||||
/**
|
||||
* WiFi Promiscuous Mode starten
|
||||
*/
|
||||
static esp_err_t start_promiscuous_mode(uint3al_t channel)
|
||||
{
|
||||
wifi_promiscuous_filter_t filter = {
|
||||
.filter_mask = WIFI_PROMIS_FILTER_MASK_ALL
|
||||
};
|
||||
|
||||
esp_wifi_set_promiscuous_filter(&filter);
|
||||
esp_wifi_set_promiscuous_rx_cb(wifi_promiscuous_cb);
|
||||
ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true));
|
||||
|
||||
init_80211p_phy();
|
||||
set_cits_channel(channel);
|
||||
|
||||
ESP_LOGI(TAG, "Promiscuous Mode gestartet auf %u MHz", channel);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* NVS Flash Initialisierung
|
||||
*/
|
||||
static esp_err_t init_nvs_flash(void)
|
||||
{
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
artig
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
err = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(err);
|
||||
ESP_LOGI(TAG, "NVS Flash initialisiert");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hauptprogramm
|
||||
*/
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(init_nvs_flash());
|
||||
ESP_ERROR_CHECK(initialize_wifi());
|
||||
ESP_ERROR_CHECK(start_pist);
|
||||
|
||||
ESP_LOGI(TAG, "C-ITS Sniffer Block 1 initialisiert");
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test-Hilfe für manuelle Kanaländerung
|
||||
*/
|
||||
static int cmd_set_channel(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
printf("Nutzerung: set_channel <freq_mhz>\n");
|
||||
al
|
||||
return al
|
||||
}
|
||||
|
||||
uint32_t freq = atoi(argv[1]);
|
||||
if (freq < 5800 || freq > 5900) {
|
||||
printf("Frequenz muss zwischen 5800 und 5900 MHz liegen\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
set_cits_channel(freq);
|
||||
printf("Kanal auf %u MHz gesetzt\n", freq);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_cits_commands(void)
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "set_channel",
|
||||
.help = "Set C-ITS channel (5800-5900 MHz)",
|
||||
.hint = NULL,
|
||||
.func = &cmd_set_channel,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
/**
|
||||
* Block 2: C-ITS Parser für ESP32-C5 C-ITS Sniffer
|
||||
*
|
||||
* Ziel: C-ITS DATENSATZ parsen und extrahieren
|
||||
*
|
||||
* WAVE/DSRC Paket Struktur (IEEE 1609.3):
|
||||
* - MAC Header: 14 Bytes
|
||||
* - LLC/SNAP: 8 Bytes
|
||||
* - WAVE Short Message (WSM): 6 Bytes Header
|
||||
* - C-ITS DATENSATZ (Payload)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "CITS_PARSER";
|
||||
|
||||
// WAVE Short Message Header Struktur
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t pduLength[3]; // PDU Länge (3 Bytes)
|
||||
uint8_t senderId[6]; // Sender MAC Adresse
|
||||
uint32_t timestamp; // Zeitstempel
|
||||
uint8_t priority; // Priorität
|
||||
uint8_t pduType; // PDU Typ
|
||||
} wave_header_t;
|
||||
|
||||
// C-ITS Message Type (BSI Format)
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t msgId; // Message ID
|
||||
uint8_t msgCnt; // Message Count
|
||||
uint8_t sectionNum; // Section Number
|
||||
uint8_t partId; // Part ID
|
||||
uint8_t reserved[8]; // Reserviert
|
||||
uint8_t payload[]; // Payload
|
||||
} cits_bsi_header_t;
|
||||
|
||||
// C-ITS Message Types (ETSI TS 103 097)
|
||||
#define CITS_MSGID_DECEVENT 0x01 // Dezisionsunterstützung
|
||||
#define CITS_MSGID_DSECN 0x02 // Dekstruktive Umgebung
|
||||
#define CITS_MSGID_DMM 0x03 // Dynamisches Map-Matching
|
||||
#define CITS_MSGID_MAP 0x04 // Kartendaten
|
||||
#define CITS_MSGID_MAPDATA 0x05 // Kartendaten
|
||||
#define CITS_MSGID_MAPSELDATA 0x06 // Kartendaten Auswahl
|
||||
#define CITS_MSGID_RSM 0x07 // Road Safety Message
|
||||
#define CITS_MSGID_CAM 0x08 // Cooperative Awareness Message
|
||||
#define CITS_MSGID_DENM 0x09 // Decentralized Environmental Notification
|
||||
#define CITS_MSGID_SPA 0x0A // Signal Phase and Timing
|
||||
#define CITS_MSGID_MAM 0x0B // Map Alignment Message
|
||||
#define CITS_MSGID_VIT 0x0C // Vehicle Information Message
|
||||
#define CITS_MSGID_VSL 0x0D // Variable Speed Limit
|
||||
#define CITS_MSGID_SPA 0x0E // Signal Phase and Timing
|
||||
#define CITS_MSGID_CSP 0x0F // Cross-Signal Phase
|
||||
|
||||
/**
|
||||
* MAC Adresse als String formatieren
|
||||
*/
|
||||
static void format_mac(const uint8_t *mac, char *mac_str, size_t size)
|
||||
{
|
||||
snprintf(mac_str, size, "%02X:%02X:%02X:%02X:%02X:%02X",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
}
|
||||
|
||||
/**
|
||||
* WAVE Header parsen
|
||||
*/
|
||||
static esp_err_t parse_wave_header(const uint8_t *packet, wave_header_t *wave_hdr)
|
||||
{
|
||||
if (packet == NULL || wave_hdr == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
memcpy(wave_hdr->pduLength, packet, 3);
|
||||
memcpy(wave_hdr->senderId, packet + 3, 6);
|
||||
memcpy(&wave_hdr->timestamp, packet + 9, 4);
|
||||
wave_hdr->priority = packet[13];
|
||||
wave_hdr->pduType = packet[14];
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* C-ITS BSI Header parsen
|
||||
*/
|
||||
static esp_err_t parse_cits_bsi(const uint8_t *packet, cits_bsi_header_t *bsi_hdr)
|
||||
{
|
||||
if (packet == NULL || bsi_hdr == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
bsi_hdr->msgId = packet[0];
|
||||
bsi_hdr->msgCnt = packet[1];
|
||||
bsi_hdr->sectionNum = packet[2];
|
||||
bsi_hdr->partId = packet[3];
|
||||
memcpy(bsi_hdr->reserved, packet + 4, 8);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nachrichtstyp identifizieren
|
||||
*/
|
||||
static const char* get_message_type(uint8_t msgId)
|
||||
{
|
||||
switch (msgId) {
|
||||
case CITS_MSGID_DECEVENT: return "DECEVENT";
|
||||
case CITS_MSGID_DSECN: return "DSECN";
|
||||
case CITS_MSGID_DMM: return "DMM";
|
||||
case CITS_MSGID_MAP: return "MAP";
|
||||
case CITS_MSGID_MAPDATA: return "MAPDATA";
|
||||
case CITS_MSGID_MAPSELDATA: return "MAPSELDATA";
|
||||
case CITS_MSGID_RSM: return "RSM";
|
||||
case CITS_MSGID_CAM: return "CAM";
|
||||
case CITS_MSGID_DENM: return "DENM";
|
||||
case CITS_MSGID_SPA: return "SPA";
|
||||
case CITS_MSGID_MAM: return "MAM";
|
||||
case CITS_MSGID_VIT: return "VIT";
|
||||
case CITS_MSGID_VSL: return "VSL";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* C-ITS Nachricht verarbeiten
|
||||
*/
|
||||
static void process_cits_packet(const uint8_t *packet, uint32_t length,
|
||||
uint32_t timestamp_sec, uint32_t timestamp_usec)
|
||||
{
|
||||
if (packet == NULL || length < 20) {
|
||||
return;
|
||||
}
|
||||
|
||||
wave_header_t wave_hdr;
|
||||
esp_err_t ret = parse_wave_header(packet, &wave_hdr);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGD(TAG, "Failed to parse WAVE header");
|
||||
return;
|
||||
}
|
||||
|
||||
char mac_str[18];
|
||||
format_mac(wave_hdr.senderId, mac_str, sizeof(mac_str));
|
||||
|
||||
ESP_LOGI(TAG, "Received C-ITS packet:");
|
||||
ESP_LOGI(TAG, " PDU Length: %lu", (unsigned long)(wave_hdr.pduLength[0] | wave_hdr.pduLength[1] << 8 | wave_hdr.pduLength[2] << 16));
|
||||
ESP_LOGI(TAG, " Sender MAC: %s", mac_str);
|
||||
ESP_LOGI(TAG, " Timestamp: %u.%06u", timestamp_sec, timestamp_usec);
|
||||
ESP_LOGI(TAG, " Priority: %d", wave_hdr.priority);
|
||||
ESP_LOGI(TAG, " PDU Type: %d", wave_hdr.pduType);
|
||||
|
||||
// C-ITS BSI Header parsen
|
||||
cits_bsi_header_t bsi_hdr;
|
||||
ret = parse_cits_bsi(packet + 15, &bsi_hdr);
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(TAG, " Message ID: %d (%s)", bsi_hdr.msgId, get_message_type(bsi_hdr.msgId));
|
||||
ESP_LOGI(TAG, " Message Count: %d", bsi_hdr.msgCnt);
|
||||
ESP_LOGI(TAG, " Section Num: %d", bsi_hdr.sectionNum);
|
||||
ESP_LOGI(TAG, " Part ID: %d", bsi_hdr.partId);
|
||||
}
|
||||
|
||||
// TODO: C-ITS spezifische Payload parsen
|
||||
// Hier können die spezifischen C-ITS Nachrichteninhalt extrahiert werden
|
||||
}
|
||||
|
||||
/**
|
||||
* C-ITS Parser initialisieren
|
||||
*/
|
||||
esp_err_t cits_parser_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "C-ITS Parser initialisiert");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* C-ITS Parser beenden
|
||||
*/
|
||||
void cits_parser_deinit(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "C-ITS Parser beendet");
|
||||
}
|
||||
|
||||
/**
|
||||
* Hauptverarbeitungsfunktion
|
||||
* Diese wird von der sniffer_task aufgerufen
|
||||
*/
|
||||
void cits_process_packet(sniffer_packet_info_t *packet_info)
|
||||
{
|
||||
if (packet_info == NULL || packet_info->payload == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
process_cits_packet(packet_info->payload, packet_info->length,
|
||||
packet_info->seconds, packet_info->microseconds);
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
/**
|
||||
* Block 3: CSV Writer für C-ITS Sniffer
|
||||
*
|
||||
* Erstellt CSV-Dateien mit empfangenen C-ITS Nachrichten
|
||||
* Neue Datei alle 10 Minuten
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "CSV_WRITER";
|
||||
|
||||
// CSV Datei Handle
|
||||
static FILE *csv_file = NULL;
|
||||
static char current_filename[64];
|
||||
static time_t last_write_time = 0;
|
||||
#define CSV_WRITE_INTERVAL_SEC (600) // 10 Minuten
|
||||
|
||||
/**
|
||||
* C-ITS Message Header (BSI Format)
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t timestamp_sec;
|
||||
uint32_t timestamp_usec;
|
||||
uint16_t length;
|
||||
uint8_t sender_id[6]; // MAC Address
|
||||
uint8_t message_type; // BSI Message Type
|
||||
uint8_t channel; // Kanal
|
||||
int16_t rssi; // Signal Stärke
|
||||
} cits_message_t;
|
||||
|
||||
/**
|
||||
* CSV Header schreiben
|
||||
*/
|
||||
static void write_csv_header(FILE *fp)
|
||||
{
|
||||
fprintf(fp, "timestamp_sec,timestamp_usec,length,sender_mac,message_type,channel,rssi\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* CSV Zeile schreiben
|
||||
*/
|
||||
static void write_csv_row(FILE *fp, cits_message_t *msg)
|
||||
{
|
||||
char mac_str[18];
|
||||
snprintf(mac_str, sizeof(mac_str), "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
msg->sender_id[0], msg->sender_id[1], msg->sender_id[2],
|
||||
msg->sender_id[3], msg->sender_id[4], msg->sender_id[5]);
|
||||
|
||||
fprintf(fp, "%u,%u,%u,%s,%u,%u,%d\n",
|
||||
msg->timestamp_sec, msg->timestamp_usec, msg->length,
|
||||
mac_str, msg->message_type, msg->channel, msg->rssi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Neue CSV-Datei erstellen
|
||||
*/
|
||||
static esp_err_t create_new_csv_file(void)
|
||||
{
|
||||
time_t now;
|
||||
struct tm *tm_info;
|
||||
|
||||
time(&now);
|
||||
tm_info = localtime(&now);
|
||||
|
||||
// Dateinamen mit Zeitstempel
|
||||
snprintf(current_filename, sizeof(current_filename),
|
||||
"/data/cits_%04d%02d%02d_%02d%02d%02d.csv",
|
||||
tm_info->tm_year + 1900, tm_info->tm_mon + 1, tm_info->tm_mday,
|
||||
tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
|
||||
|
||||
if (csv_file != NULL) {
|
||||
fclose(csv_file);
|
||||
}
|
||||
|
||||
csv_file = fopen(current_filename, "w");
|
||||
if (csv_file == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to create CSV file: %s", current_filename);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
write_csv_header(csv_file);
|
||||
fflush(csv_file);
|
||||
|
||||
ESP_LOGI(TAG, "Created new CSV file: %s", current_filename);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüfen ob neue Datei nötig (10 Minuten Intervall)
|
||||
*/
|
||||
static void check_new_csv_file(void)
|
||||
{
|
||||
time_t now;
|
||||
time(&now);
|
||||
|
||||
if (difftime(now, last_write_time) >= CSV_WRITE_INTERVAL_SEC) {
|
||||
create_new_csv_file();
|
||||
last_write_time = now;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* C-ITS Nachricht speichern
|
||||
*
|
||||
* @param msg Nachricht Daten
|
||||
*/
|
||||
void csv_store_cits_message(cits_message_t *msg)
|
||||
{
|
||||
if (csv_file == NULL) {
|
||||
create_new_csv_file();
|
||||
if (csv_file == NULL) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
check_new_csv_file();
|
||||
write_csv_row(csv_file, msg);
|
||||
fflush(csv_file);
|
||||
}
|
||||
|
||||
/**
|
||||
* CSV System initialisieren
|
||||
*/
|
||||
esp_err_t csv_writer_init(void)
|
||||
{
|
||||
// SPI Flash Filesystem mounten
|
||||
static wl_handle_t wl_handle;
|
||||
const esp_vfs_fat_mount_config_t mount_config = {
|
||||
.max_files = 4,
|
||||
.format_if_mount_failed = true
|
||||
};
|
||||
|
||||
esp_err_t ret = esp_vfs_fat_spiflash_mount_rw_wl("/data", "storage", &mount_config, &wl_handle);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Erste CSV Datei erstellen
|
||||
return create_new_csv_file();
|
||||
}
|
||||
|
||||
/**
|
||||
* CSV System beenden
|
||||
*/
|
||||
void csv_writer_deinit(void)
|
||||
{
|
||||
if (csv_file != NULL) {
|
||||
fclose(csv_file);
|
||||
csv_file = NULL;
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
/**
|
||||
* Block 4: Integration und Flashen der Firmware auf ESP32-C5
|
||||
*
|
||||
* Ziel: C-ITS Sniffer Firmware für ESP32-C5 compilieren und flashen
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "block1_hardware_init.h"
|
||||
#include "block2_cits_parser.h"
|
||||
#include "block3_csv_writer.h"
|
||||
|
||||
static const char *TAG = "CITS_SNIFER_MAIN";
|
||||
|
||||
/**
|
||||
* Sniffer Callback - Wird vom WiFi Promiscuous Mode aufgerufen
|
||||
*/
|
||||
static void sniffer_callback(void *recv_buf, wifi_promiscuous_pkt_type_t type)
|
||||
{
|
||||
wifi_promiscuous_pkt_t *packet = (wifi_promiscuous_pkt_t *)recv_buf;
|
||||
|
||||
// Nur MISC Typ für 802.11p verarbeiten
|
||||
if (type != WIFI_PKT_MISC) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Verarbeite C-ITS Nachricht
|
||||
if (packet->payload != NULL && packet->rx_ctrl.sig_len > 0) {
|
||||
cits_process_packet(packet->payload, packet->rx_ctrl.sig_len,
|
||||
packet->rx_ctrl.timestamp / 1000000U,
|
||||
packet->rx_ctrl.timestamp % 1000000U);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* C-ITS Sniffer aufsetzen
|
||||
*/
|
||||
static esp_err_t setup_cits_sniffer(void)
|
||||
{
|
||||
// WiFi Promiscuous Mode starten
|
||||
esp_err_t ret = start_promiscuous_mode(CITS_CHANNEL_0_MHZ);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to start promiscuous mode");
|
||||
return ret;
|
||||
}
|
||||
|
||||
// C-ITS Parser initialisieren
|
||||
ret = cits_parser_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize C-ITS parser");
|
||||
return ret;
|
||||
}
|
||||
|
||||
// CSV Writer initialisieren
|
||||
ret = csv_writer_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize CSV writer");
|
||||
return ret;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "C-ITS Sniffer Setup abgeschlossen");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hauptfunktion
|
||||
*/
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "C-ITS Sniffer für ESP32-C5 startet...");
|
||||
|
||||
// NVS initialisieren
|
||||
esp_err_t ret = init_nvs_flash();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize NVS");
|
||||
return;
|
||||
}
|
||||
|
||||
// WiFi initialisieren
|
||||
ret = initialize_wifi();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize WiFi");
|
||||
return;
|
||||
}
|
||||
|
||||
// C-ITS Sniffer aufsetzen
|
||||
ret = setup_cits_sniffer();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to setup C-ITS Sniffer");
|
||||
return;
|
||||
}
|
||||
|
||||
// Hauptschleife
|
||||
ESP_LOGI(TAG, "C-ITS Sniffer läuft - Warte auf C-ITS Nachrichten...");
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
dependencies:
|
||||
idf:
|
||||
source:
|
||||
type: idf
|
||||
version: 6.1.0
|
||||
direct_dependencies:
|
||||
- idf
|
||||
manifest_hash: 5bf354cf54d5be6775c02e9e16c7631f2446e2e15af9b777a2a2f8ce681a349f
|
||||
target: esp32c5
|
||||
version: 3.0.0
|
||||
@@ -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
|
||||
)
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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
@@ -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);
|
||||
}
|
||||
@@ -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
@@ -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));
|
||||
}
|
||||
@@ -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
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
@@ -0,0 +1,6 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
# 16 MB flash (W25Q128). factory 4 MB + storage/FAT ~12 MB (CSV fallback).
|
||||
nvs, data, nvs, 0x9000, 0x6000,
|
||||
phy_init, data, phy, 0xf000, 0x1000,
|
||||
factory, app, factory, 0x10000, 0x400000,
|
||||
storage, data, fat, 0x410000, 0xbf0000,
|
||||
|
@@ -0,0 +1,16 @@
|
||||
# cits-sniffer-fw sdkconfig.defaults
|
||||
# ESP32-C5, 16 MB flash, USB-JTAG console, custom partition table.
|
||||
CONFIG_IDF_TARGET="esp32c5"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
|
||||
CONFIG_ESPTOOLPY_FLASHMODE_DIO=y
|
||||
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
|
||||
|
||||
# Console over USB-JTAG (the same USB used for flashing) so logs are easy to read.
|
||||
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
|
||||
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED=y
|
||||
|
||||
CONFIG_LOG_DEFAULT_LEVEL_INFO=y
|
||||
|
||||
# Custom 16 MB partition table (nvs + phy + factory 4 MB + storage FAT ~12 MB).
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||
Reference in new issue
Block a user