diff --git a/TF_Config.example.h b/TF_Config.example.h index 15c39fd..099d026 100644 --- a/TF_Config.example.h +++ b/TF_Config.example.h @@ -30,10 +30,10 @@ #define TF_TYPE_BYTES 1 // Checksum type. Options: -// TF_CKSUM_NONE, TF_CKSUM_XOR, TF_CKSUM_CRC16, TF_CKSUM_CRC32 +// TF_CKSUM_NONE, TF_CKSUM_XOR, TF_CKSUM_CRC8, TF_CKSUM_CRC16, TF_CKSUM_CRC32 // TF_CKSUM_CUSTOM8, TF_CKSUM_CUSTOM16, TF_CKSUM_CUSTOM32 // Custom checksums require you to implement checksum functions (see TinyFrame.h) -#define TF_CKSUM_TYPE TF_CKSUM_CUSTOM8 +#define TF_CKSUM_TYPE TF_CKSUM_CRC16 // Use a SOF byte to mark the start of a frame #define TF_USE_SOF_BYTE 1 diff --git a/TinyFrame.c b/TinyFrame.c index b523be5..ae54225 100644 --- a/TinyFrame.c +++ b/TinyFrame.c @@ -43,6 +43,26 @@ TF_CKSUM TF_CksumAdd(TF_CKSUM cksum, uint8_t byte) { return cksum ^ byte; } TF_CKSUM TF_CksumEnd(TF_CKSUM cksum) { return (TF_CKSUM) ~cksum; } +#elif TF_CKSUM_TYPE == TF_CKSUM_CRC8 + + static inline uint8_t crc8_bits(uint8_t data) + { + uint8_t crc = 0; + if(data & 1) crc ^= 0x5e; + if(data & 2) crc ^= 0xbc; + if(data & 4) crc ^= 0x61; + if(data & 8) crc ^= 0xc2; + if(data & 0x10) crc ^= 0x9d; + if(data & 0x20) crc ^= 0x23; + if(data & 0x40) crc ^= 0x46; + if(data & 0x80) crc ^= 0x8c; + return crc; + } + + TF_CKSUM TF_CksumStart(void) { return 0; } + TF_CKSUM TF_CksumAdd(TF_CKSUM cksum, uint8_t byte) { return crc8_bits(byte ^ cksum); } + TF_CKSUM TF_CksumEnd(TF_CKSUM cksum) { return cksum; } + #elif TF_CKSUM_TYPE == TF_CKSUM_CRC16 // TODO try to replace with an algorithm diff --git a/TinyFrame.h b/TinyFrame.h index 6ba5acd..11eb083 100644 --- a/TinyFrame.h +++ b/TinyFrame.h @@ -20,13 +20,14 @@ //--------------------------------------------------------------------------- // Checksum type (0 = none, 8 = ~XOR, 16 = CRC16 0x8005, 32 = CRC32) -#define TF_CKSUM_NONE 0 -#define TF_CKSUM_XOR 8 -#define TF_CKSUM_CRC16 16 -#define TF_CKSUM_CRC32 32 -#define TF_CKSUM_CUSTOM8 1 -#define TF_CKSUM_CUSTOM16 2 -#define TF_CKSUM_CUSTOM32 3 +#define TF_CKSUM_NONE 0 // no checksums +#define TF_CKSUM_XOR 8 // inverted xor of all payload bytes +#define TF_CKSUM_CRC8 9 // Dallas/Maxim CRC8 (1-wire) +#define TF_CKSUM_CRC16 16 // CRC16 with the polynomial 0x8005 (x^16 + x^15 + x^2 + 1) +#define TF_CKSUM_CRC32 32 // CRC32 with the polynomial 0xedb88320 +#define TF_CKSUM_CUSTOM8 1 // Custom 8-bit checksum +#define TF_CKSUM_CUSTOM16 2 // Custom 16-bit checksum +#define TF_CKSUM_CUSTOM32 3 // Custom 32-bit checksum #include "TF_Config.h" @@ -65,7 +66,7 @@ typedef uint32_t TF_ID; #endif -#if (TF_CKSUM_TYPE == TF_CKSUM_XOR) || (TF_CKSUM_TYPE == TF_CKSUM_NONE) || (TF_CKSUM_TYPE == TF_CKSUM_CUSTOM8) +#if (TF_CKSUM_TYPE == TF_CKSUM_XOR) || (TF_CKSUM_TYPE == TF_CKSUM_NONE) || (TF_CKSUM_TYPE == TF_CKSUM_CUSTOM8) || (TF_CKSUM_TYPE == TF_CKSUM_CRC8) // ~XOR (if 0, still use 1 byte - it won't be used) typedef uint8_t TF_CKSUM; #elif (TF_CKSUM_TYPE == TF_CKSUM_CRC16) || (TF_CKSUM_TYPE == TF_CKSUM_CUSTOM16) @@ -320,12 +321,14 @@ bool TF_SendSimple(TinyFrame *tf, TF_TYPE type, const uint8_t *data, TF_LEN len) * @param timeout - listener expiry time in ticks * @return success */ -bool TF_Query(TinyFrame *tf, TF_Msg *msg, TF_Listener listener, TF_TICKS timeout); +bool TF_Query(TinyFrame *tf, TF_Msg *msg, + TF_Listener listener, TF_TICKS timeout); /** * Like TF_Query, but without the struct */ -bool TF_QuerySimple(TinyFrame *tf, TF_TYPE type, const uint8_t *data, TF_LEN len, +bool TF_QuerySimple(TinyFrame *tf, TF_TYPE type, + const uint8_t *data, TF_LEN len, TF_Listener listener, TF_TICKS timeout); /**