LIN_master_portable_Arduino 1.4
Arduino library for Local Interconnect Network master node emulation
Loading...
Searching...
No Matches
LIN_master_Base.h
Go to the documentation of this file.
1
10/*-----------------------------------------------------------------------------
11 MODULE DEFINITION FOR MULTIPLE INCLUSION
12-----------------------------------------------------------------------------*/
13#ifndef _LIN_MASTER_BASE_H_
14#define _LIN_MASTER_BASE_H_
15
16
17/*-----------------------------------------------------------------------------
18 GLOBAL DEFINES
19-----------------------------------------------------------------------------*/
20
21// misc parameters
22#define LIN_MASTER_BUFLEN_NAME 30
23#define LIN_MASTER_LIN_PORT_TIMEOUT 3000
24
25// required for CI test environment. Call arduino-cli with "-DINCLUDE_NEOHWSERIAL"
26#if defined(INCLUDE_NEOHWSERIAL)
27 #include <NeoHWSerial.h>
28#endif
29
30// optional debug output @ 115.2kBaud. Comment out for none. When using together with NeoHWSerial on AVR must use NeoSerialx to avoid linker conflict
31#if !defined(LIN_MASTER_DEBUG_SERIAL)
32 //#define LIN_MASTER_DEBUG_SERIAL Serial //!< serial interface used for debug output
33 //#define LIN_MASTER_DEBUG_SERIAL NeoSerial //!< serial interface used for debug output (optional on AVR, not together with HardwareSerial!)
34 //#include <NeoHWSerial.h> // comment in/out together with previous line
35 //#define LIN_MASTER_DEBUG_SERIAL SerialUSB //!< serial interface used for debug output (optional on Due)
36#endif
37#if !defined(LIN_MASTER_DEBUG_LEVEL)
38 #define LIN_MASTER_DEBUG_LEVEL 2
39#endif
40#if !defined(LIN_MASTER_DEBUG_PORT_TIMEOUT)
41 #define LIN_MASTER_DEBUG_PORT_TIMEOUT 3000
42#endif
43#if !defined(LIN_MASTER_DEBUG_BUFSIZE)
44 #define LIN_MASTER_DEBUG_BUFSIZE 128
45#endif
46
47// define logging macros for optional debug output.
48// Use with printf() format like: DEBUG_PRINT(2, "Text=%s, Value=%d", text, value);
49#if defined(LIN_MASTER_DEBUG_SERIAL)
50
51 // debug output macro for normal class methods
52 #define DEBUG_PRINT(level, fmt, ...) \
53 do { \
54 if (LIN_MASTER_DEBUG_LEVEL >= level) { \
55 LIN_MASTER_DEBUG_SERIAL.print(this->nameLIN); \
56 LIN_MASTER_DEBUG_SERIAL.print(F(": ")); \
57 LIN_MASTER_DEBUG_SERIAL.print(__PRETTY_FUNCTION__); \
58 LIN_MASTER_DEBUG_SERIAL.print(F(": ")); \
59 char debug_buf[LIN_MASTER_DEBUG_BUFSIZE]; \
60 snprintf(debug_buf, sizeof(debug_buf), (fmt), ##__VA_ARGS__); \
61 LIN_MASTER_DEBUG_SERIAL.println(debug_buf); \
62 } \
63 } while(0)
64
65 // debug output macro for static class methods and functions
66 #define DEBUG_PRINT_STATIC(level, fmt, ...) \
67 do { \
68 if (LIN_MASTER_DEBUG_LEVEL >= level) { \
69 LIN_MASTER_DEBUG_SERIAL.print(__PRETTY_FUNCTION__); \
70 LIN_MASTER_DEBUG_SERIAL.print(F(": ")); \
71 char debug_buf[LIN_MASTER_DEBUG_BUFSIZE]; \
72 snprintf(debug_buf, sizeof(debug_buf), (fmt), ##__VA_ARGS__); \
73 LIN_MASTER_DEBUG_SERIAL.println(debug_buf); \
74 } \
75 } while(0)
76
77
78// no debug output -> omit logging macro
79#else
80
81 // do nothing. Use safe empty macros
82 #define DEBUG_PRINT(level, fmt, ...) do {} while (0)
83 #define DEBUG_PRINT_STATIC(level, fmt, ...) do {} while (0)
84
85#endif // LIN_MASTER_DEBUG_SERIAL
86
87
88/*-----------------------------------------------------------------------------
89 INCLUDE FILES
90-----------------------------------------------------------------------------*/
91
92// generic Arduino functions
93#include <Arduino.h>
94
95
96/*-----------------------------------------------------------------------------
97 GLOBAL CLASS
98-----------------------------------------------------------------------------*/
99
106{
107 // PUBLIC TYPEDEFS
108 public:
109
111 typedef enum : uint8_t
112 {
113 LIN_V1 = 1,
114 LIN_V2 = 2
116
117
119 typedef enum : uint8_t
120 {
122 SLAVE_RESPONSE = 0x02
124
125
127 typedef enum : uint8_t
128 {
129 STATE_OFF = 0x01,
130 STATE_IDLE = 0x02,
131 STATE_BREAK = 0x04,
132 STATE_BODY = 0x08,
133 STATE_DONE = 0x10
135
136
138 typedef enum : uint8_t
139 {
140 NO_ERROR = 0x00,
141 ERROR_STATE = 0x01,
142 ERROR_ECHO = 0x02,
144 ERROR_CHK = 0x08,
145 ERROR_MISC = 0x80
147
148
149 // PROTECTED VARIABLES
150 protected:
151
152 // node properties
153 int8_t pinTxEN;
154 uint16_t baudrate;
157 uint32_t timePerByte;
158 uint32_t timeoutFrame;
159
160 // frame properties
163 uint8_t id;
164 uint8_t lenTx;
165 uint8_t bufTx[12];
166 uint8_t lenRx;
167 uint8_t bufRx[12];
168 uint32_t timeStart;
169
170
171 // PUBLIC VARIABLES
172 public:
173
174 char nameLIN[LIN_MASTER_BUFLEN_NAME];
175
176
177 // PROTECTED METHODS
178 protected:
179
181 uint8_t _calculatePID(void);
182
184 uint8_t _calculateChecksum(uint8_t NumData, uint8_t Data[]);
185
188
189
192
195
198
199
201 inline void _enableTransmitter(void)
202 {
203 // print debug message
204 DEBUG_PRINT(3, " ");
205
206 // enable tranmitter
207 if (this->pinTxEN >= 0)
208 digitalWrite(this->pinTxEN, HIGH);
209
210 } // _enableTransmitter()
211
213 inline void _disableTransmitter(void)
214 {
215 // print debug message
216 DEBUG_PRINT(3, " ");
217
218 // disable tranmitter
219 if (this->pinTxEN >= 0)
220 digitalWrite(this->pinTxEN, LOW);
221
222 } // _disableTransmitter()
223
224
225 // PUBLIC METHODS
226 public:
227
229 LIN_Master_Base(const char NameLIN[] = "Master", const int8_t PinTxEN = INT8_MIN);
230
232 virtual ~LIN_Master_Base(void) { }
233
234
236 virtual void begin(uint16_t Baudrate = 19200);
237
239 virtual void end(void);
240
241
243 inline void resetStateMachine(void)
244 {
245 // print debug message
246 DEBUG_PRINT(3, " ");
247
248 // reset state
249 this->state = LIN_Master_Base::STATE_IDLE;
250
251 } // resetStateMachine()
252
255 {
256 // print debug message
257 DEBUG_PRINT(3, " ");
258
259 // return state
260 return this->state;
261
262 } // getState()
263
264
266 inline void resetError(void)
267 {
268 // print debug message
269 DEBUG_PRINT(3, " ");
270
271 // reset error
272 this->error = LIN_Master_Base::NO_ERROR;
273
274 } // resetError()
275
278 {
279 // print debug message
280 DEBUG_PRINT(3, " ");
281
282 // return error
283 return this->error;
284
285 } // getError()
286
287
289 inline void getFrame(LIN_Master_Base::frame_t &Type, uint8_t &Id, uint8_t &NumData, uint8_t Data[])
290 {
291 // print debug message
292 DEBUG_PRINT(3, " ");
293
294 noInterrupts(); // for data consistency temporarily disable ISRs
295 Type = this->type; // frame type
296 Id = this->id; // frame ID
297 NumData = this->lenRx - 4; // number of data bytes (excl. BREAK, SYNC, ID, CHK)
298 memcpy(Data, this->bufRx+3, NumData); // copy data bytes
299 interrupts(); // re-enable ISRs
300
301 } // getFrame()
302
303
306 uint8_t Id = 0x00, uint8_t NumData = 0, uint8_t Data[] = NULL);
307
310 uint8_t Id = 0x00, uint8_t NumData = 0, uint8_t Data[] = NULL);
311
314 uint8_t Id = 0x00, uint8_t NumData = 0);
315
318 uint8_t Id = 0x00, uint8_t NumData = 0, uint8_t *Data = NULL);
319
322
323}; // class LIN_Master_Base
324
325/*-----------------------------------------------------------------------------
326 END OF MODULE DEFINITION FOR MULTIPLE INLUSION
327-----------------------------------------------------------------------------*/
328#endif // _LIN_MASTER_BASE_H_
329
330/*-----------------------------------------------------------------------------
331 END OF FILE
332-----------------------------------------------------------------------------*/
LIN master node base class.
uint8_t lenRx
receive buffer length (max. 12)
void resetStateMachine(void)
Reset LIN state machine.
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
uint16_t baudrate
communication baudrate [Baud]
frame_t
LIN frame type.
@ MASTER_REQUEST
LIN master request frame.
@ SLAVE_RESPONSE
LIN slave response frame.
void getFrame(LIN_Master_Base::frame_t &Type, uint8_t &Id, uint8_t &NumData, uint8_t Data[])
Getter for LIN frame.
virtual ~LIN_Master_Base(void)
LIN master node destructor, here dummy. Any class with virtual functions should have virtual destruct...
uint8_t bufTx[12]
send buffer incl. BREAK, SYNC, DATA and CHK (max. 12B)
LIN_Master_Base::state_t receiveSlaveResponse(LIN_Master_Base::version_t Version=LIN_Master_Base::LIN_V2, uint8_t Id=0x00, uint8_t NumData=0)
Start sending a LIN slave response frame in background (if supported)
version_t
LIN protocol version.
@ LIN_V2
LIN protocol version 2.x.
@ LIN_V1
LIN protocol version 1.x.
uint8_t _calculatePID(void)
Calculate protected frame ID.
LIN_Master_Base::error_t _checkFrame(void)
Check received LIN frame.
LIN_Master_Base::error_t error
error state. Is latched until cleared
char nameLIN[LIN_MASTER_BUFLEN_NAME]
LIN node name, e.g. for debug.
void _enableTransmitter(void)
Enable RS485 transmitter (DE=high)
LIN_Master_Base::state_t getState(void)
Getter for LIN state machine state.
LIN_Master_Base::error_t receiveSlaveResponseBlocking(LIN_Master_Base::version_t Version=LIN_Master_Base::LIN_V2, uint8_t Id=0x00, uint8_t NumData=0, uint8_t *Data=NULL)
Send a blocking LIN slave response frame (no background operation)
uint32_t timeStart
starting time [us] for frame timeout
int8_t pinTxEN
optional Tx direction pin, e.g. for LIN via RS485
uint8_t lenTx
send buffer length (max. 12)
LIN_Master_Base::state_t handler(void)
Handle LIN background operation (call until STATE_DONE is returned)
uint8_t _calculateChecksum(uint8_t NumData, uint8_t Data[])
Calculate LIN frame checksum.
LIN_Master_Base::error_t getError(void)
Getter for LIN state machine error.
LIN_Master_Base::state_t state
status of LIN state machine
void resetError(void)
Clear error of LIN state machine.
virtual LIN_Master_Base::state_t _sendFrame(void)
Send LIN frame body.
void _disableTransmitter(void)
Disable RS485 transmitter (DE=low)
error_t
LIN error codes. Use bitmasks, as error is latched. Use same as LIN_slave_portable.
@ ERROR_STATE
error in LIN state machine
@ ERROR_CHK
LIN checksum error.
@ ERROR_TIMEOUT
frame timeout error
@ ERROR_ECHO
error reading response echo
@ ERROR_MISC
misc error, should not occur
virtual LIN_Master_Base::state_t _receiveFrame(void)
Receive LIN frame.
uint32_t timePerByte
time [us] per byte at specified baudrate
LIN_Master_Base::state_t sendMasterRequest(LIN_Master_Base::version_t Version=LIN_Master_Base::LIN_V2, uint8_t Id=0x00, uint8_t NumData=0, uint8_t Data[]=NULL)
Start sending a LIN master request frame in background (if supported)
LIN_Master_Base::version_t version
LIN protocol version.
state_t
state of LIN master state machine. Use bitmasks for fast checking multiple states
@ STATE_BODY
rest of frame is being sent/received
@ STATE_OFF
LIN interface closed.
@ STATE_BREAK
sync break is being transmitted
@ STATE_IDLE
no LIN transmission ongoing
@ STATE_DONE
frame completed
virtual void end(void)
Close serial interface.
uint8_t bufRx[12]
receive buffer incl. BREAK, SYNC, DATA and CHK (max. 12B)
LIN_Master_Base::frame_t type
LIN frame type.
virtual LIN_Master_Base::state_t _sendBreak(void)
Send LIN break.
uint8_t id
LIN frame identifier (protected or unprotected)
LIN_Master_Base::error_t sendMasterRequestBlocking(LIN_Master_Base::version_t Version=LIN_Master_Base::LIN_V2, uint8_t Id=0x00, uint8_t NumData=0, uint8_t Data[]=NULL)
Send a blocking LIN master request frame (no background operation)
uint32_t timeoutFrame
max. frame duration [us]