Crate as5048a_async

Crate as5048a_async 

Source
Expand description

Asynchronous driver for AS5048A magnetic position sensor

This driver implements an async interface for the AS5048A 14-bit magnetic rotary position sensor using the SPI interface. It is compatible with embedded-hal-async and works with embassy and other async embedded frameworks

§Features

  • Async SPI communication using embedded-hal-async
  • Read 14-bit angular position
  • Read magnitude and diagnostics
  • Automatic parity checking
  • Error flag handling

§Example

use as5048a_async::As5048a;
use embedded_hal_async::spi::SpiDevice;

async fn read_angle<SPI, E>(spi: SPI) -> Result<(), as5048a_async::Error<E>>
where
    SPI: SpiDevice<u8, Error = E>,
{
    let mut sensor = As5048a::new(spi);

    // Read angle in degrees (0-359)
    match sensor.angle_degrees().await {
        Ok(degrees) => {
            // degrees is 0-359
        }
        Err(e) => {
            // Handle error
        }
    }

    // Or read raw 14-bit value (0-16383)
    let raw_angle = sensor.angle().await?;
    Ok(())
}

§Important Timing Requirements

The AS5048A requires a minimum CS (Chip Select) high time of 350ns between SPI transactions. Ensure your SpiDevice implementation provides adequate inter-frame delay to meet this requirement. The maximum SPI clock frequency is 10 MHz

§Error Handling

When Error::SensorError occurs, the sensor’s internal error flag is set To recover:

  1. Call clear_error_flag() to reset the error state
  2. Retry the operation

Example:

match sensor.angle().await {
    Err(Error::SensorError) => {
        sensor.clear_error_flag().await?;
        sensor.angle().await // Retry
    }
    result => result
}

Structs§

As5048a
AS5048A driver instance (asynchronous)
Diagnostics
Diagnostics flags from the DIAG_AGC register (0x3FFD)

Enums§

Error
Error type for AS5048A operations
Register
Register addresses for AS5048A

Constants§

ANGLE_MAX
Maximum angle value (14-bit: 0-16383, representing 0-360°)