| 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
11#include <LIN_slave_HardwareSerial.h>
12
13// optional file, see LIN_slave_HardwareSerial.h
14#if defined(_LIN_SLAVE_HW_SERIAL_H_)
15
16
17/**************************
18 * PROTECTED METHODS
19**************************/
20
26 bool LIN_Slave_HardwareSerial::_getBreakFlag()
27{
28 // return BREAK detection flag
29 return this->flagBreak;
30
31} // LIN_Slave_HardwareSerial::_getBreakFlag()
32
33
34
39 void LIN_Slave_HardwareSerial::_resetBreakFlag()
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
62 LIN_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
83 void LIN_Slave_HardwareSerial::begin(uint16_t Baudrate)
84{
85 // call base class method
86 LIN_Slave_Base::begin(Baudrate);
87
88 // open serial interface
89 pSerial->begin(this->baudrate);
90 while(!(*pSerial));
91
92 // initialize variables
93 this->_resetBreakFlag();
94
95 // optional debug output (debug level 2)
96 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 2)
97 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN);
98 LIN_SLAVE_DEBUG_SERIAL.println(": LIN_Slave_HardwareSerial::begin()");
99 #endif
100
101} // LIN_Slave_HardwareSerial::begin()
102
103
104
109 void LIN_Slave_HardwareSerial::end()
110{
111 // call base class method
112 LIN_Slave_Base::end();
113
114 // close serial interface
115 pSerial->end();
116
117 // optional debug output (debug level 2)
118 #if defined(LIN_SLAVE_DEBUG_SERIAL) && (LIN_SLAVE_DEBUG_LEVEL >= 2)
119 LIN_SLAVE_DEBUG_SERIAL.print(this->nameLIN);
120 LIN_SLAVE_DEBUG_SERIAL.println(": LIN_Slave_HardwareSerial::end()");
121 #endif
122
123} // LIN_Slave_HardwareSerial::end()
124
125
126
133 void LIN_Slave_HardwareSerial::handler()
134{
135 // sync frames based on inter-frame pause (not standard compliant!)
136 static uint32_t usLastByte = 0;
137
138 // byte received -> check it
139 if (pSerial->available())
140 {
141 // if 0x00 received and long time since last byte, start new frame and remove 0x00 from queue
142 if ((pSerial->peek() == 0x00) && ((micros() - usLastByte) > this->minFramePause))
143 {
144 this->flagBreak = true;
145 pSerial->read();
146 }
147
148 // store time of this receive
149 usLastByte = micros();
150
151 // call base-class handler
152 LIN_Slave_Base::handler();
153
154 } // if byte received
155
156} // LIN_Slave_HardwareSerial::handler()
157
158#endif // !ARDUINO_ARCH_AVR
159
160/*-----------------------------------------------------------------------------
161 END OF FILE
162-----------------------------------------------------------------------------*/
LIN_slave_HardwareSerial.h
LIN slave emulation library using a generic HardwareSerial interface.
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::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::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_HardwareSerial::pSerial
HardwareSerial * pSerial
pointer to serial interface used for LIN
Definition LIN_slave_HardwareSerial.h:46
LIN_Slave_HardwareSerial::flagBreak
bool flagBreak
a break was detected, is set in handle
Definition LIN_slave_HardwareSerial.h:47
LIN_Slave_HardwareSerial::_resetBreakFlag
void _resetBreakFlag(void)
Clear break detection flag.
Definition LIN_slave_HardwareSerial.cpp:39
LIN_Slave_HardwareSerial::handler
virtual void handler(void)
Handle LIN protocol and call user-defined frame handlers.
Definition LIN_slave_HardwareSerial.cpp:133
LIN_Slave_HardwareSerial::begin
void begin(uint16_t Baudrate=19200)
Open serial interface.
Definition LIN_slave_HardwareSerial.cpp:83
LIN_Slave_HardwareSerial::end
void end(void)
Close serial interface.
Definition LIN_slave_HardwareSerial.cpp:109
LIN_Slave_HardwareSerial::LIN_Slave_HardwareSerial
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.
Definition LIN_slave_HardwareSerial.cpp:62
LIN_Slave_HardwareSerial::_getBreakFlag
virtual bool _getBreakFlag(void)
Get break detection flag.
Definition LIN_slave_HardwareSerial.cpp:26
LIN_Slave_HardwareSerial::minFramePause
uint16_t minFramePause
min. inter-frame pause [us] to start new frame (not standard compliant!)
Definition LIN_slave_HardwareSerial.h:48