LIN_slave_portable_Arduino 1.4
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// assert platform which supports SoftwareSerial. Note: ARDUINO_ARCH_ESP32 requires library ESPSoftwareSerial
11#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
12
13// include files
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
64LIN_Slave_SoftwareSerial::LIN_Slave_SoftwareSerial(uint8_t PinRx, uint8_t PinTx, bool InverseLogic, uint16_t MinFramePause,
65 LIN_Slave_Base::version_t Version, const char NameLIN[], uint32_t TimeoutRx, const int8_t PinTxEN):
66 LIN_Slave_Base(Version, NameLIN, TimeoutRx, PinTxEN), SWSerial(PinRx, PinTx, InverseLogic)
67{
68 // Debug serial initialized in begin() -> no debug output here
69
70 // store parameters in class variables
71 this->pinRx = PinRx;
72 this->pinTx = PinTx;
73 this->inverseLogic = InverseLogic;
74 this->minFramePause = MinFramePause;
75
76} // LIN_Slave_SoftwareSerial::LIN_Slave_SoftwareSerial()
77
78
79
85void LIN_Slave_SoftwareSerial::begin(uint16_t Baudrate)
86{
87 // call base class method
88 LIN_Slave_Base::begin(Baudrate);
89
90 // open serial interface incl. used pins
91 this->SWSerial.end();
92 this->SWSerial.begin(this->baudrate);
93
94 // initialize variables
95 this->_resetBreakFlag();
96
97 // optional debug output (debug level 2)
98 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 2)
99 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN);
100 LIN_SLAVE_DEBUG_SERIAL.println(": LIN_Slave_SoftwareSerial::begin()");
101 #endif
102
103} // LIN_Slave_SoftwareSerial::begin()
104
105
106
112{
113 // call base class method
115
116 // close serial interface
117 this->SWSerial.end();
118
119 // optional debug output (debug level 2)
120 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 2)
121 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN);
122 LIN_SLAVE_DEBUG_SERIAL.println(": LIN_Slave_SoftwareSerial::end()");
123 #endif
124
125} // LIN_Slave_SoftwareSerial::end()
126
127
128
138{
139 // sync frames based on inter-frame pause (not standard compliant!)
140 static uint32_t usLastByte = 0;
141
142 // byte received -> check it
143 if (this->available())
144 {
145 // ESP32 & ESP8266 (BREAK is dropped due to missing stop bit): if SYNC=0x55 received and long time since last byte, start new frame
146 #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
147 if ((this->_serialPeek() == 0x55) && ((micros() - usLastByte) > this->minFramePause))
148 {
149 this->flagBreak = true;
150 }
151
152 // other architectures (BREAK is received): if BREAK=0x00 received and long time since last byte, start new frame and remove 0x00 from queue
153 #else
154 if ((this->_serialPeek() == 0x00) && ((micros() - usLastByte) > this->minFramePause))
155 {
156 this->flagBreak = true;
157 this->_serialRead();
158 }
159 #endif
160
161 // store time of this receive
162 usLastByte = micros();
163
164 // call base-class handler
166
167 // SoftwareSerial is blocking while sending -> skip reading echo
169 {
170 // propagate to DONE immediately
172
173 // optionally disable RS485 transmitter
175 }
176
177 } // if byte received
178
179} // LIN_Slave_SoftwareSerial::handler()
180
181
182#endif // ARDUINO_ARCH_AVR || ARDUINO_ARCH_ESP8266 || ARDUINO_ARCH_ESP32
183
184/*-----------------------------------------------------------------------------
185 END OF FILE
186-----------------------------------------------------------------------------*/
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.
char nameLIN[LIN_SLAVE_BUFLEN_NAME]
LIN node name, e.g. for debug.
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.
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.