| 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// definition of static class variables (see https://stackoverflow.com/a/51091696)
17 bool 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 {
37 (LIN_Slave_HardwareSerial_ESP32::flagBreak)[0] = true;
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 {
58 (LIN_Slave_HardwareSerial_ESP32::flagBreak)[1] = true;
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 {
79 (LIN_Slave_HardwareSerial_ESP32::flagBreak)[2] = true;
80 Serial2.read();
81 }
82
83 } // LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError2()
84#endif
85
86
87
88/**************************
89 * PROTECTED METHODS
90**************************/
91
97 bool LIN_Slave_HardwareSerial_ESP32::_getBreakFlag()
98{
99 // return BREAK detection flag of respective Serialx
100 return (LIN_Slave_HardwareSerial_ESP32::flagBreak)[LIN_Slave_HardwareSerial_ESP32::idxSerial];
101
102} // LIN_Slave_HardwareSerial_ESP32::_getBreakFlag()
103
104
105
110 void LIN_Slave_HardwareSerial_ESP32::_resetBreakFlag()
111{
112 // clear BREAK detection flag of respective Serialx
113 (LIN_Slave_HardwareSerial_ESP32::flagBreak)[LIN_Slave_HardwareSerial_ESP32::idxSerial] = false;
114
115} // LIN_Slave_HardwareSerial_ESP32::_resetBreakFlag()
116
117
118
119/**************************
120 * PUBLIC METHODS
121**************************/
122
134 LIN_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
154 void LIN_Slave_HardwareSerial_ESP32::begin(uint16_t Baudrate)
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 {
168 LIN_Slave_HardwareSerial_ESP32::idxSerial = 0;
169 pSerial->onReceiveError(LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError0);
170 }
171 #endif
172 #if (LIN_SLAVE_ESP32_MAX_SERIAL >= 2)
173 if (pSerial == &Serial1)
174 {
175 LIN_Slave_HardwareSerial_ESP32::idxSerial = 1;
176 pSerial->onReceiveError(LIN_Slave_HardwareSerial_ESP32::_onSerialReceiveError1);
177 }
178 #endif
179 #if (LIN_SLAVE_ESP32_MAX_SERIAL >= 3)
180 if (pSerial == &Serial2)
181 {
182 LIN_Slave_HardwareSerial_ESP32::idxSerial = 2;
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
204 void LIN_Slave_HardwareSerial_ESP32::end()
205{
206 // call base class method
207 LIN_Slave_Base::end();
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_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:52
LIN_Slave_Base::baudrate
uint16_t baudrate
communication baudrate [Baud]
Definition LIN_slave_Base.h:119
LIN_Slave_Base::begin
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_slave_Base.cpp:171
LIN_Slave_Base::nameLIN
char nameLIN[LIN_SLAVE_BUFLEN_NAME]
LIN node name, e.g. for debug.
Definition LIN_slave_Base.h:140
LIN_Slave_Base::version_t
version_t
LIN protocol version.
Definition LIN_slave_Base.h:58
LIN_Slave_Base::end
virtual void end(void)
Close serial interface.
Definition LIN_slave_Base.cpp:209
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:134
LIN_Slave_HardwareSerial_ESP32::end
void end(void)
Close serial interface.
Definition LIN_slave_HardwareSerial_ESP32.cpp:204
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:154
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:97
LIN_Slave_HardwareSerial_ESP32::_resetBreakFlag
void _resetBreakFlag(void)
Clear break detection flag.
Definition LIN_slave_HardwareSerial_ESP32.cpp:110
LIN_Slave_HardwareSerial_ESP32::idxSerial
uint8_t idxSerial
index to flagBreak[] of this instance
Definition LIN_slave_HardwareSerial_ESP32.h:61