| 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
14#include <LIN_slave_SoftwareSerial.h>
15
16
17/**************************
18 * PROTECTED METHODS
19**************************/
20
26 bool LIN_Slave_SoftwareSerial::_getBreakFlag()
27{
28 // return BREAK detection flag
29 return this->flagBreak;
30
31} // LIN_Slave_SoftwareSerial::_getBreakFlag()
32
33
34
39 void LIN_Slave_SoftwareSerial::_resetBreakFlag()
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 LIN_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
85 void 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. Timeout not required here
91 this->SWSerial.end();
92 this->SWSerial.begin(this->baudrate);
93
94 // initialize variables
95 this->_resetBreakFlag();
96
97 // print debug message
98 DEBUG_PRINT(2, "ok");
99
100} // LIN_Slave_SoftwareSerial::begin()
101
102
103
108 void LIN_Slave_SoftwareSerial::end()
109{
110 // call base class method
111 LIN_Slave_Base::end();
112
113 // close serial interface
114 this->SWSerial.end();
115
116 // print debug message
117 DEBUG_PRINT(2, " ");
118
119} // LIN_Slave_SoftwareSerial::end()
120
121
122
131 void LIN_Slave_SoftwareSerial::handler()
132{
133 // sync frames based on inter-frame pause (not standard compliant!)
134 static uint32_t usLastByte = 0;
135
136 // byte received -> check it
137 if (this->available())
138 {
139 // ESP32 & ESP8266 (BREAK is dropped due to missing stop bit): if SYNC=0x55 received and long time since last byte, start new frame
140 #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
141 if ((this->_serialPeek() == 0x55) && ((micros() - usLastByte) > this->minFramePause))
142 {
143 // set BREAK flag
144 this->flagBreak = true;
145
146 // print debug message
147 DEBUG_PRINT(3, "BRK detected");
148
149 }
150
151 // other architectures (BREAK is received): if BREAK=0x00 received and long time since last byte, start new frame and remove 0x00 from queue
152 #else
153 if ((this->_serialPeek() == 0x00) && ((micros() - usLastByte) > this->minFramePause))
154 {
155 // set BREAK flag
156 this->flagBreak = true;
157
158 // remove 0x00 from FiFo buffer
159 this->_serialRead();
160
161 // print debug message
162 DEBUG_PRINT(3, "BRK detected");
163
164 }
165 #endif
166
167 // store time of this receive
168 usLastByte = micros();
169
170 // call base-class handler
171 LIN_Slave_Base::handler();
172
173 // SoftwareSerial is blocking while sending -> skip reading echo
174 if (this->state == LIN_Slave_Base::STATE_RECEIVING_ECHO)
175 {
176 // propagate to DONE immediately
177 this->state = LIN_Slave_Base::STATE_DONE;
178
179 // optionally disable RS485 transmitter
180 _disableTransmitter();
181 }
182
183 } // if byte received
184
185} // LIN_Slave_SoftwareSerial::handler()
186
187
188#endif // ARDUINO_ARCH_AVR || ARDUINO_ARCH_ESP8266 || ARDUINO_ARCH_ESP32
189
190/*-----------------------------------------------------------------------------
191 END OF FILE
192-----------------------------------------------------------------------------*/
LIN_slave_SoftwareSerial.h
LIN slave emulation library using a generic SoftwareSerial interface (if available).
LIN_Slave_Base
LIN slave node base class.
Definition LIN_slave_Base.h:106
LIN_Slave_Base::baudrate
uint16_t baudrate
communication baudrate [Baud]
Definition LIN_slave_Base.h:173
LIN_Slave_Base::STATE_DONE
@ STATE_DONE
frame is completed
Definition LIN_slave_Base.h:136
LIN_Slave_Base::STATE_RECEIVING_ECHO
@ STATE_RECEIVING_ECHO
receiving slave response echo
Definition LIN_slave_Base.h:134
LIN_Slave_Base::_disableTransmitter
void _disableTransmitter(void)
Disable RS485 transmitter (DE=low)
Definition LIN_slave_Base.h:236
LIN_Slave_Base::begin
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_slave_Base.cpp:163
LIN_Slave_Base::version_t
version_t
LIN protocol version.
Definition LIN_slave_Base.h:112
LIN_Slave_Base::state
LIN_Slave_Base::state_t state
status of LIN state machine
Definition LIN_slave_Base.h:175
LIN_Slave_Base::end
virtual void end(void)
Close serial interface.
Definition LIN_slave_Base.cpp:201
LIN_Slave_Base::handler
virtual void handler(void)
Handle LIN protocol and call user-defined frame callbacks.
Definition LIN_slave_Base.cpp:267
LIN_Slave_SoftwareSerial::_serialPeek
uint8_t _serialPeek(void)
peek next byte from Rx buffer
Definition LIN_slave_SoftwareSerial.h:62
LIN_Slave_SoftwareSerial::LIN_Slave_SoftwareSerial
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.
Definition LIN_slave_SoftwareSerial.cpp:64
LIN_Slave_SoftwareSerial::handler
virtual void handler(void)
Handle LIN protocol and call user-defined frame handlers.
Definition LIN_slave_SoftwareSerial.cpp:131
LIN_Slave_SoftwareSerial::begin
void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_slave_SoftwareSerial.cpp:85
LIN_Slave_SoftwareSerial::_resetBreakFlag
void _resetBreakFlag(void)
Clear break detection flag.
Definition LIN_slave_SoftwareSerial.cpp:39
LIN_Slave_SoftwareSerial::end
void end(void)
Close serial interface.
Definition LIN_slave_SoftwareSerial.cpp:108
LIN_Slave_SoftwareSerial::_serialRead
uint8_t _serialRead(void)
read next byte from Rx buffer
Definition LIN_slave_SoftwareSerial.h:65
LIN_Slave_SoftwareSerial::available
bool available(void)
check if a byte is available in Rx buffer
Definition LIN_slave_SoftwareSerial.h:91
LIN_Slave_SoftwareSerial::_getBreakFlag
bool _getBreakFlag(void)
Get break detection flag.
Definition LIN_slave_SoftwareSerial.cpp:26