Skip to content

Commit 677470a

Browse files
committed
Add autobaud and make examples more complete
1 parent 9539195 commit 677470a

File tree

3 files changed

+144
-4
lines changed

3 files changed

+144
-4
lines changed

examples/master.cpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void open_lin_master_state_callback(t_open_lin_master_state new_state)
1818
master_state_prev = master_state;
1919
master_state = new_state;
2020
if (new_state == OPEN_LIN_MASTER_IDLE) {
21+
swLin.flush(); // to ensure every bytes are sent.
2122
swLin.endFrame();
2223
}
2324

@@ -82,7 +83,7 @@ extern "C" {
8283

8384
void open_lin_master_dl_rx_callback(open_lin_frame_slot_t* slot)
8485
{
85-
Serial.printf("[open_lin_master_dl_rx_callback]\n\t");
86+
Serial.printf("[open_lin_master_dl_rx_callback] PID=%d\n\t", (int)slot->pid);
8687
for (int i = 0; i < slot->data_length; ++i) {
8788
Serial.printf("0x%02X ", slot->data_ptr[i]);
8889
}
@@ -92,6 +93,63 @@ void open_lin_master_dl_rx_callback(open_lin_frame_slot_t* slot)
9293
void open_lin_error_handler(t_open_lin_error error_code)
9394
{
9495
Serial.printf("[open_lin_error_handler] error_code = %d\n", (int)error_code);
96+
97+
switch (error_code) {
98+
case OPEN_LIN_NO_ERROR:
99+
Serial.printf("\t%s\n", "OPEN_LIN_NO_ERROR");
100+
break;
101+
102+
case OPEN_LIN_SLAVE_ERROR_INVALID_DATA_RX:
103+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_DATA_RX");
104+
break;
105+
106+
case OPEN_LIN_SLAVE_ERROR_INVALID_CHECKSUM:
107+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_CHECKSUM");
108+
break;
109+
110+
case OPEN_LIN_SLAVE_ERROR_PID_PARITY:
111+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_PID_PARITY");
112+
break;
113+
114+
case OPEN_LIN_SLAVE_ERROR_INVALID_SYNCH:
115+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_SYNCH");
116+
break;
117+
118+
case OPEN_LIN_SLAVE_ERROR_INVALID_BREAK:
119+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_BREAK");
120+
break;
121+
122+
case OPEN_LIN_SLAVE_ERROR_ID_NOT_FOUND:
123+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_ID_NOT_FOUND");
124+
break;
125+
126+
case OPEN_LIN_SLAVE_ERROR_HW_TX:
127+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_HW_TX");
128+
break;
129+
130+
case OPEN_LIN_MASTER_ERROR_CHECKSUM:
131+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_CHECKSUM");
132+
break;
133+
134+
case OPEN_LIN_MASTER_ERROR_HEADER_TX:
135+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_HEADER_TX");
136+
break;
137+
138+
case OPEN_LIN_MASTER_ERROR_DATA_TX:
139+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_TX");
140+
break;
141+
142+
case OPEN_LIN_MASTER_ERROR_DATA_RX:
143+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_RX");
144+
break;
145+
146+
case OPEN_LIN_MASTER_ERROR_DATA_RX_TIMEOUT:
147+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_RX_TIMEOUT");
148+
break;
149+
150+
default:
151+
assert(0);
152+
}
95153
}
96154

97155
#ifdef __cplusplus

examples/slave.cpp

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ void open_lin_slave_state_callback(t_open_lin_slave_state new_state)
3131
extern "C" void app_main()
3232
{
3333
Serial.begin(115200);
34+
35+
#define LIN_AUTOBAUD
36+
37+
#ifdef LIN_AUTOBAUD
38+
const uint32_t command_baud[] = {1200, 2400, 4800, 9600, 14400, 19200};
39+
const uint32_t LIN_BAUD_MAX = 20000;
40+
swLin.begin(LIN_BAUD_MAX);
41+
#else
3442
swLin.begin(9600);
43+
#endif
3544

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

64-
uint8_t buf[3 + 8];
73+
#ifdef LIN_AUTOBAUD
74+
uint32_t autobaud = swLin.setAutoBaud(command_baud, sizeof(command_baud)/sizeof(command_baud[0]));
75+
if (autobaud) {
76+
Serial.printf("autobaud detection succeeded. Set baud = %u\n\n", autobaud);
77+
open_lin_slave_rx_header(0x55); // setAutoBaud() has successfully recognized the SYNC
78+
}
79+
else {
80+
Serial.printf("autobaud detection failed. baud is not changed = %u\n\n", swLin.baudRate());
81+
open_lin_slave_rx_header(0x00); // setAutoBaud() failed to recognize SYNC. 0x00 is not expected, thus the slave is reset.
82+
}
83+
84+
uint8_t buf[2 + 8]; // 2 for PID, CHECKSUM. SYNC is consumed by swLin.setAutoBaud()
85+
#else
86+
uint8_t buf[3 + 8]; // 3 for SYNC, PID and CHECKSUM.
87+
#endif
88+
6589
while (slave_state != OPEN_LIN_SLAVE_IDLE) {
6690
const unsigned long timeout_us = 100000; // 100ms timeout
6791
unsigned long start_micro = micros();
@@ -90,7 +114,7 @@ extern "C" {
90114

91115
void open_lin_on_rx_frame(open_lin_frame_slot_t *slot)
92116
{
93-
Serial.printf("[open_lin_on_rx_frame]\n\t");
117+
Serial.printf("[open_lin_on_rx_frame] PID=%d\n\t", (int)slot->pid);
94118
for (int i = 0; i < slot->data_length; ++i) {
95119
Serial.printf("0x%02X ", slot->data_ptr[i]);
96120
}
@@ -100,6 +124,64 @@ void open_lin_on_rx_frame(open_lin_frame_slot_t *slot)
100124
void open_lin_error_handler(t_open_lin_error error_code)
101125
{
102126
Serial.printf("[open_lin_error_handler] error_code = %d\n", (int)error_code);
127+
128+
switch (error_code) {
129+
case OPEN_LIN_NO_ERROR:
130+
Serial.printf("\t%s\n", "OPEN_LIN_NO_ERROR");
131+
break;
132+
133+
case OPEN_LIN_SLAVE_ERROR_INVALID_DATA_RX:
134+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_DATA_RX");
135+
break;
136+
137+
case OPEN_LIN_SLAVE_ERROR_INVALID_CHECKSUM:
138+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_CHECKSUM");
139+
break;
140+
141+
case OPEN_LIN_SLAVE_ERROR_PID_PARITY:
142+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_PID_PARITY");
143+
break;
144+
145+
case OPEN_LIN_SLAVE_ERROR_INVALID_SYNCH:
146+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_SYNCH");
147+
break;
148+
149+
case OPEN_LIN_SLAVE_ERROR_INVALID_BREAK:
150+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_INVALID_BREAK");
151+
break;
152+
153+
case OPEN_LIN_SLAVE_ERROR_ID_NOT_FOUND:
154+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_ID_NOT_FOUND");
155+
break;
156+
157+
case OPEN_LIN_SLAVE_ERROR_HW_TX:
158+
Serial.printf("\t%s\n", "OPEN_LIN_SLAVE_ERROR_HW_TX");
159+
break;
160+
161+
case OPEN_LIN_MASTER_ERROR_CHECKSUM:
162+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_CHECKSUM");
163+
break;
164+
165+
case OPEN_LIN_MASTER_ERROR_HEADER_TX:
166+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_HEADER_TX");
167+
break;
168+
169+
case OPEN_LIN_MASTER_ERROR_DATA_TX:
170+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_TX");
171+
break;
172+
173+
case OPEN_LIN_MASTER_ERROR_DATA_RX:
174+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_RX");
175+
break;
176+
177+
case OPEN_LIN_MASTER_ERROR_DATA_RX_TIMEOUT:
178+
Serial.printf("\t%s\n", "OPEN_LIN_MASTER_ERROR_DATA_RX_TIMEOUT");
179+
break;
180+
181+
default:
182+
assert(0);
183+
}
184+
Serial.println("\n");
103185
}
104186

105187
#ifdef __cplusplus

0 commit comments

Comments
 (0)