Skip to content

Commit 2fe78b0

Browse files
authored
configurable UART Pins
1 parent 76f9d4c commit 2fe78b0

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/Lin_Interface.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ bool Lin_Interface::readFrame(uint8_t FrameID)
2222
uint8_t ProtectedID = getProtectedID(FrameID);
2323
bool ChecksumValid = false;
2424

25-
// start transmission
26-
HardwareSerial::begin(baud, SERIAL_8N1);
27-
writeBreak(); // initiate Frame with a Break
28-
HardwareSerial::write(0x55); // Sync
29-
HardwareSerial::write(ProtectedID); // PID
25+
startTransmission(ProtectedID);
3026
HardwareSerial::flush();
27+
3128
// wait for available data
3229
delay(100);
3330

@@ -118,11 +115,8 @@ void Lin_Interface::writeFrame(uint8_t FrameID, uint8_t dataLen)
118115
uint8_t ProtectedID = getProtectedID(FrameID);
119116
uint8_t cksum = getChecksum(ProtectedID, dataLen);
120117

121-
// übertragung startet
122-
HardwareSerial::begin(baud, SERIAL_8N1);
123-
writeBreak(); // initiate Frame with a Break
124-
HardwareSerial::write(0x55); // Sync
125-
HardwareSerial::write(ProtectedID); // PID
118+
startTransmission(ProtectedID);
119+
126120
for (int i = 0; i < dataLen; ++i)
127121
{
128122
HardwareSerial::write(LinMessage[i]); // Message (array from 1..8)
@@ -210,10 +204,8 @@ void Lin_Interface::writeFrameClassic(uint8_t FrameID, uint8_t dataLen)
210204
uint8_t ProtectedID = getProtectedID(FrameID);
211205
uint8_t cksum = getChecksum(0x00, dataLen);
212206

213-
HardwareSerial::begin(baud, SERIAL_8N1);
214-
writeBreak(); // initiate Frame with a Break
215-
HardwareSerial::write(0x55); // Sync
216-
HardwareSerial::write(ProtectedID); // ID
207+
startTransmission(ProtectedID);
208+
217209
for (int i = 0; i < dataLen; ++i)
218210
{
219211
HardwareSerial::write(LinMessage[i]); // Message (array from 1..8)
@@ -226,6 +218,22 @@ void Lin_Interface::writeFrameClassic(uint8_t FrameID, uint8_t dataLen)
226218
HardwareSerial::end();
227219
} // void Lin_Interface::writeFrameClassic
228220

221+
/// Introduce Frame (Start UART, Break, Sync, PID)
222+
void Lin_Interface::startTransmission(uint8_t ProtectedID)
223+
{
224+
// start UART
225+
if (rxPin < 0 && txPin < 0) {
226+
//no custom pins are defined
227+
HardwareSerial::begin(baud, SERIAL_8N1);
228+
} else {
229+
HardwareSerial::begin(baud, SERIAL_8N1, rxPin, txPin);
230+
}
231+
232+
writeBreak(); // initiate Frame with a Break
233+
HardwareSerial::write(0x55); // Sync
234+
HardwareSerial::write(ProtectedID); // PID
235+
}
236+
229237
/// Send a Break for introduction of a Frame
230238
/// This is done by sending a Byte (0x00) + Stop Bit by using half baud rate
231239
/// @returns if the 0x00 has been send

src/Lin_Interface.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Lin_Interface : private HardwareSerial
2222

2323
int verboseMode = -1;
2424
unsigned long baud = 19200;
25+
int8_t rxPin = -1;
26+
int8_t txPin = -1;
2527

2628
// 8 Data Bytes + ChkSum + some space for receiving complete frames
2729
uint8_t LinMessage[8 + 1 + 4] = {0};
@@ -33,6 +35,7 @@ class Lin_Interface : private HardwareSerial
3335

3436
protected:
3537
uint32_t m_bitCycles;
38+
void startTransmission(uint8_t ProtectedID);
3639
size_t writeBreak();
3740
uint8_t getProtectedID(uint8_t FrameID);
3841
uint8_t getChecksum(uint8_t ProtectedID, uint8_t datalen);

0 commit comments

Comments
 (0)