| 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
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 // print debug message
109 DEBUG_PRINT(1, "Rx timeout");
110
111 // set error state and return immediately
112 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_TIMEOUT);
113 this->state = LIN_Master_Base::STATE_DONE;
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
135 LIN_Master_Base::state_t LIN_Master_HardwareSerial_ESP8266::_receiveFrame(void)
136{
137 // if state is wrong, exit immediately
138 if (this->state != LIN_Master_Base::STATE_BODY)
139 {
140 // print debug message
141 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
142
143 // set error state and return immediately
144 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
145 this->state = LIN_Master_Base::STATE_DONE;
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
167 this->state = LIN_Master_Base::STATE_DONE;
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
181 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_TIMEOUT);
182 this->state = LIN_Master_Base::STATE_DONE;
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
206 LIN_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
226 void LIN_Master_HardwareSerial_ESP8266::begin(uint16_t Baudrate)
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
255 void LIN_Master_HardwareSerial_ESP8266::end()
256{
257 // call base class method
258 LIN_Master_Base::end();
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_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:255
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:226
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:206
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:135
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