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