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
15
16// definition of static class variables (see https://stackoverflow.com/a/51091696)
17bool LIN_Slave_HardwareSerial_ESP32::flagBreak[LIN_SLAVE_ESP32_MAX_SERIAL];
18
19
20
21/**************************
22 * PRIVATE METHODS
23**************************/
24
25#if (LIN_SLAVE_ESP32_MAX_SERIAL >= 1)
32 void LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError0(hardwareSerial_error_t Err)
33 {
34 // on BREAK (=0x00 with framing error) set class variable and remove 0x00 from queue
35 if ((Serial.peek() == 0x00) && (Err == UART_BREAK_ERROR))
36 {
38 Serial.read();
39 }
40
41 } // LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError0()
42#endif
43
44
45
46#if (LIN_SLAVE_ESP32_MAX_SERIAL >= 2)
53 void LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError1(hardwareSerial_error_t Err)
54 {
55 // on BREAK (=0x00 with framing error) set class variable and remove 0x00 from queue
56 if ((Serial1.peek() == 0x00) && (Err == UART_BREAK_ERROR))
57 {
59 Serial1.read();
60 }
61
62 } // LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError1()
63#endif
64
65
66
67#if (LIN_SLAVE_ESP32_MAX_SERIAL >= 3)
74 void LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError2(hardwareSerial_error_t Err)
75 {
76 // on BREAK (=0x00 with framing error) set class variable and remove 0x00 from queue
77 if ((Serial2.peek() == 0x00) && (Err == UART_BREAK_ERROR))
78 {
80 Serial2.read();
81 }
82
83 } // LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError2()
84#endif
85
86
87
88/**************************
89 * PROTECTED METHODS
90**************************/
91
98{
99 // return BREAK detection flag of respective Serialx
101
102} // LIN_Slave_HardwareSerial_ESP32::_getBreakFlag()
103
104
105
111{
112 // clear BREAK detection flag of respective Serialx
114
115} // LIN_Slave_HardwareSerial_ESP32::_resetBreakFlag()
116
117
118
119/**************************
120 * PUBLIC METHODS
121**************************/
122
134LIN_Slave_HardwareSerial_ESP32::LIN_Slave_HardwareSerial_ESP32(HardwareSerial &Interface, uint8_t PinRx, uint8_t PinTx,
135 LIN_Slave_Base::version_t Version, const char NameLIN[], uint32_t TimeoutRx, const int8_t PinTxEN) :
136 LIN_Slave_Base::LIN_Slave_Base(Version, NameLIN, TimeoutRx, PinTxEN)
137{
138 // Debug serial initialized in begin() -> no debug output here
139
140 // store parameters in class variables
141 this->pSerial = &Interface; // pointer to used HW serial
142 this->pinRx = PinRx; // receive pin
143 this->pinTx = PinTx; // transmit pin
144
145} // LIN_Slave_HardwareSerial_ESP32::LIN_Slave_HardwareSerial_ESP32()
146
147
148
155{
156 // call base class method
157 LIN_Slave_Base::begin(Baudrate);
158
159 // open serial interface incl. used pins
160 pSerial->end();
161 pSerial->begin(this->baudrate, SERIAL_8N1, this->pinRx, this->pinTx);
162 while(!(*pSerial)) { }
163
164 // Attach corresponding error callback to Serialx receive handler
165 #if (LIN_SLAVE_ESP32_MAX_SERIAL >= 1)
166 if (pSerial == &Serial0)
167 {
169 pSerial->onReceiveError(LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError0);
170 }
171 #endif
172 #if (LIN_SLAVE_ESP32_MAX_SERIAL >= 2)
173 if (pSerial == &Serial1)
174 {
176 pSerial->onReceiveError(LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError1);
177 }
178 #endif
179 #if (LIN_SLAVE_ESP32_MAX_SERIAL >= 3)
180 if (pSerial == &Serial2)
181 {
183 pSerial->onReceiveError(LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError2);
184 }
185 #endif
186
187 // initialize variables
188 this->_resetBreakFlag();
189
190 // optional debug output (debug level 2)
191 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 2)
192 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN);
193 LIN_SLAVE_DEBUG_SERIAL.println(": LIN_Slave_HardwareSerial_ESP32::begin()");
194 #endif
195
196} // LIN_Slave_HardwareSerial_ESP32::begin()
197
198
199
205{
206 // call base class method
208
209 // close serial interface
210 pSerial->end();
211
212 // optional debug output (debug level 2)
213 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 2)
214 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN);
215 LIN_SLAVE_DEBUG_SERIAL.println(": LIN_Slave_HardwareSerial_ESP32::end()");
216 #endif
217
218} // LIN_Slave_HardwareSerial_ESP32::end()
219
220#endif // ARDUINO_ARCH_ESP32
221
222/*-----------------------------------------------------------------------------
223 END OF FILE
224-----------------------------------------------------------------------------*/
LIN slave emulation library using a HardwareSerial interface of ESP32.
LIN slave node base class.
uint16_t baudrate
communication baudrate [Baud]
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
char nameLIN[LIN_SLAVE_BUFLEN_NAME]
LIN node name, e.g. for debug.
version_t
LIN protocol version.
virtual void end(void)
Close serial interface.
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.
HardwareSerial * pSerial
pointer to serial interface used for LIN
static bool flagBreak[LIN_SLAVE_ESP32_MAX_SERIAL]
break flags for Serial0..N
void begin(uint16_t Baudrate=19200)
Open serial interface.
bool _getBreakFlag(void)
Get break detection flag.
void _resetBreakFlag(void)
Clear break detection flag.
uint8_t idxSerial
index to flagBreak[] of this instance