10
10
11
11
#include " TJA1020.hpp"
12
12
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
15
20
16
21
constexpr auto BAUD_DEFAULT = 19200 ;
17
22
18
23
// -----------------------------------------------------------------------------
19
24
// constructor
20
25
21
26
// / 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)
25
31
{
26
32
// 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);
30
35
}
31
36
32
37
// -----------------------------------------------------------------------------
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
- }
55
38
56
39
// / does control sequences to switch from one to another operational mode of the chip
57
40
// / NormalSlope, LowSlope for writing operation;
58
41
// / Sleep will release INH and may disables Power-Supply
59
42
// / @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)
61
44
{
62
45
// we don't need to act, if we're allready there
63
46
// see "setMode(sleep)" in the switch below
@@ -66,55 +49,55 @@ void Lin_TJA1020::setMode(const TJA1020_Mode mode)
66
49
return ;
67
50
}
68
51
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
71
54
72
55
// Statemachine des TJA1020 von [SLEEP] nach [NORMAL SLOPE MODE] ändern
73
56
switch (mode)
74
57
{
75
- case NormalSlope:
76
- if (_currentMode == LowSlope)
58
+ case Mode:: NormalSlope:
59
+ if (_currentMode == Mode:: LowSlope)
77
60
{
78
61
// no direct step from LowSlope to NormalSlope
79
- setMode (Sleep);
62
+ setMode (Mode:: Sleep);
80
63
}
81
64
82
65
// rising edge on /SLP while TXD = 1
83
- digitalWrite (_tx_pin , HIGH);
66
+ digitalWrite (txPin , HIGH);
84
67
delayMicroseconds (10 ); // ensure signal to settle
85
68
86
69
// create rising edge
87
- digitalWrite (_nslp_pin , LOW);
70
+ digitalWrite (nslpPin , LOW);
88
71
delayMicroseconds (15 ); // ensure t_gotosleep (min. 10us)
89
- digitalWrite (_nslp_pin , HIGH);
72
+ digitalWrite (nslpPin , HIGH);
90
73
delayMicroseconds (15 ); // ensure t_gotonorm (min. 10us)
91
74
92
75
// [Normal Slope Mode] reached
93
- _currentMode = NormalSlope;
76
+ _currentMode = Mode:: NormalSlope;
94
77
break ;
95
78
96
- case LowSlope:
97
- if (_currentMode == NormalSlope)
79
+ case Mode:: LowSlope:
80
+ if (_currentMode == Mode:: NormalSlope)
98
81
{
99
82
// no direct step from NormalSlope to LowSlope
100
- setMode (Sleep);
83
+ setMode (Mode:: Sleep);
101
84
}
102
85
103
86
// rising edge on /SLP while TXD = 0
104
- digitalWrite (_tx_pin , LOW);
87
+ digitalWrite (txPin , LOW);
105
88
delayMicroseconds (10 ); // ensure signal to settle
106
89
107
90
// create rising edge
108
- digitalWrite (_nslp_pin , LOW);
91
+ digitalWrite (nslpPin , LOW);
109
92
delayMicroseconds (15 ); // ensure t_gotosleep (min. 10us)
110
- digitalWrite (_nslp_pin , HIGH);
93
+ digitalWrite (nslpPin , HIGH);
111
94
delayMicroseconds (15 ); // ensure t_gotonorm (min. 10us)
112
95
113
96
// release tx pin, to avoid occupation of Lin Bus
114
- digitalWrite (_tx_pin , HIGH);
97
+ digitalWrite (txPin , HIGH);
115
98
116
99
// [Low Slope Mode] reached
117
- _currentMode = LowSlope;
100
+ _currentMode = Mode:: LowSlope;
118
101
break ;
119
102
120
103
default : // = Sleep
@@ -123,37 +106,37 @@ void Lin_TJA1020::setMode(const TJA1020_Mode mode)
123
106
setMode (_writingSlope);
124
107
125
108
// rising edge on /SLP while TXD = 1
126
- digitalWrite (_tx_pin , HIGH);
109
+ digitalWrite (txPin , HIGH);
127
110
delayMicroseconds (10 ); // ensure signal to settle
128
111
129
112
// create falling edge
130
- digitalWrite (_nslp_pin , HIGH);
113
+ digitalWrite (nslpPin , HIGH);
131
114
delayMicroseconds (15 ); // ensure t_gotosleep (min. 10us)
132
- digitalWrite (_nslp_pin , LOW);
115
+ digitalWrite (nslpPin , LOW);
133
116
delayMicroseconds (15 ); // ensure t_gotonorm (min. 10us)
134
117
// INH will be shut down by constant low, chip will go into sleep mode
135
118
136
119
// ensure pin level while sleeping
137
120
#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
140
123
#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
143
126
#endif
144
127
145
128
// [Sleep] reached
146
- _currentMode = Sleep;
129
+ _currentMode = Mode:: Sleep;
147
130
break ;
148
131
}
149
132
} // void Lin_TJA1020::setMode(TJA1020_Mode newMode)
150
133
151
134
// / 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)
153
136
{
154
137
_writingSlope = slope;
155
- if (_writingSlope == Sleep)
138
+ if (_writingSlope == Mode:: Sleep)
156
139
{
157
- _writingSlope = NormalSlope;
140
+ _writingSlope = Mode:: NormalSlope;
158
141
}
159
142
}
0 commit comments