LIN_master_portable_Arduino 1.4
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 {
110 this->_disableTransmitter();
111 }
112
113 } // no byte(s) received
114
115 // print debug message
116 DEBUG_PRINT(2, " ");
117
118 // return state
119 return this->state;
120
121} // LIN_Master_HardwareSerial_ESP8266::_sendFrame()
122
123
124
131{
132 // if state is wrong, exit immediately
134 {
135 // print debug message
136 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
137
138 // set error state and return immediately
141 this->_disableTransmitter();
142 return this->state;
143 }
144
145 // optionally disable transmitter for slave response frames. Len==2 because BREAK is handled already handled in _sendFrame()
146 if ((this->type == LIN_Master_Base::SLAVE_RESPONSE) && (this->pSerial->available() == 2))
147 this->_disableTransmitter();
148
149 // frame body received (-1 because BREAK is handled already handled in _sendFrame())
150 if (this->pSerial->available() >= this->lenRx-1)
151 {
152 // store bytes in Rx
153 this->pSerial->readBytes(this->bufRx+1, this->lenRx-1);
154
155 // check frame for errors
156 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) this->_checkFrame());
157
158 // optionally disable transmitter after frame is completed
159 this->_disableTransmitter();
160
161 // progress state
163
164 } // frame body received
165
166 // frame body received not yet received
167 else
168 {
169 // check for timeout
170 if (micros() - this->timeStart > this->timeoutFrame)
171 {
172 // print debug message
173 DEBUG_PRINT(1, "Rx timeout");
174
175 // set error state and return immediately
178 this->_disableTransmitter();
179 return this->state;
180 }
181
182 } // not enough bytes received
183
184 // print debug message
185 DEBUG_PRINT(2, " ");
186
187 // return state
188 return this->state;
189
190} // LIN_Master_HardwareSerial_ESP8266::_receiveFrame()
191
192
193
201LIN_Master_HardwareSerial_ESP8266::LIN_Master_HardwareSerial_ESP8266(bool SwapPins, const char NameLIN[], const int8_t PinTxEN) :
202 LIN_Master_Base::LIN_Master_Base(NameLIN, PinTxEN)
203{
204 // Debug serial initialized in begin() -> no debug output here
205
206 // store pointer to used HW serial
207 this->pSerial = &Serial; // ESP8266 only has 1 UART with send/receive
208 this->swapPins = SwapPins; // use alternate pins Rx=D7 / Tx=D8 for Serial0
209
210 // must not open connection here, else system resets
211
212} // LIN_Master_HardwareSerial_ESP8266::LIN_Master_HardwareSerial_ESP8266()
213
214
215
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, SERIAL_8N1);
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 // optionally route Serial0 to alternate pins
236 if (this->swapPins == true)
237 this->pSerial->swap();
238
239 // print debug message
240 DEBUG_PRINT(2, "ok, pin swap=%d", (int) this->swapPins);
241
242} // LIN_Master_HardwareSerial_ESP8266::begin()
243
244
245
251{
252 // call base class method
254
255 // close serial interface
256 this->pSerial->end();
257
258 // print debug message
259 DEBUG_PRINT(2, " ");
260
261} // LIN_Master_HardwareSerial_ESP8266::end()
262
263#endif // ARDUINO_ARCH_ESP8266
264
265/*-----------------------------------------------------------------------------
266 END OF FILE
267-----------------------------------------------------------------------------*/
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.