as5048a_async/lib.rs
1//! Asynchronous driver for AS5048A magnetic position sensor
2//!
3//! This driver implements an async interface for the AS5048A 14-bit magnetic rotary
4//! position sensor using the SPI interface. It is compatible with embedded-hal-async
5//! and works with embassy and other async embedded frameworks
6//!
7//! # Features
8//!
9//! - Async SPI communication using `embedded-hal-async`
10//! - Read 14-bit angular position
11//! - Read magnitude and diagnostics
12//! - Automatic parity checking
13//! - Error flag handling
14//!
15//! # Example
16//!
17//! ```no_run
18//! use as5048a_async::As5048a;
19//! use embedded_hal_async::spi::SpiDevice;
20//!
21//! async fn read_angle<SPI, E>(spi: SPI) -> Result<(), as5048a_async::Error<E>>
22//! where
23//! SPI: SpiDevice<u8, Error = E>,
24//! {
25//! let mut sensor = As5048a::new(spi);
26//!
27//! // Read angle in degrees (0-359)
28//! match sensor.angle_degrees().await {
29//! Ok(degrees) => {
30//! // degrees is 0-359
31//! }
32//! Err(e) => {
33//! // Handle error
34//! }
35//! }
36//!
37//! // Or read raw 14-bit value (0-16383)
38//! let raw_angle = sensor.angle().await?;
39//! Ok(())
40//! }
41//! ```
42//!
43//! # Important Timing Requirements
44//!
45//! The AS5048A requires a minimum CS (Chip Select) high time of **350ns** between
46//! SPI transactions. Ensure your `SpiDevice` implementation provides adequate
47//! inter-frame delay to meet this requirement. The maximum SPI clock frequency is 10 MHz
48//!
49//! # Error Handling
50//!
51//! When `Error::SensorError` occurs, the sensor's internal error flag is set
52//! To recover:
53//! 1. Call `clear_error_flag()` to reset the error state
54//! 2. Retry the operation
55//!
56//! Example:
57//! ```no_run
58//! # use as5048a_async::{As5048a, Error};
59//! # async fn example<SPI, E>(mut sensor: As5048a<SPI>) -> Result<u16, Error<E>>
60//! # where SPI: embedded_hal_async::spi::SpiDevice<u8, Error = E>
61//! # {
62//! match sensor.angle().await {
63//! Err(Error::SensorError) => {
64//! sensor.clear_error_flag().await?;
65//! sensor.angle().await // Retry
66//! }
67//! result => result
68//! }
69//! # }
70//! ```
71
72#![no_std]
73#![deny(missing_docs)]
74#![forbid(unsafe_code)]
75#![warn(clippy::pedantic)]
76
77mod diagnostics;
78mod driver;
79mod error;
80mod register;
81mod utils;
82
83pub use diagnostics::Diagnostics;
84pub use driver::{As5048a, ANGLE_MAX};
85pub use error::Error;
86pub use register::Register;