Skip to content

Commit

Permalink
Add autobaud and make examples more complete
Browse files Browse the repository at this point in the history
  • Loading branch information
CW-B-W committed Aug 3, 2023
1 parent 9539195 commit 677470a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 4 deletions.
60 changes: 59 additions & 1 deletion examples/master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void open_lin_master_state_callback(t_open_lin_master_state new_state)
master_state_prev = master_state;
master_state = new_state;
if (new_state == OPEN_LIN_MASTER_IDLE) {
swLin.flush(); // to ensure every bytes are sent.
swLin.endFrame();
}

Expand Down Expand Up @@ -82,7 +83,7 @@ extern "C" {

void open_lin_master_dl_rx_callback(open_lin_frame_slot_t* slot)
{
Serial.printf("[open_lin_master_dl_rx_callback]\n\t");
Serial.printf("[open_lin_master_dl_rx_callback] PID=%d\n\t", (int)slot->pid);
for (int i = 0; i < slot->data_length; ++i) {
Serial.printf("0x%02X ", slot->data_ptr[i]);
}
Expand All @@ -92,6 +93,63 @@ void open_lin_master_dl_rx_callback(open_lin_frame_slot_t* slot)
void open_lin_error_handler(t_open_lin_error error_code)
{
Serial.printf("[open_lin_error_handler] error_code = %d\n", (int)error_code);

switch (error_code) {
case OPEN_LIN_NO_ERROR:
Serial.printf("\t%s\n", "OPEN_LIN_NO_ERROR");
break;

case OPEN_LIN_SLAVE_ERROR_INVALID_DATA_RX:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_DATA_RX");
break;

case OPEN_LIN_SLAVE_ERROR_INVALID_CHECKSUM:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_CHECKSUM");
break;

case OPEN_LIN_SLAVE_ERROR_PID_PARITY:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_PID_PARITY");
break;

case OPEN_LIN_SLAVE_ERROR_INVALID_SYNCH:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_SYNCH");
break;

case OPEN_LIN_SLAVE_ERROR_INVALID_BREAK:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_BREAK");
break;

case OPEN_LIN_SLAVE_ERROR_ID_NOT_FOUND:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_ID_NOT_FOUND");
break;

case OPEN_LIN_SLAVE_ERROR_HW_TX:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_HW_TX");
break;

case OPEN_LIN_MASTER_ERROR_CHECKSUM:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_CHECKSUM");
break;

case OPEN_LIN_MASTER_ERROR_HEADER_TX:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_HEADER_TX");
break;

case OPEN_LIN_MASTER_ERROR_DATA_TX:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_TX");
break;

case OPEN_LIN_MASTER_ERROR_DATA_RX:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_RX");
break;

case OPEN_LIN_MASTER_ERROR_DATA_RX_TIMEOUT:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_RX_TIMEOUT");
break;

default:
assert(0);
}
}

#ifdef __cplusplus
Expand Down
86 changes: 84 additions & 2 deletions examples/slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ void open_lin_slave_state_callback(t_open_lin_slave_state new_state)
extern "C" void app_main()
{
Serial.begin(115200);

#define LIN_AUTOBAUD

#ifdef LIN_AUTOBAUD
const uint32_t command_baud[] = {1200, 2400, 4800, 9600, 14400, 19200};
const uint32_t LIN_BAUD_MAX = 20000;
swLin.begin(LIN_BAUD_MAX);
#else
swLin.begin(9600);
#endif

l_u8 frame_data_length[] = {
5,
Expand Down Expand Up @@ -61,7 +70,22 @@ extern "C" void app_main()
open_lin_hw_break_reg = true;
open_lin_slave_rx_header(0); // To notify that the break has detected

uint8_t buf[3 + 8];
#ifdef LIN_AUTOBAUD
uint32_t autobaud = swLin.setAutoBaud(command_baud, sizeof(command_baud)/sizeof(command_baud[0]));
if (autobaud) {
Serial.printf("autobaud detection succeeded. Set baud = %u\n\n", autobaud);
open_lin_slave_rx_header(0x55); // setAutoBaud() has successfully recognized the SYNC
}
else {
Serial.printf("autobaud detection failed. baud is not changed = %u\n\n", swLin.baudRate());
open_lin_slave_rx_header(0x00); // setAutoBaud() failed to recognize SYNC. 0x00 is not expected, thus the slave is reset.
}

uint8_t buf[2 + 8]; // 2 for PID, CHECKSUM. SYNC is consumed by swLin.setAutoBaud()
#else
uint8_t buf[3 + 8]; // 3 for SYNC, PID and CHECKSUM.
#endif

while (slave_state != OPEN_LIN_SLAVE_IDLE) {
const unsigned long timeout_us = 100000; // 100ms timeout
unsigned long start_micro = micros();
Expand Down Expand Up @@ -90,7 +114,7 @@ extern "C" {

void open_lin_on_rx_frame(open_lin_frame_slot_t *slot)
{
Serial.printf("[open_lin_on_rx_frame]\n\t");
Serial.printf("[open_lin_on_rx_frame] PID=%d\n\t", (int)slot->pid);
for (int i = 0; i < slot->data_length; ++i) {
Serial.printf("0x%02X ", slot->data_ptr[i]);
}
Expand All @@ -100,6 +124,64 @@ void open_lin_on_rx_frame(open_lin_frame_slot_t *slot)
void open_lin_error_handler(t_open_lin_error error_code)
{
Serial.printf("[open_lin_error_handler] error_code = %d\n", (int)error_code);

switch (error_code) {
case OPEN_LIN_NO_ERROR:
Serial.printf("\t%s\n", "OPEN_LIN_NO_ERROR");
break;

case OPEN_LIN_SLAVE_ERROR_INVALID_DATA_RX:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_DATA_RX");
break;

case OPEN_LIN_SLAVE_ERROR_INVALID_CHECKSUM:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_CHECKSUM");
break;

case OPEN_LIN_SLAVE_ERROR_PID_PARITY:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_PID_PARITY");
break;

case OPEN_LIN_SLAVE_ERROR_INVALID_SYNCH:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_SYNCH");
break;

case OPEN_LIN_SLAVE_ERROR_INVALID_BREAK:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_BREAK");
break;

case OPEN_LIN_SLAVE_ERROR_ID_NOT_FOUND:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_ID_NOT_FOUND");
break;

case OPEN_LIN_SLAVE_ERROR_HW_TX:
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_HW_TX");
break;

case OPEN_LIN_MASTER_ERROR_CHECKSUM:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_CHECKSUM");
break;

case OPEN_LIN_MASTER_ERROR_HEADER_TX:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_HEADER_TX");
break;

case OPEN_LIN_MASTER_ERROR_DATA_TX:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_TX");
break;

case OPEN_LIN_MASTER_ERROR_DATA_RX:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_RX");
break;

case OPEN_LIN_MASTER_ERROR_DATA_RX_TIMEOUT:
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_RX_TIMEOUT");
break;

default:
assert(0);
}
Serial.println("\n");
}

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion src/ESP32-SoftwareLIN

0 comments on commit 677470a

Please sign in to comment.