13#ifndef _LIN_SLAVE_BASE_H_
14#define _LIN_SLAVE_BASE_H_
22#define LIN_SLAVE_BUFLEN_NAME 30
23#define LIN_SLAVE_LIN_PORT_TIMEOUT 3000
26#if defined(INCLUDE_NEOHWSERIAL)
27 #include <NeoHWSerial.h>
31#if !defined(LIN_SLAVE_DEBUG_SERIAL)
37#if !defined(LIN_SLAVE_DEBUG_LEVEL)
38 #define LIN_SLAVE_DEBUG_LEVEL 2
40#if !defined(LIN_SLAVE_DEBUG_PORT_TIMEOUT)
41 #define LIN_SLAVE_DEBUG_PORT_TIMEOUT 3000
43#if !defined(LIN_SLAVE_DEBUG_BUFSIZE)
44 #define LIN_SLAVE_DEBUG_BUFSIZE 128
49#if defined(LIN_SLAVE_DEBUG_SERIAL)
52 #define DEBUG_PRINT(level, fmt, ...) \
54 if (LIN_SLAVE_DEBUG_LEVEL >= level) { \
55 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN); \
56 LIN_SLAVE_DEBUG_SERIAL.print(F(": ")); \
57 LIN_SLAVE_DEBUG_SERIAL.print(__PRETTY_FUNCTION__); \
58 LIN_SLAVE_DEBUG_SERIAL.print(F(": ")); \
59 char debug_buf[LIN_SLAVE_DEBUG_BUFSIZE]; \
60 snprintf(debug_buf, sizeof(debug_buf), (fmt), ##__VA_ARGS__); \
61 LIN_SLAVE_DEBUG_SERIAL.println(debug_buf); \
66 #define DEBUG_PRINT_STATIC(level, fmt, ...) \
68 if (LIN_SLAVE_DEBUG_LEVEL >= level) { \
69 LIN_SLAVE_DEBUG_SERIAL.print(__PRETTY_FUNCTION__); \
70 LIN_SLAVE_DEBUG_SERIAL.print(F(": ")); \
71 char debug_buf[LIN_SLAVE_DEBUG_BUFSIZE]; \
72 snprintf(debug_buf, sizeof(debug_buf), (fmt), ##__VA_ARGS__); \
73 LIN_SLAVE_DEBUG_SERIAL.println(debug_buf); \
82 #define DEBUG_PRINT(level, fmt, ...) do {} while (0)
83 #define DEBUG_PRINT_STATIC(level, fmt, ...) do {} while (0)
111 typedef enum : uint8_t
119 typedef enum : uint8_t
127 typedef enum : uint8_t
141 typedef enum : uint8_t
220 virtual inline void _serialWrite(uint8_t buf[], uint8_t num) { (void) buf; (void) num; }
230 if (this->pinTxEN >= 0)
231 digitalWrite(this->pinTxEN, HIGH);
242 if (this->pinTxEN >= 0)
243 digitalWrite(this->pinTxEN, LOW);
253 uint32_t TimeoutRx = 1500L,
const int8_t PinTxEN = INT8_MIN);
260 virtual void begin(uint16_t Baudrate = 19200);
263 virtual void end(
void);
325 memcpy(Data, this->bufData, NumData);
LIN slave node base class.
uint32_t timeLastRx
time [us] of last received byte in frame
uint8_t idxData
current index in bufData
bool flagBreak
flag for BREAK detected. Needs to be set in Rx-ISR
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.
void getFrame(LIN_Slave_Base::frame_t &Type, uint8_t &Id, uint8_t &NumData, uint8_t Data[])
Getter for LIN frame.
uint16_t baudrate
communication baudrate [Baud]
void resetStateMachine(void)
Reset LIN state machine.
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
virtual ~LIN_Slave_Base(void)
LIN slave node destructor, here dummy. Any class with virtual functions should have virtual destructo...
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.
virtual uint8_t _serialPeek(void)
peek next byte from Rx buffer. Here dummy
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_t
LIN state machine states. Use bitmasks for fast checking multiple states.
@ 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.
LIN_Slave_Base::state_t getState(void)
Getter for LIN state machine state.
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_STATE
error in LIN state machine
@ ERROR_PID
ID parity error.
@ ERROR_MISC
misc error, should not occur
@ 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_V2
LIN protocol version 2.x.
void resetError(void)
Clear error of LIN state machine.
uint8_t pid
protected frame identifier (0..255)
LIN_Slave_Base::error_t getError(void)
Getter for LIN state machine error.
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.
User-defined callback function with data length.
uint8_t type_numData
frame type (high nibble) and number of data bytes (low nibble)
LinMessageCallback fct
frame callback function