| LIN_master_portable_Arduino 1.9 Arduino library for Local Interconnect Network master node emulation

Loading...
Searching...
No Matches
LIN_master_HardwareSerial_ESP32.cpp
Go to the documentation of this file.
1
10// assert ESP32 platform
11#if defined(ARDUINO_ARCH_ESP32)
12
13// include files
14#include <LIN_master_HardwareSerial_ESP32.h>
15
16
22 LIN_Master_Base::state_t LIN_Master_HardwareSerial_ESP32::_sendBreak(void)
23{
24 // if state is wrong, exit immediately
25 if (this->state != LIN_Master_Base::STATE_IDLE)
26 {
27 // print debug message
28 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
29
30 // set error state and return immediately
31 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
32 this->state = LIN_Master_Base::STATE_DONE;
33 this->_disableTransmitter();
34 return this->state;
35 }
36
37 // empty buffers, just in case...
38 this->pSerial->flush();
39 while (this->pSerial->available())
40 this->pSerial->read();
41
42 // set half baudrate for BREAK
43 this->pSerial->updateBaudRate(this->baudrate >> 1);
44
45 // optionally enable transmitter
46 this->_enableTransmitter();
47
48 // send BREAK (>=13 bit low)
49 this->pSerial->write(this->bufTx[0]);
50
51 // store starting time to avoid using Serial.available(), which has >1ms delay
52 this->timeStartBreak = micros();
53
54 // progress state
55 this->state = LIN_Master_Base::STATE_BREAK;
56
57 // print debug message
58 DEBUG_PRINT(3, " ");
59
60 // return state
61 return this->state;
62
63} // LIN_Master_HardwareSerial_ESP32::_sendBreak()
64
65
66
72 LIN_Master_Base::state_t LIN_Master_HardwareSerial_ESP32::_sendFrame(void)
73{
74 // if state is wrong, exit immediately
75 if (this->state != LIN_Master_Base::STATE_BREAK)
76 {
77 // print debug message
78 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
79
80 // set error state and return immediately
81 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
82 this->state = LIN_Master_Base::STATE_DONE;
83 this->_disableTransmitter();
84 return this->state;
85 }
86
87 // Serial.available() has >1ms delay -> use duration of BREAK instead
88 if ((micros() - this->timeStartBreak) > (this->timePerByte << 1))
89 {
90 // skip reading Rx now (is not yet in buffer)
91
92 // restore nominal baudrate. Apparently this is ok for BREAK
93 this->pSerial->updateBaudRate(this->baudrate);
94
95 // send rest of frame (request frame: SYNC+ID+DATA[]+CHK; response frame: SYNC+ID)
96 this->pSerial->write(this->bufTx+1, this->lenTx-1);
97
98 // progress state
99 this->state = LIN_Master_Base::STATE_BODY;
100
101 } // BREAK duration expired
102
103 // print debug message
104 DEBUG_PRINT(2, " ");
105
106 // return state
107 return this->state;
108
109} // LIN_Master_HardwareSerial_ESP32::_sendFrame()
110
111
112
118 LIN_Master_Base::state_t LIN_Master_HardwareSerial_ESP32::_receiveFrame(void)
119{
120 // if state is wrong, exit immediately
121 if (this->state != LIN_Master_Base::STATE_BODY)
122 {
123 // print debug message
124 DEBUG_PRINT(1, "wrong state 0x%02X", this->state);
125
126 // set error state and return immediately
127 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_STATE);
128 this->state = LIN_Master_Base::STATE_DONE;
129 this->_disableTransmitter();
130 return this->state;
131 }
132
133 // optionally disable RS485 transmitter for slave response frames. Here, need to read BREAK as well due to delay of Serial.available()
134 if ((this->type == LIN_Master_Base::SLAVE_RESPONSE) && (this->pSerial->available() == 3))
135 this->_disableTransmitter();
136
137 // frame body received. Here, need to read BREAK as well due to delay of Serial.available()
138 if (this->pSerial->available() >= this->lenRx)
139 {
140 // store bytes in Rx
141 this->pSerial->readBytes(this->bufRx, this->lenRx);
142
143 // check frame for errors
144 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) this->_checkFrame());
145
146 // optionally disable RS485 transmitter after frame is completed
147 this->_disableTransmitter();
148
149 // progress state
150 this->state = LIN_Master_Base::STATE_DONE;
151
152 } // frame body received
153
154 // frame body received not yet received
155 else
156 {
157 // check for timeout
158 if (micros() - this->timeStart > this->timeoutFrame)
159 {
160 // print debug message
161 DEBUG_PRINT(1, "Rx timeout");
162
163 // set error state and return immediately
164 this->error = (LIN_Master_Base::error_t) ((int) this->error | (int) LIN_Master_Base::ERROR_TIMEOUT);
165 this->state = LIN_Master_Base::STATE_DONE;
166 this->_disableTransmitter();
167 return this->state;
168 }
169
170 } // not enough bytes received
171
172 // print debug message
173 DEBUG_PRINT(2, " ");
174
175 // return state
176 return this->state;
177
178} // LIN_Master_HardwareSerial_ESP32::_receiveFrame()
179
180
181
191 LIN_Master_HardwareSerial_ESP32::LIN_Master_HardwareSerial_ESP32(HardwareSerial &Interface, uint8_t PinRx, uint8_t PinTx, const char NameLIN[], const int8_t PinTxEN) :
192 LIN_Master_Base::LIN_Master_Base(NameLIN, PinTxEN)
193{
194 // Debug serial initialized in begin() -> no debug output here
195
196 // store pointer to used HW serial
197 this->pSerial = &Interface; // used serial interface
198 this->pinRx = PinRx; // receive pin
199 this->pinTx = PinTx; // transmit pin
200
201 // must not open connection here, else system resets
202
203} // LIN_Master_HardwareSerial_ESP32::LIN_Master_HardwareSerial_ESP32()
204
205
206
212 void LIN_Master_HardwareSerial_ESP32::begin(uint16_t Baudrate)
213{
214 // call base class method
215 LIN_Master_Base::begin(Baudrate);
216
217 // open serial interface incl. used pins with optional timeout
218 this->pSerial->end();
219 this->pSerial->begin(this->baudrate, SERIAL_8N1, this->pinRx, this->pinTx);
220 #if defined(LIN_MASTER_LIN_PORT_TIMEOUT) && (LIN_MASTER_LIN_PORT_TIMEOUT > 0)
221 uint32_t startMillis = millis();
222 while ((!(*(this->pSerial))) && (millis() - startMillis < LIN_MASTER_LIN_PORT_TIMEOUT));
223 #else
224 while(!(*(this->pSerial)));
225 #endif
226
227 // print debug message
228 DEBUG_PRINT(2, "ok");
229
230} // LIN_Master_HardwareSerial_ESP32::begin()
231
232
233
238 void LIN_Master_HardwareSerial_ESP32::end()
239{
240 // call base class method
241 LIN_Master_Base::end();
242
243 // close serial interface
244 this->pSerial->end();
245
246 // print debug message
247 DEBUG_PRINT(2, " ");
248
249} // LIN_Master_HardwareSerial_ESP32::end()
250
251#endif // ARDUINO_ARCH_ESP32
252
253/*-----------------------------------------------------------------------------
254 END OF FILE
255-----------------------------------------------------------------------------*/
LIN_master_HardwareSerial_ESP32.h
LIN master emulation library using a HardwareSerial interface of ESP32.
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::timePerByte
uint32_t timePerByte
time [us] per byte at specified baudrate
Definition LIN_master_Base.h:157
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_ESP32::LIN_Master_HardwareSerial_ESP32
LIN_Master_HardwareSerial_ESP32(HardwareSerial &Interface, uint8_t PinRx, uint8_t PinTx, const char NameLIN[]="Master", const int8_t PinTxEN=INT8_MIN)
Class constructor.
Definition LIN_master_HardwareSerial_ESP32.cpp:191
LIN_Master_HardwareSerial_ESP32::_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_ESP32.cpp:72
LIN_Master_HardwareSerial_ESP32::timeStartBreak
uint32_t timeStartBreak
micros() when BREAK was sent
Definition LIN_master_HardwareSerial_ESP32.h:43
LIN_Master_HardwareSerial_ESP32::begin
void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_master_HardwareSerial_ESP32.cpp:212
LIN_Master_HardwareSerial_ESP32::pinTx
uint8_t pinTx
pin used for transmit
Definition LIN_master_HardwareSerial_ESP32.h:42
LIN_Master_HardwareSerial_ESP32::end
void end(void)
Close serial interface.
Definition LIN_master_HardwareSerial_ESP32.cpp:238
LIN_Master_HardwareSerial_ESP32::pSerial
HardwareSerial * pSerial
serial interface used for LIN
Definition LIN_master_HardwareSerial_ESP32.h:40
LIN_Master_HardwareSerial_ESP32::_receiveFrame
LIN_Master_Base::state_t _receiveFrame(void)
Read and check LIN frame.
Definition LIN_master_HardwareSerial_ESP32.cpp:118
LIN_Master_HardwareSerial_ESP32::_sendBreak
LIN_Master_Base::state_t _sendBreak(void)
Send LIN break.
Definition LIN_master_HardwareSerial_ESP32.cpp:22
LIN_Master_HardwareSerial_ESP32::pinRx
uint8_t pinRx
pin used for receive
Definition LIN_master_HardwareSerial_ESP32.h:41