| 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 on AVR, see LIN_slave_NeoHWSerial_AVR.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 with optional timeout
89 this->pSerial->begin(this->baudrate);
90 #if defined(LIN_SLAVE_LIN_PORT_TIMEOUT) && (LIN_SLAVE_LIN_PORT_TIMEOUT > 0)
91 uint32_t startMillis = millis();
92 while ((!(*(this->pSerial))) && (millis() - startMillis < LIN_SLAVE_LIN_PORT_TIMEOUT));
93 #else
94 while(!(*(this->pSerial)));
95 #endif
96
97 // initialize variables
98 this->_resetBreakFlag();
99
100 // print debug message
101 DEBUG_PRINT(2, "ok");
102
103} // LIN_Slave_HardwareSerial::begin()
104
105
106
111 void LIN_Slave_HardwareSerial::end()
112{
113 // call base class method
114 LIN_Slave_Base::end();
115
116 // close serial interface
117 this->pSerial->end();
118
119 // print debug message
120 DEBUG_PRINT(2, " ");
121
122} // LIN_Slave_HardwareSerial::end()
123
124
125
132 void LIN_Slave_HardwareSerial::handler()
133{
134 // sync frames based on inter-frame pause (not standard compliant!)
135 static uint32_t usLastByte = 0;
136
137 // print debug message
138 //DEBUG_PRINT(3, "state=%d", (int) this->state);
139
140 // byte received -> check it
141 if (this->pSerial->available())
142 {
143 // 0x00 received and long time since last byte (=BREAK) -> start new frame and remove 0x00 from queue
144 if ((this->pSerial->peek() == 0x00) && ((micros() - usLastByte) > this->minFramePause))
145 {
146 this->flagBreak = true;
147 this->pSerial->read();
148 }
149
150 // store time of this receive
151 usLastByte = micros();
152
153 // call base-class handler
154 LIN_Slave_Base::handler();
155
156 } // if byte received
157
158} // LIN_Slave_HardwareSerial::handler()
159
160#endif // !ARDUINO_ARCH_AVR
161
162/*-----------------------------------------------------------------------------
163 END OF FILE
164-----------------------------------------------------------------------------*/
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:106
LIN_Slave_Base::baudrate
uint16_t baudrate
communication baudrate [Baud]
Definition LIN_slave_Base.h:173
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::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_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:132
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:111
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