rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
as5600.c File Reference

The AS5600 codec and driver. More...

#include "as5600.h"

Functions

uint16_t as5600_u12 (const uint8_t *b)
 A 12-bit register pair, most significant byte first: RAW ANGLE, ANGLE or MAGNITUDE.
bool as5600_status_possible (uint8_t status)
 Whether status is one an AS5600 can give: not ML and MH together.
bool as5600_md (uint8_t status)
 The magnet bits of status.
bool as5600_ml (uint8_t status)
 Read the ML bit of a STATUS value.
bool as5600_mh (uint8_t status)
 Read the MH bit of a STATUS value.
int16_t as5600_delta (uint16_t a, uint16_t b)
 a minus b on the circle, -2048 to 2047 counts: the shortest way from b to a.
int32_t as5600_cdeg (int32_t counts)
 counts in hundredths of a degree, rounded to the nearest, halves away from zero.
void as5600_init (as5600_t *d, sense_bus_t *bus)
 A part on bus at AS5600_ADDR, not yet probed.
static sense_state_t probe (as5600_t *d)
 Probe the part: read STATUS and classify the answer.
bool as5600_step (as5600_t *d, uint32_t now_ms)
 Every tick: probes the part when it is due – at the first call, and SENSE_RETRY_MS after it was found absent, wrong or offline.
sense_state_t as5600_state (const as5600_t *d)
 The part's state as of the last probe.
sense_err_t as5600_read_status (as5600_t *d, uint8_t *status)
 STATUS: 1 byte from 0x0B.
sense_err_t as5600_read_raw (as5600_t *d, uint16_t *raw)
 RAW ANGLE: 2 bytes from 0x0C, 0 to 4095; the same rule.
sense_err_t as5600_read_agc (as5600_t *d, uint8_t *agc)
 AGC: 1 byte from 0x1A; the same rule.
sense_err_t as5600_read_mag (as5600_t *d, uint16_t *magnitude)
 MAGNITUDE: 2 bytes from 0x1B, 0 to 4095; the same rule.
sense_err_t as5600_read_angle (as5600_t *d, uint8_t *status, uint16_t *raw)
 STATUS, then RAW ANGLE: two transactions.
sense_err_t as5600_read_magnitude (as5600_t *d, uint8_t *agc, uint16_t *magnitude)
 AGC, then MAGNITUDE: two transactions; the same rule.

Detailed Description

The AS5600 codec and driver.

See as5600.h.

SPDX-License-Identifier: MIT

Function Documentation

◆ as5600_cdeg()

int32_t as5600_cdeg ( int32_t counts)

counts in hundredths of a degree, rounded to the nearest, halves away from zero.

Any int32_t is accepted. Above |counts| 244,335,917 the result saturates at +-2,147,483,647.

Parameters
countsAngle in counts; 4096 counts make a turn.
Returns
The angle in hundredths of a degree.

◆ as5600_delta()

int16_t as5600_delta ( uint16_t a,
uint16_t b )

a minus b on the circle, -2048 to 2047 counts: the shortest way from b to a.

Both are taken modulo 4096.

Parameters
aAn angle, in counts.
bAn angle, in counts.
Returns
a - b in counts, -2048 to 2047.

◆ as5600_init()

void as5600_init ( as5600_t * d,
sense_bus_t * bus )

A part on bus at AS5600_ADDR, not yet probed.

Parameters
dThe driver.
busThe sensor bus the part sits on.

◆ as5600_md()

bool as5600_md ( uint8_t status)

The magnet bits of status.

RAW ANGLE is a position only with MD.

Parameters
statusThe STATUS byte.
Returns
true when MD (magnet detected) is set.

◆ as5600_mh()

bool as5600_mh ( uint8_t status)

Read the MH bit of a STATUS value.

Parameters
statusThe STATUS byte.
Returns
true when MH (magnet too strong) is set.

◆ as5600_ml()

bool as5600_ml ( uint8_t status)

Read the ML bit of a STATUS value.

Parameters
statusThe STATUS byte.
Returns
true when ML (magnet too weak) is set.

◆ as5600_read_agc()

sense_err_t as5600_read_agc ( as5600_t * d,
uint8_t * agc )

AGC: 1 byte from 0x1A; the same rule.

Parameters
dThe driver.
agcReceives the AGC byte.
Returns
SENSE_OK, or the bus error.

◆ as5600_read_angle()

sense_err_t as5600_read_angle ( as5600_t * d,
uint8_t * status,
uint16_t * raw )

STATUS, then RAW ANGLE: two transactions.

On anything but SENSE_OK both are left as they were.

Parameters
dThe driver.
statusReceives the STATUS byte.
rawReceives the angle, 0 to 4095.
Returns
SENSE_OK, or the bus error.

◆ as5600_read_mag()

sense_err_t as5600_read_mag ( as5600_t * d,
uint16_t * magnitude )

MAGNITUDE: 2 bytes from 0x1B, 0 to 4095; the same rule.

Parameters
dThe driver.
magnitudeReceives the magnitude, 0 to 4095.
Returns
SENSE_OK, or the bus error.

◆ as5600_read_magnitude()

sense_err_t as5600_read_magnitude ( as5600_t * d,
uint8_t * agc,
uint16_t * magnitude )

AGC, then MAGNITUDE: two transactions; the same rule.

Parameters
dThe driver.
agcReceives the AGC byte.
magnitudeReceives the magnitude, 0 to 4095.
Returns
SENSE_OK, or the bus error.

◆ as5600_read_raw()

sense_err_t as5600_read_raw ( as5600_t * d,
uint16_t * raw )

RAW ANGLE: 2 bytes from 0x0C, 0 to 4095; the same rule.

Parameters
dThe driver.
rawReceives the angle, 0 to 4095.
Returns
SENSE_OK, or the bus error.

◆ as5600_read_status()

sense_err_t as5600_read_status ( as5600_t * d,
uint8_t * status )

STATUS: 1 byte from 0x0B.

On anything but SENSE_OK status is left as it was.

Parameters
dThe driver.
statusReceives the STATUS byte.
Returns
SENSE_OK, or the bus error.

◆ as5600_state()

sense_state_t as5600_state ( const as5600_t * d)

The part's state as of the last probe.

Parameters
dThe driver.
Returns
The sense_state_t of the part.

◆ as5600_status_possible()

bool as5600_status_possible ( uint8_t status)

Whether status is one an AS5600 can give: not ML and MH together.

Bits 7, 6, 2, 1 and 0 are not looked at.

Parameters
statusThe STATUS byte.
Returns
false when ML and MH are both set, else true.

◆ as5600_step()

bool as5600_step ( as5600_t * d,
uint32_t now_ms )

Every tick: probes the part when it is due – at the first call, and SENSE_RETRY_MS after it was found absent, wrong or offline.

Returns whether it is online.

Parameters
dThe driver.
now_msThe current time, in ms.
Returns
true when the part is online.

◆ as5600_u12()

uint16_t as5600_u12 ( const uint8_t * b)

A 12-bit register pair, most significant byte first: RAW ANGLE, ANGLE or MAGNITUDE.

0 to 4095.

Parameters
bThe two bytes, high byte first.
Returns
The 12-bit value, 0 to 4095.

◆ probe()

sense_state_t probe ( as5600_t * d)
static

Probe the part: read STATUS and classify the answer.

Parameters
dThe driver.
Returns
SENSE_PART_ABSENT when the read fails, SENSE_PART_WRONG_ID when ML and MH are both set, else SENSE_PART_ONLINE.