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

Loading...
Searching...
No Matches
LIN_slave_NeoHWSerial_AVR.cpp
Go to the documentation of this file.
1
10// include files
11#include <LIN_slave_NeoHWSerial_AVR.h>
12
13// optional on AVR, see LIN_slave_NeoHWSerial_AVR.h
14#if defined(_LIN_SLAVE_NEOHWSERIAL_AVR_H_)
15
16
17// definition of static class variables (see https://stackoverflow.com/a/51091696)
18 bool LIN_Slave_NeoHWSerial_AVR::flagBreak[];
19
20
21/**************************
22 * PRIVATE METHODS
23**************************/
24
25#if defined(HAVE_HWSERIAL0)
26
34 bool LIN_Slave_NeoHWSerial_AVR::_onSerialReceive0(uint8_t byte, uint8_t status)
35 {
36 // on BREAK (=0x00 with framing error) set class variable and don't store in queue (return false)
37 if ((byte ==0x00) && (status & (0x01<< FE0)))
38 {
39 // set BREAK flag for Serial0
40 (LIN_Slave_NeoHWSerial_AVR::flagBreak)[0] = true;
41
42 // print debug message
43 DEBUG_PRINT_STATIC(3, "Rx=0x%02X, BRK", byte);
44
45 // return true -> byte is dropped
46 return false;
47 }
48
49 // print debug message
50 DEBUG_PRINT_STATIC(3, "Rx=0x%02X", byte);
51
52 // return true -> byte is stored in Serial0 buffer
53 return true;
54
55 } // LIN_Slave_NeoHWSerial_AVR::_onSerialReceive0()
56
57#endif // HAVE_HWSERIAL0
58
59
60#if defined(HAVE_HWSERIAL1)
61
69 bool LIN_Slave_NeoHWSerial_AVR::_onSerialReceive1(uint8_t byte, uint8_t status)
70 {
71 // on BREAK (=0x00 with framing error) set class variable and don't store in queue (return false)
72 if ((byte ==0x00) && (status & (0x01<< FE0)))
73 {
74 // set BREAK flag for Serial1
75 (LIN_Slave_NeoHWSerial_AVR::flagBreak)[1] = true;
76
77 // print debug message
78 DEBUG_PRINT_STATIC(3, "Rx=0x%02X, BRK", byte);
79
80 // return true -> byte is dropped
81 return false;
82 }
83
84 // print debug message
85 DEBUG_PRINT_STATIC(3, "Rx=0x%02X", byte);
86
87 // return true -> byte is stored in Serial1 buffer
88 return true;
89
90 } // LIN_Slave_NeoHWSerial_AVR::_onSerialReceive1()
91
92#endif // HAVE_HWSERIAL1
93
94
95#if defined(HAVE_HWSERIAL2)
96
104 bool LIN_Slave_NeoHWSerial_AVR::_onSerialReceive2(uint8_t byte, uint8_t status)
105 {
106 // on BREAK (=0x00 with framing error) set class variable and don't store in queue (return false)
107 if ((byte ==0x00) && (status & (0x01<< FE0)))
108 {
109 // set BREAK flag for Serial2
110 (LIN_Slave_NeoHWSerial_AVR::flagBreak)[2] = true;
111
112 // print debug message
113 DEBUG_PRINT_STATIC(3, "Rx=0x%02X, BRK", byte);
114
115 // return true -> byte is dropped
116 return false;
117 }
118
119 // print debug message
120 DEBUG_PRINT_STATIC(3, "Rx=0x%02X", byte);
121
122 // return true -> byte is stored in Serial2 buffer
123 return true;
124
125 } // LIN_Slave_NeoHWSerial_AVR::_onSerialReceive2()
126
127#endif // HAVE_HWSERIAL2
128
129
130#if defined(HAVE_HWSERIAL3)
131
139 bool LIN_Slave_NeoHWSerial_AVR::_onSerialReceive3(uint8_t byte, uint8_t status)
140 {
141 // on BREAK (=0x00 with framing error) set class variable and don't store in queue (return false)
142 if ((byte ==0x00) && (status & (0x01<< FE0)))
143 {
144 // set BREAK flag for Serial3
145 (LIN_Slave_NeoHWSerial_AVR::flagBreak)[3] = true;
146
147 // print debug message
148 DEBUG_PRINT_STATIC(3, "Rx=0x%02X, BRK", byte);
149
150 // return true -> byte is dropped
151 return false;
152 }
153
154 // print debug message
155 DEBUG_PRINT_STATIC(3, "Rx=0x%02X", byte);
156
157 // return true -> byte is stored in Serial3 buffer
158 return true;
159
160 } // LIN_Slave_NeoHWSerial_AVR::_onSerialReceive3()
161
162#endif // HAVE_HWSERIAL3
163
164
165
166/**************************
167 * PROTECTED METHODS
168**************************/
169
175 bool LIN_Slave_NeoHWSerial_AVR::_getBreakFlag()
176{
177 // return BREAK detection flag of respective Serialx
178 return (this->flagBreak)[this->idxSerial];
179
180} // LIN_Slave_NeoHWSerial_AVR::_getBreakFlag()
181
182
183
188 void LIN_Slave_NeoHWSerial_AVR::_resetBreakFlag()
189{
190 // clear BREAK detection flag of respective Serialx
191 (this->flagBreak)[this->idxSerial] = false;
192
193} // LIN_Slave_NeoHWSerial_AVR::_resetBreakFlag()
194
195
196
197/**************************
198 * PUBLIC METHODS
199**************************/
200
210 LIN_Slave_NeoHWSerial_AVR::LIN_Slave_NeoHWSerial_AVR(NeoHWSerial &Interface,
211 LIN_Slave_Base::version_t Version, const char NameLIN[], uint32_t TimeoutRx, const int8_t PinTxEN) :
212 LIN_Slave_Base::LIN_Slave_Base(Version, NameLIN, TimeoutRx, PinTxEN)
213{
214 // Debug serial initialized in begin() -> no debug output here
215
216 // store parameters in class variables
217 this->pSerial = &Interface; // pointer to used HW serial
218
219} // LIN_Slave_NeoHWSerial_AVR::LIN_Slave_NeoHWSerial_AVR()
220
221
222
228 void LIN_Slave_NeoHWSerial_AVR::begin(uint16_t Baudrate)
229{
230 // call base class method
231 LIN_Slave_Base::begin(Baudrate);
232
233 // open serial interface incl. used pins with optional timeout
234 this->pSerial->end();
235 this->pSerial->begin(this->baudrate);
236 #if defined(LIN_SLAVE_LIN_PORT_TIMEOUT) && (LIN_SLAVE_LIN_PORT_TIMEOUT > 0)
237 uint32_t startMillis = millis();
238 while ((!(*(this->pSerial))) && (millis() - startMillis < LIN_SLAVE_LIN_PORT_TIMEOUT));
239 #else
240 while(!(*(this->pSerial)));
241 #endif
242
243 // Attach corresponding error callback to Serialx receive handler
244 #if defined(HAVE_HWSERIAL0)
245 if (this->pSerial == &NeoSerial)
246 {
247 this->idxSerial = 0;
248 this->pSerial->attachInterrupt(this->_onSerialReceive0);
249 }
250 #endif
251 #if defined(HAVE_HWSERIAL1)
252 if (this->pSerial == &NeoSerial1)
253 {
254 this->idxSerial = 1;
255 pSerial->attachInterrupt(this->_onSerialReceive1);
256 }
257 #endif
258 #if defined(HAVE_HWSERIAL2)
259 if (this->pSerial == &NeoSerial2)
260 {
261 this->idxSerial = 2;
262 pSerial->attachInterrupt(this->_onSerialReceive2);
263 }
264 #endif
265 #if defined(HAVE_HWSERIAL3)
266 if (this->pSerial == &NeoSerial3)
267 {
268 this->idxSerial = 3;
269 pSerial->attachInterrupt(this->_onSerialReceive3);
270 }
271 #endif
272
273 // initialize variables
274 this->_resetBreakFlag();
275
276 // optional debug output (debug level 2)
277 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 2)
278 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN);
279 LIN_SLAVE_DEBUG_SERIAL.println(": LIN_Slave_NeoHWSerial_AVR::begin()");
280 #endif
281
282} // LIN_Slave_NeoHWSerial_AVR::begin()
283
284
285
290 void LIN_Slave_NeoHWSerial_AVR::end()
291{
292 // call base class method
293 LIN_Slave_Base::end();
294
295 // close serial interface
296 this->pSerial->end();
297
298 // print debug message
299 DEBUG_PRINT(2, " ");
300
301} // LIN_Slave_NeoHWSerial_AVR::end()
302
303#endif // ARDUINO_ARCH_AVR
304
305/*-----------------------------------------------------------------------------
306 END OF FILE
307-----------------------------------------------------------------------------*/
LIN_slave_NeoHWSerial_AVR.h
LIN slave emulation library using a NeoHWSerial interface of AVR.
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::nameLIN
char nameLIN[LIN_SLAVE_BUFLEN_NAME]
LIN node name, e.g. for debug.
Definition LIN_slave_Base.h:194
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_NeoHWSerial_AVR::LIN_Slave_NeoHWSerial_AVR
LIN_Slave_NeoHWSerial_AVR(NeoHWSerial &Interface, 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_NeoHWSerial_AVR.cpp:210
LIN_Slave_NeoHWSerial_AVR::_getBreakFlag
bool _getBreakFlag(void)
Get break detection flag.
Definition LIN_slave_NeoHWSerial_AVR.cpp:175
LIN_Slave_NeoHWSerial_AVR::_resetBreakFlag
void _resetBreakFlag(void)
Clear break detection flag.
Definition LIN_slave_NeoHWSerial_AVR.cpp:188
LIN_Slave_NeoHWSerial_AVR::begin
void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_slave_NeoHWSerial_AVR.cpp:228
LIN_Slave_NeoHWSerial_AVR::end
void end(void)
Close serial interface.
Definition LIN_slave_NeoHWSerial_AVR.cpp:290