Projet Drone de surveillance du labo TRSE (INGESUP)

Dependencies:   mbed PID ADXL345 Camera_LS_Y201 ITG3200 RangeFinder mbos xbee_lib Motor Servo

Committer:
Gaetan
Date:
Wed Mar 19 11:01:10 2014 +0000
Revision:
36:1bbd2fb7d2c8
Parent:
35:95cb34636703
Checksum correct pour l'envoi de n'importe quel message Mavlink. Reste ? g?rer la reception de messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Gaetan 35:95cb34636703 1 #ifdef __cplusplus
Gaetan 35:95cb34636703 2 extern "C" {
Gaetan 35:95cb34636703 3 #endif
Gaetan 35:95cb34636703 4
Gaetan 35:95cb34636703 5 #ifndef _CHECKSUM_H_
Gaetan 35:95cb34636703 6 #define _CHECKSUM_H_
Gaetan 35:95cb34636703 7
Gaetan 35:95cb34636703 8 #include "inttypes.h"
Gaetan 35:95cb34636703 9
Gaetan 35:95cb34636703 10
Gaetan 35:95cb34636703 11 /**
Gaetan 35:95cb34636703 12 *
Gaetan 35:95cb34636703 13 * CALCULATE THE CHECKSUM
Gaetan 35:95cb34636703 14 *
Gaetan 35:95cb34636703 15 */
Gaetan 35:95cb34636703 16
Gaetan 35:95cb34636703 17 #define X25_INIT_CRC 0xffff
Gaetan 35:95cb34636703 18 #define X25_VALIDATE_CRC 0xf0b8
Gaetan 35:95cb34636703 19
Gaetan 35:95cb34636703 20 /**
Gaetan 36:1bbd2fb7d2c8 21 *
Gaetan 36:1bbd2fb7d2c8 22 * Additionnal seed for crc calculation in MavLink 1.0
Gaetan 36:1bbd2fb7d2c8 23 *
Gaetan 36:1bbd2fb7d2c8 24 */
Gaetan 36:1bbd2fb7d2c8 25
Gaetan 36:1bbd2fb7d2c8 26 #define MAVLINK_MESSAGE_CRCS {50, 124, 137, 0, 237, 217, 104, 119, 0, 0, 0, 89, 0, 0, 0, 0, 0, 0, 0, 0, 214, 159, 220, 168, 24, 23, 170, 144, 67, 115, 39, 246, 185, 104, 237, 244, 222, 212, 9, 254, 230, 28, 28, 132, 221, 232, 11, 153, 41, 39, 214, 223, 141, 33, 15, 3, 100, 24, 239, 238, 30, 200, 183, 0, 130, 0, 148, 21, 0, 52, 124, 0, 0, 0, 20, 0, 152, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 231, 183, 63, 54, 0, 0, 0, 0, 0, 0, 0, 175, 102, 158, 208, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 49, 170, 44, 83, 46, 0}
Gaetan 36:1bbd2fb7d2c8 27
Gaetan 36:1bbd2fb7d2c8 28 /**
Gaetan 35:95cb34636703 29 * @brief Accumulate the X.25 CRC by adding one char at a time.
Gaetan 35:95cb34636703 30 *
Gaetan 35:95cb34636703 31 * The checksum function adds the hash of one char at a time to the
Gaetan 35:95cb34636703 32 * 16 bit checksum (uint16_t).
Gaetan 35:95cb34636703 33 *
Gaetan 35:95cb34636703 34 * @param data new char to hash
Gaetan 35:95cb34636703 35 * @param crcAccum the already accumulated checksum
Gaetan 35:95cb34636703 36 **/
Gaetan 35:95cb34636703 37 static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum)
Gaetan 35:95cb34636703 38 {
Gaetan 35:95cb34636703 39 /*Accumulate one byte of data into the CRC*/
Gaetan 35:95cb34636703 40 uint8_t tmp;
Gaetan 35:95cb34636703 41
Gaetan 35:95cb34636703 42 tmp=data ^ (uint8_t)(*crcAccum &0xff);
Gaetan 35:95cb34636703 43 tmp^= (tmp<<4);
Gaetan 35:95cb34636703 44 *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4);
Gaetan 35:95cb34636703 45 }
Gaetan 35:95cb34636703 46
Gaetan 35:95cb34636703 47 /**
Gaetan 35:95cb34636703 48 * @brief Initiliaze the buffer for the X.25 CRC
Gaetan 35:95cb34636703 49 *
Gaetan 35:95cb34636703 50 * @param crcAccum the 16 bit X.25 CRC
Gaetan 35:95cb34636703 51 */
Gaetan 35:95cb34636703 52 static inline void crc_init(uint16_t* crcAccum)
Gaetan 35:95cb34636703 53 {
Gaetan 35:95cb34636703 54 *crcAccum = X25_INIT_CRC;
Gaetan 35:95cb34636703 55 }
Gaetan 35:95cb34636703 56
Gaetan 35:95cb34636703 57
Gaetan 35:95cb34636703 58 /**
Gaetan 35:95cb34636703 59 * @brief Calculates the X.25 checksum on a byte buffer
Gaetan 35:95cb34636703 60 *
Gaetan 35:95cb34636703 61 * @param pBuffer buffer containing the byte array to hash
Gaetan 35:95cb34636703 62 * @param length length of the byte array
Gaetan 35:95cb34636703 63 * @return the checksum over the buffer bytes
Gaetan 35:95cb34636703 64 **/
Gaetan 35:95cb34636703 65 static inline uint16_t crc_calculate(uint8_t* pBuffer, int length)
Gaetan 35:95cb34636703 66 {
Gaetan 35:95cb34636703 67
Gaetan 35:95cb34636703 68 // For a "message" of length bytes contained in the unsigned char array
Gaetan 35:95cb34636703 69 // pointed to by pBuffer, calculate the CRC
Gaetan 35:95cb34636703 70 // crcCalculate(unsigned char* pBuffer, int length, unsigned short* checkConst) < not needed
Gaetan 35:95cb34636703 71
Gaetan 35:95cb34636703 72 uint16_t crcTmp;
Gaetan 35:95cb34636703 73 //uint16_t tmp;
Gaetan 35:95cb34636703 74 uint8_t* pTmp;
Gaetan 35:95cb34636703 75 int i;
Gaetan 35:95cb34636703 76
Gaetan 35:95cb34636703 77 pTmp=pBuffer;
Gaetan 35:95cb34636703 78
Gaetan 35:95cb34636703 79
Gaetan 35:95cb34636703 80 /* init crcTmp */
Gaetan 35:95cb34636703 81 crc_init(&crcTmp);
Gaetan 35:95cb34636703 82
Gaetan 35:95cb34636703 83 for (i = 0; i < length; i++){
Gaetan 35:95cb34636703 84 crc_accumulate(*pTmp++, &crcTmp);
Gaetan 35:95cb34636703 85 }
Gaetan 35:95cb34636703 86
Gaetan 35:95cb34636703 87 /* This is currently not needed, as only the checksum over payload should be computed
Gaetan 35:95cb34636703 88 tmp = crcTmp;
Gaetan 35:95cb34636703 89 crcAccumulate((unsigned char)(~crcTmp & 0xff),&tmp);
Gaetan 35:95cb34636703 90 crcAccumulate((unsigned char)((~crcTmp>>8)&0xff),&tmp);
Gaetan 35:95cb34636703 91 *checkConst = tmp;
Gaetan 35:95cb34636703 92 */
Gaetan 35:95cb34636703 93 return(crcTmp);
Gaetan 35:95cb34636703 94 }
Gaetan 35:95cb34636703 95
Gaetan 35:95cb34636703 96
Gaetan 35:95cb34636703 97
Gaetan 35:95cb34636703 98
Gaetan 35:95cb34636703 99 #endif /* _CHECKSUM_H_ */
Gaetan 35:95cb34636703 100
Gaetan 35:95cb34636703 101 #ifdef __cplusplus
Gaetan 35:95cb34636703 102 }
Gaetan 35:95cb34636703 103 #endif