CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001
| 136 | |
| 137 | // CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001 |
| 138 | uint16_t crc16(const uint8_t *array, uint16_t length, const uint16_t polynome, |
| 139 | const uint16_t startmask, const uint16_t endmask, |
| 140 | const bool reverseIn, const bool reverseOut) |
| 141 | { |
| 142 | uint16_t crc = startmask; |
| 143 | while (length--) |
| 144 | { |
| 145 | if ((length & 0xFF) == 0) yield(); // RTOS |
| 146 | uint8_t data = *array++; |
| 147 | if (reverseIn) data = reverse8(data); |
| 148 | crc ^= ((uint16_t)data) << 8; |
| 149 | for (uint8_t i = 8; i; i--) |
| 150 | { |
| 151 | if (crc & (1 << 15)) |
| 152 | { |
| 153 | crc <<= 1; |
| 154 | crc ^= polynome; |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | crc <<= 1; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | if (reverseOut) crc = reverse16(crc); |
| 163 | crc ^= endmask; |
| 164 | return crc; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | // CRC-CCITT POLYNOME = x13 + X5 + 1 = 0001 0000 0010 0001 = 0x1021 |