From 1c70570b11ac7484bf6ae474951f111139db5db1 Mon Sep 17 00:00:00 2001 From: tico-tico Date: Thu, 3 Mar 2016 04:42:08 +0300 Subject: [PATCH 1/3] Use avr-libc implementation of crc8 --- OneWire.cpp | 10 ++-------- OneWire.h | 1 + 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/OneWire.cpp b/OneWire.cpp index cf34933..428ecaf 100644 --- a/OneWire.cpp +++ b/OneWire.cpp @@ -519,15 +519,9 @@ uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) { uint8_t crc = 0; - + while (len--) { - uint8_t inbyte = *addr++; - for (uint8_t i = 8; i; i--) { - uint8_t mix = (crc ^ inbyte) & 0x01; - crc >>= 1; - if (mix) crc ^= 0x8C; - inbyte >>= 1; - } + crc = _crc_ibutton_update( crc, *addr++ ); } return crc; } diff --git a/OneWire.h b/OneWire.h index 8753e91..741f0a1 100644 --- a/OneWire.h +++ b/OneWire.h @@ -2,6 +2,7 @@ #define OneWire_h #include +#include #if ARDUINO >= 100 #include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc From d38ce2bc53bc0951b818b16e4cb10d5b2b0122f8 Mon Sep 17 00:00:00 2001 From: tico-tico Date: Thu, 3 Mar 2016 04:49:02 +0300 Subject: [PATCH 2/3] keep the style --- OneWire.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OneWire.cpp b/OneWire.cpp index 428ecaf..78a7b9b 100644 --- a/OneWire.cpp +++ b/OneWire.cpp @@ -521,7 +521,7 @@ uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) uint8_t crc = 0; while (len--) { - crc = _crc_ibutton_update( crc, *addr++ ); + crc = _crc_ibutton_update(crc, *addr++); } return crc; } From 27589d37d28b843edd76b503ccd2365735496fe3 Mon Sep 17 00:00:00 2001 From: tico-tico Date: Thu, 3 Mar 2016 06:37:27 +0300 Subject: [PATCH 3/3] it's for avr only --- OneWire.cpp | 10 ++++++++++ OneWire.h | 3 +++ 2 files changed, 13 insertions(+) diff --git a/OneWire.cpp b/OneWire.cpp index 78a7b9b..4499246 100644 --- a/OneWire.cpp +++ b/OneWire.cpp @@ -521,7 +521,17 @@ uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) uint8_t crc = 0; while (len--) { +#if defined(__AVR__) crc = _crc_ibutton_update(crc, *addr++); +#else + uint8_t inbyte = *addr++; + for (uint8_t i = 8; i; i--) { + uint8_t mix = (crc ^ inbyte) & 0x01; + crc >>= 1; + if (mix) crc ^= 0x8C; + inbyte >>= 1; + } +#endif } return crc; } diff --git a/OneWire.h b/OneWire.h index 741f0a1..b66b7eb 100644 --- a/OneWire.h +++ b/OneWire.h @@ -2,7 +2,10 @@ #define OneWire_h #include + +#if defined(__AVR__) #include +#endif #if ARDUINO >= 100 #include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc