X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;ds=sidebyside;f=bertos%2Falgo%2Fcrc.h;h=b64d140feb16adcb33f002120d16cbf35661b19b;hb=fa680c1a4d8250aee2ad4dd46a833b0279c7e9de;hp=be8f0df8be16915f3318852f3329f2bda9e70fc1;hpb=37efb5bdc0504ab6df2e8db0635c9c6f7477e23e;p=bertos.git diff --git a/bertos/algo/crc.h b/bertos/algo/crc.h index be8f0df8..b64d140f 100644 --- a/bertos/algo/crc.h +++ b/bertos/algo/crc.h @@ -31,7 +31,7 @@ * * --> * - * \brief Cyclic Redundancy Check 16 (CRC). + * \brief Cyclic Redundancy Check 16 (CRC). This algorithm is the one used by the XMODEM protocol. * * \note This algorithm is incompatible with the CCITT-CRC16. * @@ -53,11 +53,9 @@ #define ALGO_CRC_H #include +#include -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - +EXTERN_C_BEGIN /* CRC table */ extern const uint16_t crc16tab[256]; @@ -72,16 +70,26 @@ extern const uint16_t crc16tab[256]; * \param c New octet (range 0-255) * \param oldcrc Previous CRC16 value (referenced twice, beware of side effects) */ -#define UPDCRC16(c, oldcrc) (crc16tab[((oldcrc) >> 8) ^ ((unsigned char)(c))] ^ ((oldcrc) << 8)) +#if CPU_HARVARD + #define UPDCRC16(c, oldcrc) (pgm_read_uint16_t(&crc16tab[((oldcrc) >> 8) ^ ((unsigned char)(c))]) ^ ((oldcrc) << 8)) +#else + #define UPDCRC16(c, oldcrc) ((crc16tab[((oldcrc) >> 8) ^ ((unsigned char)(c))]) ^ ((oldcrc) << 8)) +#endif +/** CRC-16 init value */ +#define CRC16_INIT_VAL ((uint16_t)0) #ifdef INLINE /** - * \brief Compute the updated CRC16 value for one octet (macro version) + * \brief Compute the updated CRC16 value for one octet (inline version) */ INLINE uint16_t updcrc16(uint8_t c, uint16_t oldcrc) { +#if CPU_HARVARD + return pgm_read_uint16_t(&crc16tab[(oldcrc >> 8) ^ c]) ^ (oldcrc << 8); +#else return crc16tab[(oldcrc >> 8) ^ c] ^ (oldcrc << 8); +#endif } #endif // INLINE @@ -97,8 +105,6 @@ INLINE uint16_t updcrc16(uint8_t c, uint16_t oldcrc) */ extern uint16_t crc16(uint16_t crc, const void *buf, size_t len); -#ifdef __cplusplus -} -#endif /* __cplusplus */ +EXTERN_C_END #endif /* ALGO_CRC_H */