LIN_slave_portable_Arduino 1.4
Arduino library for Local Interconnect Network slave node emulation
Loading...
Searching...
No Matches
LIN_slave_HardwareSerial.cpp
Go to the documentation of this file.
1
10// include files
12
13// optional file, see LIN_slave_HardwareSerial.h
14#if defined(_LIN_SLAVE_HW_SERIAL_H_)
15
16
17/**************************
18 * PROTECTED METHODS
19**************************/
20
27{
28 // return BREAK detection flag
29 return this->flagBreak;
30
31} // LIN_Slave_HardwareSerial::_getBreakFlag()
32
33
34
40{
41 // clear BREAK detection flag
42 this->flagBreak = false;
43
44} // LIN_Slave_HardwareSerial::_resetBreakFlag()
45
46
47
48/**************************
49 * PUBLIC METHODS
50**************************/
51
62LIN_Slave_HardwareSerial::LIN_Slave_HardwareSerial(HardwareSerial &Interface, uint16_t MinFramePause,
63 LIN_Slave_Base::version_t Version, const char NameLIN[], uint32_t TimeoutRx, const int8_t PinTxEN) :
64 LIN_Slave_Base::LIN_Slave_Base(Version, NameLIN, TimeoutRx, PinTxEN)
65{
66 // Debug serial initialized in begin() -> no debug output here
67
68 // store parameters in class variables
69 this->pSerial = &Interface;
70 this->minFramePause = MinFramePause;
71
72 // must not open connection here, else (at least) ESP32 and ESP8266 fail
73
74} // LIN_Slave_HardwareSerial::LIN_Slave_HardwareSerial()
75
76
77
83void LIN_Slave_HardwareSerial::begin(uint16_t Baudrate)
84{
85 // call base class method
86 LIN_Slave_Base::begin(Baudrate);
87
88 // open serial interface
89 pSerial->begin(this->baudrate);
90 while(!(*pSerial));
91
92 // initialize variables
93 this->_resetBreakFlag();
94
95 // optional debug output (debug level 2)
96 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 2)
97 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN);
98 LIN_SLAVE_DEBUG_SERIAL.println(": LIN_Slave_HardwareSerial::begin()");
99 #endif
100
101} // LIN_Slave_HardwareSerial::begin()
102
103
104
110{
111 // call base class method
113
114 // close serial interface
115 pSerial->end();
116
117 // optional debug output (debug level 2)
118 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 2)
119 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN);
120 LIN_SLAVE_DEBUG_SERIAL.println(": LIN_Slave_HardwareSerial::end()");
121 #endif
122
123} // LIN_Slave_HardwareSerial::end()
124
125
126
134{
135 // sync frames based on inter-frame pause (not standard compliant!)
136 static uint32_t usLastByte = 0;
137
138 // byte received -> check it
139 if (pSerial->available())
140 {
141 // if 0x00 received and long time since last byte, start new frame and remove 0x00 from queue
142 if ((pSerial->peek() == 0x00) && ((micros() - usLastByte) > this->minFramePause))
143 {
144 this->flagBreak = true;
145 pSerial->read();
146 }
147
148 // store time of this receive
149 usLastByte = micros();
150
151 // call base-class handler
153
154 } // if byte received
155
156} // LIN_Slave_HardwareSerial::handler()
157
158#endif // !ARDUINO_ARCH_AVR
159
160/*-----------------------------------------------------------------------------
161 END OF FILE
162-----------------------------------------------------------------------------*/
LIN slave emulation library using a generic HardwareSerial interface.
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.
virtual void handler(void)
Handle LIN protocol and call user-defined frame callbacks.
HardwareSerial * pSerial
pointer to serial interface used for LIN
bool flagBreak
a break was detected, is set in handle
void _resetBreakFlag(void)
Clear break detection flag.
virtual void handler(void)
Handle LIN protocol and call user-defined frame handlers.
void begin(uint16_t Baudrate=19200)
Open serial interface.
void end(void)
Close serial interface.
LIN_Slave_HardwareSerial(HardwareSerial &Interface, uint16_t MinFramePause=1000L, 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.
virtual bool _getBreakFlag(void)
Get break detection flag.
uint16_t minFramePause
min. inter-frame pause [us] to start new frame (not standard compliant!)