1
0
mirror of https://github.com/halleysfifthinc/Toyota-AVC-LAN synced 2025-06-07 07:56:21 +00:00

Add binary printing and reading/parsing from serial

This commit is contained in:
Allen Hill 2023-09-15 18:43:41 -04:00
parent cabcd06e67
commit ebb5ec95ca
5 changed files with 163 additions and 73 deletions

View File

@ -93,6 +93,7 @@
#include <avr/io.h>
#include <avr/sfr_defs.h>
#include <stdint.h>
#include <stdlib.h>
#include "avclandrv.h"
#include "com232.h"
@ -674,7 +675,7 @@ uint8_t AVCLAN_readframe() {
STARTEvent;
if (printAllFrames)
AVCLAN_printframe(&frame);
AVCLAN_printframe(&frame, printBinary);
if (for_me) {
if (CheckCmd(&frame, stat1)) {
@ -746,6 +747,43 @@ uint8_t AVCLAN_readframe() {
return 1;
}
AVCLAN_frame_t *AVCLAN_parseframe(const uint8_t *bytes, uint8_t len) {
if (len < sizeof(AVCLAN_frame_t))
return NULL;
AVCLAN_frame_t *frame = malloc(sizeof(AVCLAN_frame_t) + 1);
if (!frame)
return NULL;
frame->broadcast = *bytes++;
frame->controller_addr = *(uint16_t *)bytes++;
bytes++;
frame->peripheral_addr = *(uint16_t *)bytes++;
bytes++;
frame->control = *bytes++;
frame->length = *bytes++;
if (frame->length <= (len - 8)) {
free(frame);
return NULL;
} else {
AVCLAN_frame_t *framedata =
realloc(frame, sizeof(AVCLAN_frame_t) + frame->length);
if (!framedata) {
free(frame);
return NULL;
}
frame = framedata;
frame->data = (uint8_t *)frame + sizeof(AVCLAN_frame_t);
for (uint8_t i = 0; i < frame->length; i++) {
frame->data[i] = *bytes++;
}
}
return frame;
}
uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame) {
STOPEvent;
@ -834,36 +872,39 @@ uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame) {
STARTEvent;
if (printAllFrames)
AVCLAN_printframe(frame);
AVCLAN_printframe(frame, printBinary);
return 0;
}
void AVCLAN_printframe(const AVCLAN_frame_t *frame) {
if (frame->peripheral_addr == CD_ID ||
(frame->broadcast && frame->peripheral_addr == 0x1FF))
RS232_Print(" < ");
else
RS232_Print(">< ");
void AVCLAN_printframe(const AVCLAN_frame_t *frame, uint8_t binary) {
if (binary) {
RS232_SendByte(0x10); // Data Link Escape, signaling binary data forthcoming
RS232_sendbytes((uint8_t *)frame,
sizeof(AVCLAN_frame_t) - sizeof(uint8_t *));
RS232_sendbytes(frame->data, frame->length);
RS232_SendByte(0x17); // End of transmission block
RS232_Print("\n");
} else {
RS232_PrintHex4(frame->broadcast);
RS232_PrintHex4(frame->broadcast);
RS232_Print(" 0x");
RS232_PrintHex12(frame->controller_addr);
RS232_Print(" 0x");
RS232_PrintHex12(frame->peripheral_addr);
RS232_Print(" 0x");
RS232_PrintHex4(frame->control);
RS232_Print(" 0x");
RS232_PrintHex4(frame->length);
for (uint8_t i = 0; i < frame->length; i++) {
RS232_Print(" 0x");
RS232_PrintHex8(frame->data[i]);
RS232_PrintHex12(frame->controller_addr);
RS232_Print(" 0x");
RS232_PrintHex12(frame->peripheral_addr);
RS232_Print(" 0x");
RS232_PrintHex4(frame->control);
RS232_Print(" 0x");
RS232_PrintHex4(frame->length);
for (uint8_t i = 0; i < frame->length; i++) {
RS232_Print(" 0x");
RS232_PrintHex8(frame->data[i]);
}
RS232_Print("\n");
}
RS232_Print("\n");
}
uint8_t CheckCmd(const AVCLAN_frame_t *frame, const uint8_t *cmd) {

View File

@ -49,6 +49,7 @@ extern uint16_t HU_ID; // Head-unit ID
extern uint8_t printAllFrames;
extern uint8_t verbose;
extern uint8_t printBinary;
typedef enum {
cm_Null = 0,
@ -97,7 +98,8 @@ typedef struct AVCLAN_frame_struct {
uint8_t AVCLAN_readframe();
uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame);
void AVCLAN_printframe(const AVCLAN_frame_t *frame);
void AVCLAN_printframe(const AVCLAN_frame_t *frame, uint8_t binary);
AVCLAN_frame_t *AVCLAN_parseframe(const uint8_t *bytes, uint8_t len);
void AVCLAN_init();
void AVCLan_Send_Status();

View File

@ -69,6 +69,13 @@ void RS232_SendByte(uint8_t Data) {
USART0_TXDATAL = Data; // send character
}
void RS232_sendbytes(const uint8_t *bytes, uint8_t len) {
const uint8_t *end = bytes + len;
while (bytes < end) {
RS232_SendByte(*bytes++);
}
}
void RS232_Print(const char *pBuf) {
register uint8_t c;
while ((c = *pBuf++)) {

View File

@ -31,12 +31,12 @@ extern uint8_t readkey;
void RS232_Init(void);
void RS232_Print_P(const char *str_addr);
void RS232_SendByte(uint8_t Data);
void RS232_sendbytes(const uint8_t *bytes, uint8_t len);
void RS232_Print(const char *pBuf);
void RS232_PrintHex4(uint8_t Data);
void RS232_PrintHex8(uint8_t Data);
void RS232_PrintHex12(uint16_t x);
void RS232_PrintDec(uint8_t Data);
void RS232_PrintDec2(uint8_t Data);
char *itoa(int i, char b[]);
#endif // __COM232_H

View File

@ -34,6 +34,7 @@
uint8_t Event;
uint8_t echoCharacters;
uint8_t readBinary;
const char const *offon[] = {"OFF", "ON"};
@ -88,6 +89,24 @@ int main() {
case '?':
print_help();
break;
case 'v':
verbose ^= 1;
RS232_Print("Verbose: ");
RS232_Print(offon[verbose]);
RS232_Print("\n");
break;
case 'X':
printBinary = 1;
RS232_Print("Binary: ");
RS232_Print(offon[1]);
RS232_Print("\n");
break;
case 'x':
printBinary = 0;
RS232_Print("Binary: ");
RS232_Print(offon[0]);
RS232_Print("\n");
break;
case 'S': // Read sequence
printAllFrames = 0;
RS232_Print("READ SEQUENCE > \n");
@ -122,24 +141,19 @@ int main() {
case 'r': // Register into the abyss
AVCLan_Register();
break;
case 'v':
verbose ^= 1;
RS232_Print("Verbose: ");
RS232_Print(offon[verbose]);
RS232_Print("\n");
break;
case 'l': // Print received messages
printAllFrames ^= 1;
RS232_Print("Logging:");
RS232_Print("Logging: ");
RS232_Print(offon[printAllFrames]);
RS232_Print("\n");
break;
case 'k': // Echo input
echoCharacters ^= 1;
RS232_Print("Echo characters:");
RS232_Print("Echo characters: ");
RS232_Print(offon[echoCharacters]);
RS232_Print("\n");
break;
case 'b':
case 'B': // Beep
data_tmp[0] = 0x00;
data_tmp[1] = 0x5E;
@ -149,56 +163,81 @@ int main() {
s_len = 5;
msg.length = s_len;
msg.broadcast = UNICAST;
msg.controller_addr = 0x110;
msg.peripheral_addr = 0x440;
AVCLAN_sendframe(&msg);
break;
case 'e': // Beep
data_tmp[0] = 0x00;
data_tmp[1] = 0x01;
data_tmp[2] = 0x11;
data_tmp[3] = 0x45;
data_tmp[4] = 0x63;
s_len = 5;
msg.length = s_len;
msg.broadcast = UNICAST;
msg.controller_addr = CD_ID;
msg.peripheral_addr = HU_ID;
AVCLAN_sendframe(&msg);
break;
#ifdef HARDWARE_DEBUG
case '1':
SetHighLow();
break;
case 'E':
if (INPUT_IS_SET) {
RS232_Print("Set/High/1\n");
} else if (INPUT_IS_CLEAR) {
RS232_Print("Unset/Low/0\n");
} else {
RS232_Print("WTF?\n");
}
break;
#endif
#ifdef SOFTWARE_DEBUG
case 'M':
AVCLan_Measure();
break;
#endif
default:
if (readSeq == 1) {
s_c[s_dig] = readkey;
s_dig++;
if (s_dig == 2) {
if (s_c[0] < ':')
s_c[0] -= 48;
else
s_c[0] -= 55;
data_tmp[s_len] = 16 * s_c[0];
if (s_c[1] < ':')
s_c[1] -= 48;
else
s_c[1] -= 55;
data_tmp[s_len] += s_c[1];
s_len++;
s_dig = 0;
s_c[0] = s_c[1] = 0;
case 0x10: // Signals binary sequence incoming
if (!readSeq && !readBinary) {
readSeq = 1;
readBinary = 1;
s_len = 0;
break;
} // else (readSeq || readBinary); fall through to default
case '\n':
if (readSeq && readBinary && data_tmp[s_len] == 0x17) {
s_len--;
AVCLAN_frame_t *frame = AVCLAN_parseframe(data_tmp, s_len);
if (frame) {
AVCLAN_sendframe(frame);
free(frame);
readSeq = 0;
readBinary = 0;
}
if (echoCharacters) {
RS232_Print("CURRENT SEQUENCE > ");
for (i = 0; i < s_len; i++) {
RS232_PrintHex8(data_tmp[i]);
RS232_SendByte(' ');
break;
} // else (readSeq || readBinary || most recent char != 0x17);
// fall through to default
default:
if (readSeq) {
if (readBinary) {
data_tmp[s_len++] = readkey;
} else {
s_c[s_dig] = readkey;
s_dig++;
if (s_dig == 2) {
if (s_c[0] < ':')
s_c[0] -= 48;
else
s_c[0] -= 55;
data_tmp[s_len] = 16 * s_c[0];
if (s_c[1] < ':')
s_c[1] -= 48;
else
s_c[1] -= 55;
data_tmp[s_len] += s_c[1];
s_len++;
s_dig = 0;
s_c[0] = s_c[1] = 0;
}
if (echoCharacters) {
RS232_Print("CURRENT SEQUENCE > ");
for (i = 0; i < s_len; i++) {
RS232_PrintHex8(data_tmp[i]);
RS232_SendByte(' ');
}
RS232_Print("\n");
}
RS232_Print("\n");
}
}
} // switch (readkey)
@ -213,6 +252,7 @@ void Setup() {
printAllFrames = 1;
echoCharacters = 1;
readBinary = 0;
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, (CLK_PRESCALE | CLK_PRESCALE_DIV));