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 09:27:19 2014 +0000
Revision:
35:95cb34636703
Child:
36:1bbd2fb7d2c8
Ajout de la librairie MavLink avec un main de test pour QGroundControl.; Pour l'instant seul le heartbeat fonctionne;

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