Code for autonomous ground vehicle, Data Bus, 3rd place winner in 2012 Sparkfun AVC.
Dependencies: Watchdog mbed Schedule SimpleFilter LSM303DLM PinDetect DebounceIn Servo
checksum.h
00001 #ifdef __cplusplus 00002 extern "C" { 00003 #endif 00004 00005 #ifndef _CHECKSUM_H_ 00006 #define _CHECKSUM_H_ 00007 00008 #include "inttypes.h" 00009 00010 00011 /** 00012 * 00013 * CALCULATE THE CHECKSUM 00014 * 00015 */ 00016 00017 #define X25_INIT_CRC 0xffff 00018 #define X25_VALIDATE_CRC 0xf0b8 00019 00020 /** 00021 * @brief Accumulate the X.25 CRC by adding one char at a time. 00022 * 00023 * The checksum function adds the hash of one char at a time to the 00024 * 16 bit checksum (uint16_t). 00025 * 00026 * @param data new char to hash 00027 * @param crcAccum the already accumulated checksum 00028 **/ 00029 static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum) 00030 { 00031 /*Accumulate one byte of data into the CRC*/ 00032 uint8_t tmp; 00033 00034 tmp=data ^ (uint8_t)(*crcAccum &0xff); 00035 tmp^= (tmp<<4); 00036 *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4); 00037 } 00038 00039 /** 00040 * @brief Initiliaze the buffer for the X.25 CRC 00041 * 00042 * @param crcAccum the 16 bit X.25 CRC 00043 */ 00044 static inline void crc_init(uint16_t* crcAccum) 00045 { 00046 *crcAccum = X25_INIT_CRC; 00047 } 00048 00049 00050 /** 00051 * @brief Calculates the X.25 checksum on a byte buffer 00052 * 00053 * @param pBuffer buffer containing the byte array to hash 00054 * @param length length of the byte array 00055 * @return the checksum over the buffer bytes 00056 **/ 00057 static inline uint16_t crc_calculate(uint8_t* pBuffer, int length) 00058 { 00059 00060 // For a "message" of length bytes contained in the unsigned char array 00061 // pointed to by pBuffer, calculate the CRC 00062 // crcCalculate(unsigned char* pBuffer, int length, unsigned short* checkConst) < not needed 00063 00064 uint16_t crcTmp; 00065 //uint16_t tmp; 00066 uint8_t* pTmp; 00067 int i; 00068 00069 pTmp=pBuffer; 00070 00071 00072 /* init crcTmp */ 00073 crc_init(&crcTmp); 00074 00075 for (i = 0; i < length; i++){ 00076 crc_accumulate(*pTmp++, &crcTmp); 00077 } 00078 00079 /* This is currently not needed, as only the checksum over payload should be computed 00080 tmp = crcTmp; 00081 crcAccumulate((unsigned char)(~crcTmp & 0xff),&tmp); 00082 crcAccumulate((unsigned char)((~crcTmp>>8)&0xff),&tmp); 00083 *checkConst = tmp; 00084 */ 00085 return(crcTmp); 00086 } 00087 00088 00089 00090 00091 #endif /* _CHECKSUM_H_ */ 00092 00093 #ifdef __cplusplus 00094 } 00095 #endif
Generated on Tue Jul 12 2022 14:09:25 by 1.7.2