HF-TMC9660 Driver 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC9660
Loading...
Searching...
No Matches
bootloader_utils.hpp
Go to the documentation of this file.
1
6#pragma once
7#include <array>
8#include <cstddef>
9#include <cstdint>
10
11namespace tmc9660 {
12
31static constexpr uint8_t reverseByte(uint8_t b) noexcept {
32 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
33 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
34 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
35 return b;
36}
37
54static constexpr uint8_t crc8Bootloader(const uint8_t* data, size_t len) noexcept {
55 uint16_t crc = 0; // 9-bit register for polynomial division
56 const uint16_t POLY = 0x107; // x^8 + x^2 + x^1 + x^0 in 9-bit form
57
58 // Process each byte (bit-reversed, LSB-first)
59 for (size_t i = 0; i < len; i++) {
60 uint8_t byte_reversed = reverseByte(data[i]);
61
62 // Feed 8 bits into CRC register (MSB-first from reversed byte)
63 for (int bit = 7; bit >= 0; bit--) {
64 crc = (crc << 1) | ((byte_reversed >> bit) & 1);
65 if (crc & 0x100) { // If bit 8 is set
66 crc ^= POLY;
67 }
68 }
69 }
70
71 // Append 8 zero bits (flush remaining data through CRC)
72 for (int i = 0; i < 8; i++) {
73 crc <<= 1;
74 if (crc & 0x100) {
75 crc ^= POLY;
76 }
77 }
78
79 return static_cast<uint8_t>(crc & 0xFF);
80}
81
82} // namespace tmc9660
Definition bootloader_config.hpp:9
static constexpr uint8_t crc8Bootloader(const uint8_t *data, size_t len) noexcept
CRC-8 calculation for UART protocol (TMC9660 datasheet method).
Definition bootloader_utils.hpp:54
static constexpr uint8_t reverseByte(uint8_t b) noexcept
Helper function for CRC-8 calculation (UART only).
Definition bootloader_utils.hpp:31