mavlink library

Dependents:   mavlink F429ZI_LCD_demo

Fork of mavlink_bridge by Benjamin Hepp

Committer:
wupinxian
Date:
Mon Jul 23 16:34:26 2018 +0000
Revision:
11:76345550efb4
Parent:
0:28183cc7963f
mavlink for stm32 Nucleo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bhepp 0:28183cc7963f 1 #ifdef __cplusplus
bhepp 0:28183cc7963f 2 extern "C" {
bhepp 0:28183cc7963f 3 #endif
bhepp 0:28183cc7963f 4
bhepp 0:28183cc7963f 5 #ifndef _CHECKSUM_H_
bhepp 0:28183cc7963f 6 #define _CHECKSUM_H_
bhepp 0:28183cc7963f 7
bhepp 0:28183cc7963f 8 // Visual Studio versions before 2010 don't have stdint.h, so we just error out.
bhepp 0:28183cc7963f 9 #if (defined _MSC_VER) && (_MSC_VER < 1600)
bhepp 0:28183cc7963f 10 #error "The C-MAVLink implementation requires Visual Studio 2010 or greater"
bhepp 0:28183cc7963f 11 #endif
bhepp 0:28183cc7963f 12
bhepp 0:28183cc7963f 13 #include <stdint.h>
bhepp 0:28183cc7963f 14
bhepp 0:28183cc7963f 15 /**
bhepp 0:28183cc7963f 16 *
bhepp 0:28183cc7963f 17 * CALCULATE THE CHECKSUM
bhepp 0:28183cc7963f 18 *
bhepp 0:28183cc7963f 19 */
bhepp 0:28183cc7963f 20
bhepp 0:28183cc7963f 21 #define X25_INIT_CRC 0xffff
bhepp 0:28183cc7963f 22 #define X25_VALIDATE_CRC 0xf0b8
bhepp 0:28183cc7963f 23
bhepp 0:28183cc7963f 24 #ifndef HAVE_CRC_ACCUMULATE
bhepp 0:28183cc7963f 25 /**
bhepp 0:28183cc7963f 26 * @brief Accumulate the X.25 CRC by adding one char at a time.
bhepp 0:28183cc7963f 27 *
bhepp 0:28183cc7963f 28 * The checksum function adds the hash of one char at a time to the
bhepp 0:28183cc7963f 29 * 16 bit checksum (uint16_t).
bhepp 0:28183cc7963f 30 *
bhepp 0:28183cc7963f 31 * @param data new char to hash
bhepp 0:28183cc7963f 32 * @param crcAccum the already accumulated checksum
bhepp 0:28183cc7963f 33 **/
bhepp 0:28183cc7963f 34 static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum)
bhepp 0:28183cc7963f 35 {
bhepp 0:28183cc7963f 36 /*Accumulate one byte of data into the CRC*/
bhepp 0:28183cc7963f 37 uint8_t tmp;
bhepp 0:28183cc7963f 38
bhepp 0:28183cc7963f 39 tmp = data ^ (uint8_t)(*crcAccum &0xff);
bhepp 0:28183cc7963f 40 tmp ^= (tmp<<4);
bhepp 0:28183cc7963f 41 *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4);
bhepp 0:28183cc7963f 42 }
bhepp 0:28183cc7963f 43 #endif
bhepp 0:28183cc7963f 44
bhepp 0:28183cc7963f 45
bhepp 0:28183cc7963f 46 /**
bhepp 0:28183cc7963f 47 * @brief Initiliaze the buffer for the X.25 CRC
bhepp 0:28183cc7963f 48 *
bhepp 0:28183cc7963f 49 * @param crcAccum the 16 bit X.25 CRC
bhepp 0:28183cc7963f 50 */
bhepp 0:28183cc7963f 51 static inline void crc_init(uint16_t* crcAccum)
bhepp 0:28183cc7963f 52 {
bhepp 0:28183cc7963f 53 *crcAccum = X25_INIT_CRC;
bhepp 0:28183cc7963f 54 }
bhepp 0:28183cc7963f 55
bhepp 0:28183cc7963f 56
bhepp 0:28183cc7963f 57 /**
bhepp 0:28183cc7963f 58 * @brief Calculates the X.25 checksum on a byte buffer
bhepp 0:28183cc7963f 59 *
bhepp 0:28183cc7963f 60 * @param pBuffer buffer containing the byte array to hash
bhepp 0:28183cc7963f 61 * @param length length of the byte array
bhepp 0:28183cc7963f 62 * @return the checksum over the buffer bytes
bhepp 0:28183cc7963f 63 **/
bhepp 0:28183cc7963f 64 static inline uint16_t crc_calculate(const uint8_t* pBuffer, uint16_t length)
bhepp 0:28183cc7963f 65 {
bhepp 0:28183cc7963f 66 uint16_t crcTmp;
bhepp 0:28183cc7963f 67 crc_init(&crcTmp);
bhepp 0:28183cc7963f 68 while (length--) {
bhepp 0:28183cc7963f 69 crc_accumulate(*pBuffer++, &crcTmp);
bhepp 0:28183cc7963f 70 }
bhepp 0:28183cc7963f 71 return crcTmp;
bhepp 0:28183cc7963f 72 }
bhepp 0:28183cc7963f 73
bhepp 0:28183cc7963f 74
bhepp 0:28183cc7963f 75 /**
bhepp 0:28183cc7963f 76 * @brief Accumulate the X.25 CRC by adding an array of bytes
bhepp 0:28183cc7963f 77 *
bhepp 0:28183cc7963f 78 * The checksum function adds the hash of one char at a time to the
bhepp 0:28183cc7963f 79 * 16 bit checksum (uint16_t).
bhepp 0:28183cc7963f 80 *
bhepp 0:28183cc7963f 81 * @param data new bytes to hash
bhepp 0:28183cc7963f 82 * @param crcAccum the already accumulated checksum
bhepp 0:28183cc7963f 83 **/
bhepp 0:28183cc7963f 84 static inline void crc_accumulate_buffer(uint16_t *crcAccum, const char *pBuffer, uint16_t length)
bhepp 0:28183cc7963f 85 {
bhepp 0:28183cc7963f 86 const uint8_t *p = (const uint8_t *)pBuffer;
bhepp 0:28183cc7963f 87 while (length--) {
bhepp 0:28183cc7963f 88 crc_accumulate(*p++, crcAccum);
bhepp 0:28183cc7963f 89 }
bhepp 0:28183cc7963f 90 }
bhepp 0:28183cc7963f 91
bhepp 0:28183cc7963f 92 #endif /* _CHECKSUM_H_ */
bhepp 0:28183cc7963f 93
bhepp 0:28183cc7963f 94 #ifdef __cplusplus
bhepp 0:28183cc7963f 95 }
bhepp 0:28183cc7963f 96 #endif