9#include "as5047u_version.h"
73 std::array<uint16_t, kOtpRegisterCount>
written{};
79inline constexpr float kPi = 3.14159265358979323846F;
113template <
typename SpiType>
133 for (
int i = 0; i < 16; ++i) {
134 bool bit = ((data16 >> (15 - i)) & 1) ^ ((crc >> 7) & 1);
135 crc = (crc << 1) ^ (bit ? 0x1D : 0x00);
209 return static_cast<uint16_t
>((degrees * 16384.0F) / 360.0F) & 0x3FFF;
213 return static_cast<uint16_t
>((radians * 16384.0F) / (2.0F *
kPi)) & 0x3FFF;
422 [[nodiscard]]
bool VerifyOTP(
const std::array<uint16_t, kOtpRegisterCount>& burned,
433 void SetPad(uint8_t pad)
noexcept;
491 template <
typename RegT>
493 return decode<RegT>(readRegister(RegT::ADDRESS));
506 template <
typename RegT>
508 return readWithRetries(RegT::ADDRESS, retries, reg.value);
522 template <
typename RegT>
524 return writeRegister(RegT::ADDRESS, encode(reg), retries);
553 return HF_AS5047U_VERSION_STRING;
558 return HF_AS5047U_VERSION_MAJOR;
563 return HF_AS5047U_VERSION_MINOR;
568 return HF_AS5047U_VERSION_PATCH;
576 static constexpr uint16_t kReadBit = 0x4000;
577 static constexpr uint16_t kReplyWarningBit = 0x8000;
578 static constexpr uint16_t kReplyErrorBit = 0x4000;
583 bool transferFrame(
FrameFormat format, uint16_t word, uint16_t& reply,
584 bool inspect =
false)
const;
586 bool rawReadRegister(uint16_t addr, uint16_t& value)
const;
589 bool readRegister(uint16_t addr, uint16_t& value)
const;
590 uint16_t readRegister(uint16_t addr)
const;
593 bool readWithRetries(uint16_t addr, uint8_t retries, uint16_t& value)
const;
594 uint16_t readWithRetries(uint16_t addr, uint8_t retries)
const;
598 bool writeRegister(uint16_t addr, uint16_t val, uint8_t retries,
bool verify =
true)
const;
602 bool readOtpRegisters(std::array<uint16_t, kOtpRegisterCount>& regs, uint8_t retries)
const;
603 static constexpr uint32_t kOtpPollLimit = 15000;
609 static constexpr uint16_t kCommandErrorMask =
614 uint8_t pad_byte_{0};
616 mutable std::atomic<uint16_t> sticky_errors_{0};
617 void updateStickyErrors(uint16_t err_fl)
const;
620 template <
typename RegT>
621 static constexpr uint16_t encode(
const RegT& r) {
625 template <
typename RegT>
626 static constexpr RegT decode(uint16_t raw) {
634template <
typename SpiType>
636 : spi_(bus), frame_format_(format) {
640template <
typename SpiType>
643 if (!ReadReg(s2, retries)) {
646 s2.
bits.
DIR = clockwise ? 0 : 1;
647 return WriteReg(s2, retries);
652 return HF_AS5047U_VERSION_STRING;
658#define AS5047U_HEADER_INCLUDED
661#undef AS5047U_HEADER_INCLUDED
AS5047U_Error
Definition as5047u.hpp:21
@ CommandError
Invalid SPI command received.
@ BusError
Driver: the port's transfer() reported a bus error.
@ P2ramWarning
ECC corrected 1 bit in P2RAM customer area.
@ OffCompError
Internal offset compensation not finished.
@ P2ramError
ECC detected 2+ uncorrectable errors in P2RAM.
@ ReplyError
Chip: error bit set in a MISO frame (DS Figures 22, 25, 28)
@ ReplyCrcError
Driver: the CRC of a 24/32-bit MISO frame was wrong.
@ AgcWarning
AGC reached minimum (0) or maximum (255) value.
@ ReplyWarning
Chip: warning bit set in a MISO frame (DS Figures 22, 25, 28)
@ FramingError
SPI framing error.
@ WatchdogError
Internal oscillator or watchdog not working proper.
@ CrcError
CRC error during SPI communication.
@ MagHalf
Magnetic field is half of regulated value (AGC=255)
@ CordicOverflow
CORDIC algorithm overflow.
Template implementation of AS5047U driver methods.
Compile-time configuration options for the AS5047U driver.
Register definitions for the AS5047U Rotary Position Sensor.
CRTP-based SPI bus interface for AS5047U driver.
Type definitions for the AS5047U driver.
FilterPreset
Presets for the adaptive velocity/angle filter (Dynamic Filter System).
Definition as5047u_types.hpp:26
FrameFormat
Supported SPI frame formats for AS5047U communication.
Definition as5047u_types.hpp:12
AS5047U magnetic rotary sensor driver class.
Definition as5047u.hpp:114
float GetVelocityDegPerSec(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Get rotational velocity in degrees per second.
Definition as5047u.ipp:97
bool SetAdaptiveFilter(bool enable, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Enable/disable the adaptive filter (Dynamic Filter System).
Definition as5047u.ipp:240
uint16_t GetRawAngle(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read the 14-bit absolute angle without dynamic compensation (raw angle).
Definition as5047u.ipp:68
void SetPad(uint8_t pad) noexcept
Set the daisy-chain pad byte for 32-bit SPI frames.
Definition as5047u.ipp:495
AS5047U(SpiType &bus, FrameFormat format=AS5047U_CFG::DEFAULT_FRAME_FORMAT) noexcept
Construct a new AS5047U encoder driver.
Definition as5047u.hpp:635
AS5047U_REG::SETTINGS3::Hysteresis GetHysteresis() const
Get current incremental output hysteresis setting.
Definition as5047u.ipp:511
uint16_t GetMagnitude(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read the current magnetic field magnitude (14-bit value).
Definition as5047u.ipp:118
uint16_t GetErrorFlags(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read the ERRFL register (0x0001) from the chip.
Definition as5047u.ipp:124
static constexpr uint8_t GetDriverVersionPatch() noexcept
Get the compiled driver patch version number.
Definition as5047u.hpp:567
bool ReadReg(RegT ®, uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read a register and report whether the read succeeded.
Definition as5047u.hpp:507
static constexpr uint8_t GetDriverVersionMajor() noexcept
Get the compiled driver major version number.
Definition as5047u.hpp:557
static constexpr const char * GetDriverVersion() noexcept
Get the compiled driver version string.
Definition as5047u.hpp:552
bool SetDynamicAngleCompensation(bool enable, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Enable/disable Dynamic Angle Error Compensation (DAEC).
Definition as5047u.ipp:230
AS5047U_REG::SETTINGS2::AngleOutputSource GetAngleOutputSource() const
Get currently selected angle output source for 0x3FFF reads.
Definition as5047u.ipp:528
bool SetFilterPreset(FilterPreset preset, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Set adaptive filter from a preset (easiest way to configure filter).
Definition as5047u.ipp:263
uint16_t GetAngle(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read the 14-bit absolute angle with dynamic compensation (DAEC active).
Definition as5047u.ipp:38
bool ConfigureInterface(bool abi, bool uvw, bool pwm, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Configure interface outputs (ABI, UVW) and PWM output.
Definition as5047u.ipp:205
float GetVelocityRPM(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Get rotational velocity in revolutions per minute.
Definition as5047u.ipp:107
RegT ReadReg() const
Reads data from a specified register in the AS5047U sensor.
Definition as5047u.hpp:492
void SetFrameFormat(FrameFormat format) noexcept
Set the SPI frame format (16, 24, or 32-bit).
Definition as5047u.ipp:21
bool SetABIResolution(uint8_t resolution_bits, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Set the ABI (incremental encoder) resolution.
Definition as5047u.ipp:160
bool VerifyOTP(const std::array< uint16_t, kOtpRegisterCount > &burned, uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Steps 21 and 22 of the OTP procedure: after the power-on cycle (step 20), read DISABLE....
Definition as5047u.ipp:452
bool SetDirection(bool clockwise, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Define the rotation direction for increasing angle.
Definition as5047u.hpp:641
float GetAngleDegrees(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read absolute angle in degrees.
Definition as5047u.ipp:58
bool SetUVWPolePairs(uint8_t pairs, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Set the number of pole pairs for UVW commutation outputs.
Definition as5047u.ipp:173
uint16_t GetZeroPosition(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Get the currently configured soft zero position offset (14-bit).
Definition as5047u.ipp:140
bool WriteReg(const RegT ®, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Writes data to a specified register in the AS5047U sensor.
Definition as5047u.hpp:523
static constexpr uint8_t GetDriverVersionMinor() noexcept
Get the compiled driver minor version number.
Definition as5047u.hpp:562
bool Set150CTemperatureMode(bool enable, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Set temperature mode for 150°C operation (NOISESET bit).
Definition as5047u.ipp:303
bool SetHysteresis(AS5047U_REG::SETTINGS3::Hysteresis hysteresis, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Set incremental output hysteresis level.
Definition as5047u.ipp:500
static constexpr uint8_t ComputeCRC8(uint16_t data16)
Definition as5047u.hpp:131
std::pair< uint8_t, uint8_t > GetFilterParameters(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read current K_min and K_max register codes (0–7 each).
Definition as5047u.ipp:296
bool GetAdaptiveFilterEnabled(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read whether the adaptive filter is enabled.
Definition as5047u.ipp:289
void DumpStatus() const
Dump formatted status and diagnostics using printf.
Definition as5047u.ipp:698
bool SetZeroPosition(uint16_t angle_lsb, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Set a new zero reference position (soft offset).
Definition as5047u.ipp:151
bool SetAngleOutputSource(AS5047U_REG::SETTINGS2::AngleOutputSource source, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Select which angle register (0x3FFF) is returned on reads.
Definition as5047u.ipp:517
AS5047U_Error ClearStickyErrorFlags() noexcept
Clear the sticky error flags.
Definition as5047u.ipp:489
bool SetFilterParameters(uint8_t k_min, uint8_t k_max, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Set adaptive filter parameters (K_min and K_max as 3-bit register codes).
Definition as5047u.ipp:250
bool ProgramOTP(OtpResult *result=nullptr)
Permanently program the current settings into OTP memory.
Definition as5047u.ipp:313
int16_t GetVelocity(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read the current rotational velocity (signed 14-bit).
Definition as5047u.ipp:74
AS5047U_Error GetStickyErrorFlags() const noexcept
Error flags accumulated since the last ClearStickyErrorFlags().
Definition as5047u.ipp:484
AS5047U_REG::DIA GetDiagnostics() const
Read the full diagnostic register (DIA).
Definition as5047u.ipp:534
float GetAngleRadians(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read absolute angle in radians.
Definition as5047u.ipp:63
bool SetIndexPulseLength(uint8_t lsb_len, uint8_t retries=AS5047U_CFG::CRC_RETRIES)
Set the index pulse width for ABI output.
Definition as5047u.ipp:184
float GetVelocityRadPerSec(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Get rotational velocity in radians per second.
Definition as5047u.ipp:102
uint8_t GetAGC(uint8_t retries=AS5047U_CFG::CRC_RETRIES) const
Read the current Automatic Gain Control (AGC) value (0-255).
Definition as5047u.ipp:112
constexpr FrameFormat DEFAULT_FRAME_FORMAT
Definition as5047u_config.hpp:20
constexpr uint8_t CRC_RETRIES
Definition as5047u_config.hpp:26
Definition as5047u.hpp:52
VelocityUnit
Units for velocity-returning APIs.
Definition as5047u.hpp:89
constexpr float kPi
Definition as5047u.hpp:79
constexpr std::size_t kOtpRegisterCount
Number of non-volatile registers burned into OTP: DISABLE (0x0015) to ECC (0x001B).
Definition as5047u.hpp:55
const char * GetDriverVersion() noexcept
Definition as5047u.hpp:651
constexpr uint16_t kOtpFirstAddress
First non-volatile register address (DISABLE).
Definition as5047u.hpp:57
AngleUnit
Units for angle-returning APIs.
Definition as5047u.hpp:82
DIA – Diagnostic register (0x3FF5, read-only)
Definition as5047u_registers.hpp:180
SETTINGS2 – Custom setting register 2 (0x0019, default 0x0000)
Definition as5047u_registers.hpp:597
AngleOutputSource
Angle data source for 0x3FFF output (Data_select bit)
Definition as5047u_registers.hpp:644
uint16_t DIR
Definition as5047u_registers.hpp:604
struct AS5047U_REG::SETTINGS2::@48::@50 bits
Hysteresis
Incremental output hysteresis (HYS) settings:
Definition as5047u_registers.hpp:714
Angle conversion helpers.
Definition as5047u.hpp:204
static constexpr uint16_t RadiansToLsb(float radians) noexcept
Definition as5047u.hpp:212
static constexpr float RAD_PER_LSB
Definition as5047u.hpp:206
static constexpr uint16_t DegreesToLsb(float degrees) noexcept
Definition as5047u.hpp:208
static constexpr float DEG_PER_LSB
Definition as5047u.hpp:205
Helper constants and methods for velocity unit conversions.
Definition as5047u.hpp:218
static constexpr float RPM_PER_LSB
Definition as5047u.hpp:221
static constexpr float DEG_PER_LSB
Definition as5047u.hpp:219
static constexpr float RAD_PER_LSB
Definition as5047u.hpp:220
Outcome of AS5047U::ProgramOTP().
Definition as5047u.hpp:65
std::array< uint16_t, kOtpRegisterCount > written
Definition as5047u.hpp:73
bool burn_started
Definition as5047u.hpp:70
uint8_t failed_step
Datasheet step that failed (4 to 19), or 0 when every step succeeded.
Definition as5047u.hpp:67