Skip to content

Commit 928d803

Browse files
committed
include corrected, basic example added
1 parent b719fd3 commit 928d803

File tree

4 files changed

+111
-9
lines changed

4 files changed

+111
-9
lines changed

README.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# LIN-Transceiver-Library (TJA1020)
22

3-
Extends the Lin-Interface class and ensure the TJA1020 statemachine will be served in the correct way
3+
Using a LIN Bus via TJA1020 Transceiver needs to handle the statemachine bevor you will be able to send or receive data from the bus. This is encapsulated within this class.
44

5-
example:
5+
# dependencies
66

7-
#include "TJA1020.h"
7+
This library extends the Lin-Interface Library:
8+
https://github.com/mestrode/Lin-Interface-Library
9+
10+
# example
11+
Take a look into the basic example.
12+
13+
Library should work like the Lin-Interface Library, but considers the statemachine in every readFrame and writeFrame method.
14+
You can also control the statemachine, eg. the default slope mode.
15+
16+
```cpp
17+
#include "TJA1020.hpp"
818

919
#define LIN_SERIAL_SPEED 19200
1020
#define lin_NSLP_Pin 32
@@ -14,20 +24,28 @@ example:
1424

1525
void setup()
1626
{
17-
27+
// use LowSlope mode all the time
28+
LinBus.Slope(LinBus.LowSlope);
1829
}
1930

20-
void loop()
31+
uint8_t getData()
2132
{
2233
uint8_t data = 0x00;
2334

24-
// Read Frame FID = 0x20
35+
// Read Frame ID = 0x20
2536
if (LinBus.readFrame(0x20))
2637
{
2738
// Read succesfull
28-
data = LinBus.LinMessage[0];
39+
data = LinBus.LinMessage[0]; // only consider byte 0 of the received data
2940
}
3041

31-
// let LIN-Tranceiver sleep
42+
// let LIN-Tranceiver sleep --> changes also the INH Pin of the TJA1020
3243
LinBus.setMode(LinBus.Sleep);
44+
45+
return data;
3346
}
47+
```
48+
49+
# see also
50+
51+
* Datasheet of TJA1020 https://www.nxp.com/docs/en/data-sheet/TJA1020.pdf

example/basic/platformio.ini

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:esp32doit-devkit-v1]
12+
platform = espressif32
13+
board = esp32doit-devkit-v1
14+
framework = arduino
15+
lib_deps =
16+
https://github.com/mestrode/Lin-Interface-Library.git
17+
https://github.com/mestrode/Lin-Transceiver-Library.git

example/basic/src/main.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <Arduino.h>
2+
#include <TJA1020.hpp>
3+
4+
#define PIN_NSLP 23
5+
6+
// using a TJA1020 chip
7+
// using UART 1 for LinBus
8+
// configure 19200 Baud
9+
// using GPIO 23 for /NSLP pin of TJA1020
10+
Lin_TJA1020 LinBus(1, 19200, PIN_NSLP);
11+
12+
// data to be filled by bus request
13+
float Cap_Max = 0.0;
14+
float Cap_Available = 0.0;
15+
uint8_t Cap_Configured = 0;
16+
uint8_t CalibByte = 0x00;
17+
bool CalibrationDone = false;
18+
19+
void setup()
20+
{
21+
// Serial represents Serial(0)
22+
Serial.begin(115200);
23+
24+
// configure slope rate
25+
Serial.print("configure low slope rate of TJA1020\n");
26+
LinBus.setSlope(LinBus.LowSlope);
27+
}
28+
29+
bool readLinData()
30+
{
31+
// all handling of
32+
bool chkSumValid = LinBus.readFrame(0x2C);
33+
if (chkSumValid)
34+
{
35+
// Data now avaliabele in LinBus.LinMessage
36+
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;
40+
41+
// receive a single byte
42+
Cap_Configured = LinBus.LinMessage[4];
43+
44+
// decode flags within a byte
45+
CalibByte = LinBus.LinMessage[5];
46+
CalibrationDone = bitRead(LinBus.LinMessage[5], 0);
47+
}
48+
return chkSumValid;
49+
}
50+
51+
void loop()
52+
{
53+
if (readLinData())
54+
{
55+
Serial.print("Data reveived:\n");
56+
Serial.printf(" Cap_Max = %f\n", Cap_Max);
57+
Serial.printf(" Cap_Available = %f\n", Cap_Available);
58+
Serial.printf(" Cap_Configured = %d\n", Cap_Configured);
59+
Serial.printf(" CalibByte = %02Xh\n", CalibByte);
60+
Serial.printf(" CalibrationDone = %d\n", CalibrationDone);
61+
}
62+
63+
delay(5000);
64+
65+
//shut TJA1020 down, this also releases the INH pin.
66+
LinBus.setSlope(LinBus.Sleep);
67+
}

src/TJA1020.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include "TJA1020.hpp"
1212

13-
#include <Lin-Interface.h>
13+
#include <Lin-Interface.hpp>
1414
// #include <Arduino.h>
1515

1616
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)