| 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
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
111 void LIN_Slave_SoftwareSerial::end()
112{
113 // call base class method
114 LIN_Slave_Base::end();
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
137 void LIN_Slave_SoftwareSerial::handler()
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
165 LIN_Slave_Base::handler();
166
167 // SoftwareSerial is blocking while sending -> skip reading echo
168 if (this->state == LIN_Slave_Base::STATE_RECEIVING_ECHO)
169 {
170 // propagate to DONE immediately
171 this->state = LIN_Slave_Base::STATE_DONE;
172
173 // optionally disable RS485 transmitter
174 _disableTransmitter();
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_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:52
LIN_Slave_Base::baudrate
uint16_t baudrate
communication baudrate [Baud]
Definition LIN_slave_Base.h:119
LIN_Slave_Base::STATE_DONE
@ STATE_DONE
frame is completed
Definition LIN_slave_Base.h:82
LIN_Slave_Base::STATE_RECEIVING_ECHO
@ STATE_RECEIVING_ECHO
receiving slave response echo
Definition LIN_slave_Base.h:80
LIN_Slave_Base::_disableTransmitter
void _disableTransmitter(void)
Disable RS485 transmitter (DE=low)
Definition LIN_slave_Base.h:184
LIN_Slave_Base::begin
virtual void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_slave_Base.cpp:171
LIN_Slave_Base::nameLIN
char nameLIN[LIN_SLAVE_BUFLEN_NAME]
LIN node name, e.g. for debug.
Definition LIN_slave_Base.h:140
LIN_Slave_Base::version_t
version_t
LIN protocol version.
Definition LIN_slave_Base.h:58
LIN_Slave_Base::state
LIN_Slave_Base::state_t state
status of LIN state machine
Definition LIN_slave_Base.h:121
LIN_Slave_Base::end
virtual void end(void)
Close serial interface.
Definition LIN_slave_Base.cpp:209
LIN_Slave_Base::handler
virtual void handler(void)
Handle LIN protocol and call user-defined frame callbacks.
Definition LIN_slave_Base.cpp:288
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:137
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:111
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