| 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
105 class LIN_Master_Base
106{
107 // PUBLIC TYPEDEFS
108 public:
109
111 typedef enum : uint8_t
112 {
113 LIN_V1 = 1,
114 LIN_V2 = 2
115 } version_t;
116
117
119 typedef enum : uint8_t
120 {
121 MASTER_REQUEST = 0x01,
122 SLAVE_RESPONSE = 0x02
123 } frame_t;
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
134 } state_t;
135
136
138 typedef enum : uint8_t
139 {
140 NO_ERROR = 0x00,
141 ERROR_STATE = 0x01,
142 ERROR_ECHO = 0x02,
143 ERROR_TIMEOUT = 0x04,
144 ERROR_CHK = 0x08,
145 ERROR_MISC = 0x80
146 } error_t;
147
148
149 // PROTECTED VARIABLES
150 protected:
151
152 // node properties
153 int8_t pinTxEN;
154 uint16_t baudrate;
155 LIN_Master_Base::state_t state;
156 LIN_Master_Base::error_t error;
157 uint32_t timePerByte;
158 uint32_t timeoutFrame;
159
160 // frame properties
161 LIN_Master_Base::version_t version;
162 LIN_Master_Base::frame_t type;
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
187 LIN_Master_Base::error_t _checkFrame(void);
188
189
191 virtual LIN_Master_Base::state_t _sendBreak(void);
192
194 virtual LIN_Master_Base::state_t _sendFrame(void);
195
197 virtual LIN_Master_Base::state_t _receiveFrame(void);
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
254 inline LIN_Master_Base::state_t getState(void)
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
277 inline LIN_Master_Base::error_t getError(void)
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
305 LIN_Master_Base::state_t sendMasterRequest(LIN_Master_Base::version_t Version = LIN_Master_Base::LIN_V2,
306 uint8_t Id = 0x00, uint8_t NumData = 0, uint8_t Data[] = NULL);
307
309 LIN_Master_Base::error_t sendMasterRequestBlocking(LIN_Master_Base::version_t Version = LIN_Master_Base::LIN_V2,
310 uint8_t Id = 0x00, uint8_t NumData = 0, uint8_t Data[] = NULL);
311
313 LIN_Master_Base::state_t receiveSlaveResponse(LIN_Master_Base::version_t Version = LIN_Master_Base::LIN_V2,
314 uint8_t Id = 0x00, uint8_t NumData = 0);
315
317 LIN_Master_Base::error_t receiveSlaveResponseBlocking(LIN_Master_Base::version_t Version = LIN_Master_Base::LIN_V2,
318 uint8_t Id = 0x00, uint8_t NumData = 0, uint8_t *Data = NULL);
319
321 LIN_Master_Base::state_t handler(void);
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_Base
LIN master node base class.
Definition LIN_master_Base.h:106
LIN_Master_Base::lenRx
uint8_t lenRx
receive buffer length (max. 12)
Definition LIN_master_Base.h:166
LIN_Master_Base::resetStateMachine
void resetStateMachine(void)
Reset LIN state machine.
Definition LIN_master_Base.h:243
LIN_Master_Base::begin
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_master_Base.cpp:240
LIN_Master_Base::baudrate
uint16_t baudrate
communication baudrate [Baud]
Definition LIN_master_Base.h:154
LIN_Master_Base::frame_t
frame_t
LIN frame type.
Definition LIN_master_Base.h:120
LIN_Master_Base::MASTER_REQUEST
@ MASTER_REQUEST
LIN master request frame.
Definition LIN_master_Base.h:121
LIN_Master_Base::SLAVE_RESPONSE
@ SLAVE_RESPONSE
LIN slave response frame.
Definition LIN_master_Base.h:122
LIN_Master_Base::getFrame
void getFrame(LIN_Master_Base::frame_t &Type, uint8_t &Id, uint8_t &NumData, uint8_t Data[])
Getter for LIN frame.
Definition LIN_master_Base.h:289
LIN_Master_Base::~LIN_Master_Base
virtual ~LIN_Master_Base(void)
LIN master node destructor, here dummy. Any class with virtual functions should have virtual destruct...
Definition LIN_master_Base.h:232
LIN_Master_Base::bufTx
uint8_t bufTx[12]
send buffer incl. BREAK, SYNC, DATA and CHK (max. 12B)
Definition LIN_master_Base.h:165
LIN_Master_Base::receiveSlaveResponse
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)
Definition LIN_master_Base.cpp:378
LIN_Master_Base::version_t
version_t
LIN protocol version.
Definition LIN_master_Base.h:112
LIN_Master_Base::LIN_V2
@ LIN_V2
LIN protocol version 2.x.
Definition LIN_master_Base.h:114
LIN_Master_Base::LIN_V1
@ LIN_V1
LIN protocol version 1.x.
Definition LIN_master_Base.h:113
LIN_Master_Base::_calculatePID
uint8_t _calculatePID(void)
Calculate protected frame ID.
Definition LIN_master_Base.cpp:29
LIN_Master_Base::_checkFrame
LIN_Master_Base::error_t _checkFrame(void)
Check received LIN frame.
Definition LIN_master_Base.cpp:92
LIN_Master_Base::error
LIN_Master_Base::error_t error
error state. Is latched until cleared
Definition LIN_master_Base.h:156
LIN_Master_Base::nameLIN
char nameLIN[LIN_MASTER_BUFLEN_NAME]
LIN node name, e.g. for debug.
Definition LIN_master_Base.h:174
LIN_Master_Base::_enableTransmitter
void _enableTransmitter(void)
Enable RS485 transmitter (DE=high)
Definition LIN_master_Base.h:201
LIN_Master_Base::getState
LIN_Master_Base::state_t getState(void)
Getter for LIN state machine state.
Definition LIN_master_Base.h:254
LIN_Master_Base::receiveSlaveResponseBlocking
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)
Definition LIN_master_Base.cpp:420
LIN_Master_Base::timeStart
uint32_t timeStart
starting time [us] for frame timeout
Definition LIN_master_Base.h:168
LIN_Master_Base::pinTxEN
int8_t pinTxEN
optional Tx direction pin, e.g. for LIN via RS485
Definition LIN_master_Base.h:153
LIN_Master_Base::lenTx
uint8_t lenTx
send buffer length (max. 12)
Definition LIN_master_Base.h:164
LIN_Master_Base::handler
LIN_Master_Base::state_t handler(void)
Handle LIN background operation (call until STATE_DONE is returned)
Definition LIN_master_Base.cpp:449
LIN_Master_Base::_calculateChecksum
uint8_t _calculateChecksum(uint8_t NumData, uint8_t Data[])
Calculate LIN frame checksum.
Definition LIN_master_Base.cpp:58
LIN_Master_Base::getError
LIN_Master_Base::error_t getError(void)
Getter for LIN state machine error.
Definition LIN_master_Base.h:277
LIN_Master_Base::state
LIN_Master_Base::state_t state
status of LIN state machine
Definition LIN_master_Base.h:155
LIN_Master_Base::resetError
void resetError(void)
Clear error of LIN state machine.
Definition LIN_master_Base.h:266
LIN_Master_Base::_sendFrame
virtual LIN_Master_Base::state_t _sendFrame(void)
Send LIN frame body.
Definition LIN_master_Base.cpp:171
LIN_Master_Base::_disableTransmitter
void _disableTransmitter(void)
Disable RS485 transmitter (DE=low)
Definition LIN_master_Base.h:213
LIN_Master_Base::error_t
error_t
LIN error codes. Use bitmasks, as error is latched. Use same as LIN_slave_portable.
Definition LIN_master_Base.h:139
LIN_Master_Base::ERROR_STATE
@ ERROR_STATE
error in LIN state machine
Definition LIN_master_Base.h:141
LIN_Master_Base::ERROR_CHK
@ ERROR_CHK
LIN checksum error.
Definition LIN_master_Base.h:144
LIN_Master_Base::ERROR_TIMEOUT
@ ERROR_TIMEOUT
frame timeout error
Definition LIN_master_Base.h:143
LIN_Master_Base::ERROR_ECHO
@ ERROR_ECHO
error reading response echo
Definition LIN_master_Base.h:142
LIN_Master_Base::NO_ERROR
@ NO_ERROR
no error
Definition LIN_master_Base.h:140
LIN_Master_Base::ERROR_MISC
@ ERROR_MISC
misc error, should not occur
Definition LIN_master_Base.h:145
LIN_Master_Base::_receiveFrame
virtual LIN_Master_Base::state_t _receiveFrame(void)
Receive LIN frame.
Definition LIN_master_Base.cpp:191
LIN_Master_Base::timePerByte
uint32_t timePerByte
time [us] per byte at specified baudrate
Definition LIN_master_Base.h:157
LIN_Master_Base::sendMasterRequest
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)
Definition LIN_master_Base.cpp:305
LIN_Master_Base::version
LIN_Master_Base::version_t version
LIN protocol version.
Definition LIN_master_Base.h:161
LIN_Master_Base::state_t
state_t
state of LIN master state machine. Use bitmasks for fast checking multiple states
Definition LIN_master_Base.h:128
LIN_Master_Base::STATE_BODY
@ STATE_BODY
rest of frame is being sent/received
Definition LIN_master_Base.h:132
LIN_Master_Base::STATE_OFF
@ STATE_OFF
LIN interface closed.
Definition LIN_master_Base.h:129
LIN_Master_Base::STATE_BREAK
@ STATE_BREAK
sync break is being transmitted
Definition LIN_master_Base.h:131
LIN_Master_Base::STATE_IDLE
@ STATE_IDLE
no LIN transmission ongoing
Definition LIN_master_Base.h:130
LIN_Master_Base::STATE_DONE
@ STATE_DONE
frame completed
Definition LIN_master_Base.h:133
LIN_Master_Base::end
virtual void end(void)
Close serial interface.
Definition LIN_master_Base.cpp:279
LIN_Master_Base::bufRx
uint8_t bufRx[12]
receive buffer incl. BREAK, SYNC, DATA and CHK (max. 12B)
Definition LIN_master_Base.h:167
LIN_Master_Base::type
LIN_Master_Base::frame_t type
LIN frame type.
Definition LIN_master_Base.h:162
LIN_Master_Base::_sendBreak
virtual LIN_Master_Base::state_t _sendBreak(void)
Send LIN break.
Definition LIN_master_Base.cpp:137
LIN_Master_Base::id
uint8_t id
LIN frame identifier (protected or unprotected)
Definition LIN_master_Base.h:163
LIN_Master_Base::sendMasterRequestBlocking
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)
Definition LIN_master_Base.cpp:349
LIN_Master_Base::timeoutFrame
uint32_t timeoutFrame
max. frame duration [us]
Definition LIN_master_Base.h:158