34#ifndef _FREERTOS_DRIVERS_COMMON_TMAG5273_HXX_
35#define _FREERTOS_DRIVERS_COMMON_TMAG5273_HXX_
213 void init(
const char *i2c_path)
215 int fd = ::open(i2c_path, O_RDWR);
248 rc[1] != 0x49 || rc[2] != 0x54)
250 LOG(
VERBOSE,
"read %x %x %x", rc[0], rc[1], rc[2]);
253 return static_cast<DeviceID>(rc[0]);
285 static_cast<uint8_t
>(t));
300 static_cast<uint8_t
>(mode));
344 second ? SCONF2_MAG_GAIN_CH_ADJ2 : SCONF2_MAG_GAIN_CH_ADJ1);
382 xyz[0] = be16toh(xyz[0]);
383 xyz[1] = be16toh(xyz[1]);
384 xyz[2] = be16toh(xyz[2]);
392 register_read(ANGLE_RESULT_MSB, (uint8_t*)&angle,
sizeof(int16_t));
393 return be16toh(angle);
411 SCONF2_MAG_GAIN_CH_ADJ1 = 0,
424 DEVICE_CONFIG_1 = 0x0,
425 DEVICE_CONFIG_2 = 0x1,
426 SENSOR_CONFIG_1 = 0x2,
427 SENSOR_CONFIG_2 = 0x3,
433 MAG_GAIN_CONFIG = 0x9,
434 MAG_OFFSET_CONFIG_1 = 0xA,
435 MAG_OFFSET_CONFIG_2 = 0xB,
438 MANUFACTURER_ID_LSB = 0xE,
439 MANUFACTURER_ID_MSB = 0xF,
449 ANGLE_RESULT_MSB = 0x19,
450 ANGLE_RESULT_LSB = 0x1A,
451 MAGNITUDE_RESULT = 0x1B,
452 DEVICE_STATUS = 0x1C,
463 struct i2c_msg msgs[] = {
464 {.addr =
i2cAddress_, .flags = 0, .len = 1, .buf = ®},
465 {.addr =
i2cAddress_, .flags = I2C_M_RD, .len = len, .buf = data}};
467 struct i2c_rdwr_ioctl_data ioctl_data = {
470 return ::ioctl(
fd_, I2C_RDWR, &ioctl_data);
490 .len = (uint16_t)(len + 1),
493 struct i2c_rdwr_ioctl_data ioctl_data = {
532 current |= (value & mask);
549 static_cast<uint8_t
>(a) |
static_cast<uint8_t
>(b));
559 static_cast<uint8_t
>(a) |
static_cast<uint8_t
>(b));
int ioctl(int fd, unsigned long int key,...)
Request and ioctl transaction.
TMAG5273::DeviceStatus operator|(const TMAG5273::DeviceStatus &a, const TMAG5273::DeviceStatus &b)
'|' operator for DeviceStatus.
Lightweight locking class for protecting small critical sections.
Implementation of TMAG5273 sensor driver.
TMAG5273(const char *i2c_path, uint8_t address=ADDR_A)
Constructor.
ConversionStatus
conversion status.
@ POR_MASK
Device powered up, 0 = no power on reset, 1 = power on reset.
@ SET_COUNT_MASK
rolling count of conversion data sets mask
@ DIAG_STATUS_MASK
Diagnositic status, 0 = no diag fail, 1 diag fail detected.
@ RESULT_STATUS_MASK
Conversion, 0 = conversion not complete, 1 = conversion complete.
@ SET_COUNT_SHIFT
rolling count of conversion data sets shift
void register_write(uint8_t reg, uint8_t value)
Writes one register.
void clr_por_conversion_status()
Clear the power on reset bit of the conversion status.
uint8_t register_read(uint8_t reg)
Reads a single register.
DeviceStatus get_device_status()
Get the device status.
ConversionAverage
Conversion oversampling average configuration.
@ AVG_32
average 32 saples
@ AVG_16
average 16 saples
void read_conversion_results(int16_t xyz[3])
Read the conversion results.
void register_write(uint8_t reg, const uint8_t *data, uint16_t len)
Writes one or more (sequential) register.
DeviceID is_present()
Checks whether the magnetic sensor is present.
void set_sleep_time(SleepTime t)
Set the sleep time between channels in wake and sleep mode.
ChannelEnable
Channels to enabled.
@ XYZ_ENABLE
X, Y, and Z channels enabled.
@ YXY_ENABLE
pseudo-simultaneous Y plus X channels enabled
@ X_ENABLE
X channel enabled.
@ XY_ENABLE
X and Y channels enabled.
@ XZ_ENABLE
X and Z channels enabled.
@ YZ_ENABLE
Y and Z channels enabled.
@ Z_ENABLE
Z channel enabled.
@ ALL_OFF
all channels off, default
@ Y_ENABLE
Y channel enabled.
@ XZX_ENABLE
pseudo-simultaneous X plus Z channels enabled
@ YZY_ENABLE
pseudo-simultaneous Y plus Z channels enabled
@ XYX_ENABLE
pseudo-simultaneous X plus Y channels enabled
InterruptConfig
Interrupt configuration.
@ MASK_INT_N_MASK
mask interrupt pin when INT_N is connected to ground
@ CONVERSION_COMPLETE_ENABLE
interrupt on conversion complete, 0 = disable, 1 = enable
@ MODE_SCL
interrupt mode through SCL
@ MODE_INT_N_EXCEPT_I2C_BUSY
interrupt mode through INT_N, except when I2C is busy
@ MODE_SCL_EXCEPT_I2C_BUSY
interrupt mode through SCL, except when I2C is busy
@ THRESHOLD_ENABLE
interrupt on threshold, 0 = disabled, 1 = enabled
@ MODE_INT_N
interrupt mode through INT_N
@ STATE_MASK
interrupt state, 0 = lached until clear, 1 = pulse for 10 usec
OperatingMode
Operating modes of the device.
@ WAKE_UP_AND_SLEEP
wake-up and sleep mode
@ CONTINUOUS
continuous measurement mode
@ STANDBY
starts new conversion at trigger event
void set_offset_2(int8_t offset)
Sets offset correction for the second axis.
void set_angle_gain(uint8_t gain, bool second)
Sets angle gain parameters.
I2CAddress
Supported I2C addresses.
@ ADDR_D
D1 and D2 device address.
@ ADDR_C
C1 and C2 device address.
@ ADDR_A
A1 and A2 device address.
@ ADDR_B
B1 and B2 device address.
void enable_channels(ChannelEnable enable)
Enable/disable channels.
void init(int i2c_fd)
Initializes the device.
TMAG5273(uint8_t address=ADDR_A)
Constructor.
void set_oversampling(ConversionAverage avg)
Sets the oversampling+averaging mode.
BitMasks
Useful bit masks.
@ DCONF2_OPERATING_MODE_MASK
Mask for the operating mode in DEVICE_CONFIG_2 register.
@ DCONF1_AVG_MASK
Mask for averaging field in DEVICE_CONFIG_1 register.
@ SCONF2_ANGLE_EN_MASK
Mask for ANGLE_EN bits in the SENSOR_CONFIG_2 register.
@ DEVICE_ID_RESERVED_MASK
reserved bits.
@ SCONF2_MAG_GAIN_CH_MASK
Mask for MAG_GAIN_CH bits in the SENSOR_CONFIG_2 register.
@ SCONF1_SLEEP_TIME_MASK
sleep time mask.
@ DEVICE_ID_VERSION_MASK
version bits.
@ SCONF1_MAG_CH_MASK
Channel enable mask.
Registers
Device register address offsets.
void set_offset_1(int8_t offset)
Sets offset correction for the first axis.
int16_t read_angle_result()
Read the angle result.
void clr_device_status(DeviceStatus status)
Get the device status.
AngleEnable
Angle calculation to enable.
SleepTime
Sleep time between conversions when operating mode is wake-up and sleep.
@ SLEEP_1_MSEC
1 millisecond, default
@ SLEEP_500_MSEC
500 milliseconds
@ SLEEP_1000_MSEC
1000 milliseconds
@ SLEEP_2000_MSEC
2000 milliseconds
@ SLEEP_50_MSEC
50 milliseconds
@ SLEEP_5000_MSEC
5000 milliseconds
@ SLEEP_15_MSEC
15 milliseconds
@ SLEEP_5_MSEC
5 milliseconds
@ SLEEP_100_MSEC
100 milliseconds
@ SLEEP_30_MSEC
30 milliseconds
@ SLEEP_10_MSEC
10 milliseconds
@ SLEEP_20_MSEC
20 milliseconds
@ SLEEP_20000_MSEC
20000 milliseconds
DeviceID
Device Identifier.
@ MT_40_AND_80
40-mT and 80-mT range supported
@ UNKNOWN
ID unknown or device not detected.
@ MT_133_AND_266
133-mT and 266-mT range supported
@ ERROR
some kind of error occured
int register_read(uint8_t reg, uint8_t *data, uint16_t len)
Reads one or more (sequential) registers.
void register_modify(uint8_t reg, uint8_t mask, uint8_t value)
Modifies a register value in place.
uint8_t get_conversion_status()
Get the conversion status.
const uint8_t i2cAddress_
7-bit address, right aligned.
void set_angle_en(AngleEnable angle)
Sets whether angle measurement should be enabled.
void set_interrupt_config(InterruptConfig config)
Set the interrupt config.
void set_operating_mode(OperatingMode mode)
Set the operating mode.
DeviceStatus
Device status bit masks.
@ INT_N_PIN_ERROR_MASK
INT_N pin error, 0 = no error, 1 = error, write 1 to clear.
@ OTP_CRC_ERROR_MASK
OTP CRC error, 0 = no error, 1 = error, write 1 to clear.
@ VCC_UV_ERROR_MASK
VCC < 2.3V, 0 = no VCC UV, 1 = VCC UV, write 1 to clear.
@ INT_N_PIN_RESET_LEVEL_MASK
value of the INT_N pin read at reset, 0 = low, 1 = high
@ OSC_ERROR_MASK
Oscillator error, 0 = no error, 1 = error, write 1 to clear.
#define LOG(level, message...)
Conditionally write a message to the logging output.
static const int VERBOSE
Loglevel that is usually not printed, reporting debugging information.
#define ARRAYSIZE(a)
Returns the number of elements in a statically defined array (of static size)
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.