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
11
12
19{
20 // if state is wrong, exit immediately
22 {
23 // print debug message
24 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
25
26 // set error state and return immediately
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
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
67{
68 // if state is wrong, exit immediately
70 {
71 // print debug message
72 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
73
74 // set error state and return immediately
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
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
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
133{
134 // if state is wrong, exit immediately
136 {
137 // print debug message
138 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
139
140 // set error state and return immediately
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
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
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
202LIN_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
221void 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
247{
248 // call base class method
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 emulation library using a HardwareSerial interface.
LIN master node base class.
uint8_t lenRx
receive buffer length (max. 12)
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
uint16_t baudrate
communication baudrate [Baud]
@ SLAVE_RESPONSE
LIN slave response frame.
uint8_t bufTx[12]
send buffer incl. BREAK, SYNC, DATA and CHK (max. 12B)
LIN_Master_Base::error_t _checkFrame(void)
Check received LIN frame.
LIN_Master_Base::error_t error
error state. Is latched until cleared
void _enableTransmitter(void)
Enable RS485 transmitter (DE=high)
uint32_t timeStart
starting time [us] for frame timeout
uint8_t lenTx
send buffer length (max. 12)
LIN_Master_Base::state_t state
status of LIN state machine
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_TIMEOUT
frame timeout error
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_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.
uint32_t timeoutFrame
max. frame duration [us]
void end(void)
Close serial interface.
HardwareSerial * pSerial
serial interface used for LIN
LIN_Master_HardwareSerial(HardwareSerial &Interface, const char NameLIN[]="Master", const int8_t PinTxEN=INT8_MIN)
Class constructor.
void begin(uint16_t Baudrate=19200)
Open serial interface.
LIN_Master_Base::state_t _sendBreak(void)
Send LIN break.
LIN_Master_Base::state_t _sendFrame(void)
Send LIN bytes (request frame: SYNC+ID+DATA[]+CHK; response frame: SYNC+ID)
LIN_Master_Base::state_t _receiveFrame(void)
Read and check LIN frame.