Skip to content

Commit 52ff135

Browse files
committed
getMode()
1 parent 6562bd7 commit 52ff135

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

src/TJA1020.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void Lin_TJA1020::setMode(const Mode mode)
4444
{
4545
// we don't need to act, if we're allready there
4646
// see "setMode(sleep)" in the switch below
47-
if (mode == _currentMode)
47+
if (mode == getMode())
4848
{
4949
return;
5050
}
@@ -103,7 +103,7 @@ void Lin_TJA1020::setMode(const Mode mode)
103103
default: // = Sleep
104104
// no direct step from Standby to Sleep, but we don't know if we're in
105105
// we're going over _writingSlope
106-
setMode(_writingSlope);
106+
setMode(Mode::NormalSlope);
107107

108108
// rising edge on /SLP while TXD = 1
109109
digitalWrite(txPin, HIGH);
@@ -131,12 +131,30 @@ void Lin_TJA1020::setMode(const Mode mode)
131131
}
132132
} // void Lin_TJA1020::setMode(TJA1020_Mode newMode)
133133

134-
/// Defines standard slope, to be used, when writing to the bus
135-
void Lin_TJA1020::setSlope(const Mode slope)
134+
Lin_TJA1020::Mode Lin_TJA1020::getMode()
136135
{
137-
_writingSlope = slope;
138-
if (_writingSlope == Mode::Sleep)
139-
{
140-
_writingSlope = Mode::NormalSlope;
136+
if (_currentMode == Mode::Sleep) {
137+
138+
// test MCU PullUp against TJA Pulldown
139+
pinMode(txPin, INPUT);
140+
delayMicroseconds(1); // settle time
141+
bool hasWeakPullDn = !digitalRead(txPin);
142+
143+
pinMode(txPin, INPUT_PULLUP);
144+
delayMicroseconds(1); // settle time
145+
bool hasStrongPullDn = !digitalRead(txPin);
146+
147+
// revert setting
148+
pinMode(txPin, INPUT);
149+
150+
if (hasWeakPullDn && !hasStrongPullDn)
151+
{
152+
// TXD has weak pull-down on remote wake-up
153+
_currentMode = Mode::StandbyWakeupRemote;
154+
} else {
155+
// TXD has strong pull-down on local wake-up (caused by /WAKE pin)
156+
_currentMode = Mode::StandbyWakeupLocal;
157+
}
141158
}
159+
return _currentMode;
142160
}

src/TJA1020.hpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// Requires class Lin_Interface: https://github.com/mestrode/Lin-Interface-Library
88
//
9-
// Tested with ESP32
9+
// Datasheet TJA1020: https://www.nxp.com/docs/en/data-sheet/TJA1020.pdf
1010

1111
#pragma once
1212

@@ -24,8 +24,27 @@ class Lin_TJA1020 : public HardwareSerial
2424
enum class Mode
2525
{
2626
Sleep,
27+
// after Power on or setMode(Sleep)
28+
// NSLP TXD (OUTPUT) RXD INH TRANSMITTER REMARKS
29+
// 0 weak pull-down floating floating off no wake-up request detected
30+
// TERM = high-ohmic
31+
2732
NormalSlope,
28-
LowSlope
33+
// NSLP TXD (OUTPUT) RXD INH TRANSMITTER
34+
// 1 weak pull-down active HIGH normal slope TERM = 30k
35+
36+
LowSlope,
37+
// NSLP TXD (OUTPUT) RXD INH TRANSMITTER
38+
// 1 weak pull-down active HIGH low slope TERM = 30k
39+
40+
StandbyWakeupRemote,
41+
StandbyWakeupLocal
42+
// NSLP TXD (OUTPUT) RXD INH TRANSMITTER REMARKS
43+
// 0 weak pull-down if remote wake-up; LOW HIGH off wake-up request detected;
44+
// strong pull-down if local wake-up; in this mode the microcontroller
45+
// note 2 can read the wake-up source:
46+
// remote or local wake-up
47+
// TERM = 30k
2948
};
3049

3150
/// provides HW-Lin Interface via TJA1020 Chip
@@ -37,13 +56,10 @@ class Lin_TJA1020 : public HardwareSerial
3756
/// switches the operational mode of TJA1020 chip
3857
/// @param mode target mode
3958
void setMode(const Mode mode);
40-
/// Defines standard slope rate, when using to the bus
41-
/// @param slope "NormalSlope" or "LowSlope" is only valid
42-
void setSlope(const Mode slope);
59+
Mode getMode();
4360

4461
private:
45-
Mode _writingSlope = Mode::NormalSlope;
46-
Mode _currentMode = Mode::Sleep;
62+
Mode _currentMode = Mode::Sleep; // after power on
4763
int8_t txPin;
4864
int8_t nslpPin;
4965
};

0 commit comments

Comments
 (0)