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 on AVR, see LIN_slave_NeoHWSerial_AVR.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 with optional timeout
89 this->pSerial->begin(this->baudrate);
90 #if defined(LIN_SLAVE_LIN_PORT_TIMEOUT) && (LIN_SLAVE_LIN_PORT_TIMEOUT > 0)
91 uint32_t startMillis = millis();
92 while ((!(*(this->pSerial))) && (millis() - startMillis < LIN_SLAVE_LIN_PORT_TIMEOUT));
93 #else
94 while(!(*(this->pSerial)));
95 #endif
96
97 // initialize variables
98 this->_resetBreakFlag();
99
100 // print debug message
101 DEBUG_PRINT(2, "ok");
102
103} // LIN_Slave_HardwareSerial::begin()
104
105
106
112{
113 // call base class method
115
116 // close serial interface
117 this->pSerial->end();
118
119 // print debug message
120 DEBUG_PRINT(2, " ");
121
122} // LIN_Slave_HardwareSerial::end()
123
124
125
133{
134 // sync frames based on inter-frame pause (not standard compliant!)
135 static uint32_t usLastByte = 0;
136
137 // print debug message
138 //DEBUG_PRINT(3, "state=%d", (int) this->state);
139
140 // byte received -> check it
141 if (this->pSerial->available())
142 {
143 // 0x00 received and long time since last byte (=BREAK) -> start new frame and remove 0x00 from queue
144 if ((this->pSerial->peek() == 0x00) && ((micros() - usLastByte) > this->minFramePause))
145 {
146 this->flagBreak = true;
147 this->pSerial->read();
148 }
149
150 // store time of this receive
151 usLastByte = micros();
152
153 // call base-class handler
155
156 } // if byte received
157
158} // LIN_Slave_HardwareSerial::handler()
159
160#endif // !ARDUINO_ARCH_AVR
161
162/*-----------------------------------------------------------------------------
163 END OF FILE
164-----------------------------------------------------------------------------*/
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.
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!)