Code for autonomous ground vehicle, Data Bus, 3rd place winner in 2012 Sparkfun AVC.

Dependencies:   Watchdog mbed Schedule SimpleFilter LSM303DLM PinDetect DebounceIn Servo

Committer:
shimniok
Date:
Wed Jun 20 14:57:48 2012 +0000
Revision:
0:826c6171fc1b
Updated documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 0:826c6171fc1b 1 #ifdef __cplusplus
shimniok 0:826c6171fc1b 2 extern "C" {
shimniok 0:826c6171fc1b 3 #endif
shimniok 0:826c6171fc1b 4
shimniok 0:826c6171fc1b 5 #ifndef _CHECKSUM_H_
shimniok 0:826c6171fc1b 6 #define _CHECKSUM_H_
shimniok 0:826c6171fc1b 7
shimniok 0:826c6171fc1b 8 #include "inttypes.h"
shimniok 0:826c6171fc1b 9
shimniok 0:826c6171fc1b 10
shimniok 0:826c6171fc1b 11 /**
shimniok 0:826c6171fc1b 12 *
shimniok 0:826c6171fc1b 13 * CALCULATE THE CHECKSUM
shimniok 0:826c6171fc1b 14 *
shimniok 0:826c6171fc1b 15 */
shimniok 0:826c6171fc1b 16
shimniok 0:826c6171fc1b 17 #define X25_INIT_CRC 0xffff
shimniok 0:826c6171fc1b 18 #define X25_VALIDATE_CRC 0xf0b8
shimniok 0:826c6171fc1b 19
shimniok 0:826c6171fc1b 20 /**
shimniok 0:826c6171fc1b 21 * @brief Accumulate the X.25 CRC by adding one char at a time.
shimniok 0:826c6171fc1b 22 *
shimniok 0:826c6171fc1b 23 * The checksum function adds the hash of one char at a time to the
shimniok 0:826c6171fc1b 24 * 16 bit checksum (uint16_t).
shimniok 0:826c6171fc1b 25 *
shimniok 0:826c6171fc1b 26 * @param data new char to hash
shimniok 0:826c6171fc1b 27 * @param crcAccum the already accumulated checksum
shimniok 0:826c6171fc1b 28 **/
shimniok 0:826c6171fc1b 29 static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum)
shimniok 0:826c6171fc1b 30 {
shimniok 0:826c6171fc1b 31 /*Accumulate one byte of data into the CRC*/
shimniok 0:826c6171fc1b 32 uint8_t tmp;
shimniok 0:826c6171fc1b 33
shimniok 0:826c6171fc1b 34 tmp=data ^ (uint8_t)(*crcAccum &0xff);
shimniok 0:826c6171fc1b 35 tmp^= (tmp<<4);
shimniok 0:826c6171fc1b 36 *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4);
shimniok 0:826c6171fc1b 37 }
shimniok 0:826c6171fc1b 38
shimniok 0:826c6171fc1b 39 /**
shimniok 0:826c6171fc1b 40 * @brief Initiliaze the buffer for the X.25 CRC
shimniok 0:826c6171fc1b 41 *
shimniok 0:826c6171fc1b 42 * @param crcAccum the 16 bit X.25 CRC
shimniok 0:826c6171fc1b 43 */
shimniok 0:826c6171fc1b 44 static inline void crc_init(uint16_t* crcAccum)
shimniok 0:826c6171fc1b 45 {
shimniok 0:826c6171fc1b 46 *crcAccum = X25_INIT_CRC;
shimniok 0:826c6171fc1b 47 }
shimniok 0:826c6171fc1b 48
shimniok 0:826c6171fc1b 49
shimniok 0:826c6171fc1b 50 /**
shimniok 0:826c6171fc1b 51 * @brief Calculates the X.25 checksum on a byte buffer
shimniok 0:826c6171fc1b 52 *
shimniok 0:826c6171fc1b 53 * @param pBuffer buffer containing the byte array to hash
shimniok 0:826c6171fc1b 54 * @param length length of the byte array
shimniok 0:826c6171fc1b 55 * @return the checksum over the buffer bytes
shimniok 0:826c6171fc1b 56 **/
shimniok 0:826c6171fc1b 57 static inline uint16_t crc_calculate(uint8_t* pBuffer, int length)
shimniok 0:826c6171fc1b 58 {
shimniok 0:826c6171fc1b 59
shimniok 0:826c6171fc1b 60 // For a "message" of length bytes contained in the unsigned char array
shimniok 0:826c6171fc1b 61 // pointed to by pBuffer, calculate the CRC
shimniok 0:826c6171fc1b 62 // crcCalculate(unsigned char* pBuffer, int length, unsigned short* checkConst) < not needed
shimniok 0:826c6171fc1b 63
shimniok 0:826c6171fc1b 64 uint16_t crcTmp;
shimniok 0:826c6171fc1b 65 //uint16_t tmp;
shimniok 0:826c6171fc1b 66 uint8_t* pTmp;
shimniok 0:826c6171fc1b 67 int i;
shimniok 0:826c6171fc1b 68
shimniok 0:826c6171fc1b 69 pTmp=pBuffer;
shimniok 0:826c6171fc1b 70
shimniok 0:826c6171fc1b 71
shimniok 0:826c6171fc1b 72 /* init crcTmp */
shimniok 0:826c6171fc1b 73 crc_init(&crcTmp);
shimniok 0:826c6171fc1b 74
shimniok 0:826c6171fc1b 75 for (i = 0; i < length; i++){
shimniok 0:826c6171fc1b 76 crc_accumulate(*pTmp++, &crcTmp);
shimniok 0:826c6171fc1b 77 }
shimniok 0:826c6171fc1b 78
shimniok 0:826c6171fc1b 79 /* This is currently not needed, as only the checksum over payload should be computed
shimniok 0:826c6171fc1b 80 tmp = crcTmp;
shimniok 0:826c6171fc1b 81 crcAccumulate((unsigned char)(~crcTmp & 0xff),&tmp);
shimniok 0:826c6171fc1b 82 crcAccumulate((unsigned char)((~crcTmp>>8)&0xff),&tmp);
shimniok 0:826c6171fc1b 83 *checkConst = tmp;
shimniok 0:826c6171fc1b 84 */
shimniok 0:826c6171fc1b 85 return(crcTmp);
shimniok 0:826c6171fc1b 86 }
shimniok 0:826c6171fc1b 87
shimniok 0:826c6171fc1b 88
shimniok 0:826c6171fc1b 89
shimniok 0:826c6171fc1b 90
shimniok 0:826c6171fc1b 91 #endif /* _CHECKSUM_H_ */
shimniok 0:826c6171fc1b 92
shimniok 0:826c6171fc1b 93 #ifdef __cplusplus
shimniok 0:826c6171fc1b 94 }
shimniok 0:826c6171fc1b 95 #endif