| 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
15#include <LIN_master_HardwareSerial_ESP8266.h>
16
17
23 LIN_Master_Base::state_t LIN_Master_HardwareSerial_ESP8266::_sendBreak(void)
24{
25 // if state is wrong, exit immediately
26 if (this->state != LIN_Master_Base::STATE_IDLE)
27 {
28 // print debug message
29 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
30
31 // set error state and return immediately
32 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
33 this->state = LIN_Master_Base::STATE_DONE;
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
53 this->state = LIN_Master_Base::STATE_BREAK;
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
70 LIN_Master_Base::state_t LIN_Master_HardwareSerial_ESP8266::_sendFrame(void)
71{
72 // if state is wrong, exit immediately
73 if (this->state != LIN_Master_Base::STATE_BREAK)
74 {
75 // print debug message
76 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
77
78 // set error state and return immediately
79 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
80 this->state = LIN_Master_Base::STATE_DONE;
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
98 this->state = LIN_Master_Base::STATE_BODY;
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 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_TIMEOUT);
109 this->state = LIN_Master_Base::STATE_DONE;
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
130 LIN_Master_Base::state_t LIN_Master_HardwareSerial_ESP8266::_receiveFrame(void)
131{
132 // if state is wrong, exit immediately
133 if (this->state != LIN_Master_Base::STATE_BODY)
134 {
135 // print debug message
136 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
137
138 // set error state and return immediately
139 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
140 this->state = LIN_Master_Base::STATE_DONE;
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
162 this->state = LIN_Master_Base::STATE_DONE;
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
176 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_TIMEOUT);
177 this->state = LIN_Master_Base::STATE_DONE;
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
201 LIN_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
221 void LIN_Master_HardwareSerial_ESP8266::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, 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
250 void LIN_Master_HardwareSerial_ESP8266::end()
251{
252 // call base class method
253 LIN_Master_Base::end();
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_HardwareSerial_ESP8266.h
LIN master emulation library using hardware Serial0 interface of ESP8266.
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_ESP8266::end
void end(void)
Close serial interface.
Definition LIN_master_HardwareSerial_ESP8266.cpp:250
LIN_Master_HardwareSerial_ESP8266::_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_ESP8266.cpp:70
LIN_Master_HardwareSerial_ESP8266::begin
void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_master_HardwareSerial_ESP8266.cpp:221
LIN_Master_HardwareSerial_ESP8266::LIN_Master_HardwareSerial_ESP8266
LIN_Master_HardwareSerial_ESP8266(bool SwapPins=false, const char NameLIN[]="Master", const int8_t PinTxEN=INT8_MIN)
Class constructor.
Definition LIN_master_HardwareSerial_ESP8266.cpp:201
LIN_Master_HardwareSerial_ESP8266::swapPins
bool swapPins
use alternate pins for Serial0
Definition LIN_master_HardwareSerial_ESP8266.h:41
LIN_Master_HardwareSerial_ESP8266::_receiveFrame
LIN_Master_Base::state_t _receiveFrame(void)
Read and check LIN frame.
Definition LIN_master_HardwareSerial_ESP8266.cpp:130
LIN_Master_HardwareSerial_ESP8266::pSerial
HardwareSerial * pSerial
serial interface used for LIN
Definition LIN_master_HardwareSerial_ESP8266.h:40
LIN_Master_HardwareSerial_ESP8266::_sendBreak
LIN_Master_Base::state_t _sendBreak(void)
Send LIN break.
Definition LIN_master_HardwareSerial_ESP8266.cpp:23