Skip to content

Commit 7a2d7d4

Browse files
committed
code
1 parent 0dfd135 commit 7a2d7d4

15 files changed

+786
-105
lines changed

example/basic/src/main.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#include <Arduino.h>
22
#include <TJA1020.hpp>
3+
#include <LinFrameTransfer.hpp>
34

4-
#define PIN_NSLP 23
5+
constexpr int PIN_NSLP = 23;
56

67
// using a TJA1020 chip
78
// using UART 1 for LinBus
89
// configure 19200 Baud
910
// using GPIO 23 for /NSLP pin of TJA1020
10-
Lin_TJA1020 LinBus(1, 19200, PIN_NSLP);
11+
Lin_TJA1020 linBus(1, 19200, PIN_NSLP);
12+
LinFrameTransfer linMaster(linBus, Serial, 2);
1113

1214
// data to be filled by bus request
1315
float Cap_Max = 0.0;
@@ -23,29 +25,40 @@ void setup()
2325

2426
// configure slope rate
2527
Serial.print("configure low slope rate of TJA1020\n");
26-
LinBus.setSlope(LinBus.LowSlope);
28+
linBus.setSlope(Lin_TJA1020::Mode::LowSlope);
2729
}
2830

2931
bool readLinData()
3032
{
31-
// all handling of
32-
bool chkSumValid = LinBus.readFrame(0x2C);
33-
if (chkSumValid)
33+
auto rawData = linMaster.readFrame(0x2C);
34+
if (!rawData)
3435
{
35-
// Data now avaliabele in LinBus.LinMessage
36+
return false;
37+
}
3638

37-
// decode some bytes (incl. rescaling)
38-
Cap_Max = (float((LinBus.LinMessage[1] << 8) + LinBus.LinMessage[0])) / 10;
39-
Cap_Available = (float((LinBus.LinMessage[3] << 8) + LinBus.LinMessage[2])) / 10;
39+
// Data now avaliabele in data.value() or at address data.data()
40+
struct ResponseCap {
41+
uint8_t capMax_LSB;
42+
uint8_t capMax_MSB;
43+
uint8_t capAvaliable_LSB;
44+
uint8_t capAvaliable_MSB;
45+
uint8_t capConfigured;
46+
uint8_t capFlags;
47+
};
48+
ResponseCap* data = reinterpret_cast<ResponseCap*>(rawData.value().data());
4049

41-
// receive a single byte
42-
Cap_Configured = LinBus.LinMessage[4];
50+
// decode some bytes (incl. rescaling)
51+
Cap_Max = ((data->capMax_MSB << 8) + data->capMax_LSB) / 10;
52+
Cap_Available = (float((data->capAvaliable_MSB << 8) + data->capAvaliable_LSB)) / 10;
4353

44-
// decode flags within a byte
45-
CalibByte = LinBus.LinMessage[5];
46-
CalibrationDone = bitRead(LinBus.LinMessage[5], 0);
47-
}
48-
return chkSumValid;
54+
// receive a single byte
55+
Cap_Configured = data->capConfigured;
56+
57+
// decode flags within a byte
58+
CalibByte = data->capFlags;
59+
CalibrationDone = bitRead(data->capFlags, 0);
60+
61+
return true;
4962
}
5063

5164
void loop()
@@ -63,5 +76,5 @@ void loop()
6376
delay(5000);
6477

6578
//shut TJA1020 down, this also releases the INH pin.
66-
LinBus.setSlope(LinBus.Sleep);
79+
linBus.setSlope(Lin_TJA1020::Mode::Sleep);
6780
}

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Lin-Transceiver",
3-
"version": "0.0.1",
3+
"version": "1.0.0",
44
"description": "utilizes a LIN Transceiver TJA1020: Sleep / NormalSlope / LowSlope",
55
"keywords": [
66
"TJA1020",
@@ -21,7 +21,7 @@
2121
"dependencies": [
2222
{
2323
"name": "Lin-Interface",
24-
"version": "https://github.com/mestrode/Lin-Interface-Library"
24+
"version": "https://github.com/mestrode/Lin-Interface-Library.git"
2525
}
2626
]
2727
}

library.properties

Lines changed: 0 additions & 12 deletions
This file was deleted.

platformio.ini

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[env]
2+
3+
[env:test-native]
4+
platform = native
5+
6+
build_type = debug
7+
build_flags =
8+
-DUNIT_TEST
9+
-Isrc
10+
-Itest
11+
-std=gnu++17
12+
13+
test_framework = unity
14+
test_build_src = true
15+
;test_filter = native/test_LinFrameTransfer
16+
;test_filter = native/test_LinTransportLayer
17+
;test_filter = native/test_LinNodeConfig
18+
debug_test = *
19+
20+
lib_deps =
21+
https://github.com/mestrode/Lin-Interface-Library.git
22+
Arduino
23+
24+
lib_ldf_mode = chain+
25+
26+
27+
[env:example-basic]
28+
platform = espressif32@^6.9.0
29+
board = esp32dev
30+
framework = arduino
31+
32+
src_dir = example/basic/src
33+
lib_extra_dirs = src
34+
35+
build_flags =
36+
-std=gnu++17
37+
build_unflags =
38+
-std=gnu++11
39+
40+
lib_deps =
41+
https://github.com/mestrode/Lin-Interface-Library.git
42+
43+
lib_ldf_mode = chain+

src/TJA1020.cpp

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,37 @@
1010

1111
#include "TJA1020.hpp"
1212

13-
#include <Lin_Interface.hpp>
14-
// #include <Arduino.h>
13+
#ifdef UNIT_TEST
14+
#include "mock_HardwareSerial.h"
15+
using HardwareSerial = mock_HardwareSerial;
16+
#include "mock_delay.h"
17+
#else
18+
#include <Arduino.h>
19+
#endif
1520

1621
constexpr auto BAUD_DEFAULT = 19200;
1722

1823
//-----------------------------------------------------------------------------
1924
// constructor
2025

2126
/// Provides HW UART via TJA1020 Chip
22-
Lin_TJA1020::Lin_TJA1020(const int uart_nr, const uint32_t _baud, const int8_t rxPin, const int8_t txPin, const int8_t nslpPin) : Lin_Interface(uart_nr),
23-
_tx_pin(txPin),
24-
_nslp_pin(nslpPin)
27+
Lin_TJA1020::Lin_TJA1020(const int _uart_nr, const uint32_t _baud, const int8_t _rxPin, const int8_t _txPin, const int8_t _nslpPin) :
28+
HardwareSerial(_uart_nr),
29+
txPin(_txPin),
30+
nslpPin(_nslpPin)
2531
{
2632
// use default baud rate, if not specified
27-
Lin_Interface::baud = _baud ? _baud : BAUD_DEFAULT;
28-
Lin_Interface::rxPin = rxPin;
29-
Lin_Interface::txPin = txPin;
33+
// TODO: HardwareSerial::baudRate = _baud ? _baud : BAUD_DEFAULT;
34+
HardwareSerial::setPins(_rxPin, _txPin);
3035
}
3136

3237
//-----------------------------------------------------------------------------
33-
// write / read on bus
34-
35-
/// Requests a Lin 2.0 Frame
36-
bool Lin_TJA1020::readFrame(const uint8_t FrameID)
37-
{
38-
setMode(_writingSlope);
39-
return Lin_Interface::readFrame(FrameID);
40-
}
41-
42-
/// Sends a Lin 2.0 Frame
43-
bool Lin_TJA1020::writeFrame(const uint8_t FrameID, const size_t size)
44-
{
45-
setMode(_writingSlope);
46-
return Lin_Interface::writeFrame(FrameID, size);
47-
}
48-
49-
/// Sends a Lin 1.3 Frame (Legacy Checksum)
50-
void Lin_TJA1020::writeFrameClassic(const uint8_t FrameID, const size_t size)
51-
{
52-
setMode(_writingSlope);
53-
Lin_Interface::writeFrameClassic(FrameID, size);
54-
}
5538

5639
/// does control sequences to switch from one to another operational mode of the chip
5740
/// NormalSlope, LowSlope for writing operation;
5841
/// Sleep will release INH and may disables Power-Supply
5942
/// @param mode TJA1020 Mode to be the next
60-
void Lin_TJA1020::setMode(const TJA1020_Mode mode)
43+
void Lin_TJA1020::setMode(const Mode mode)
6144
{
6245
// we don't need to act, if we're allready there
6346
// see "setMode(sleep)" in the switch below
@@ -66,55 +49,55 @@ void Lin_TJA1020::setMode(const TJA1020_Mode mode)
6649
return;
6750
}
6851

69-
pinMode(_tx_pin, OUTPUT); // TX Signal to LIN Tranceiver
70-
pinMode(_nslp_pin, OUTPUT); // /SLP Signal to LIN Tranceiver
52+
pinMode(txPin, OUTPUT); // TX Signal to LIN Tranceiver
53+
pinMode(nslpPin, OUTPUT); // /SLP Signal to LIN Tranceiver
7154

7255
// Statemachine des TJA1020 von [SLEEP] nach [NORMAL SLOPE MODE] ändern
7356
switch (mode)
7457
{
75-
case NormalSlope:
76-
if (_currentMode == LowSlope)
58+
case Mode::NormalSlope:
59+
if (_currentMode == Mode::LowSlope)
7760
{
7861
// no direct step from LowSlope to NormalSlope
79-
setMode(Sleep);
62+
setMode(Mode::Sleep);
8063
}
8164

8265
// rising edge on /SLP while TXD = 1
83-
digitalWrite(_tx_pin, HIGH);
66+
digitalWrite(txPin, HIGH);
8467
delayMicroseconds(10); // ensure signal to settle
8568

8669
// create rising edge
87-
digitalWrite(_nslp_pin, LOW);
70+
digitalWrite(nslpPin, LOW);
8871
delayMicroseconds(15); // ensure t_gotosleep (min. 10us)
89-
digitalWrite(_nslp_pin, HIGH);
72+
digitalWrite(nslpPin, HIGH);
9073
delayMicroseconds(15); // ensure t_gotonorm (min. 10us)
9174

9275
// [Normal Slope Mode] reached
93-
_currentMode = NormalSlope;
76+
_currentMode = Mode::NormalSlope;
9477
break;
9578

96-
case LowSlope:
97-
if (_currentMode == NormalSlope)
79+
case Mode::LowSlope:
80+
if (_currentMode == Mode::NormalSlope)
9881
{
9982
// no direct step from NormalSlope to LowSlope
100-
setMode(Sleep);
83+
setMode(Mode::Sleep);
10184
}
10285

10386
// rising edge on /SLP while TXD = 0
104-
digitalWrite(_tx_pin, LOW);
87+
digitalWrite(txPin, LOW);
10588
delayMicroseconds(10); // ensure signal to settle
10689

10790
// create rising edge
108-
digitalWrite(_nslp_pin, LOW);
91+
digitalWrite(nslpPin, LOW);
10992
delayMicroseconds(15); // ensure t_gotosleep (min. 10us)
110-
digitalWrite(_nslp_pin, HIGH);
93+
digitalWrite(nslpPin, HIGH);
11194
delayMicroseconds(15); // ensure t_gotonorm (min. 10us)
11295

11396
// release tx pin, to avoid occupation of Lin Bus
114-
digitalWrite(_tx_pin, HIGH);
97+
digitalWrite(txPin, HIGH);
11598

11699
// [Low Slope Mode] reached
117-
_currentMode = LowSlope;
100+
_currentMode = Mode::LowSlope;
118101
break;
119102

120103
default: // = Sleep
@@ -123,37 +106,37 @@ void Lin_TJA1020::setMode(const TJA1020_Mode mode)
123106
setMode(_writingSlope);
124107

125108
// rising edge on /SLP while TXD = 1
126-
digitalWrite(_tx_pin, HIGH);
109+
digitalWrite(txPin, HIGH);
127110
delayMicroseconds(10); // ensure signal to settle
128111

129112
// create falling edge
130-
digitalWrite(_nslp_pin, HIGH);
113+
digitalWrite(nslpPin, HIGH);
131114
delayMicroseconds(15); // ensure t_gotosleep (min. 10us)
132-
digitalWrite(_nslp_pin, LOW);
115+
digitalWrite(nslpPin, LOW);
133116
delayMicroseconds(15); // ensure t_gotonorm (min. 10us)
134117
// INH will be shut down by constant low, chip will go into sleep mode
135118

136119
// ensure pin level while sleeping
137120
#ifdef ESP32
138-
pinMode(_tx_pin, INPUT_PULLDOWN); // ensure Low level while in sleep mode (since TJA1020 has internally a fixed pulldown)
139-
pinMode(_nslp_pin, INPUT_PULLDOWN); // ensure Low level while in sleep mode
121+
pinMode(txPin, INPUT_PULLDOWN); // ensure Low level while in sleep mode (since TJA1020 has internally a fixed pulldown)
122+
pinMode(nslpPin, INPUT_PULLDOWN); // ensure Low level while in sleep mode
140123
#else
141-
pinMode(_tx_pin, INPUT); // ensure Low level while in sleep mode (since TJA1020 has internally a fixed pulldown)
142-
pinMode(_nslp_pin, INPUT); // ensure Low level while in sleep mode
124+
pinMode(txPin, INPUT); // ensure Low level while in sleep mode (since TJA1020 has internally a fixed pulldown)
125+
pinMode(nslpPin, INPUT); // ensure Low level while in sleep mode
143126
#endif
144127

145128
// [Sleep] reached
146-
_currentMode = Sleep;
129+
_currentMode = Mode::Sleep;
147130
break;
148131
}
149132
} // void Lin_TJA1020::setMode(TJA1020_Mode newMode)
150133

151134
/// Defines standard slope, to be used, when writing to the bus
152-
void Lin_TJA1020::setSlope(const TJA1020_Mode slope)
135+
void Lin_TJA1020::setSlope(const Mode slope)
153136
{
154137
_writingSlope = slope;
155-
if (_writingSlope == Sleep)
138+
if (_writingSlope == Mode::Sleep)
156139
{
157-
_writingSlope = NormalSlope;
140+
_writingSlope = Mode::NormalSlope;
158141
}
159142
}

src/TJA1020.hpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010

1111
#pragma once
1212

13-
#include <Lin_Interface.hpp>
13+
#ifdef UNIT_TEST
14+
#include "mock_Arduino.h"
15+
#include "mock_HardwareSerial.h"
16+
using HardwareSerial = mock_HardwareSerial;
17+
#else
18+
#include <Arduino.h>
19+
#endif
1420

15-
class Lin_TJA1020 : public Lin_Interface
21+
class Lin_TJA1020 : public HardwareSerial
1622
{
1723
public:
18-
enum TJA1020_Mode
24+
enum class Mode
1925
{
2026
Sleep,
2127
NormalSlope,
@@ -26,22 +32,18 @@ class Lin_TJA1020 : public Lin_Interface
2632
/// @param uart_nr Index of HW UART to be used (0..2)
2733
/// @param baud Baud rate for RX/TX (0 = default 19200 baud)
2834
/// @param nslpPin /SLP Pin to control TJA1020
29-
Lin_TJA1020(const int uart_nr, const uint32_t _baud, const int8_t rxPin, const int8_t txPin, const int8_t nslpPin);
30-
31-
bool readFrame(const uint8_t FrameID);
32-
bool writeFrame(const uint8_t FrameID, const size_t size);
33-
void writeFrameClassic(const uint8_t FrameID, const size_t size);
35+
Lin_TJA1020(const int _uart_nr, const uint32_t _baud, const int8_t _rxPin, const int8_t _txPin, const int8_t _nslpPin);
3436

3537
/// switches the operational mode of TJA1020 chip
3638
/// @param mode target mode
37-
void setMode(const TJA1020_Mode mode);
39+
void setMode(const Mode mode);
3840
/// Defines standard slope rate, when using to the bus
3941
/// @param slope "NormalSlope" or "LowSlope" is only valid
40-
void setSlope(const TJA1020_Mode slope);
42+
void setSlope(const Mode slope);
4143

4244
private:
43-
TJA1020_Mode _writingSlope = NormalSlope;
44-
TJA1020_Mode _currentMode = Sleep;
45-
int8_t _tx_pin;
46-
int8_t _nslp_pin;
45+
Mode _writingSlope = Mode::NormalSlope;
46+
Mode _currentMode = Mode::Sleep;
47+
int8_t txPin;
48+
int8_t nslpPin;
4749
};

0 commit comments

Comments
 (0)