as5048a-async 0.1.0

Async driver for AS5048A 14-bit magnetic rotary position sensor with SPI interface
Documentation

as5048a-async

Crates.io Documentation [License]

Async Rust driver for the AS5048A 14-bit magnetic rotary position sensor using the SPI interface.

This driver is designed for embedded systems using embedded-hal-async and works seamlessly with async runtimes like Embassy.

  • Fully async using embedded-hal-async traits
  • 14-bit angular position reading (0.022° resolution)
  • Optional defmt logging for debugging
  • no_std compatible
  • forbid(unsafe_code) - 100% safe Rust

AS5048A Requirements

The AS5048A is a 14-bit rotary position sensor with SPI interface:

  • Resolution: 14-bit (16384 positions, 0.022°/LSB)
  • SPI Mode: Mode 1 (CPOL=0, CPHA=1)
  • Max Clock: 10 MHz (recommend 1-3 MHz for initial testing)
  • Supply: 3.3V or 5V

For more details read the full AS5048A Datasheet.

Installation

Add to your Cargo.toml:

[dependencies]
as5048a-async = "0.1"
embedded-hal-async = "1.0"

Usage

Quick Start

use as5048a_async::As5048a;

// Create sensor with an SpiDevice (bus + CS pin)
let mut sensor = As5048a::new(spi_device);

// Read angle (0-16383, representing 0-360°)
let angle = sensor.angle().await?;
let degrees = (angle as f32) * 360.0 / 16384.0;

For complete examples see the examples folder.

Diagnostics and Error Handling

// Check magnetic field strength and sensor status
let diag = sensor.diagnostics().await?;
if diag.is_valid() {
    // Read AGC value, check COMP flags, etc.
}

// Clear error flags
sensor.clear_error_flag().await?;

API Reference

See the API documentation for complete details on available methods and types.

Main methods:

  • angle() - Read 14-bit angle (0-16383)
  • angle_degrees() - Read angle in degrees (0-359) using integer math
  • magnitude() - Read CORDIC magnitude
  • diagnostics() - Read diagnostics with AGC value and status flags
  • clear_error_flag() - Clear error flag

Constants:

  • ANGLE_MAX - Maximum angle value (16384) for custom conversions

MSRV (Minimum Supported Rust Version)

This crate requires Rust 1.87 or later.

Resources

Inspired by

Contributing

Contributions are welcome! Please run cargo test and cargo clippy before submitting.