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