feat: add decoder module, host unit tests, and GitHub Actions CI pipeline
Build and Package Firmware / build (push) Failing after 2m0s

This commit is contained in:
2026-06-12 14:04:58 +01:00
parent f5ec189dc4
commit 98d35a82e1
7 changed files with 587 additions and 174 deletions
+7 -1
View File
@@ -1,2 +1,8 @@
idf_component_register(SRCS "main.c"
idf_component_register(SRCS "main.c" "decoder.c"
INCLUDE_DIRS ".")
if(EXISTS "${PROJECT_DIR}/version.txt")
file(READ "${PROJECT_DIR}/version.txt" PROJECT_VER_CONTENT)
string(STRIP "${PROJECT_VER_CONTENT}" PROJECT_VER_CONTENT)
target_compile_definitions(${COMPONENT_LIB} PRIVATE PROJECT_VER="${PROJECT_VER_CONTENT}")
endif()
+115
View File
@@ -0,0 +1,115 @@
#include "decoder.h"
static enqueue_frame_cb_t s_enqueue_cb = NULL;
static print_frame_cb_t s_print_cb = NULL;
void decoder_init(enqueue_frame_cb_t enqueue_cb, print_frame_cb_t print_cb) {
s_enqueue_cb = enqueue_cb;
s_print_cb = print_cb;
}
uint8_t classify_pulse(uint32_t us) {
if (us < MIN_VALID_US) return PC_GLITCH;
if (us > MAX_VALID_US) return PC_IDLE_GAP;
if (us > MERGE_THRESHOLD_US) return PC_MERGED;
if (us < THRESHOLD_US) return PC_LOGIC_0;
return PC_LOGIC_1;
}
void reset_decoder(struct DecoderContext *ctx_ptr) {
ctx_ptr->state = DS_HUNT_SYNC;
ctx_ptr->sync_count = 0;
ctx_ptr->bit_count = 0;
ctx_ptr->byte_count = 0;
ctx_ptr->separator_count = 0;
ctx_ptr->frame_errors = 0;
ctx_ptr->bytes_this_frame = 0;
}
static void feed_bit(struct DecoderContext *ctx_ptr, uint8_t pc) {
switch (ctx_ptr->state) {
case DS_HUNT_SYNC:
if (pc == PC_LOGIC_1) {
ctx_ptr->sync_count++;
if (ctx_ptr->sync_count >= SYNC_ONES_NEEDED) {
ctx_ptr->sync_count = 0;
ctx_ptr->byte_count = 0;
ctx_ptr->bit_count = 0;
ctx_ptr->separator_count = 0;
ctx_ptr->frame_errors = 0;
ctx_ptr->bytes_this_frame = 0;
ctx_ptr->state = DS_AWAIT_START;
}
} else {
ctx_ptr->sync_count = 0;
}
break;
case DS_AWAIT_START:
if (pc == PC_LOGIC_0) {
ctx_ptr->current_byte = 0;
ctx_ptr->bit_count = 0;
ctx_ptr->separator_count = 0;
ctx_ptr->state = DS_READ_BITS;
} else {
ctx_ptr->separator_count++;
if (ctx_ptr->separator_count > MAX_SEPARATORS) {
reset_decoder(ctx_ptr);
}
}
break;
case DS_READ_BITS: {
uint8_t bit_val = (pc == PC_LOGIC_1) ? 1u : 0u;
ctx_ptr->current_byte = (uint8_t)((ctx_ptr->current_byte << 1) | bit_val);
ctx_ptr->bit_count++;
if (ctx_ptr->bit_count == 8) {
ctx_ptr->frame[ctx_ptr->byte_count] = ctx_ptr->current_byte;
ctx_ptr->bytes_this_frame++;
ctx_ptr->bit_count = 0;
ctx_ptr->byte_count++;
if (ctx_ptr->byte_count >= PAYLOAD_BYTES) {
ctx_ptr->frames_decoded++;
if (s_print_cb) {
s_print_cb(ctx_ptr->frames_decoded, ctx_ptr->frame);
}
if (s_enqueue_cb) {
s_enqueue_cb(ctx_ptr->frame, PAYLOAD_BYTES);
}
reset_decoder(ctx_ptr);
} else {
ctx_ptr->separator_count = 0;
ctx_ptr->state = DS_AWAIT_START;
}
}
break;
}
default:
reset_decoder(ctx_ptr);
break;
}
}
void process_pulse(struct DecoderContext *ctx_ptr, uint32_t pulse_us) {
uint8_t pc = classify_pulse(pulse_us);
if (pc == PC_GLITCH) return;
if (pc == PC_IDLE_GAP) {
reset_decoder(ctx_ptr);
return;
}
if (pc == PC_MERGED) {
uint32_t hidden_est = pulse_us - LOGIC1_PULSE_US;
uint8_t hidden_bit = (hidden_est >= THRESHOLD_US) ? PC_LOGIC_1 : PC_LOGIC_0;
feed_bit(ctx_ptr, hidden_bit);
feed_bit(ctx_ptr, PC_LOGIC_1);
return;
}
feed_bit(ctx_ptr, pc);
}
+83
View File
@@ -0,0 +1,83 @@
#ifndef DECODER_H
#define DECODER_H
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#define LOGIC0_PULSE_US 1111u
#define LOGIC1_PULSE_US 4167u
#define THRESHOLD_US 2639u
#define MIN_VALID_US 300u
#define MERGE_THRESHOLD_US 8000u
#define MAX_VALID_US 13500u
#define MAX_SEPARATORS 12u
#define SYNC_ONES_NEEDED 8u
#define PAYLOAD_BYTES 25u
#define PC_GLITCH ((uint8_t)0)
#define PC_LOGIC_0 ((uint8_t)1)
#define PC_LOGIC_1 ((uint8_t)2)
#define PC_IDLE_GAP ((uint8_t)3)
#define PC_MERGED ((uint8_t)4)
#define DS_HUNT_SYNC ((uint8_t)0)
#define DS_AWAIT_START ((uint8_t)1)
#define DS_READ_BITS ((uint8_t)2)
struct BtFrame {
uint8_t data[PAYLOAD_BYTES];
uint8_t len;
};
struct DecoderContext {
uint8_t state;
uint8_t sync_count;
uint8_t bit_count;
uint8_t current_byte;
uint8_t byte_count;
uint8_t separator_count;
uint8_t frame[PAYLOAD_BYTES];
uint32_t frame_errors;
uint32_t frames_decoded;
uint32_t bytes_this_frame;
};
struct RingBuffer {
volatile uint32_t data[256];
volatile uint16_t head;
volatile uint16_t tail;
};
#define RB_MASK ((uint16_t)255u)
// Callbacks for hardware bridging (e.g. FreeRTOS queueing, hardware logging)
typedef void (*enqueue_frame_cb_t)(const uint8_t *frame_data, uint8_t len);
typedef void (*print_frame_cb_t)(uint32_t frames_decoded, const uint8_t *frame_data);
// Initialize decoder callbacks
void decoder_init(enqueue_frame_cb_t enqueue_cb, print_frame_cb_t print_cb);
// Core decoding interface
uint8_t classify_pulse(uint32_t us);
void reset_decoder(struct DecoderContext *ctx_ptr);
void process_pulse(struct DecoderContext *ctx_ptr, uint32_t pulse_us);
// Ring buffer inline helpers
static inline void rb_push(struct RingBuffer *rb_ptr, uint32_t v) {
uint16_t next = (rb_ptr->head + 1u) & RB_MASK;
if (next == rb_ptr->tail) return;
rb_ptr->data[rb_ptr->head] = v;
__asm__ __volatile__("" ::: "memory");
rb_ptr->head = next;
}
static inline bool rb_pop(struct RingBuffer *rb_ptr, uint32_t *out) {
if (rb_ptr->tail == rb_ptr->head) return false;
*out = rb_ptr->data[rb_ptr->tail];
__asm__ __volatile__("" ::: "memory");
rb_ptr->tail = (rb_ptr->tail + 1u) & RB_MASK;
return true;
}
#endif // DECODER_H
+21 -173
View File
@@ -13,58 +13,19 @@
#include "esp_bt_device.h"
#include "esp_gap_bt_api.h"
#include "esp_spp_api.h"
#include "decoder.h"
#define ALDL_PIN GPIO_NUM_4
#define LOGIC0_PULSE_US 1111u
#define LOGIC1_PULSE_US 4167u
#define THRESHOLD_US 2639u
#define MIN_VALID_US 300u
#define MERGE_THRESHOLD_US 8000u
#define MAX_VALID_US 13500u
#define MAX_SEPARATORS 12u
#define SYNC_ONES_NEEDED 8u
#define PAYLOAD_BYTES 25u
#define BT_DEVICE_NAME "ESP32-ALDL"
#define BT_QUEUE_DEPTH 4u
#define PC_GLITCH ((uint8_t)0)
#define PC_LOGIC_0 ((uint8_t)1)
#define PC_LOGIC_1 ((uint8_t)2)
#define PC_IDLE_GAP ((uint8_t)3)
#define PC_MERGED ((uint8_t)4)
#define DS_HUNT_SYNC ((uint8_t)0)
#define DS_AWAIT_START ((uint8_t)1)
#define DS_READ_BITS ((uint8_t)2)
#ifndef PROJECT_VER
#define PROJECT_VER "unknown"
#endif
static const char *TAG = "ALDL";
struct BtFrame {
uint8_t data[PAYLOAD_BYTES];
uint8_t len;
};
struct DecoderContext {
uint8_t state;
uint8_t sync_count;
uint8_t bit_count;
uint8_t current_byte;
uint8_t byte_count;
uint8_t separator_count;
uint8_t frame[PAYLOAD_BYTES];
uint32_t frame_errors;
uint32_t frames_decoded;
uint32_t bytes_this_frame;
};
struct RingBuffer {
volatile uint32_t data[256];
volatile uint16_t head;
volatile uint16_t tail;
};
static struct RingBuffer rb;
static struct DecoderContext ctx;
static QueueHandle_t bt_queue = NULL;
@@ -72,24 +33,6 @@ static QueueHandle_t bt_queue = NULL;
static uint32_t spp_handle = 0;
static bool bt_connected = false;
#define RB_MASK ((uint16_t)255u)
static inline void IRAM_ATTR rb_push(uint32_t v) {
uint16_t next = (rb.head + 1u) & RB_MASK;
if (next == rb.tail) return;
rb.data[rb.head] = v;
__asm__ __volatile__("" ::: "memory");
rb.head = next;
}
static inline bool rb_pop(uint32_t *out) {
if (rb.tail == rb.head) return false;
*out = rb.data[rb.tail];
__asm__ __volatile__("" ::: "memory");
rb.tail = (rb.tail + 1u) & RB_MASK;
return true;
}
static volatile uint64_t isr_fall_us = 0;
static void IRAM_ATTR aldl_gpio_isr(void* arg) {
@@ -98,128 +41,30 @@ static void IRAM_ATTR aldl_gpio_isr(void* arg) {
isr_fall_us = now;
} else {
if (isr_fall_us != 0) {
rb_push((uint32_t)(now - isr_fall_us));
rb_push(&rb, (uint32_t)(now - isr_fall_us));
isr_fall_us = 0;
}
}
}
static uint8_t classify_pulse(uint32_t us) {
if (us < MIN_VALID_US) return PC_GLITCH;
if (us > MAX_VALID_US) return PC_IDLE_GAP;
if (us > MERGE_THRESHOLD_US) return PC_MERGED;
if (us < THRESHOLD_US) return PC_LOGIC_0;
return PC_LOGIC_1;
}
static void reset_decoder(void) {
ctx.state = DS_HUNT_SYNC;
ctx.sync_count = 0;
ctx.bit_count = 0;
ctx.byte_count = 0;
ctx.separator_count = 0;
ctx.frame_errors = 0;
ctx.bytes_this_frame = 0;
}
static void enqueue_frame(void) {
// Hardware callback to queue frames for Bluetooth transmission
static void enqueue_frame_hw(const uint8_t *frame_data, uint8_t len) {
struct BtFrame f;
memcpy(f.data, ctx.frame, PAYLOAD_BYTES);
f.len = PAYLOAD_BYTES;
memcpy(f.data, frame_data, len);
f.len = len;
if (xQueueSend(bt_queue, &f, 0) != pdTRUE) {
ESP_LOGW(TAG, "BT queue full");
}
}
static void print_frame(void) {
// Hardware callback to print frames to ESP Log console
static void print_frame_hw(uint32_t frames_decoded, const uint8_t *frame_data) {
char hex_str[ PAYLOAD_BYTES * 3 + 1 ];
int offset = 0;
for (uint8_t i = 0; i < PAYLOAD_BYTES; i++) {
offset += sprintf(hex_str + offset, "%02X ", ctx.frame[i]);
offset += sprintf(hex_str + offset, "%02X ", frame_data[i]);
}
ESP_LOGI(TAG, "[FRAME #%lu] %s", ctx.frames_decoded, hex_str);
}
static void feed_bit(uint8_t pc) {
switch (ctx.state) {
case DS_HUNT_SYNC:
if (pc == PC_LOGIC_1) {
ctx.sync_count++;
if (ctx.sync_count >= SYNC_ONES_NEEDED) {
ctx.sync_count = 0;
ctx.byte_count = 0;
ctx.bit_count = 0;
ctx.separator_count = 0;
ctx.frame_errors = 0;
ctx.bytes_this_frame = 0;
ctx.state = DS_AWAIT_START;
}
} else {
ctx.sync_count = 0;
}
break;
case DS_AWAIT_START:
if (pc == PC_LOGIC_0) {
ctx.current_byte = 0;
ctx.bit_count = 0;
ctx.separator_count = 0;
ctx.state = DS_READ_BITS;
} else {
ctx.separator_count++;
if (ctx.separator_count > MAX_SEPARATORS) {
reset_decoder();
}
}
break;
case DS_READ_BITS: {
uint8_t bit_val = (pc == PC_LOGIC_1) ? 1u : 0u;
ctx.current_byte = (uint8_t)((ctx.current_byte << 1) | bit_val);
ctx.bit_count++;
if (ctx.bit_count == 8) {
ctx.frame[ctx.byte_count] = ctx.current_byte;
ctx.bytes_this_frame++;
ctx.bit_count = 0;
ctx.byte_count++;
if (ctx.byte_count >= PAYLOAD_BYTES) {
ctx.frames_decoded++;
print_frame();
enqueue_frame();
reset_decoder();
} else {
ctx.separator_count = 0;
ctx.state = DS_AWAIT_START;
}
}
break;
}
default:
reset_decoder();
break;
}
}
static void process_pulse(uint32_t pulse_us) {
uint8_t pc = classify_pulse(pulse_us);
if (pc == PC_GLITCH) return;
if (pc == PC_IDLE_GAP) {
reset_decoder();
return;
}
if (pc == PC_MERGED) {
uint32_t hidden_est = pulse_us - LOGIC1_PULSE_US;
uint8_t hidden_bit = (hidden_est >= THRESHOLD_US) ? PC_LOGIC_1 : PC_LOGIC_0;
feed_bit(hidden_bit);
feed_bit(PC_LOGIC_1);
return;
}
feed_bit(pc);
ESP_LOGI(TAG, "[FRAME #%lu] %s", (unsigned long)frames_decoded, hex_str);
}
static void btTransmitTask(void* pvParameters) {
@@ -246,8 +91,8 @@ static void aldlDecodeTask(void* pvParameters) {
uint32_t pulse_us = 0;
for (;;) {
bool did_work = false;
while (rb_pop(&pulse_us)) {
process_pulse(pulse_us);
while (rb_pop(&rb, &pulse_us)) {
process_pulse(&ctx, pulse_us);
did_work = true;
}
if (!did_work) vTaskDelay(1);
@@ -258,7 +103,7 @@ static void statusTask(void* pvParameters) {
for (;;) {
vTaskDelay(pdMS_TO_TICKS(5000));
ESP_LOGI(TAG, "[STATUS] frames=%lu bt=%s",
ctx.frames_decoded,
(unsigned long)ctx.frames_decoded,
bt_connected ? "UP" : "waiting");
}
}
@@ -305,7 +150,6 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
case ESP_SPP_SRV_STOP_EVT:
ESP_LOGI(TAG, "ESP_SPP_SRV_STOP_EVT");
break;
default:
break;
}
@@ -360,12 +204,16 @@ void app_main(void) {
ESP_LOGI(TAG, "============================================");
ESP_LOGI(TAG, " ESP32 ALDL Bridge — GM 1227170 Fiero 2.8 ");
ESP_LOGI(TAG, " Version: %s", PROJECT_VER);
ESP_LOGI(TAG, " 160-baud PWM — AA55 Hard Sync Active ");
ESP_LOGI(TAG, "============================================");
memset(&rb, 0, sizeof(rb));
memset(&ctx, 0, sizeof(ctx));
ctx.state = DS_HUNT_SYNC;
// Initialize the decoder with hardware callbacks
decoder_init(enqueue_frame_hw, print_frame_hw);
reset_decoder(&ctx);
gpio_config_t io = {};
io.intr_type = GPIO_INTR_ANYEDGE;