Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "Arduino.h"
#include "lin.h"
#include <util/delay.h>

//void p(char *fmt, ... );

Expand Down Expand Up @@ -120,7 +119,8 @@ uint8_t Lin::addrParity(uint8_t addr)
void Lin::send(uint8_t addr, const uint8_t* message, uint8_t nBytes,uint8_t proto)
{
uint8_t addrbyte = (addr&0x3f) | addrParity(addr);
uint8_t cksum = dataChecksum(message,nBytes,(proto==1) ? 0:addrbyte);
// LIN diagnostic frame shall always use CHKSUM of protocol version 1.x.
uint8_t cksum = dataChecksum(message, nBytes, (proto == 1 || addr == 0x3C) ? 0 : addrbyte);
serialBreak(); // Generate the low signal that exceeds 1 char.
serial.write(0x55); // Sync byte
serial.write(addrbyte); // ID byte
Expand All @@ -143,26 +143,27 @@ uint8_t Lin::recv(uint8_t addr, uint8_t* message, uint8_t nBytes,uint8_t proto)
pinMode(txPin, INPUT);
digitalWrite(txPin, LOW); // don't pull up
do { // I hear myself
while(!serial.available()) { _delay_us(100); timeoutCount+= 100; if (timeoutCount>=timeout) goto done; }
while(!serial.available()) { delayMicroseconds(100); timeoutCount+= 100; if (timeoutCount>=timeout) goto done; }
} while(serial.read() != 0x55);
do {
while(!serial.available()) { _delay_us(100); timeoutCount+= 100; if (timeoutCount>=timeout) goto done; }
while(!serial.available()) { delayMicroseconds(100); timeoutCount+= 100; if (timeoutCount>=timeout) goto done; }
} while(serial.read() != idByte);


for (uint8_t i=0;i<nBytes;i++)
{
// This while loop strategy does not take into account the added time for the logic. So the actual timeout will be slightly longer then written here.
while(!serial.available()) { _delay_us(100); timeoutCount+= 100; if (timeoutCount>=timeout) goto done; }
while(!serial.available()) { delayMicroseconds(100); timeoutCount+= 100; if (timeoutCount>=timeout) goto done; }
message[i] = serial.read();
bytesRcvd++;
}
while(!serial.available()) { _delay_us(100); timeoutCount+= 100; if (timeoutCount>=timeout) goto done; }
while(!serial.available()) { delayMicroseconds(100); timeoutCount+= 100; if (timeoutCount>=timeout) goto done; }
if (serial.available())
{
uint8_t cksum = serial.read();
bytesRcvd++;
if (proto==1) idByte = 0; // Don't cksum the ID byte in LIN 1.x
// LIN diagnostic frame shall always use CHKSUM of LIN protocol version 1.x.
if (proto == 1 || addr == 0x3D) idByte = 0; // Don't cksum the ID byte in LIN 1.x
if (dataChecksum(message,nBytes,idByte) == cksum) bytesRcvd = 0xff;
//p("cksum byte %x, calculated %x %x\n",cksum,dataChecksum(message,nBytes,idByte),dataChecksum(message,nBytes,0));
}
Expand Down
4 changes: 4 additions & 0 deletions lin.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef LIN_H_
#define LIN_H_

//typedef unsigned char uint8_t;
//typedef unsigned int uint16_t;
Expand Down Expand Up @@ -124,3 +126,5 @@ class Lin
void loop();

};

#endif // LIN_H_