| LIN_slave_portable_Arduino 1.4 Arduino library for Local Interconnect Network slave node emulation

Loading...
Searching...
No Matches
LIN_slave_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_slave_HardwareSerial_ESP32.h>
15
16
17// definition of static class variables (see https://stackoverflow.com/a/51091696)
18 bool LIN_Slave_HardwareSerial_ESP32::flagBreak[LIN_SLAVE_ESP32_MAX_SERIAL];
19
20
21
22/**************************
23 * PRIVATE METHODS
24**************************/
25
26#if (LIN_SLAVE_ESP32_MAX_SERIAL >= 1)
27
34 void LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError0(hardwareSerial_error_t Err)
35 {
36 // on BREAK (=0x00 with framing error) set class variable and remove 0x00 from queue
37 if ((Serial.peek() == 0x00) && (Err == UART_BREAK_ERROR))
38 {
39 // set BREAK flag for Serial0
40 (LIN_Slave_HardwareSerial_ESP32::flagBreak)[0] = true;
41
42 // remove 0x00 from queue
43 uint8_t byte = Serial.read();
44
45 // avoid unused variable warning in case of no debug output
46 (void) byte;
47
48 // print debug message
49 DEBUG_PRINT_STATIC(3, "Rx=0x%02X, BRK", byte);
50
51 } // if BREAK detected
52
53 } // LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError0()
54
55#endif // LIN_SLAVE_ESP32_MAX_SERIAL >= 1
56
57
58
59#if (LIN_SLAVE_ESP32_MAX_SERIAL >= 2)
60
67 void LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError1(hardwareSerial_error_t Err)
68 {
69 // on BREAK (=0x00 with framing error) set class variable and remove 0x00 from queue
70 if ((Serial1.peek() == 0x00) && (Err == UART_BREAK_ERROR))
71 {
72 // set BREAK flag for Serial1
73 (LIN_Slave_HardwareSerial_ESP32::flagBreak)[1] = true;
74
75 // remove 0x00 from queue
76 uint8_t byte = Serial1.read();
77
78 // avoid unused variable warning in case of no debug output
79 (void) byte;
80
81 // print debug message
82 DEBUG_PRINT_STATIC(3, "Rx=0x%02X, BRK", byte);
83
84 } // if BREAK detected
85
86 } // LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError1()
87
88#endif // LIN_SLAVE_ESP32_MAX_SERIAL >= 2
89
90
91#if (LIN_SLAVE_ESP32_MAX_SERIAL >= 3)
92
99 void LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError2(hardwareSerial_error_t Err)
100 {
101 // on BREAK (=0x00 with framing error) set class variable and remove 0x00 from queue
102 if ((Serial2.peek() == 0x00) && (Err == UART_BREAK_ERROR))
103 {
104 // set BREAK flag for Serial2
105 (LIN_Slave_HardwareSerial_ESP32::flagBreak)[2] = true;
106
107 // remove 0x00 from queue
108 uint8_t byte = Serial2.read();
109
110 // avoid unused variable warning in case of no debug output
111 (void) byte;
112
113 // print debug message
114 DEBUG_PRINT_STATIC(3, "Rx=0x%02X, BRK", byte);
115
116 } // if BREAK detected
117
118 } // LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError2()
119
120#endif // LIN_SLAVE_ESP32_MAX_SERIAL >= 3
121
122
123
124/**************************
125 * PROTECTED METHODS
126**************************/
127
133 bool LIN_Slave_HardwareSerial_ESP32::_getBreakFlag()
134{
135 // return BREAK detection flag of respective Serialx
136 return (this->flagBreak)[this->idxSerial];
137
138} // LIN_Slave_HardwareSerial_ESP32::_getBreakFlag()
139
140
141
146 void LIN_Slave_HardwareSerial_ESP32::_resetBreakFlag()
147{
148 // clear BREAK detection flag of respective Serialx
149 (this->flagBreak)[this->idxSerial] = false;
150
151} // LIN_Slave_HardwareSerial_ESP32::_resetBreakFlag()
152
153
154
155/**************************
156 * PUBLIC METHODS
157**************************/
158
170 LIN_Slave_HardwareSerial_ESP32::LIN_Slave_HardwareSerial_ESP32(HardwareSerial &Interface, uint8_t PinRx, uint8_t PinTx,
171 LIN_Slave_Base::version_t Version, const char NameLIN[], uint32_t TimeoutRx, const int8_t PinTxEN) :
172 LIN_Slave_Base::LIN_Slave_Base(Version, NameLIN, TimeoutRx, PinTxEN)
173{
174 // Debug serial initialized in begin() -> no debug output here
175
176 // store parameters in class variables
177 this->pSerial = &Interface; // pointer to used HW serial
178 this->pinRx = PinRx; // receive pin
179 this->pinTx = PinTx; // transmit pin
180
181} // LIN_Slave_HardwareSerial_ESP32::LIN_Slave_HardwareSerial_ESP32()
182
183
184
190 void LIN_Slave_HardwareSerial_ESP32::begin(uint16_t Baudrate)
191{
192 // call base class method
193 LIN_Slave_Base::begin(Baudrate);
194
195 // open serial interface incl. used pins with optional timeout
196 this->pSerial->end();
197 this->pSerial->begin(this->baudrate, SERIAL_8N1, this->pinRx, this->pinTx);
198 #if defined(LIN_SLAVE_LIN_PORT_TIMEOUT) && (LIN_SLAVE_LIN_PORT_TIMEOUT > 0)
199 uint32_t startMillis = millis();
200 while ((!(*(this->pSerial))) && (millis() - startMillis < LIN_SLAVE_LIN_PORT_TIMEOUT));
201 #else
202 while(!(*(this->pSerial)));
203 #endif
204
205 // Attach corresponding error callback to Serialx receive handler
206 #if (LIN_SLAVE_ESP32_MAX_SERIAL >= 1)
207 if (this->pSerial == &Serial0)
208 {
209 this->idxSerial = 0;
210 this->pSerial->onReceiveError(this->_onSerialReceiveError0);
211 }
212 #endif
213 #if (LIN_SLAVE_ESP32_MAX_SERIAL >= 2)
214 if (this->pSerial == &Serial1)
215 {
216 this->idxSerial = 1;
217 this->pSerial->onReceiveError(this->_onSerialReceiveError1);
218 }
219 #endif
220 #if (LIN_SLAVE_ESP32_MAX_SERIAL >= 3)
221 if (this->pSerial == &Serial2)
222 {
223 this->idxSerial = 2;
224 this->pSerial->onReceiveError(this->_onSerialReceiveError2);
225 }
226 #endif
227
228 // initialize variables
229 this->_resetBreakFlag();
230
231 // print debug message
232 DEBUG_PRINT(2, "ok");
233
234} // LIN_Slave_HardwareSerial_ESP32::begin()
235
236
237
242 void LIN_Slave_HardwareSerial_ESP32::end()
243{
244 // call base class method
245 LIN_Slave_Base::end();
246
247 // close serial interface
248 this->pSerial->end();
249
250 // print debug message
251 DEBUG_PRINT(2, " ");
252
253} // LIN_Slave_HardwareSerial_ESP32::end()
254
255#endif // ARDUINO_ARCH_ESP32
256
257/*-----------------------------------------------------------------------------
258 END OF FILE
259-----------------------------------------------------------------------------*/
LIN_slave_HardwareSerial_ESP32.h
LIN slave emulation library using a HardwareSerial interface of ESP32.
LIN_Slave_Base
LIN slave node base class.
Definition LIN_slave_Base.h:106
LIN_Slave_Base::baudrate
uint16_t baudrate
communication baudrate [Baud]
Definition LIN_slave_Base.h:173
LIN_Slave_Base::begin
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_slave_Base.cpp:163
LIN_Slave_Base::version_t
version_t
LIN protocol version.
Definition LIN_slave_Base.h:112
LIN_Slave_Base::end
virtual void end(void)
Close serial interface.
Definition LIN_slave_Base.cpp:201
LIN_Slave_HardwareSerial_ESP32::pinTx
uint8_t pinTx
pin used for transmit
Definition LIN_slave_HardwareSerial_ESP32.h:60
LIN_Slave_HardwareSerial_ESP32::LIN_Slave_HardwareSerial_ESP32
LIN_Slave_HardwareSerial_ESP32(HardwareSerial &Interface, uint8_t PinRx, uint8_t PinTx, LIN_Slave_Base::version_t Version=LIN_Slave_Base::LIN_V2, const char NameLIN[]="Slave", uint32_t TimeoutRx=1500L, const int8_t PinTxEN=INT8_MIN)
Class constructor.
Definition LIN_slave_HardwareSerial_ESP32.cpp:170
LIN_Slave_HardwareSerial_ESP32::end
void end(void)
Close serial interface.
Definition LIN_slave_HardwareSerial_ESP32.cpp:242
LIN_Slave_HardwareSerial_ESP32::pSerial
HardwareSerial * pSerial
pointer to serial interface used for LIN
Definition LIN_slave_HardwareSerial_ESP32.h:58
LIN_Slave_HardwareSerial_ESP32::flagBreak
static bool flagBreak[LIN_SLAVE_ESP32_MAX_SERIAL]
break flags for Serial0..N
Definition LIN_slave_HardwareSerial_ESP32.h:62
LIN_Slave_HardwareSerial_ESP32::begin
void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_slave_HardwareSerial_ESP32.cpp:190
LIN_Slave_HardwareSerial_ESP32::pinRx
uint8_t pinRx
pin used for receive
Definition LIN_slave_HardwareSerial_ESP32.h:59
LIN_Slave_HardwareSerial_ESP32::_getBreakFlag
bool _getBreakFlag(void)
Get break detection flag.
Definition LIN_slave_HardwareSerial_ESP32.cpp:133
LIN_Slave_HardwareSerial_ESP32::_resetBreakFlag
void _resetBreakFlag(void)
Clear break detection flag.
Definition LIN_slave_HardwareSerial_ESP32.cpp:146
LIN_Slave_HardwareSerial_ESP32::idxSerial
uint8_t idxSerial
index to flagBreak[] of this instance
Definition LIN_slave_HardwareSerial_ESP32.h:61