12#ifndef AS5047U_TYPES_HPP
13#define AS5047U_TYPES_HPP
20#include "as5047u_version.h"
37enum class [[nodiscard]]
Error : uint8_t {
82 return "NotInitialized";
84 return "InvalidArgument";
90 return "DeviceIdMismatch";
104 return "WriteMismatch";
112 return "MagnetWarning";
114 return "DeviceReset";
134 [[nodiscard]]
constexpr bool ok() const noexcept {
135 return error == Error::Ok;
138 constexpr explicit operator bool() const noexcept {
148 return {v, Error::Ok};
166 [[nodiscard]]
constexpr bool ok() const noexcept {
167 return error == Error::Ok;
170 constexpr explicit operator bool() const noexcept {
332 const Config& rhs)
noexcept =
default;
343inline constexpr float kPi = 3.14159265358979323846F;
423 if (!detail::IsEnumerator(config.frame) || !detail::IsEnumerator(config.direction) ||
424 !detail::IsEnumerator(config.incremental) || !detail::IsEnumerator(config.index_width)) {
445 return static_cast<float>(
static_cast<uint32_t
>(counts) &
kMaxAngleCount) * (360.0F / 16384.0F);
454 return static_cast<float>(
static_cast<uint32_t
>(counts) &
kMaxAngleCount) *
455 (2.0F *
kPi / 16384.0F);
460 return AS5047U_VERSION_STRING;
464 return AS5047U_VERSION_MAJOR;
468 return AS5047U_VERSION_MINOR;
472 return AS5047U_VERSION_PATCH;
Compile-time helpers of the AS5047U driver: exact hook matching, the dependent-false helper,...
AS5047U register map: addresses, field masks and shifts.
constexpr float kPi
Pi, as a float (C++20's std::numbers is not on every embedded toolchain).
Definition as5047u_types.hpp:343
LogLevel
Severity passed to the optional Log() hook.
Definition as5047u_types.hpp:189
constexpr uint8_t kMax3BitCode
The highest field code for 3-bit fields (filter_k_*_code, abi_resolution_code).
Definition as5047u_types.hpp:357
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition as5047u_types.hpp:37
constexpr uint16_t kCountsPerRevolution
Angle counts per revolution (14 bits).
Definition as5047u_types.hpp:336
constexpr uint8_t kMaxUvwPolePairs
Highest UVW pole-pair count.
Definition as5047u_types.hpp:361
constexpr uint32_t GetDriverVersionPatch() noexcept
Definition as5047u_types.hpp:471
std::array< uint16_t, reg::kNonVolatileCount > NonVolatileRegisters
The non-volatile registers DISABLE to ECC, in address order; index with reg::At(reg::NvIndex).
Definition as5047u_types.hpp:399
constexpr uint8_t kMaxHysteresisCode
The highest hysteresis code (2-bit field).
Definition as5047u_types.hpp:359
constexpr uint32_t kPowerOnTimeMs
Definition as5047u_types.hpp:276
constexpr float kVelocityDegPerSecPerLsb
Velocity resolution in degrees per second per LSB (datasheet, VEL register).
Definition as5047u_types.hpp:340
constexpr uint32_t kStartupTimeoutMaxMs
Largest Config::startup_timeout_ms that Validate() accepts.
Definition as5047u_types.hpp:278
constexpr float CountsToRadians(uint16_t counts) noexcept
Convert angle counts to radians.
Definition as5047u_types.hpp:453
constexpr const char * ToString(Error error) noexcept
The name of an error, for logs.
Definition as5047u_types.hpp:77
constexpr const char * GetDriverVersion() noexcept
Definition as5047u_types.hpp:459
constexpr uint32_t kOtpBurnTimeoutMs
Definition as5047u_types.hpp:346
IndexWidth
Length of the ABI index pulse (SETTINGS2.IWIDTH).
Definition as5047u_types.hpp:230
constexpr Error Validate(const Config &config) noexcept
Check a configuration without touching the chip.
Definition as5047u_types.hpp:422
Direction
Direction in which the angle increases (SETTINGS2.DIR).
Definition as5047u_types.hpp:218
constexpr OtpBurnConfirm kOtpConfirm
The confirmation to pass to ProgramOtp().
Definition as5047u_types.hpp:355
IncrementalOutput
Which incremental interface the A/B/I-U/V/W pins carry (SETTINGS2.UVW_ABI).
Definition as5047u_types.hpp:224
Verify
Whether a run-time write reads the register back, chosen per call.
Definition as5047u_types.hpp:261
FrameFormat
SPI frame length. The chip recognises the length of each frame, so the driver may mix them.
Definition as5047u_types.hpp:203
constexpr uint32_t GetDriverVersionMinor() noexcept
Definition as5047u_types.hpp:467
constexpr float CountsToDegrees(uint16_t counts) noexcept
Convert angle counts to degrees.
Definition as5047u_types.hpp:444
constexpr uint16_t kMaxAngleCount
Highest angle count.
Definition as5047u_types.hpp:338
constexpr uint32_t GetDriverVersionMajor() noexcept
Definition as5047u_types.hpp:463
@ Warning
Something unexpected that the driver recovered from.
@ Info
Lifecycle events: initialised, OTP burned.
@ Debug
Detail for bring-up.
@ InvalidArgument
An argument or a Config field is out of range; nothing was sent.
@ Framing
The chip saw an SPI framing error (ERRFL.Framing_error).
@ Crc
A reply's CRC was wrong, or the chip saw a wrong CRC (ERRFL.CRC_error).
@ Bus
A port hook returned false.
@ NotInitialized
EnsureInitialized() has not succeeded yet, or Deinitialize() ran since.
@ Unsupported
Not returned by this driver.
@ Timeout
The OTP burn did not finish in kOtpBurnTimeoutMs.
@ Busy
Not returned by this driver (the chip has no busy state).
@ Command
The chip saw an invalid command (ERRFL.Command_error).
@ WriteMismatch
A register read back different from what was written.
@ ThreeLsb
3 LSB. Reset value.
@ CounterClockwise
Inverted (DIR = 1).
@ Clockwise
The datasheet's default direction, seen from above the magnet.
@ Abi
A, B and index pulses; PWM, when on, goes on pin W. Reset value.
@ Uvw
U, V and W commutation signals; PWM, when on, goes on pin I.
@ None
Command and data frames only. The default: nothing extra on a timed path.
@ ReadBack
Read each written register back and compare.
@ Bits16
16-bit frames, no CRC: the fastest reads, but a corrupted reply goes unnoticed.
@ Bits32
32-bit frames: a pad byte, then a 24-bit frame (daisy chains).
@ Bits24
24-bit frames with CRC-8 in both directions. Default.
The AS5047U driver: everything it declares lives here; internals in detail.
Definition as5047u.hpp:24
Everything EnsureInitialized() and ApplyConfig() write to the chip.
Definition as5047u_types.hpp:287
Direction direction
SETTINGS2.DIR.
Definition as5047u_types.hpp:305
uint8_t pad_byte
Driver side: the pad byte of 32-bit frames (the daisy-chain address).
Definition as5047u_types.hpp:294
bool high_temperature_noise
Definition as5047u_types.hpp:320
uint16_t zero_position
ZPOSM/ZPOSL, counts 0 to 16383.
Definition as5047u_types.hpp:306
bool uvw_enable
U/V/W pins driven (DISABLE.UVW_off = 0).
Definition as5047u_types.hpp:313
bool write_settings
Definition as5047u_types.hpp:298
bool pwm_enable
SETTINGS2.PWMon.
Definition as5047u_types.hpp:314
uint8_t filter_k_max_code
SETTINGS1.K_max code, 0 to 7.
Definition as5047u_types.hpp:310
bool uncompensated_at_angle_register
SETTINGS2.Data_select: address 0x3FFF (and the PWM output) carry the angle without DAEC.
Definition as5047u_types.hpp:322
bool abi_decimal
SETTINGS2.ABI_DEC.
Definition as5047u_types.hpp:316
uint8_t uvw_pole_pairs
SETTINGS3.UVWPP + 1, 1 to 7.
Definition as5047u_types.hpp:318
uint8_t filter_k_min_code
SETTINGS1.K_min code, 0 to 7.
Definition as5047u_types.hpp:309
uint8_t hysteresis_code
SETTINGS3.HYS, 0 to 3.
Definition as5047u_types.hpp:319
IndexWidth index_width
SETTINGS2.IWIDTH.
Definition as5047u_types.hpp:317
bool abi_enable
A/B/I pins driven (DISABLE.ABI_off = 0).
Definition as5047u_types.hpp:312
bool adaptive_filter
Velocity filter on (DISABLE bit 6 = 0).
Definition as5047u_types.hpp:308
friend constexpr bool operator==(const Config &lhs, const Config &rhs) noexcept=default
Field by field; every member is an integer, a bool or an enumeration, so equality is exact (KB-CPP20-...
IncrementalOutput incremental
SETTINGS2.UVW_ABI.
Definition as5047u_types.hpp:311
uint32_t startup_timeout_ms
Definition as5047u_types.hpp:303
bool dynamic_compensation
DAEC on (SETTINGS2.DAECDIS = 0).
Definition as5047u_types.hpp:307
FrameFormat frame
Definition as5047u_types.hpp:292
uint8_t abi_resolution_code
SETTINGS3.ABIRES, 0 to 7.
Definition as5047u_types.hpp:315
DIA, decoded: the state of the chip's internal loops.
Definition as5047u_types.hpp:385
bool cordic_overflow
CORDIC overflow.
Definition as5047u_types.hpp:388
uint16_t raw
The register value.
Definition as5047u_types.hpp:395
uint8_t spi_frame_count
SPI_cnt: frames since the last read of DIA, modulo 4.
Definition as5047u_types.hpp:394
bool agc_high
AGC at its high limit (field too weak).
Definition as5047u_types.hpp:390
bool mag_half
Field at half the regulated value.
Definition as5047u_types.hpp:391
bool agc_low
AGC at its low limit (field too strong).
Definition as5047u_types.hpp:389
bool offset_comp_done
Offset compensation finished.
Definition as5047u_types.hpp:392
bool loops_finished
Every magnetic loop has settled.
Definition as5047u_types.hpp:387
bool vdd_5v
VDD_mode: the chip runs from 5 V (else 3.3 V).
Definition as5047u_types.hpp:386
bool agc_done
AGC loop finished.
Definition as5047u_types.hpp:393
ERRFL, decoded. Reading ERRFL clears it on the chip.
Definition as5047u_types.hpp:364
bool command
Invalid SPI command.
Definition as5047u_types.hpp:370
bool crc
Wrong CRC on a MOSI frame.
Definition as5047u_types.hpp:371
bool framing
SPI framing error.
Definition as5047u_types.hpp:369
bool mag_half
Field at half the regulated value.
Definition as5047u_types.hpp:366
uint16_t raw
The register value.
Definition as5047u_types.hpp:381
bool offset_comp
Offset compensation not finished.
Definition as5047u_types.hpp:373
bool p2ram_error
ECC found an uncorrectable P2RAM error.
Definition as5047u_types.hpp:368
bool flags_lost
Not an ERRFL bit: in GetLatchedErrorFlags() only, true when a read of ERRFL failed after its command ...
Definition as5047u_types.hpp:380
bool p2ram_warning
ECC corrected one bit in the P2RAM.
Definition as5047u_types.hpp:367
bool watchdog
Internal oscillator or watchdog fault.
Definition as5047u_types.hpp:372
bool agc_warning
AGC at its minimum or maximum: magnet too far or too close.
Definition as5047u_types.hpp:365
bool cordic_overflow
CORDIC overflow.
Definition as5047u_types.hpp:374
The typed confirmation ProgramOtp() requires: burning the OTP cannot be undone, so the call names its...
Definition as5047u_types.hpp:351
uint32_t code
Must be kOtpConfirm.code.
Definition as5047u_types.hpp:352
Outcome of AS5047U::ProgramOtp().
Definition as5047u_types.hpp:402
NonVolatileRegisters written
DISABLE to ECC as verified before the burn: what was burned.
Definition as5047u_types.hpp:410
bool burn_started
Definition as5047u_types.hpp:408
uint8_t failed_step
Datasheet step that failed (6 to 19), or 0 when every step succeeded.
Definition as5047u_types.hpp:404
constexpr bool ok() const noexcept
Definition as5047u_types.hpp:166
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition as5047u_types.hpp:183
static constexpr Result Success() noexcept
Definition as5047u_types.hpp:175
The value of a call, or why there is none.
Definition as5047u_types.hpp:129
static constexpr Result Success(T v) noexcept
A successful result.
Definition as5047u_types.hpp:147
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition as5047u_types.hpp:155
constexpr bool ok() const noexcept
Definition as5047u_types.hpp:134