LIN_slave_portable_Arduino 1.3
Arduino library for Local Interconnect Network slave node emulation
Loading...
Searching...
No Matches
LIN_slave_SoftwareSerial.cpp
Go to the documentation of this file.
1
10// include files
12
13// assert platform which supports SoftwareSerial. Note: ARDUINO_ARCH_ESP32 requires library ESPSoftwareSerial
14#if defined(_LIN_SLAVE_SW_SERIAL_H_)
15
16
17/**************************
18 * PROTECTED METHODS
19**************************/
20
27{
28 // return BREAK detection flag
29 return this->flagBreak;
30
31} // LIN_Slave_SoftwareSerial::_getBreakFlag()
32
33
34
40{
41 // clear BREAK detection flag
42 this->flagBreak = false;
43
44} // LIN_Slave_SoftwareSerial::_resetBreakFlag()
45
46
47
48/**************************
49 * PUBLIC METHODS
50**************************/
51
64#if defined(ARDUINO_ARCH_RENESAS) // for Renesas core inverse logic is parameter for begin()
65 LIN_Slave_SoftwareSerial::LIN_Slave_SoftwareSerial(uint8_t PinRx, uint8_t PinTx, bool InverseLogic, uint16_t MinFramePause,
66 LIN_Slave_Base::version_t Version, const char NameLIN[], uint32_t TimeoutRx, const int8_t PinTxEN):
67 LIN_Slave_Base(Version, NameLIN, TimeoutRx, PinTxEN), SWSerial(PinRx, PinTx)
68#else
69 LIN_Slave_SoftwareSerial::LIN_Slave_SoftwareSerial(uint8_t PinRx, uint8_t PinTx, bool InverseLogic, uint16_t MinFramePause,
70 LIN_Slave_Base::version_t Version, const char NameLIN[], uint32_t TimeoutRx, const int8_t PinTxEN):
71 LIN_Slave_Base(Version, NameLIN, TimeoutRx, PinTxEN), SWSerial(PinRx, PinTx, InverseLogic)
72#endif
73{
74 // Debug serial initialized in begin() -> no debug output here
75
76 // store parameters in class variables
77 this->pinRx = PinRx;
78 this->pinTx = PinTx;
79 this->inverseLogic = InverseLogic;
80 this->minFramePause = MinFramePause;
81
82} // LIN_Slave_SoftwareSerial::LIN_Slave_SoftwareSerial()
83
84
85
91void LIN_Slave_SoftwareSerial::begin(uint16_t Baudrate)
92{
93 // call base class method
94 LIN_Slave_Base::begin(Baudrate);
95
96 // open serial interface incl. used pins. Timeout not required here
97 this->SWSerial.end();
98 #if defined(ARDUINO_ARCH_RENESAS)
99 this->SWSerial.begin(this->baudrate, SERIAL_8N1, this->inverseLogic);
100 #else
101 this->SWSerial.begin(this->baudrate);
102 #endif
103
104 // initialize variables
105 this->_resetBreakFlag();
106
107 // print debug message
108 DEBUG_PRINT(2, "ok");
109
110} // LIN_Slave_SoftwareSerial::begin()
111
112
113
119{
120 // call base class method
122
123 // close serial interface
124 this->SWSerial.end();
125
126 // print debug message
127 DEBUG_PRINT(2, " ");
128
129} // LIN_Slave_SoftwareSerial::end()
130
131
132
142{
143 // sync frames based on inter-frame pause (not standard compliant!)
144 static uint32_t usLastByte = 0;
145
146 // byte received -> check it
147 if (this->available())
148 {
149 // ESP32 & ESP8266 (BREAK is dropped due to missing stop bit): if SYNC=0x55 received and long time since last byte, start new frame
150 #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
151 if ((this->_serialPeek() == 0x55) && ((micros() - usLastByte) > this->minFramePause))
152 {
153 // set BREAK flag
154 this->flagBreak = true;
155
156 // print debug message
157 DEBUG_PRINT(3, "BRK detected");
158
159 }
160
161 // STM32 (BREAK is received, but value !=0x00): if byte received and long time since last byte, start new frame and remove byte from queue
162 #elif defined(ARDUINO_ARCH_STM32)
163 if ((micros() - usLastByte) > this->minFramePause)
164 {
165 // set BREAK flag
166 this->flagBreak = true;
167
168 // remove byte from FiFo buffer
169 this->_serialRead();
170
171 // print debug message
172 DEBUG_PRINT(3, "BRK detected");
173
174 }
175
176 // other architectures (BREAK is received): if BREAK=0x00 received and long time since last byte, start new frame and remove 0x00 from queue
177 #else
178 if ((this->_serialPeek() == 0x00) && ((micros() - usLastByte) > this->minFramePause))
179 {
180 // set BREAK flag
181 this->flagBreak = true;
182
183 // remove 0x00 from FiFo buffer
184 this->_serialRead();
185
186 // print debug message
187 DEBUG_PRINT(3, "BRK detected");
188
189 }
190 #endif
191
192 // store time of this receive
193 usLastByte = micros();
194
195 // call base-class handler
197
198 // SoftwareSerial is blocking while sending -> skip reading echo
200 {
201 // propagate to DONE immediately
203
204 // optionally disable RS485 transmitter
206 }
207
208 } // if byte received
209
210} // LIN_Slave_SoftwareSerial::handler()
211
212
213#endif // _LIN_SLAVE_SW_SERIAL_H_
214
215/*-----------------------------------------------------------------------------
216 END OF FILE
217-----------------------------------------------------------------------------*/
LIN slave emulation library using a generic SoftwareSerial interface (if available).
LIN slave node base class.
uint16_t baudrate
communication baudrate [Baud]
@ STATE_DONE
frame is completed
@ STATE_RECEIVING_ECHO
receiving slave response echo
void _disableTransmitter(void)
Disable RS485 transmitter (DE=low)
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
version_t
LIN protocol version.
LIN_Slave_Base::state_t state
status of LIN state machine
virtual void end(void)
Close serial interface.
virtual void handler(void)
Handle LIN protocol and call user-defined frame callbacks.
LIN slave node class via generic SoftwareSerial.
uint8_t _serialPeek(void)
peek next byte from Rx buffer
LIN_Slave_SoftwareSerial(uint8_t PinRx, uint8_t PinTx, bool InverseLogic=false, 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 void handler(void)
Handle LIN protocol and call user-defined frame handlers.
void begin(uint16_t Baudrate=19200)
Open serial interface.
void _resetBreakFlag(void)
Clear break detection flag.
void end(void)
Close serial interface.
uint8_t _serialRead(void)
read next byte from Rx buffer
bool available(void)
check if a byte is available in Rx buffer
bool _getBreakFlag(void)
Get break detection flag.