| LIN_slave_portable_Arduino 1.4 Arduino library for Local Interconnect Network slave node emulation

Loading...
Searching...
No Matches
LIN_slave_Base.h
Go to the documentation of this file.
1
10/*-----------------------------------------------------------------------------
11 MODULE DEFINITION FOR MULTIPLE INCLUSION
12-----------------------------------------------------------------------------*/
13#ifndef _LIN_SLAVE_BASE_H_
14#define _LIN_SLAVE_BASE_H_
15
16
17/*-----------------------------------------------------------------------------
18 GLOBAL DEFINES
19-----------------------------------------------------------------------------*/
20
21// misc parameters
22#define LIN_SLAVE_BUFLEN_NAME 30
23
24// optional LIN debug output @ 115.2kBaud. When using together with NeoHWSerial on AVR must use NeoSerialx to avoid linker conflict
25#if !defined(LIN_SLAVE_DEBUG_SERIAL)
26 //#define LIN_SLAVE_DEBUG_SERIAL Serial //!< serial interface used for debug output. Comment out for none
27 //#define LIN_SLAVE_DEBUG_SERIAL NeoSerial //!< serial interface used for debug output (required for AVR). Comment out for none
28 //#include <NeoHWSerial.h> // comment in/out together with previous line
29#endif
30#if !defined(LIN_SLAVE_DEBUG_LEVEL)
31 //#define LIN_SLAVE_DEBUG_LEVEL 2 //!< debug verbosity 0..3 (1=errors only, 3=chatty)
32#endif
33
34/*-----------------------------------------------------------------------------
35 INCLUDE FILES
36-----------------------------------------------------------------------------*/
37
38// generic Arduino functions
39#include <Arduino.h>
40
41
42/*-----------------------------------------------------------------------------
43 GLOBAL CLASS
44-----------------------------------------------------------------------------*/
45
51 class LIN_Slave_Base
52{
53 // PUBLIC TYPEDEFS
54 public:
55
57 typedef enum : uint8_t
58 {
59 LIN_V1 = 1,
60 LIN_V2 = 2
61 } version_t;
62
63
65 typedef enum : uint8_t
66 {
67 MASTER_REQUEST = 0x10,
68 SLAVE_RESPONSE = 0x20
69 } frame_t;
70
71
73 typedef enum : uint8_t
74 {
75 STATE_OFF = 0x01,
76 STATE_WAIT_FOR_BREAK = 0x02,
77 STATE_WAIT_FOR_SYNC = 0x04,
78 STATE_WAIT_FOR_PID = 0x08,
79 STATE_RECEIVING_DATA = 0x10,
80 STATE_RECEIVING_ECHO = 0x20,
81 STATE_WAIT_FOR_CHK = 0x40,
82 STATE_DONE = 0x80
83 } state_t;
84
85
87 typedef enum : uint8_t
88 {
89 NO_ERROR = 0x00,
90 ERROR_STATE = 0x01,
91 ERROR_ECHO = 0x02,
92 ERROR_TIMEOUT = 0x04,
93 ERROR_CHK = 0x08,
94 ERROR_SYNC = 0x10,
95 ERROR_PID = 0x20,
96 ERROR_MISC = 0x80
97 } error_t;
98
99
100 // PROTECTED TYPEDEFS
101 protected:
102
104 typedef void (*LinMessageCallback)(uint8_t numData, uint8_t* data);
105
107 typedef struct
108 {
109 uint8_t type_numData;
110 LinMessageCallback fct;
111 } callback_t;
112
113
114 // PROTECTED VARIABLES
115 protected:
116
117 // node properties
118 int8_t pinTxEN;
119 uint16_t baudrate;
120 LIN_Slave_Base::version_t version;
121 LIN_Slave_Base::state_t state;
122 LIN_Slave_Base::error_t error;
123 bool flagBreak;
124 LIN_Slave_Base::callback_t callback[64];
125
126 // latest frame properties
127 uint8_t pid;
128 uint8_t id;
129 LIN_Slave_Base::frame_t type;
130 uint8_t numData;
131 uint8_t bufData[9];
132 uint8_t idxData;
133 uint32_t timeoutRx;
134 uint32_t timeLastRx;
135
136
137 // PUBLIC VARIABLES
138 public:
139
140 char nameLIN[LIN_SLAVE_BUFLEN_NAME];
141
142
143 // PROTECTED METHODS
144 protected:
145
147 uint8_t _calculatePID(uint8_t ID);
148
150 uint8_t _calculateChecksum(uint8_t NumData, uint8_t Data[]);
151
153 virtual bool _getBreakFlag(void);
154
156 virtual void _resetBreakFlag(void);
157
158
160 virtual inline uint8_t _serialPeek(void) { return 0x00; }
161
163 virtual inline uint8_t _serialRead(void) { return 0x00; }
164
166 virtual inline void _serialWrite(uint8_t buf[], uint8_t num) { (void) buf; (void) num; }
167
168
170 inline void _enableTransmitter(void)
171 {
172 // print debug message (debug level 3)
173 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 3)
174 LIN_SLAVE_DEBUG_SERIAL.println("LIN_Slave_Base::_enableTransmitter()");
175 #endif
176
177 // enable tranmitter
178 if (this->pinTxEN >= 0)
179 digitalWrite(this->pinTxEN, HIGH);
180
181 } // _enableTransmitter()
182
184 inline void _disableTransmitter(void)
185 {
186 // print debug message (debug level 3)
187 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 3)
188 LIN_SLAVE_DEBUG_SERIAL.println("LIN_Slave_Base::_disableTransmitter()");
189 #endif
190
191 // disable tranmitter
192 if (this->pinTxEN >= 0)
193 digitalWrite(this->pinTxEN, LOW);
194
195 } // _disableTransmitter()
196
197
198 // PUBLIC METHODS
199 public:
200
202 LIN_Slave_Base(LIN_Slave_Base::version_t Version = LIN_Slave_Base::LIN_V2, const char NameLIN[] = "Slave",
203 uint32_t TimeoutRx = 1500L, const int8_t PinTxEN = INT8_MIN);
204
206 virtual ~LIN_Slave_Base(void) {};
207
208
210 virtual void begin(uint16_t Baudrate = 19200);
211
213 virtual void end(void);
214
216 virtual inline bool available(void) { return false; }
217
218
220 inline void resetStateMachine(void)
221 {
222 // print debug message (debug level 3)
223 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 3)
224 LIN_SLAVE_DEBUG_SERIAL.println("LIN_Slave_Base::resetStateMachine()");
225 #endif
226
227 // reset state
228 this->state = LIN_Slave_Base::STATE_WAIT_FOR_BREAK;
229
230 } // resetStateMachine()
231
233 inline LIN_Slave_Base::state_t getState(void)
234 {
235 // print debug message (debug level 3)
236 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 3)
237 LIN_SLAVE_DEBUG_SERIAL.println("LIN_Slave_Base::getState()");
238 #endif
239
240 // return state
241 return this->state;
242
243 } // getState()
244
245
247 inline void resetError(void)
248 {
249 // print debug message (debug level 3)
250 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 3)
251 LIN_SLAVE_DEBUG_SERIAL.println("LIN_Slave_Base::resetError()");
252 #endif
253
254 // reset error
255 this->error = LIN_Slave_Base::NO_ERROR;
256
257 } // resetError()
258
260 inline LIN_Slave_Base::error_t getError(void)
261 {
262 // print debug message (debug level 3)
263 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 3)
264 LIN_SLAVE_DEBUG_SERIAL.println("LIN_Slave_Base::getError()");
265 #endif
266
267 // return error
268 return this->error;
269
270 } // getError()
271
272
274 inline void getFrame(LIN_Slave_Base::frame_t &Type, uint8_t &Id, uint8_t &NumData, uint8_t Data[])
275 {
276 // print debug message (debug level 3)
277 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 3)
278 LIN_SLAVE_DEBUG_SERIAL.println("LIN_Slave_Base::getFrame()");
279 #endif
280
281 noInterrupts(); // for data consistency temporarily disable ISRs
282 Type = this->type; // frame type
283 Id = this->id; // frame ID
284 NumData = this->numData; // number of data bytes (excl. BREAK, SYNC, ID, CHK)
285 memcpy(Data, this->bufData, NumData); // copy data bytes w/o checksum
286 interrupts(); // re-enable ISRs
287
288 } // getFrame()
289
290
292 void registerMasterRequestHandler(uint8_t ID, LIN_Slave_Base::LinMessageCallback Fct, uint8_t NumData);
293
295 void registerSlaveResponseHandler(uint8_t ID, LIN_Slave_Base::LinMessageCallback Fct, uint8_t NumData);
296
297
299 virtual void handler(void);
300
301}; // class LIN_Slave_Base
302
303/*-----------------------------------------------------------------------------
304 END OF MODULE DEFINITION FOR MULTIPLE INLUSION
305-----------------------------------------------------------------------------*/
306#endif // _LIN_SLAVE_BASE_H_
307
308/*-----------------------------------------------------------------------------
309 END OF FILE
310-----------------------------------------------------------------------------*/
LIN_Slave_Base
LIN slave node base class.
Definition LIN_slave_Base.h:52
LIN_Slave_Base::timeLastRx
uint32_t timeLastRx
time [us] of last received byte in frame
Definition LIN_slave_Base.h:134
LIN_Slave_Base::idxData
uint8_t idxData
current index in bufData
Definition LIN_slave_Base.h:132
LIN_Slave_Base::flagBreak
bool flagBreak
flag for BREAK detected. Needs to be set in Rx-ISR
Definition LIN_slave_Base.h:123
LIN_Slave_Base::error
LIN_Slave_Base::error_t error
error state. Is latched until cleared
Definition LIN_slave_Base.h:122
LIN_Slave_Base::registerSlaveResponseHandler
void registerSlaveResponseHandler(uint8_t ID, LIN_Slave_Base::LinMessageCallback Fct, uint8_t NumData)
Attach user callback function for slave response frame.
Definition LIN_slave_Base.cpp:263
LIN_Slave_Base::getFrame
void getFrame(LIN_Slave_Base::frame_t &Type, uint8_t &Id, uint8_t &NumData, uint8_t Data[])
Getter for LIN frame.
Definition LIN_slave_Base.h:274
LIN_Slave_Base::baudrate
uint16_t baudrate
communication baudrate [Baud]
Definition LIN_slave_Base.h:119
LIN_Slave_Base::resetStateMachine
void resetStateMachine(void)
Reset LIN state machine.
Definition LIN_slave_Base.h:220
LIN_Slave_Base::_serialWrite
virtual void _serialWrite(uint8_t buf[], uint8_t num)
write bytes to Tx buffer. Here dummy
Definition LIN_slave_Base.h:166
LIN_Slave_Base::pinTxEN
int8_t pinTxEN
optional Tx direction pin, e.g. for LIN via RS485
Definition LIN_slave_Base.h:118
LIN_Slave_Base::~LIN_Slave_Base
virtual ~LIN_Slave_Base(void)
LIN slave node destructor, here dummy. Any class with virtual functions should have virtual destructo...
Definition LIN_slave_Base.h:206
LIN_Slave_Base::frame_t
frame_t
LIN frame type. Use high nibble for type, low nibble for number of data bytes -> minimize callback[] ...
Definition LIN_slave_Base.h:66
LIN_Slave_Base::SLAVE_RESPONSE
@ SLAVE_RESPONSE
LIN slave response frame.
Definition LIN_slave_Base.h:68
LIN_Slave_Base::MASTER_REQUEST
@ MASTER_REQUEST
LIN master request frame.
Definition LIN_slave_Base.h:67
LIN_Slave_Base::_serialPeek
virtual uint8_t _serialPeek(void)
peek next byte from Rx buffer. Here dummy
Definition LIN_slave_Base.h:160
LIN_Slave_Base::bufData
uint8_t bufData[9]
buffer for data bytes (max. 8B) + checksum
Definition LIN_slave_Base.h:131
LIN_Slave_Base::timeoutRx
uint32_t timeoutRx
timeout [us] for bytes in frame
Definition LIN_slave_Base.h:133
LIN_Slave_Base::_getBreakFlag
virtual bool _getBreakFlag(void)
Get break detection flag. Is hardware dependent.
Definition LIN_slave_Base.cpp:93
LIN_Slave_Base::numData
uint8_t numData
number of data bytes in frame
Definition LIN_slave_Base.h:130
LIN_Slave_Base::type
LIN_Slave_Base::frame_t type
frame type (master request or slave response)
Definition LIN_slave_Base.h:129
LIN_Slave_Base::state_t
state_t
LIN state machine states. Use bitmasks for fast checking multiple states.
Definition LIN_slave_Base.h:74
LIN_Slave_Base::STATE_DONE
@ STATE_DONE
frame is completed
Definition LIN_slave_Base.h:82
LIN_Slave_Base::STATE_WAIT_FOR_PID
@ STATE_WAIT_FOR_PID
SYNC received, wait for frame PID.
Definition LIN_slave_Base.h:78
LIN_Slave_Base::STATE_OFF
@ STATE_OFF
LIN interface closed.
Definition LIN_slave_Base.h:75
LIN_Slave_Base::STATE_WAIT_FOR_BREAK
@ STATE_WAIT_FOR_BREAK
no LIN transmission ongoing, wait for BRK
Definition LIN_slave_Base.h:76
LIN_Slave_Base::STATE_WAIT_FOR_SYNC
@ STATE_WAIT_FOR_SYNC
BRK received, wait for SYNC.
Definition LIN_slave_Base.h:77
LIN_Slave_Base::STATE_RECEIVING_ECHO
@ STATE_RECEIVING_ECHO
receiving slave response echo
Definition LIN_slave_Base.h:80
LIN_Slave_Base::STATE_RECEIVING_DATA
@ STATE_RECEIVING_DATA
receiving master request data
Definition LIN_slave_Base.h:79
LIN_Slave_Base::STATE_WAIT_FOR_CHK
@ STATE_WAIT_FOR_CHK
waiting for checksum
Definition LIN_slave_Base.h:81
LIN_Slave_Base::version
LIN_Slave_Base::version_t version
LIN protocol version.
Definition LIN_slave_Base.h:120
LIN_Slave_Base::id
uint8_t id
unprotected frame identifier
Definition LIN_slave_Base.h:128
LIN_Slave_Base::_disableTransmitter
void _disableTransmitter(void)
Disable RS485 transmitter (DE=low)
Definition LIN_slave_Base.h:184
LIN_Slave_Base::begin
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_slave_Base.cpp:171
LIN_Slave_Base::registerMasterRequestHandler
void registerMasterRequestHandler(uint8_t ID, LIN_Slave_Base::LinMessageCallback Fct, uint8_t NumData)
Attach user callback function for master request frame.
Definition LIN_slave_Base.cpp:235
LIN_Slave_Base::getState
LIN_Slave_Base::state_t getState(void)
Getter for LIN state machine state.
Definition LIN_slave_Base.h:233
LIN_Slave_Base::error_t
error_t
LIN error codes. Use bitmasks, as error is latched. Use same as LIN_master_portable.
Definition LIN_slave_Base.h:88
LIN_Slave_Base::ERROR_SYNC
@ ERROR_SYNC
error in SYNC (not 0x55)
Definition LIN_slave_Base.h:94
LIN_Slave_Base::ERROR_TIMEOUT
@ ERROR_TIMEOUT
frame timeout error
Definition LIN_slave_Base.h:92
LIN_Slave_Base::ERROR_ECHO
@ ERROR_ECHO
error reading response echo
Definition LIN_slave_Base.h:91
LIN_Slave_Base::ERROR_STATE
@ ERROR_STATE
error in LIN state machine
Definition LIN_slave_Base.h:90
LIN_Slave_Base::ERROR_PID
@ ERROR_PID
ID parity error.
Definition LIN_slave_Base.h:95
LIN_Slave_Base::ERROR_MISC
@ ERROR_MISC
misc error, should not occur
Definition LIN_slave_Base.h:96
LIN_Slave_Base::ERROR_CHK
@ ERROR_CHK
LIN checksum error.
Definition LIN_slave_Base.h:93
LIN_Slave_Base::NO_ERROR
@ NO_ERROR
no error
Definition LIN_slave_Base.h:89
LIN_Slave_Base::_serialRead
virtual uint8_t _serialRead(void)
read next byte from Rx buffer. Here dummy
Definition LIN_slave_Base.h:163
LIN_Slave_Base::_calculateChecksum
uint8_t _calculateChecksum(uint8_t NumData, uint8_t Data[])
Calculate LIN frame checksum.
Definition LIN_slave_Base.cpp:56
LIN_Slave_Base::nameLIN
char nameLIN[LIN_SLAVE_BUFLEN_NAME]
LIN node name, e.g. for debug.
Definition LIN_slave_Base.h:140
LIN_Slave_Base::LinMessageCallback
void(* LinMessageCallback)(uint8_t numData, uint8_t *data)
Type for frame callback function.
Definition LIN_slave_Base.h:104
LIN_Slave_Base::_calculatePID
uint8_t _calculatePID(uint8_t ID)
Calculate protected frame ID.
Definition LIN_slave_Base.cpp:30
LIN_Slave_Base::_resetBreakFlag
virtual void _resetBreakFlag(void)
Clear break detection flag. Is hardware dependent.
Definition LIN_slave_Base.cpp:106
LIN_Slave_Base::version_t
version_t
LIN protocol version.
Definition LIN_slave_Base.h:58
LIN_Slave_Base::LIN_V1
@ LIN_V1
LIN protocol version 1.x.
Definition LIN_slave_Base.h:59
LIN_Slave_Base::LIN_V2
@ LIN_V2
LIN protocol version 2.x.
Definition LIN_slave_Base.h:60
LIN_Slave_Base::resetError
void resetError(void)
Clear error of LIN state machine.
Definition LIN_slave_Base.h:247
LIN_Slave_Base::pid
uint8_t pid
protected frame identifier
Definition LIN_slave_Base.h:127
LIN_Slave_Base::getError
LIN_Slave_Base::error_t getError(void)
Getter for LIN state machine error.
Definition LIN_slave_Base.h:260
LIN_Slave_Base::state
LIN_Slave_Base::state_t state
status of LIN state machine
Definition LIN_slave_Base.h:121
LIN_Slave_Base::_enableTransmitter
void _enableTransmitter(void)
Enable RS485 transmitter (DE=high)
Definition LIN_slave_Base.h:170
LIN_Slave_Base::end
virtual void end(void)
Close serial interface.
Definition LIN_slave_Base.cpp:209
LIN_Slave_Base::available
virtual bool available(void)
check if a byte is available in Rx buffer. Here dummy
Definition LIN_slave_Base.h:216
LIN_Slave_Base::callback
LIN_Slave_Base::callback_t callback[64]
array of user callback functions for IDs 0x00..0x3F
Definition LIN_slave_Base.h:124
LIN_Slave_Base::handler
virtual void handler(void)
Handle LIN protocol and call user-defined frame callbacks.
Definition LIN_slave_Base.cpp:288
LIN_Slave_Base::callback_t
User-defined callback function with data length.
Definition LIN_slave_Base.h:108
LIN_Slave_Base::callback_t::type_numData
uint8_t type_numData
frame type (high nibble) and number of data bytes (low nibble)
Definition LIN_slave_Base.h:109
LIN_Slave_Base::callback_t::fct
LinMessageCallback fct
frame callback function
Definition LIN_slave_Base.h:110