![]() |
no-OS
|
Implementation of ADMT4000 Driver. More...
#include <errno.h>#include <stdlib.h>#include <stdint.h>#include <stdbool.h>#include "admt4000.h"#include "no_os_alloc.h"#include "no_os_delay.h"Functions | |
| int | admt4000_reinitialize (struct admt4000_dev *device) |
| Reinitialize device using stored configuration. | |
| int | admt4000_hard_reset (struct admt4000_dev *device) |
| Perform hardware reset via reset pin. | |
| int | admt4000_init (struct admt4000_dev **device, struct admt4000_init_param *init_param) |
| Initializes the admt4000. | |
| int | admt4000_remove (struct admt4000_dev *device) |
| Free the resources allocated by admt4000_init(). | |
| int | admt4000_reg_read (struct admt4000_dev *device, uint8_t reg_addr, uint16_t *reg_data, uint8_t *verif) |
| Reads data from a certain ADMT400x Register. | |
| int | admt4000_reg_write (struct admt4000_dev *device, uint8_t reg_addr, uint16_t reg_data) |
| Writes data to a certain ADMT400x Register. | |
| int | admt4000_reg_update (struct admt4000_dev *device, uint8_t reg_addr, uint16_t update_mask, uint16_t update_val) |
| Updates the contents of selected register. | |
| int | admt4000_toggle_cnv (struct admt4000_dev *device) |
| Toggle the CNV to stop/start conversion. | |
| int | admt4000_raw_angle_read (struct admt4000_dev *device, uint16_t *angle_data) |
| Reads ANGLE and ABSANGLE register contents in 1 CS frame. | |
| int | admt4000_get_raw_turns_and_angle (struct admt4000_dev *device, uint8_t *turns, uint16_t *angle) |
| Get the number of turns and angle information (Raw). | |
| int | admt4000_quarter_turns_cnt (uint8_t raw_turns, int16_t *quarter_turns) |
| Convert raw quarter turn count to equivalent signed value. | |
| int | admt4000_get_cos (struct admt4000_dev *device, int16_t *val, bool *is_new_data) |
| Get the cosine value from the ADMT4000 device. This function reads the cosine portion of the AMR angle sensor without post- processing. | |
| int | admt4000_get_sin (struct admt4000_dev *device, int16_t *val, bool *is_new_data) |
| Retrieve the sine value from the ADMT4000 device. This function reads the sine portion of the AMR angle sensor without post- processing. | |
| int | admt4000_get_gpio (struct admt4000_dev *device, uint8_t gpio, bool *logic) |
| Get status of selected GPIO. | |
| int | admt4000_set_gpio (struct admt4000_dev *device, uint8_t gpio, bool logic) |
| Configure selected GPIO. | |
| int | admt4000_clear_all_faults (struct admt4000_dev *device) |
| Clear all faults from fault registers. | |
| int | admt4000_get_faults (struct admt4000_dev *device, uint16_t *faults) |
| Get all fault flags from the fault register. | |
| int | admt4000_get_radius (struct admt4000_dev *device, uint16_t *radius, bool *is_new_data) |
| Get RADIUS vector value. | |
| int | admt4000_radius_raw_to_scaled (uint16_t raw_radius, float *radius) |
| Convert raw RADIUS value to scaled mV/V. | |
| int | admt4000_get_temp (struct admt4000_dev *device, uint16_t *temp) |
| Get raw temperature reading. | |
| int | admt4000_temp_raw_to_scaled (uint16_t temp_raw, float *temp) |
| Convert raw temperature value to scaled temperature (degrees Celsius). | |
| int | admt4000_angle_raw_to_scaled (uint16_t raw_angle, float *scaled_angle) |
| Convert raw angle value to scaled degrees. | |
| int | admt4000_harmonic_raw_to_scaled (const struct admt4000_harmonic_coeff *raw_coeff, float *scaled_mag, float *scaled_phase) |
| Convert raw harmonic coefficients to scaled format. | |
| int | admt4000_harmonic_scaled_to_raw (uint32_t scaled_mag, uint32_t scaled_phase, struct admt4000_harmonic_coeff *raw_coeff) |
| Convert scaled harmonic values to raw coefficients. | |
| int | admt4000_get_storage_config (struct admt4000_dev *device, uint8_t *storage) |
| Gets storage general config. | |
| int | admt4000_set_storage_config (struct admt4000_dev *device, uint8_t storage) |
| Sets storage general config. | |
| int | admt4000_get_angle_filt (struct admt4000_dev *device, bool *is_filtered) |
| Gets current filter setting. | |
| int | admt4000_set_angle_filt (struct admt4000_dev *device, bool is_filtered) |
| Sets the filter setting. | |
| int | admt4000_get_h8_ctrl (struct admt4000_dev *device, enum admt4000_harmonic_corr_src *source) |
| Gets 8th harmonic correction source. | |
| int | admt4000_set_h8_ctrl (struct admt4000_dev *device, enum admt4000_harmonic_corr_src source) |
| Sets 8th harmonic correction source. | |
| int | admt4000_get_conv_sync_mode (struct admt4000_dev *device, enum admt4000_conv_sync_mode *mode) |
| Obtains conversion synchronization mode. | |
| int | admt4000_set_conv_sync_mode (struct admt4000_dev *device, enum admt4000_conv_sync_mode mode) |
| Configures conversion synchronization mode. | |
| int | admt4000_get_conv_mode (struct admt4000_dev *device, bool *is_one_shot) |
| Obtains the conversion mode. | |
| int | admt4000_set_conv_mode (struct admt4000_dev *device, bool is_one_shot) |
| Configures the conversion mode: one shot or continuous. | |
| int | admt4000_gpio_config (struct admt4000_dev *device, uint8_t gpio, bool alt_mode, bool output_en, bool init_state) |
| Configure GPIO mode, direction, and initial state. | |
| int | admt4000_get_gpio_func (struct admt4000_dev *device, uint8_t gpio, bool *alt_mode) |
| Get current GPIO function configuration. | |
| int | admt4000_get_cnv_cnt (struct admt4000_dev *device, uint8_t *cnt) |
| Get current conversion count. | |
| int | admt4000_get_id (struct admt4000_dev *device, uint8_t id_num, uint16_t *id) |
| Read Unique ID from register/s. | |
| int | admt4000_get_harmonic (struct admt4000_dev *device, uint8_t harmonic, struct admt4000_harmonic_coeff *config) |
| Get harmonic configuration (magnitude and phase together). | |
| int | admt4000_set_harmonic (struct admt4000_dev *device, uint8_t harmonic, const struct admt4000_harmonic_coeff *config) |
| Set harmonic configuration (magnitude and phase together). | |
| int | admt4000_get_harmonics_calib (struct admt4000_dev *device, struct admt4000_harmonics_calib *calib) |
| Get all harmonic configurations in a single operation. | |
| int | admt4000_set_harmonics_calib (struct admt4000_dev *device, const struct admt4000_harmonics_calib *calib) |
| Set all harmonic configurations in a single operation. | |
Implementation of ADMT4000 Driver.
This file contains the implementation of the ADMT4000 driver. It includes functions for initializing the device, reading and writing registers, computing CRC, ECC encoding, and more.
Copyright 2026(c) Analog Devices, Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
| int admt4000_angle_raw_to_scaled | ( | uint16_t | raw_angle, |
| float * | scaled_angle ) |
Convert raw angle value to scaled degrees.
| raw_angle | - Input raw angle value |
| scaled_angle | - Output scaled angle in degrees |
Scales raw angle register value to physical units using device-specific resolution factors. Angle: raw * 360 / 4096 (for 12-bit angle resolution)
| int admt4000_clear_all_faults | ( | struct admt4000_dev * | device | ) |
Clear all faults from fault registers.
| device | - The device structure. |
| int admt4000_get_angle_filt | ( | struct admt4000_dev * | device, |
| bool * | is_filtered ) |
Gets current filter setting.
| device | - The device structure. |
| is_filtered | - Stores the logic level of the filter enable bit. |
| int admt4000_get_cnv_cnt | ( | struct admt4000_dev * | device, |
| uint8_t * | cnt ) |
Get current conversion count.
| device | - The device structure. |
| cnt | - Stores the count data. |
| int admt4000_get_conv_mode | ( | struct admt4000_dev * | device, |
| bool * | is_one_shot ) |
Obtains the conversion mode.
| device | - The device structure. |
| is_one_shot | - Stores the conversion mode bit value. |
| int admt4000_get_conv_sync_mode | ( | struct admt4000_dev * | device, |
| enum admt4000_conv_sync_mode * | mode ) |
Obtains conversion synchronization mode.
| device | - The device structure. |
| mode | - The conversion synchronization mode configured. |
| int admt4000_get_cos | ( | struct admt4000_dev * | device, |
| int16_t * | val, | ||
| bool * | is_new_data ) |
Get the cosine value from the ADMT4000 device. This function reads the cosine portion of the AMR angle sensor without post- processing.
| device | - The device structure. |
| val | - Pointer to a variable where cosine value will be stored. |
| is_new_data | - Optional pointer to store new data flag (can be NULL). |
| int admt4000_get_faults | ( | struct admt4000_dev * | device, |
| uint16_t * | faults ) |
Get all fault flags from the fault register.
| device | - The device structure. |
| faults | - Pointer to store the fault register value. |
| int admt4000_get_gpio | ( | struct admt4000_dev * | device, |
| uint8_t | gpio, | ||
| bool * | logic ) |
Get status of selected GPIO.
| device | - The device structure. |
| gpio | - GPIO number (0 to 5). |
| logic | - Stores the truth value of selected GPIO. |
| int admt4000_get_gpio_func | ( | struct admt4000_dev * | device, |
| uint8_t | gpio, | ||
| bool * | alt_mode ) |
Get current GPIO function configuration.
| device | - The device structure. |
| gpio | - GPIO number (0-5). |
| alt_mode | - Pointer to store result (true = alt, false = normal). |
| int admt4000_get_h8_ctrl | ( | struct admt4000_dev * | device, |
| enum admt4000_harmonic_corr_src * | source ) |
Gets 8th harmonic correction source.
| device | - The device structure. |
| source | - The harmonic correction source. |
| int admt4000_get_harmonic | ( | struct admt4000_dev * | device, |
| uint8_t | harmonic, | ||
| struct admt4000_harmonic_coeff * | config ) |
Get harmonic configuration (magnitude and phase together).
| device | - The device structure. |
| harmonic | - Harmonic number (1, 2, 3, or 8). |
| config | - Output structure for magnitude and phase. |
| int admt4000_get_harmonics_calib | ( | struct admt4000_dev * | device, |
| struct admt4000_harmonics_calib * | calib ) |
Get all harmonic configurations in a single operation.
| device | - The device structure. |
| calib | - Output structure for all 4 harmonics (1, 2, 3, 8). |
| int admt4000_get_id | ( | struct admt4000_dev * | device, |
| uint8_t | id_num, | ||
| uint16_t * | id ) |
Read Unique ID from register/s.
| device | - The device structure. |
| id_num | - The ID register to read from. |
| id | - Stores the ID value read. |
| int admt4000_get_radius | ( | struct admt4000_dev * | device, |
| uint16_t * | radius, | ||
| bool * | is_new_data ) |
Get RADIUS vector value.
| device | - The device structure. |
| radius | - Stores the radius value read. |
| is_new_data | - Optional pointer to store new data flag (can be NULL). |
| int admt4000_get_raw_turns_and_angle | ( | struct admt4000_dev * | device, |
| uint8_t * | turns, | ||
| uint16_t * | angle ) |
Get the number of turns and angle information (Raw).
| device | - The device structure. |
| turns | - Stores the number of turns read (Raw). |
| angle | - Stores the angle values read (Raw, extracted). |
| int admt4000_get_sin | ( | struct admt4000_dev * | device, |
| int16_t * | val, | ||
| bool * | is_new_data ) |
Retrieve the sine value from the ADMT4000 device. This function reads the sine portion of the AMR angle sensor without post- processing.
| device | - The device structure. |
| val | - Pointer to a variable where sine value will be stored. |
| is_new_data | - Optional pointer to store new data flag (can be NULL). |
| int admt4000_get_storage_config | ( | struct admt4000_dev * | device, |
| uint8_t * | storage ) |
Gets storage general config.
| device | - The device structure. |
| storage | - Stores the storage bits read. |
| int admt4000_get_temp | ( | struct admt4000_dev * | device, |
| uint16_t * | temp ) |
Get raw temperature reading.
| device | - The device structure. |
| temp | - Raw temperature ADC value. |
| int admt4000_gpio_config | ( | struct admt4000_dev * | device, |
| uint8_t | gpio, | ||
| bool | alt_mode, | ||
| bool | output_en, | ||
| bool | init_state ) |
Configure GPIO mode, direction, and initial state.
GPIO Functions
The ADMT4000 has 6 GPIO pins (GPIO0-GPIO5) that can be configured as either:
Alternate Functions:
When a GPIO is in alternate function mode:
GPIO direction defaults to input mode at init time. If direction configured at init conflicts with the selected alternate function, alternate function's required direction takes precedence.
| device | - The device structure. |
| gpio | - GPIO number (0-5). |
| alt_mode | - Alternate function flag (true = alt, false = normal). |
| output_en | - Direction flag (true = output, false = input). Ignored in alt mode. |
| init_state | - Initial output state (true = high, false = low). Only applied if output_en is true and alt_mode is false. |
| int admt4000_hard_reset | ( | struct admt4000_dev * | device | ) |
Perform hardware reset via reset pin.
| device | - The device structure. |
| int admt4000_harmonic_raw_to_scaled | ( | const struct admt4000_harmonic_coeff * | raw_coeff, |
| float * | scaled_mag, | ||
| float * | scaled_phase ) |
Convert raw harmonic coefficients to scaled format.
| raw_coeff | - Input raw calibration coefficients |
| scaled_mag | - Output scaled magnitude |
| scaled_phase | - Output scaled phase |
Scales raw register values to physical units using device-specific resolution factors. Magnitude: raw * 0.005493/0.6072 Phase: raw * 0.087891
| int admt4000_harmonic_scaled_to_raw | ( | uint32_t | scaled_mag, |
| uint32_t | scaled_phase, | ||
| struct admt4000_harmonic_coeff * | raw_coeff ) |
Convert scaled harmonic values to raw coefficients.
| scaled_mag | - Input scaled magnitude |
| scaled_phase | - Input scaled phase |
| raw_coeff | - Output raw calibration coefficients |
Inverse conversion for writing scaled values to hardware registers. Uses CORDIC scaler (0.6072) to compensate for hardware scaling.
| int admt4000_init | ( | struct admt4000_dev ** | device, |
| struct admt4000_init_param * | init_param ) |
Initializes the admt4000.
| device | - The device structure. |
| init_param | - The structure for the device initial parameters. |
| int admt4000_quarter_turns_cnt | ( | uint8_t | raw_turns, |
| int16_t * | quarter_turns ) |
Convert raw quarter turn count to equivalent signed value.
| raw_turns | - Raw quarter turn count read from device. |
| quarter_turns | - Pointer to store the signed quarter turn count. |
| int admt4000_radius_raw_to_scaled | ( | uint16_t | raw_radius, |
| float * | radius ) |
Convert raw RADIUS value to scaled mV/V.
| raw_radius | - Raw RADIUS value. |
| radius | - Stores Magnitude value (mV/V). |
| int admt4000_raw_angle_read | ( | struct admt4000_dev * | device, |
| uint16_t * | angle_data ) |
Reads ANGLE and ABSANGLE register contents in 1 CS frame.
| device | - The device structure. |
| angle_data | - Stores data read from the registers. |
| int admt4000_reg_read | ( | struct admt4000_dev * | device, |
| uint8_t | reg_addr, | ||
| uint16_t * | reg_data, | ||
| uint8_t * | verif ) |
Reads data from a certain ADMT400x Register.
| device | - The device structure. |
| reg_addr | - Address of the register. |
| reg_data | - Stores data read from the register. |
| verif | - Stores the Fault, and CRC bits returned. |
| int admt4000_reg_update | ( | struct admt4000_dev * | device, |
| uint8_t | reg_addr, | ||
| uint16_t | update_mask, | ||
| uint16_t | update_val ) |
Updates the contents of selected register.
| device | - The device structure. |
| reg_addr | - Address of the register. |
| update_mask | - Mask to update to selected register bit/s. |
| update_val | - Value used to update on selected register. |
| int admt4000_reg_write | ( | struct admt4000_dev * | device, |
| uint8_t | reg_addr, | ||
| uint16_t | reg_data ) |
Writes data to a certain ADMT400x Register.
| device | - The device structure. |
| reg_addr | - Address of the register. |
| reg_data | - Data to write to the chosen register (16 bit wide). |
| int admt4000_reinitialize | ( | struct admt4000_dev * | device | ) |
Reinitialize device using stored configuration.
Convenience function to restore device configuration after magnetic reset or power cycle events using the parameters from initial initialization.
| device | - The device structure. |
| int admt4000_remove | ( | struct admt4000_dev * | device | ) |
Free the resources allocated by admt4000_init().
| device | - The device structure. |
| int admt4000_set_angle_filt | ( | struct admt4000_dev * | device, |
| bool | is_filtered ) |
Sets the filter setting.
| device | - The device structure. |
| is_filtered | - Set true to enable filter, false otherwise. |
| int admt4000_set_conv_mode | ( | struct admt4000_dev * | device, |
| bool | is_one_shot ) |
Configures the conversion mode: one shot or continuous.
| device | - The device structure. |
| is_one_shot | - Set to true for one shot setting, false otherwise. |
| int admt4000_set_conv_sync_mode | ( | struct admt4000_dev * | device, |
| enum admt4000_conv_sync_mode | mode ) |
Configures conversion synchronization mode.
| device | - The device structure. |
| mode | - The conversion synchronization mode to configure. |
| int admt4000_set_gpio | ( | struct admt4000_dev * | device, |
| uint8_t | gpio, | ||
| bool | logic ) |
Configure selected GPIO.
| device | - The device structure. |
| gpio | - GPIO number (0 to 5). |
| logic | - Logic level to set for GPIO. |
| int admt4000_set_h8_ctrl | ( | struct admt4000_dev * | device, |
| enum admt4000_harmonic_corr_src | source ) |
Sets 8th harmonic correction source.
| device | - The device structure. |
| source | - The harmonic correction source. |
| int admt4000_set_harmonic | ( | struct admt4000_dev * | device, |
| uint8_t | harmonic, | ||
| const struct admt4000_harmonic_coeff * | config ) |
Set harmonic configuration (magnitude and phase together).
| device | - The device structure. |
| harmonic | - Harmonic number (1, 2, 3, or 8). |
| config | - Input structure for magnitude and phase. |
| int admt4000_set_harmonics_calib | ( | struct admt4000_dev * | device, |
| const struct admt4000_harmonics_calib * | calib ) |
Set all harmonic configurations in a single operation.
| device | - The device structure. |
| calib | - Input structure for all 4 harmonics (1, 2, 3, 8). |
| int admt4000_set_storage_config | ( | struct admt4000_dev * | device, |
| uint8_t | storage ) |
Sets storage general config.
| device | - The device structure. |
| storage | - Data to write on storage bits. |
| int admt4000_temp_raw_to_scaled | ( | uint16_t | temp_raw, |
| float * | temp ) |
Convert raw temperature value to scaled temperature (degrees Celsius).
| temp_raw | - Raw temperature ADC value. |
| temp | - Temperature data (degrees Celsius). |
| int admt4000_toggle_cnv | ( | struct admt4000_dev * | device | ) |
Toggle the CNV to stop/start conversion.
| device | - The device structure. |