![](/media/cache/profiles/2012_sf_avc_095.jpg.50x50_q85.jpg)
Code for autonomous rover for Sparkfun AVC. DataBus won 3rd in 2012 and the same code was used on Troubled Child, a 1986 Jeep Grand Wagoneer to win 1st in 2014.
Dependencies: mbed Watchdog SDFileSystem DigoleSerialDisp
MAVlink/include/checksum.h@25:bb5356402687, 2018-11-30 (annotated)
- Committer:
- shimniok
- Date:
- Fri Nov 30 16:11:53 2018 +0000
- Revision:
- 25:bb5356402687
- Parent:
- 0:a6a169de725f
Initial publish of revised version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:a6a169de725f | 1 | #ifdef __cplusplus |
shimniok | 0:a6a169de725f | 2 | extern "C" { |
shimniok | 0:a6a169de725f | 3 | #endif |
shimniok | 0:a6a169de725f | 4 | |
shimniok | 0:a6a169de725f | 5 | #ifndef _CHECKSUM_H_ |
shimniok | 0:a6a169de725f | 6 | #define _CHECKSUM_H_ |
shimniok | 0:a6a169de725f | 7 | |
shimniok | 0:a6a169de725f | 8 | #include "inttypes.h" |
shimniok | 0:a6a169de725f | 9 | |
shimniok | 0:a6a169de725f | 10 | |
shimniok | 0:a6a169de725f | 11 | /** |
shimniok | 0:a6a169de725f | 12 | * |
shimniok | 0:a6a169de725f | 13 | * CALCULATE THE CHECKSUM |
shimniok | 0:a6a169de725f | 14 | * |
shimniok | 0:a6a169de725f | 15 | */ |
shimniok | 0:a6a169de725f | 16 | |
shimniok | 0:a6a169de725f | 17 | #define X25_INIT_CRC 0xffff |
shimniok | 0:a6a169de725f | 18 | #define X25_VALIDATE_CRC 0xf0b8 |
shimniok | 0:a6a169de725f | 19 | |
shimniok | 0:a6a169de725f | 20 | /** |
shimniok | 0:a6a169de725f | 21 | * @brief Accumulate the X.25 CRC by adding one char at a time. |
shimniok | 0:a6a169de725f | 22 | * |
shimniok | 0:a6a169de725f | 23 | * The checksum function adds the hash of one char at a time to the |
shimniok | 0:a6a169de725f | 24 | * 16 bit checksum (uint16_t). |
shimniok | 0:a6a169de725f | 25 | * |
shimniok | 0:a6a169de725f | 26 | * @param data new char to hash |
shimniok | 0:a6a169de725f | 27 | * @param crcAccum the already accumulated checksum |
shimniok | 0:a6a169de725f | 28 | **/ |
shimniok | 0:a6a169de725f | 29 | static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum) |
shimniok | 0:a6a169de725f | 30 | { |
shimniok | 0:a6a169de725f | 31 | /*Accumulate one byte of data into the CRC*/ |
shimniok | 0:a6a169de725f | 32 | uint8_t tmp; |
shimniok | 0:a6a169de725f | 33 | |
shimniok | 0:a6a169de725f | 34 | tmp=data ^ (uint8_t)(*crcAccum &0xff); |
shimniok | 0:a6a169de725f | 35 | tmp^= (tmp<<4); |
shimniok | 0:a6a169de725f | 36 | *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4); |
shimniok | 0:a6a169de725f | 37 | } |
shimniok | 0:a6a169de725f | 38 | |
shimniok | 0:a6a169de725f | 39 | /** |
shimniok | 0:a6a169de725f | 40 | * @brief Initiliaze the buffer for the X.25 CRC |
shimniok | 0:a6a169de725f | 41 | * |
shimniok | 0:a6a169de725f | 42 | * @param crcAccum the 16 bit X.25 CRC |
shimniok | 0:a6a169de725f | 43 | */ |
shimniok | 0:a6a169de725f | 44 | static inline void crc_init(uint16_t* crcAccum) |
shimniok | 0:a6a169de725f | 45 | { |
shimniok | 0:a6a169de725f | 46 | *crcAccum = X25_INIT_CRC; |
shimniok | 0:a6a169de725f | 47 | } |
shimniok | 0:a6a169de725f | 48 | |
shimniok | 0:a6a169de725f | 49 | |
shimniok | 0:a6a169de725f | 50 | /** |
shimniok | 0:a6a169de725f | 51 | * @brief Calculates the X.25 checksum on a byte buffer |
shimniok | 0:a6a169de725f | 52 | * |
shimniok | 0:a6a169de725f | 53 | * @param pBuffer buffer containing the byte array to hash |
shimniok | 0:a6a169de725f | 54 | * @param length length of the byte array |
shimniok | 0:a6a169de725f | 55 | * @return the checksum over the buffer bytes |
shimniok | 0:a6a169de725f | 56 | **/ |
shimniok | 0:a6a169de725f | 57 | static inline uint16_t crc_calculate(uint8_t* pBuffer, int length) |
shimniok | 0:a6a169de725f | 58 | { |
shimniok | 0:a6a169de725f | 59 | |
shimniok | 0:a6a169de725f | 60 | // For a "message" of length bytes contained in the unsigned char array |
shimniok | 0:a6a169de725f | 61 | // pointed to by pBuffer, calculate the CRC |
shimniok | 0:a6a169de725f | 62 | // crcCalculate(unsigned char* pBuffer, int length, unsigned short* checkConst) < not needed |
shimniok | 0:a6a169de725f | 63 | |
shimniok | 0:a6a169de725f | 64 | uint16_t crcTmp; |
shimniok | 0:a6a169de725f | 65 | //uint16_t tmp; |
shimniok | 0:a6a169de725f | 66 | uint8_t* pTmp; |
shimniok | 0:a6a169de725f | 67 | int i; |
shimniok | 0:a6a169de725f | 68 | |
shimniok | 0:a6a169de725f | 69 | pTmp=pBuffer; |
shimniok | 0:a6a169de725f | 70 | |
shimniok | 0:a6a169de725f | 71 | |
shimniok | 0:a6a169de725f | 72 | /* init crcTmp */ |
shimniok | 0:a6a169de725f | 73 | crc_init(&crcTmp); |
shimniok | 0:a6a169de725f | 74 | |
shimniok | 0:a6a169de725f | 75 | for (i = 0; i < length; i++){ |
shimniok | 0:a6a169de725f | 76 | crc_accumulate(*pTmp++, &crcTmp); |
shimniok | 0:a6a169de725f | 77 | } |
shimniok | 0:a6a169de725f | 78 | |
shimniok | 0:a6a169de725f | 79 | /* This is currently not needed, as only the checksum over payload should be computed |
shimniok | 0:a6a169de725f | 80 | tmp = crcTmp; |
shimniok | 0:a6a169de725f | 81 | crcAccumulate((unsigned char)(~crcTmp & 0xff),&tmp); |
shimniok | 0:a6a169de725f | 82 | crcAccumulate((unsigned char)((~crcTmp>>8)&0xff),&tmp); |
shimniok | 0:a6a169de725f | 83 | *checkConst = tmp; |
shimniok | 0:a6a169de725f | 84 | */ |
shimniok | 0:a6a169de725f | 85 | return(crcTmp); |
shimniok | 0:a6a169de725f | 86 | } |
shimniok | 0:a6a169de725f | 87 | |
shimniok | 0:a6a169de725f | 88 | |
shimniok | 0:a6a169de725f | 89 | |
shimniok | 0:a6a169de725f | 90 | |
shimniok | 0:a6a169de725f | 91 | #endif /* _CHECKSUM_H_ */ |
shimniok | 0:a6a169de725f | 92 | |
shimniok | 0:a6a169de725f | 93 | #ifdef __cplusplus |
shimniok | 0:a6a169de725f | 94 | } |
shimniok | 0:a6a169de725f | 95 | #endif |