ESP32-SoftwareLIN
SoftwareLin.h
1 /*
2 SoftwareLin.h - Implementation of the software LIN bus emulation for ESP32.
3 Copyright (c) 2023 CW-B-W All rights reserved.
4 
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9 
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 */
20 
21 #ifndef __SoftwareLin_h
22 #define __SoftwareLin_h
23 
24 #include "espsoftwareserial/src/SoftwareSerial.h"
25 #include "freertos/semphr.h"
26 
27 class SoftwareLin : public EspSoftwareSerial::UART
28 {
29 public:
36  SoftwareLin(int8_t rxPin, int8_t txPin);
37 
42  ~SoftwareLin();
43 
50  void sendBreak(int breakBits = 13, int delimiterBits = 1);
51 
62  bool checkBreak();
63 
81  uint32_t setAutoBaud(const uint32_t commonBaud[], int commonBaudSize);
82 
89  void endFrame();
90 
98  int read(uint8_t* buffer, size_t size);
99 
107  size_t write(const uint8_t* buffer, size_t size) override;
108 
109 protected:
120  bool m_inFrame;
121 
125  SemaphoreHandle_t m_isrSem;
129  StaticSemaphore_t m_isrSemBuf;
130 
139  static void wakeCheckBreak(SoftwareLin* pThis);
140 };
141 
142 #endif
Definition: SoftwareLin.h:28
static void wakeCheckBreak(SoftwareLin *pThis)
This function wake checkBreak() from blocking. When the UART ISR is triggered, m_isrSem is given so t...
Definition: SoftwareLin.cpp:25
size_t write(const uint8_t *buffer, size_t size) override
Write bytes to the bus.
Definition: SoftwareLin.cpp:166
uint32_t setAutoBaud(const uint32_t commonBaud[], int commonBaudSize)
setAutoBaud() is used for automatically detect and set baud rate after the break field has been detec...
Definition: SoftwareLin.cpp:101
SemaphoreHandle_t m_isrSem
The semaphore for notifying checkBreak() that the ISR has been triggered.
Definition: SoftwareLin.h:125
SoftwareLin(int8_t rxPin, int8_t txPin)
Construct a new Software Lin object.
Definition: SoftwareLin.cpp:31
StaticSemaphore_t m_isrSemBuf
The buffer for static allocation of m_isrSem.
Definition: SoftwareLin.h:129
void sendBreak(int breakBits=13, int delimiterBits=1)
Send Break Field and Break Delimiter to the bus.
Definition: SoftwareLin.cpp:47
bool checkBreak()
Check whether there is Break Field sent on the bus. This function blocks until the UART ISR is trigge...
Definition: SoftwareLin.cpp:57
int read(uint8_t *buffer, size_t size)
Read bytes to buffer.
Definition: SoftwareLin.cpp:159
~SoftwareLin()
Destroy the Software Lin object.
Definition: SoftwareLin.cpp:40
bool m_inFrame
Indicate whether SoftwareLin is still processing a frame. Initially, m_inFrame is false....
Definition: SoftwareLin.h:120
void endFrame()
Notify SoftwareLin the LIN frame has ended. This is for SoftwareLin to reset the internal state regis...
Definition: SoftwareLin.cpp:154