LIN_master_portable_Arduino 1.9
Arduino library for Local Interconnect Network master node emulation
Loading...
Searching...
No Matches
LIN_master_HardwareSerial_ESP8266.cpp
Go to the documentation of this file.
1
11// assert ESP8266 platform
12#if defined(ARDUINO_ARCH_ESP8266)
13
14// include files
16
17
24{
25 // if state is wrong, exit immediately
27 {
28 // print debug message
29 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
30
31 // set error state and return immediately
34 this->_disableTransmitter();
35 return this->state;
36 }
37
38 // empty buffers, just in case...
39 //this->pSerial->flush(); // skip, as this causes a ~500us delay, see https://github.com/esp8266/Arduino/blob/master/cores/esp8266/HardwareSerial.cpp
40 while (this->pSerial->available())
41 this->pSerial->read();
42
43 // set half baudrate for BREAK
44 this->pSerial->updateBaudRate(this->baudrate >> 1);
45
46 // optionally enable transmitter
47 this->_enableTransmitter();
48
49 // send BREAK (>=13 bit low)
50 this->pSerial->write(this->bufTx[0]);
51
52 // progress state
54
55 // print debug message
56 DEBUG_PRINT(3, " ");
57
58 // return state
59 return this->state;
60
61} // LIN_Master_HardwareSerial_ESP8266::_sendBreak()
62
63
64
71{
72 // if state is wrong, exit immediately
74 {
75 // print debug message
76 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
77
78 // set error state and return immediately
81 this->_disableTransmitter();
82 return this->state;
83 }
84
85 // byte(s) received (likely BREAK echo)
86 if (this->pSerial->available())
87 {
88 // store echo in Rx
89 this->bufRx[0] = this->pSerial->read();
90
91 // restore nominal baudrate
92 this->pSerial->updateBaudRate(this->baudrate);
93
94 // send rest of frame (request frame: SYNC+ID+DATA[]+CHK; response frame: SYNC+ID)
95 this->pSerial->write(this->bufTx+1, this->lenTx-1);
96
97 // progress state
99
100 } // BREAK echo received
101
102 // no byte(s) received
103 else
104 {
105 // check for timeout
106 if (micros() - this->timeStart > this->timeoutFrame)
107 {
108 // print debug message
109 DEBUG_PRINT(1, "Rx timeout");
110
111 // set error state and return immediately
114 this->_disableTransmitter();
115 return this->state;
116 }
117
118 } // no byte(s) received
119
120 // print debug message
121 DEBUG_PRINT(2, " ");
122
123 // return state
124 return this->state;
125
126} // LIN_Master_HardwareSerial_ESP8266::_sendFrame()
127
128
129
136{
137 // if state is wrong, exit immediately
139 {
140 // print debug message
141 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
142
143 // set error state and return immediately
146 this->_disableTransmitter();
147 return this->state;
148 }
149
150 // optionally disable RS485 transmitter for slave response frames. Len==2 because BREAK is handled already handled in _sendFrame()
151 if ((this->type == LIN_Master_Base::SLAVE_RESPONSE) && (this->pSerial->available() == 2))
152 this->_disableTransmitter();
153
154 // frame body received (-1 because BREAK is handled already handled in _sendFrame())
155 if (this->pSerial->available() >= this->lenRx-1)
156 {
157 // store bytes in Rx
158 this->pSerial->readBytes(this->bufRx+1, this->lenRx-1);
159
160 // check frame for errors
161 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) this->_checkFrame());
162
163 // optionally disable RS485 transmitter after frame is completed
164 this->_disableTransmitter();
165
166 // progress state
168
169 } // frame body received
170
171 // frame body received not yet received
172 else
173 {
174 // check for timeout
175 if (micros() - this->timeStart > this->timeoutFrame)
176 {
177 // print debug message
178 DEBUG_PRINT(1, "Rx timeout");
179
180 // set error state and return immediately
183 this->_disableTransmitter();
184 return this->state;
185 }
186
187 } // not enough bytes received
188
189 // print debug message
190 DEBUG_PRINT(2, " ");
191
192 // return state
193 return this->state;
194
195} // LIN_Master_HardwareSerial_ESP8266::_receiveFrame()
196
197
198
206LIN_Master_HardwareSerial_ESP8266::LIN_Master_HardwareSerial_ESP8266(bool SwapPins, const char NameLIN[], const int8_t PinTxEN) :
207 LIN_Master_Base::LIN_Master_Base(NameLIN, PinTxEN)
208{
209 // Debug serial initialized in begin() -> no debug output here
210
211 // store pointer to used HW serial
212 this->pSerial = &Serial; // ESP8266 only has 1 UART with send/receive
213 this->swapPins = SwapPins; // use alternate pins Rx=D7 / Tx=D8 for Serial0
214
215 // must not open connection here, else system resets
216
217} // LIN_Master_HardwareSerial_ESP8266::LIN_Master_HardwareSerial_ESP8266()
218
219
220
227{
228 // call base class method
229 LIN_Master_Base::begin(Baudrate);
230
231 // open serial interface with optional timeout
232 this->pSerial->begin(this->baudrate, SERIAL_8N1);
233 #if defined(LIN_MASTER_LIN_PORT_TIMEOUT) && (LIN_MASTER_LIN_PORT_TIMEOUT > 0)
234 uint32_t startMillis = millis();
235 while ((!(*(this->pSerial))) && (millis() - startMillis < LIN_MASTER_LIN_PORT_TIMEOUT));
236 #else
237 while(!(*(this->pSerial)));
238 #endif
239
240 // optionally route Serial0 to alternate pins
241 if (this->swapPins == true)
242 this->pSerial->swap();
243
244 // print debug message
245 DEBUG_PRINT(2, "ok, pin swap=%d", (int) this->swapPins);
246
247} // LIN_Master_HardwareSerial_ESP8266::begin()
248
249
250
256{
257 // call base class method
259
260 // close serial interface
261 this->pSerial->end();
262
263 // print debug message
264 DEBUG_PRINT(2, " ");
265
266} // LIN_Master_HardwareSerial_ESP8266::end()
267
268#endif // ARDUINO_ARCH_ESP8266
269
270/*-----------------------------------------------------------------------------
271 END OF FILE
272-----------------------------------------------------------------------------*/
LIN master emulation library using hardware Serial0 interface of ESP8266.
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]
LIN_Master_Base::state_t _sendFrame(void)
Send LIN bytes (request frame: SYNC+ID+DATA[]+CHK; response frame: SYNC+ID)
void begin(uint16_t Baudrate=19200)
Open serial interface.
LIN_Master_HardwareSerial_ESP8266(bool SwapPins=false, const char NameLIN[]="Master", const int8_t PinTxEN=INT8_MIN)
Class constructor.
bool swapPins
use alternate pins for Serial0
LIN_Master_Base::state_t _receiveFrame(void)
Read and check LIN frame.
HardwareSerial * pSerial
serial interface used for LIN
LIN_Master_Base::state_t _sendBreak(void)
Send LIN break.