Files
audiocodec/DATA_TRANSFER_FIX.md
2026-07-14 00:26:01 +02:00

2.8 KiB

Data Transfer Fix - AudioCoder

Problem Identified

The GUI would show "CLOCK LOCK pattern=AA" repeatedly but never receive any data. The decoder got stuck in the RX_SEARCH state.

Root Cause Analysis

Critical Bug #1: Missing syncBytes Declaration (PRIMARY)

File: decoder.c / decoder.h Issue: The try_phase_search() function compares decoded symbols against syncBytes[]:

if (byte != syncBytes[i])  // Line 177
    break;

However, syncBytes was only defined in encoder.c and NOT declared as extern in decoder.h. This meant:

  • The decoder could not access the sync bytes array
  • Phase search was comparing against uninitialized memory or garbage values
  • Sync bytes would NEVER match
  • Decoder would never transition from RX_SEARCH to RX_LOCKED
  • No data would ever be received

Fix Applied: Added to decoder.h:

extern const uint8_t syncBytes[4];

Critical Bug #2: State Machine Deadlock (SECONDARY)

File: decoder.c line 444-450 Issue: When try_phase_search() returned false (failed to find sync):

if (try_phase_search(dec)) {
    // Reset on success
} 
// BUG: goodClocks NOT reset on failure!

This caused:

  • goodClocks remained >= 8
  • Every symbol period, state machine would try phase search again
  • Infinite loop of failed phase searches
  • CPU spinning without progress

Fix Applied: Added reset on failure:

} else {
    // Phase search failed, reset and try again
    dec->goodClocks = 0;
    dec->clockHistory = 0;
    dec->clockCount = 0;
}

Enhancement #3: Debug Visibility

File: decoder.c line 193-194 Issue: No feedback when phase search fails

Fix Applied: Added diagnostic output:

printf("PHASE SEARCH FAILED bestScore=%d (need %zu)\n", bestScore, sizeof(syncBytes));

This shows:

  • How many sync bytes matched (0-4)
  • How many are needed (4)
  • Helps diagnose frequency/timing mismatches

How It Should Work Now

  1. Encoder Sends:

    • Preamble (128 clock cycles)
    • Sync bytes: 'B' (0x42), 'R' (0x52), 'N' (0x4E), 'Q' (0x51)
    • Data payload
  2. Decoder Receives:

    • RX_SEARCH: Listen for alternating clock pattern (0xAA alternating)
    • When found: goodClocks = 8
    • searchOffset increments, triggers phase search after 4 clock detections
    • Phase search: Find exact alignment of the 4 sync bytes
    • If found: RX_LOCKED state
    • RX_LOCKED: Decode symbols into data buffer
  3. Success Indicators:

    • CLOCK LOCK pattern=AA (multiple times)
    • PHASE SEARCH FAILED bestScore=3 or 4 (diagnosing alignment)
    • SYNC ALIGN phase=XXX score=4 (SUCCESS - decoder locked)
    • DATA XXXXXXXX (receiving data bytes)

Testing

The binary has been rebuilt with all fixes applied. Expected output should progress from CLOCK LOCK → PHASE SEARCH FAILED (or SUCCEED) → SYNC ALIGN → DATA.