| LIN_master_portable_Arduino 1.4 Arduino library for Local Interconnect Network master node emulation

Loading...
Searching...
No Matches
LIN_master_HardwareSerial.cpp
Go to the documentation of this file.
1
9// include files
10#include <LIN_master_HardwareSerial.h>
11
12
18 LIN_Master_Base::state_t LIN_Master_HardwareSerial::_sendBreak(void)
19{
20 // if state is wrong, exit immediately
21 if (this->state != LIN_Master_Base::STATE_IDLE)
22 {
23 // print debug message
24 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
25
26 // set error state and return immediately
27 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
28 this->state = LIN_Master_Base::STATE_DONE;
29 this->_disableTransmitter();
30 return this->state;
31 }
32
33 // empty buffers, just in case...
34 this->pSerial->flush();
35 while (this->pSerial->available())
36 this->pSerial->read();
37
38 // set half baudrate for BREAK (w/o timeout)
39 this->pSerial->begin(this->baudrate >> 1);
40 while(!(*(this->pSerial)));
41
42 // optionally enable transmitter
43 this->_enableTransmitter();
44
45 // send BREAK (>=13 bit low)
46 this->pSerial->write(this->bufTx[0]);
47
48 // progress state
49 this->state = LIN_Master_Base::STATE_BREAK;
50
51 // print debug message
52 DEBUG_PRINT(3, " ");
53
54 // return state
55 return this->state;
56
57} // LIN_Master_HardwareSerial::_sendBreak()
58
59
60
66 LIN_Master_Base::state_t LIN_Master_HardwareSerial::_sendFrame(void)
67{
68 // if state is wrong, exit immediately
69 if (this->state != LIN_Master_Base::STATE_BREAK)
70 {
71 // print debug message
72 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
73
74 // set error state and return immediately
75 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
76 this->state = LIN_Master_Base::STATE_DONE;
77 this->_disableTransmitter();
78 return this->state;
79 }
80
81 // byte(s) received (likely BREAK echo)
82 if (this->pSerial->available())
83 {
84 // store echo in Rx
85 this->bufRx[0] = this->pSerial->read();
86
87 // restore nominal baudrate (w/o timeout)
88 this->pSerial->begin(this->baudrate);
89 while(!(*(this->pSerial)));
90
91 // send rest of frame (request frame: SYNC+ID+DATA[]+CHK; response frame: SYNC+ID)
92 this->pSerial->write(this->bufTx+1, this->lenTx-1);
93
94 // progress state
95 this->state = LIN_Master_Base::STATE_BODY;
96
97 } // BREAK echo received
98
99 // no byte(s) received
100 else
101 {
102 // check for timeout
103 if (micros() - this->timeStart > this->timeoutFrame)
104 {
105 // print debug message
106 DEBUG_PRINT(1, "Rx timeout");
107
108 // set error state and return immediately
109 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_TIMEOUT);
110 this->state = LIN_Master_Base::STATE_DONE;
111 this->_disableTransmitter();
112 return this->state;
113 }
114
115 } // no byte(s) received
116
117 // print debug message
118 DEBUG_PRINT(2, " ");
119
120 // return state
121 return this->state;
122
123} // LIN_Master_HardwareSerial::_sendFrame()
124
125
126
132 LIN_Master_Base::state_t LIN_Master_HardwareSerial::_receiveFrame(void)
133{
134 // if state is wrong, exit immediately
135 if (this->state != LIN_Master_Base::STATE_BODY)
136 {
137 // print debug message
138 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
139
140 // set error state and return immediately
141 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
142 this->state = LIN_Master_Base::STATE_DONE;
143 this->_disableTransmitter();
144 return this->state;
145 }
146
147 // optionally disable transmitter for slave response frames. Len==2 because BREAK is handled already handled in _sendFrame()
148 if ((this->type == LIN_Master_Base::SLAVE_RESPONSE) && (this->pSerial->available() == 2))
149 this->_disableTransmitter();
150
151 // frame body received (-1 because BREAK is handled already handled in _sendFrame())
152 if (this->pSerial->available() >= this->lenRx-1)
153 {
154 // store bytes in Rx
155 this->pSerial->readBytes(this->bufRx+1, this->lenRx-1);
156
157 // check frame for errors
158 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) this->_checkFrame());
159
160 // optionally disable transmitter after frame is completed
161 this->_disableTransmitter();
162
163 // progress state
164 this->state = LIN_Master_Base::STATE_DONE;
165
166 } // frame body received
167
168 // frame body received not yet received
169 else
170 {
171 // check for timeout
172 if (micros() - this->timeStart > this->timeoutFrame)
173 {
174 // print debug message
175 DEBUG_PRINT(1, "Rx timeout");
176
177 // set error state and return immediately
178 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_TIMEOUT);
179 this->state = LIN_Master_Base::STATE_DONE;
180 this->_disableTransmitter();
181 return this->state;
182 }
183
184 } // not enough bytes received
185
186 // print debug message
187 DEBUG_PRINT(2, " ");
188
189 // return state
190 return this->state;
191
192} // LIN_Master_HardwareSerial::_receiveFrame()
193
194
202 LIN_Master_HardwareSerial::LIN_Master_HardwareSerial(HardwareSerial &Interface, const char NameLIN[], const int8_t PinTxEN) :
203 LIN_Master_Base::LIN_Master_Base(NameLIN, PinTxEN)
204{
205 // Debug serial initialized in begin() -> no debug output here
206
207 // store pointer to used HW serial
208 this->pSerial = &Interface;
209
210 // must not open connection here, else (at least) ESP32 and ESP8266 fail
211
212} // LIN_Master_HardwareSerial::LIN_Master_HardwareSerial()
213
214
215
221 void LIN_Master_HardwareSerial::begin(uint16_t Baudrate)
222{
223 // call base class method
224 LIN_Master_Base::begin(Baudrate);
225
226 // open serial interface with optional timeout
227 this->pSerial->begin(this->baudrate);
228 #if defined(LIN_MASTER_LIN_PORT_TIMEOUT) && (LIN_MASTER_LIN_PORT_TIMEOUT > 0)
229 uint32_t startMillis = millis();
230 while ((!(*(this->pSerial))) && (millis() - startMillis < LIN_MASTER_LIN_PORT_TIMEOUT));
231 #else
232 while(!(*(this->pSerial)));
233 #endif
234
235 // print debug message
236 DEBUG_PRINT(2, "ok");
237
238} // LIN_Master_HardwareSerial::begin()
239
240
241
246 void LIN_Master_HardwareSerial::end()
247{
248 // call base class method
249 LIN_Master_Base::end();
250
251 // close serial interface
252 this->pSerial->end();
253
254 // print debug message
255 DEBUG_PRINT(2, " ");
256
257} // LIN_Master_HardwareSerial::end()
258
259/*-----------------------------------------------------------------------------
260 END OF FILE
261-----------------------------------------------------------------------------*/
LIN_master_HardwareSerial.h
LIN master emulation library using a HardwareSerial interface.
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::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::SLAVE_RESPONSE
@ SLAVE_RESPONSE
LIN slave response frame.
Definition LIN_master_Base.h:122
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::_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::_enableTransmitter
void _enableTransmitter(void)
Enable RS485 transmitter (DE=high)
Definition LIN_master_Base.h:201
LIN_Master_Base::timeStart
uint32_t timeStart
starting time [us] for frame timeout
Definition LIN_master_Base.h:168
LIN_Master_Base::lenTx
uint8_t lenTx
send buffer length (max. 12)
Definition LIN_master_Base.h:164
LIN_Master_Base::state
LIN_Master_Base::state_t state
status of LIN state machine
Definition LIN_master_Base.h:155
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_TIMEOUT
@ ERROR_TIMEOUT
frame timeout error
Definition LIN_master_Base.h:143
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_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::timeoutFrame
uint32_t timeoutFrame
max. frame duration [us]
Definition LIN_master_Base.h:158
LIN_Master_HardwareSerial::end
void end(void)
Close serial interface.
Definition LIN_master_HardwareSerial.cpp:246
LIN_Master_HardwareSerial::pSerial
HardwareSerial * pSerial
serial interface used for LIN
Definition LIN_master_HardwareSerial.h:37
LIN_Master_HardwareSerial::LIN_Master_HardwareSerial
LIN_Master_HardwareSerial(HardwareSerial &Interface, const char NameLIN[]="Master", const int8_t PinTxEN=INT8_MIN)
Class constructor.
Definition LIN_master_HardwareSerial.cpp:202
LIN_Master_HardwareSerial::begin
void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_master_HardwareSerial.cpp:221
LIN_Master_HardwareSerial::_sendBreak
LIN_Master_Base::state_t _sendBreak(void)
Send LIN break.
Definition LIN_master_HardwareSerial.cpp:18
LIN_Master_HardwareSerial::_sendFrame
LIN_Master_Base::state_t _sendFrame(void)
Send LIN bytes (request frame: SYNC+ID+DATA[]+CHK; response frame: SYNC+ID)
Definition LIN_master_HardwareSerial.cpp:66
LIN_Master_HardwareSerial::_receiveFrame
LIN_Master_Base::state_t _receiveFrame(void)
Read and check LIN frame.
Definition LIN_master_HardwareSerial.cpp:132