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