diff --git a/lin.cpp b/lin.cpp index ce5123a..79a6106 100644 --- a/lin.cpp +++ b/lin.cpp @@ -24,7 +24,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "Arduino.h" #include "lin.h" -#include //void p(char *fmt, ... ); @@ -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 @@ -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=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)); } diff --git a/lin.h b/lin.h index aad89ef..761a969 100644 --- a/lin.h +++ b/lin.h @@ -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; @@ -124,3 +126,5 @@ class Lin void loop(); }; + +#endif // LIN_H_