14#if defined(LIN_SLAVE_DEBUG_SERIAL)
15 #warning Debug interface is active, see file 'LIN_slave_Base.h'
36 pid_tmp = (uint8_t) (ID & 0x3F);
37 tmp = (uint8_t) ((pid_tmp ^ (pid_tmp>>1) ^ (pid_tmp>>2) ^ (pid_tmp>>4)) & 0x01);
38 pid_tmp |= (uint8_t) (tmp << 6);
39 tmp = (uint8_t) (~((pid_tmp>>1) ^ (pid_tmp>>3) ^ (pid_tmp>>4) ^ (pid_tmp>>5)) & 0x01);
40 pid_tmp |= (uint8_t) (tmp << 7);
43 DEBUG_PRINT(3,
"ID=0x%02X / PID=0x%02X", (ID & 0x3F), pid_tmp);
66 if (!((this->
version ==
LIN_V1) || (this->
id == 0x3C) || (this->
id == 0x3D)))
70 for (uint8_t i = 0; i < NumData; i++)
72 chk += (uint16_t) (Data[i]);
76 chk = (uint8_t)(0xFF - ((uint8_t) chk));
79 DEBUG_PRINT(3,
"CHK=0x%02X", chk);
133 memcpy(this->
nameLIN, NameLIN, LIN_SLAVE_BUFLEN_NAME);
140 for (uint8_t i = 0; i < 64; i++)
149 for (uint8_t i = 0; i < 9; i++)
166 #if defined(LIN_SLAVE_DEBUG_SERIAL)
167 LIN_SLAVE_DEBUG_SERIAL.begin(115200);
168 #if defined(LIN_SLAVE_DEBUG_PORT_TIMEOUT) && (LIN_SLAVE_DEBUG_PORT_TIMEOUT > 0)
169 uint32_t startMillis = millis();
170 while ((!LIN_SLAVE_DEBUG_SERIAL) && (millis() - startMillis < LIN_SLAVE_DEBUG_PORT_TIMEOUT));
172 while (!LIN_SLAVE_DEBUG_SERIAL);
186 digitalWrite(this->
pinTxEN, LOW);
187 pinMode(this->
pinTxEN, OUTPUT);
191 DEBUG_PRINT(2,
"BR=%d", (
int) Baudrate);
234 DEBUG_PRINT(2,
"registered ID=0x%02X / PID=0x%02X", ID, this->
_calculatePID(ID));
257 DEBUG_PRINT(2,
"registered ID=0x%02X / PID=0x%02X", ID, this->
_calculatePID(ID));
290 DEBUG_PRINT(1,
"frame timeout after %ldus", (
long) (micros() - this->
timeLastRx));
312 DEBUG_PRINT(3,
"BREAK detected");
346 if (byteReceived == 0x55)
349 DEBUG_PRINT(3,
"SYNC detected");
370 DEBUG_PRINT(1,
"SYNC error, received 0x%02X", byteReceived);
384 this->
pid = byteReceived;
387 this->
id = this->
pid & 0x3F;
404 DEBUG_PRINT(1,
"PID parity error, received 0x%02X, calculated 0x%02X", this->
pid, this->
_calculatePID(this->
id));
420 DEBUG_PRINT(2,
"handle slave response PID 0x%02X", this->
pid);
457 DEBUG_PRINT(2,
"drop frame PID 0x%02X", this->
pid);
498 DEBUG_PRINT(1,
"echo error, received 0x%02X, expected 0x%02X", byteReceived, this->
bufData[(this->
idxData)-1]);
524 if (byteReceived == chk_calc)
530 DEBUG_PRINT(2,
"handle master request PID 0x%02X", this->
pid);
549 DEBUG_PRINT(1,
"CHK error, received 0x%02X, calculated 0x%02X", byteReceived, chk_calc);
580 DEBUG_PRINT(1,
"illegal state %d, this should never happen...", this->
state);
Base class for LIN slave emulation (non-functional)
uint32_t timeLastRx
time [us] of last received byte in frame
uint8_t idxData
current index in bufData
LIN_Slave_Base::error_t error
error state. Is latched until cleared
void registerSlaveResponseHandler(uint8_t ID, LIN_Slave_Base::LinMessageCallback Fct, uint8_t NumData)
Attach user callback function for slave response frame.
uint16_t baudrate
communication baudrate [Baud]
virtual void _serialWrite(uint8_t buf[], uint8_t num)
write bytes to Tx buffer. Here dummy
int8_t pinTxEN
optional Tx direction pin, e.g. for LIN via RS485
frame_t
LIN frame type. Use high nibble for type, low nibble for number of data bytes -> minimize callback[] ...
@ SLAVE_RESPONSE
LIN slave response frame.
@ MASTER_REQUEST
LIN master request frame.
uint8_t bufData[9]
buffer for data bytes (max. 8B) + checksum
uint32_t timeoutRx
timeout [us] for bytes in frame
virtual bool _getBreakFlag(void)
Get break detection flag. Is hardware dependent.
uint8_t numData
number of data bytes in frame
LIN_Slave_Base::frame_t type
frame type (master request or slave response)
@ STATE_DONE
frame is completed
@ STATE_WAIT_FOR_PID
SYNC received, wait for frame PID.
@ STATE_OFF
LIN interface closed.
@ STATE_WAIT_FOR_BREAK
no LIN transmission ongoing, wait for BRK
@ STATE_WAIT_FOR_SYNC
BRK received, wait for SYNC.
@ STATE_RECEIVING_ECHO
receiving slave response echo
@ STATE_RECEIVING_DATA
receiving master request data
@ STATE_WAIT_FOR_CHK
waiting for checksum
LIN_Slave_Base::version_t version
LIN protocol version.
uint8_t id
unprotected frame identifier (0..63)
void _disableTransmitter(void)
Disable RS485 transmitter (DE=low)
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
void registerMasterRequestHandler(uint8_t ID, LIN_Slave_Base::LinMessageCallback Fct, uint8_t NumData)
Attach user callback function for master request frame.
error_t
LIN error codes. Use bitmasks, as error is latched. Use same as LIN_master_portable.
@ ERROR_SYNC
error in SYNC (not 0x55)
@ ERROR_TIMEOUT
frame timeout error
@ ERROR_ECHO
error reading response echo
@ ERROR_PID
ID parity error.
@ ERROR_CHK
LIN checksum error.
virtual uint8_t _serialRead(void)
read next byte from Rx buffer. Here dummy
uint8_t _calculateChecksum(uint8_t NumData, uint8_t Data[])
Calculate LIN frame checksum.
char nameLIN[LIN_SLAVE_BUFLEN_NAME]
LIN node name, e.g. for debug.
void(* LinMessageCallback)(uint8_t numData, uint8_t *data)
Type for frame callback function.
uint8_t _calculatePID(uint8_t ID)
Calculate protected frame ID.
virtual void _resetBreakFlag(void)
Clear break detection flag. Is hardware dependent.
version_t
LIN protocol version.
@ LIN_V1
LIN protocol version 1.x.
LIN_Slave_Base(LIN_Slave_Base::version_t Version=LIN_Slave_Base::LIN_V2, const char NameLIN[]="Slave", uint32_t TimeoutRx=1500L, const int8_t PinTxEN=INT8_MIN)
LIN slave node constructor.
uint8_t pid
protected frame identifier (0..255)
LIN_Slave_Base::state_t state
status of LIN state machine
void _enableTransmitter(void)
Enable RS485 transmitter (DE=high)
virtual void end(void)
Close serial interface.
virtual bool available(void)
check if a byte is available in Rx buffer. Here dummy
LIN_Slave_Base::callback_t callback[64]
array of user callback functions for IDs 0x00..0x3F
virtual void handler(void)
Handle LIN protocol and call user-defined frame callbacks.
uint8_t type_numData
frame type (high nibble) and number of data bytes (low nibble)
LinMessageCallback fct
frame callback function