Skip to content

Commit 67f1937

Browse files
committed
initial commit
1 parent addaa60 commit 67f1937

File tree

5 files changed

+290
-0
lines changed

5 files changed

+290
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# LIN-Transceiver-Library (TJA1020)
2+
3+
Extends the Lin-Interface class and ensure the TJA1020 statemachine will be served in the correct way
4+
5+
example:
6+
7+
#include "TJA1020.h"
8+
9+
#define LIN_SERIAL_SPEED 19200
10+
#define lin_NSLP_Pin 32
11+
12+
// RX and TX Pin is defined per UART_nr
13+
TJA1020 LinBus(2, LIN_SERIAL_SPEED, lin_NSLP_Pin); // UART_nr, Baudrate, /SLP-Pin
14+
15+
void setup()
16+
{
17+
18+
}
19+
20+
void loop()
21+
{
22+
uint8_t data = 0x00;
23+
24+
// Read Frame FID = 0x20
25+
if (LinBus.readFrame(0x20))
26+
{
27+
// Read succesfull
28+
data = LinBus.LinMessage[0];
29+
}
30+
31+
// let LIN-Tranceiver sleep
32+
LinBus.setMode(LinBus.Sleep);
33+
}

library.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "Lin-Transceiver",
3+
"version": "0.0.1",
4+
"description": "utilizes a LIN Transceiver TJA1020: Sleep / NormalSlope / LowSlope",
5+
"keywords": [
6+
"TJA1020",
7+
"LIN",
8+
"Lin-Bus"
9+
],
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/mestrode/Lin-Transceiver-Library"
13+
},
14+
"authors": [
15+
{
16+
"name": "mestrode",
17+
"maintainer": true
18+
}
19+
],
20+
"license": "GPL-2.0-only",
21+
"dependencies": [
22+
{
23+
"name": "LIN-Interface",
24+
"version": "https://github.com/mestrode/LIN-Interface-Library.git"
25+
}
26+
]
27+
}

library.properties

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name=Lin-Transceiver
2+
version=0.0.1
3+
author=mestrode <[email protected]>
4+
maintainer=mestrode <[email protected]>
5+
sentence=provides Lin-Interface via TJA1020
6+
paragraph=provides Lin-Interface via TJA1020, therefore the statemachine within the TJA1020 is considered by read and write operations
7+
category=Communication
8+
url=https://github.com/mestrode/Lin-Transceiver-Library
9+
architectures=*
10+
depends=Lin-Interface
11+
includes=TJA1020.hpp
12+
license=GPL-2.0-only

src/TJA1020.cpp

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Utilizes a TJA1020 Lin-Transceiver
2+
// Provides Lin-Interface but considers the statemachin in all steps
3+
//
4+
// Copyright mestrode <[email protected]>
5+
// Original Source: "https://github.com/mestrode/Lin-Transceiver-Library"
6+
//
7+
// Requires class Lin_Interface: https://github.com/mestrode/Lin-Interface-Library
8+
//
9+
// Tested with ESP32
10+
11+
#include "TJA1020.hpp"
12+
13+
#include <Lin-Interface.h>
14+
// #include <Arduino.h>
15+
16+
//-----------------------------------------------------------------------------
17+
// Pin definitions, we need the TX Pin to control the TJA1020 statemachine
18+
// See "HardwareSerial.cpp"
19+
#ifdef ESP32
20+
#ifndef TX0
21+
#define TX0 1
22+
#endif
23+
24+
#ifndef TX1
25+
#define TX1 10
26+
#endif
27+
28+
#ifndef TX2
29+
#define TX2 17
30+
#endif
31+
#else
32+
#error "Need Pin definitions for LIN Transceiver"
33+
#endif
34+
35+
//-----------------------------------------------------------------------------
36+
// constructor
37+
38+
/// Provides HW UART via TJA1020 Chip
39+
Lin_TJA1020::Lin_TJA1020(int uart_nr, uint32_t _baud, int8_t nslpPin) : Lin_Interface(uart_nr)
40+
{
41+
// Use typical pin configuration if interfaced by uart_nr
42+
//############
43+
// See "HardwareSerial.cpp"
44+
if(uart_nr == 0) {
45+
_tx_pin = TX0;
46+
}
47+
if(uart_nr == 1) {
48+
_tx_pin = TX1;
49+
}
50+
if(uart_nr == 2) {
51+
_tx_pin = TX2;
52+
}
53+
//############
54+
55+
// use default baud rate, if not specified
56+
Lin_Interface::baud = _baud ? _baud : 19200;
57+
58+
_nslp_pin = nslpPin;
59+
}
60+
61+
//-----------------------------------------------------------------------------
62+
// write / read on bus
63+
64+
/// Requests a Lin 2.0 Frame
65+
bool Lin_TJA1020::readFrame(uint8_t FrameID)
66+
{
67+
setMode(_writingSlope);
68+
return Lin_Interface::readFrame(FrameID);
69+
}
70+
71+
/// Sends a Lin 2.0 Frame
72+
void Lin_TJA1020::writeFrame(uint8_t FrameID, size_t size)
73+
{
74+
setMode(_writingSlope);
75+
return Lin_Interface::writeFrame(FrameID, size);
76+
}
77+
78+
/// Sends a Lin 1.3 Frame (Legacy Checksum)
79+
void Lin_TJA1020::writeFrameClassic(uint8_t FrameID, size_t size)
80+
{
81+
setMode(_writingSlope);
82+
Lin_Interface::writeFrameClassic(FrameID, size);
83+
}
84+
85+
/// does control sequences to switch from one to another operational mode of the chip
86+
/// NormalSlope, LowSlope for writing operation;
87+
/// Sleep will release INH and may disables Power-Supply
88+
/// @param newMode TJA1020 Mode to be the next
89+
void Lin_TJA1020::setMode(TJA1020_Mode mode)
90+
{
91+
// we don't need to act, if we're allready there
92+
// see "setMode(sleep)" in the switch below
93+
if (mode == _currentMode)
94+
{
95+
break;
96+
}
97+
98+
pinMode(_tx_pin, OUTPUT); // TX Signal to LIN Tranceiver
99+
pinMode(_nslp_pin, OUTPUT); // /SLP Signal to LIN Tranceiver
100+
101+
// Statemachine des TJA1020 von [SLEEP] nach [NORMAL SLOPE MODE] ändern
102+
switch (mode)
103+
{
104+
case NormalSlope:
105+
if (_currentMode == LowSlope)
106+
{
107+
// no direct step from LowSlope to NormalSlope
108+
setMode(Sleep);
109+
}
110+
111+
// rising edge on /SLP while TXD = 1
112+
digitalWrite(_tx_pin, HIGH);
113+
delayMicroseconds(10); // ensure signal to settle
114+
115+
// create rising edge
116+
digitalWrite(_nslp_pin, LOW);
117+
delayMicroseconds(15); // ensure t_gotosleep (min. 10us)
118+
digitalWrite(_nslp_pin, HIGH);
119+
delayMicroseconds(15); // ensure t_gotonorm (min. 10us)
120+
121+
// [Normal Slope Mode] reached
122+
_currentMode = NormalSlope;
123+
break;
124+
125+
case LowSlope:
126+
if (_currentMode == NormalSlope)
127+
{
128+
// no direct step from NormalSlope to LowSlope
129+
setMode(Sleep);
130+
}
131+
132+
// rising edge on /SLP while TXD = 0
133+
digitalWrite(_tx_pin, LOW);
134+
delayMicroseconds(10); // ensure signal to settle
135+
136+
// create rising edge
137+
digitalWrite(_nslp_pin, LOW);
138+
delayMicroseconds(15); // ensure t_gotosleep (min. 10us)
139+
digitalWrite(_nslp_pin, HIGH);
140+
delayMicroseconds(15); // ensure t_gotonorm (min. 10us)
141+
142+
// [Low Slope Mode] reached
143+
_currentMode = LowSlope;
144+
break;
145+
146+
default: // = Sleep
147+
// no direct step from Standby to Sleep, but we don't know if we're in
148+
// we're going over _writingSlope
149+
setMode(_writingSlope);
150+
151+
// rising edge on /SLP while TXD = 1
152+
digitalWrite(_tx_pin, HIGH);
153+
delayMicroseconds(10); // ensure signal to settle
154+
155+
// create falling edge
156+
digitalWrite(_nslp_pin, HIGH);
157+
delayMicroseconds(15); // ensure t_gotosleep (min. 10us)
158+
digitalWrite(_nslp_pin, LOW);
159+
delayMicroseconds(15); // ensure t_gotonorm (min. 10us)
160+
break;
161+
162+
// [Sleep] reached
163+
_currentMode = Sleep;
164+
}
165+
} // void Lin_TJA1020::setMode(TJA1020_Mode newMode)
166+
167+
/// Defines standard slope, to be used, when writing to the bus
168+
void Lin_TJA1020::setSlope(TJA1020_Mode slope) {
169+
_writingSlope = slope;
170+
if (_writingSlope == Sleep) {
171+
_writingSlope = NormalSlope;
172+
}
173+
}

src/TJA1020.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Utilizes a TJA1020 Lin-Transceiver
2+
// Provides Lin-Interface but considers the statemachin in all steps
3+
//
4+
// Copyright mestrode <[email protected]>
5+
// Original Source: "https://github.com/mestrode/Lin-Transceiver-Library"
6+
//
7+
// Requires class Lin_Interface: https://github.com/mestrode/Lin-Interface-Library
8+
//
9+
// Tested with ESP32
10+
11+
#pragma once
12+
13+
// #include <Arduino.h>
14+
#include <Lin-Interface.hpp>
15+
16+
class Lin_TJA1020 : public Lin_Interface
17+
{
18+
public:
19+
enum TJA1020_Mode { Sleep, NormalSlope, LowSlope };
20+
21+
/// provides HW-Lin Interface via TJA1020 Chip
22+
/// @param uart_nr Index of HW UART to be used (0..2)
23+
/// @param baud Baud rate for RX/TX (0 = default 19200 baud)
24+
/// @param nslpPin /SLP Pin to control TJA1020
25+
Lin_TJA1020(int uart_nr, uint32_t _baud, int8_t nslpPin);
26+
27+
bool readFrame(uint8_t FrameID);
28+
void writeFrame(uint8_t FrameID, size_t size);
29+
void writeFrameClassic(uint8_t FrameID, size_t size);
30+
31+
/// returns the current mode of TJA1020 chip
32+
TJA1020_Mode getMode();
33+
/// switches the operational mode of TJA1020 chip
34+
/// @param mode target mode
35+
void setMode(TJA1020_Mode mode);
36+
/// Defines standard slope rate, when using to the bus
37+
/// @param slope "NormalSlope" or "LowSlope" is only valid
38+
void setSlope(TJA1020_Mode slope);
39+
40+
private:
41+
TJA1020_Mode _writingSlope = NormalSlope;
42+
TJA1020_Mode _currentMode = Sleep;
43+
int8_t _tx_pin;
44+
int8_t _nslp_pin;
45+
};

0 commit comments

Comments
 (0)