From 18202c5264d2cda63350756fe1ae810ea3be9ce5 Mon Sep 17 00:00:00 2001 From: Allen Hill Date: Tue, 12 Sep 2023 19:21:19 -0400 Subject: [PATCH] Add toggleable verbose debugging messages and tweak/improve the serial interface --- src/avclandrv.c | 106 ++++++++++++++++++++++++++++++++++-------------- src/avclandrv.h | 1 + src/sniffer.c | 71 ++++++++++++++++++++------------ 3 files changed, 121 insertions(+), 57 deletions(-) diff --git a/src/avclandrv.c b/src/avclandrv.c index 4255f7e..04ebe7c 100644 --- a/src/avclandrv.c +++ b/src/avclandrv.c @@ -128,6 +128,7 @@ uint16_t CD_ID; uint16_t HU_ID; uint8_t printAllFrames; +uint8_t verbose; uint8_t playMode; @@ -538,45 +539,58 @@ uint8_t AVCLAN_readframe() { uint8_t i; uint8_t for_me = 0; - AVCLAN_frame_t frame = {}; + AVCLAN_frame_t frame = {0}; - // RS232_Print("$ "); - // TCCR1B |= (1 << CS11)|(1 << CS10); // Timer1 prescaler at 64 - // TCNT1 = 0; - // TCNT0 = 0; - // while (INPUT_IS_SET) { - // if ( TCNT0 > 255 ) { // 170 us - // // TCCR1B = 0; - // // TCCR1B |= (1 << WGM12)|(1 << CS12); // Set CTC, prescaler at 256 - // STARTEvent; - // RS232_Print("LAN>T1\n"); - // return 0; - // } - // } - // - // if ( TCNT0 < 20 ) { // 20 us - // // TCCR1B = 0; - // // TCCR1B |= (1 << WGM12)|(1 << CS12); - // STARTEvent; - // RS232_Print("LAN>T2\n"); - // return 0; - // } uint8_t parity = 0; uint8_t tmp = 0; - AVCLAN_readbits(&tmp, 1); // Start bit + + TCB1.CNT = 0; + while (INPUT_IS_SET) { + if (TCB1.CNT > (uint16_t)AVCLAN_STARTBIT_LOGIC_0 * 1.2) { + STARTEvent; + return 0; + } + } + uint16_t startbitlen = TCB1.CNT; + if (startbitlen < (uint16_t)(AVCLAN_STARTBIT_LOGIC_0 * 0.8)) { + RS232_Print("ERR: Short start bit.\n"); + STARTEvent; + return 0; + } + // Otherwise that was a start bit AVCLAN_readbits((uint8_t *)&frame.broadcast, 1); parity = AVCLAN_readbits(&frame.controller_addr, 12); AVCLAN_readbits(&tmp, 1); - if (parity != tmp) { + if (parity != (tmp & 1)) { + RS232_Print("ERR: Bad controller addr. parity"); + if (verbose) { + RS232_Print("; read 0x"); + RS232_PrintHex12(frame.controller_addr); + RS232_Print(" and calculated parity="); + RS232_PrintHex4(parity); + RS232_Print(" but got "); + RS232_PrintHex4(tmp & 1); + } + RS232_Print(".\n"); STARTEvent; return 0; } parity = AVCLAN_readbits(&frame.peripheral_addr, 12); AVCLAN_readbits(&tmp, 1); - if (parity != tmp) { + if (parity != (tmp & 1)) { + RS232_Print("Bad peripheral addr. parity"); + if (verbose) { + RS232_Print("; read 0x"); + RS232_PrintHex12(frame.peripheral_addr); + RS232_Print(" and calculated parity="); + RS232_PrintHex4(parity); + RS232_Print(" but got "); + RS232_PrintHex4(tmp & 1); + } + RS232_Print(".\n"); STARTEvent; return 0; } @@ -591,7 +605,17 @@ uint8_t AVCLAN_readframe() { parity = AVCLAN_readbits(&frame.control, 4); AVCLAN_readbits(&tmp, 1); - if (parity != tmp) { + if (parity != (tmp & 1)) { + RS232_Print("Bad control parity"); + if (verbose) { + RS232_Print("; read 0x"); + RS232_PrintHex4(frame.control); + RS232_Print(" and calculated parity="); + RS232_PrintHex4(parity); + RS232_Print(" but got "); + RS232_PrintHex4(tmp & 1); + } + RS232_Print(".\n"); STARTEvent; return 0; } else if (for_me) { @@ -602,7 +626,17 @@ uint8_t AVCLAN_readframe() { parity = AVCLAN_readbyte(&frame.length); AVCLAN_readbits(&tmp, 1); - if (parity != tmp) { + if (parity != (tmp & 1)) { + RS232_Print("Bad length parity"); + if (verbose) { + RS232_Print("; read 0x"); + RS232_PrintHex4(frame.length); + RS232_Print(" and calculated parity="); + RS232_PrintHex4(parity); + RS232_Print(" but got "); + RS232_PrintHex4(tmp & 1); + } + RS232_Print(".\n"); STARTEvent; return 0; } else if (for_me) { @@ -611,8 +645,10 @@ uint8_t AVCLAN_readframe() { AVCLAN_readbits(&tmp, 1); } - if (frame.length > MAXMSGLEN) { - // RS232_Print("LAN> Command error"); + if (frame.length == 0 || frame.length > MAXMSGLEN) { + RS232_Print("Bad length; got 0x"); + RS232_PrintHex4(frame.length); + RS232_Print(".\n"); STARTEvent; return 0; } @@ -620,7 +656,17 @@ uint8_t AVCLAN_readframe() { for (i = 0; i < frame.length; i++) { parity = AVCLAN_readbyte(&frame.data[i]); AVCLAN_readbits(&tmp, 1); - if (parity != tmp) { + if (parity != (tmp & 1)) { + RS232_Print("Bad data parity"); + if (verbose) { + RS232_Print("; read 0x"); + RS232_PrintHex4(frame.data[i]); + RS232_Print(" and calculated parity="); + RS232_PrintHex4(parity); + RS232_Print(" but got "); + RS232_PrintHex4(tmp & 1); + } + RS232_Print(".\n"); STARTEvent; return 0; } else if (for_me) { diff --git a/src/avclandrv.h b/src/avclandrv.h index 287940c..7c43f6a 100644 --- a/src/avclandrv.h +++ b/src/avclandrv.h @@ -49,6 +49,7 @@ extern uint16_t CD_ID; // CD Changer ID extern uint16_t HU_ID; // Head-unit ID extern uint8_t printAllFrames; +extern uint8_t verbose; typedef enum { cm_Null = 0, diff --git a/src/sniffer.c b/src/sniffer.c index cdecc92..6d8f62a 100644 --- a/src/sniffer.c +++ b/src/sniffer.c @@ -35,8 +35,11 @@ uint8_t Event; uint8_t echoCharacters; +const char const *offon[] = {"OFF", "ON"}; + void Setup(); void general_GPIO_init(); +void print_help(); int main() { uint8_t readSeq = 0; @@ -53,17 +56,7 @@ int main() { }; Setup(); - - RS232_Print("AVCLan reader 1.00\nReady\n\n"); - RS232_Print("\nS - read sequence\nW - send command\nQ - send " - "broadcast\nL/l - log on/off\nK/k - seq. echo on/off\n"); - RS232_Print("R/r - register device\nB - Beep\n"); -#ifdef HARDWARE_DEBUG - RS232_Print("1 - Hold High/low\nE - Print line status\n"); -#endif -#ifdef SOFTWARE_DEBUG - RS232_Print("M - Measure high and low lengths\n"); -#endif + print_help(); while (1) { @@ -87,13 +80,15 @@ int main() { // Key handler if (RS232_RxCharEnd) { cli(); - readkey = RS232_RxCharBuffer[RS232_RxCharBegin]; // read begin of received - // Buffer + readkey = RS232_RxCharBuffer[RS232_RxCharBegin]; RS232_RxCharBegin++; - if (RS232_RxCharBegin == RS232_RxCharEnd) // if Buffer is empty - RS232_RxCharBegin = RS232_RxCharEnd = 0; // reset Buffer + if (RS232_RxCharBegin == RS232_RxCharEnd) // if buffer is consumed + RS232_RxCharBegin = RS232_RxCharEnd = 0; // reset buffer sei(); switch (readkey) { + case '?': + print_help(); + break; case 'S': // Read sequence printAllFrames = 0; RS232_Print("READ SEQUENCE > \n"); @@ -128,21 +123,23 @@ int main() { case 'r': // Register into the abyss AVCLan_Register(); break; - case 'l': // Print received messages - RS232_Print("Log OFF\n"); - printAllFrames = 0; + case 'v': + verbose ^= 1; + RS232_Print("Verbose: "); + RS232_Print(offon[verbose]); + RS232_Print("\n"); break; - case 'L': - RS232_Print("Log ON\n"); - printAllFrames = 1; + case 'l': // Print received messages + printAllFrames ^= 1; + RS232_Print("Logging:"); + RS232_Print(offon[printAllFrames]); + RS232_Print("\n"); break; case 'k': // Echo input - RS232_Print("str OFF\n"); - echoCharacters = 0; - break; - case 'K': - RS232_Print("str ON\n"); - echoCharacters = 1; + echoCharacters ^= 1; + RS232_Print("Echo characters:"); + RS232_Print(offon[echoCharacters]); + RS232_Print("\n"); break; case 'B': // Beep data_tmp[0] = 0x00; @@ -266,6 +263,26 @@ void general_GPIO_init() { PORTC.PIN1CTRL = PORT_ISC_INPUT_DISABLE_gc; // WOD } +void print_help() { + RS232_Print("AVCLAN Mockingboard v1\n"); + RS232_Print("S - read sequence\n" + "W - send command\n" + "Q - send broadcast\n" + "l - Toggle message logging\n" + "k - Toggle character echo\n" + "R/r - register device\n" + "B - Beep\n" + "v - Toggle verbose logging\n" +#ifdef SOFTWARE_DEBUG + "M - Measure bit-timing (pulse-widths and periods)\n" +#endif +#ifdef HARDWARE_DEBUG + "1 - Hold High/low\n" + "E - Print line status\n" +#endif + "? - Print this message\n"); +} + /* Increment packed 2-digit BCD number. WARNING: Overflow behavior is incorrect (e.g. `incBCD(0x99) != 0x00`) */ uint8_t incBCD(uint8_t data) {