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
1621constexpr 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}
0 commit comments