|
Open Model Railroad Network (OpenMRN)
|
Implementation of TMAG5273 sensor driver. More...
#include <TMAG5273.hxx>
Public Types | |
| enum | I2CAddress : uint8_t { ADDR_A = 0x35 , ADDR_B = 0x22 , ADDR_C = 0x78 , ADDR_D = 0x44 } |
| Supported I2C addresses. More... | |
| enum class | DeviceID : uint8_t { UNKNOWN = 0x00 , MT_40_AND_80 = 0x01 , MT_133_AND_266 = 0x02 , ERROR = 0xFF } |
| Device Identifier. More... | |
| enum class | ChannelEnable : uint8_t { ALL_OFF = 0x00 , X_ENABLE = 0x10 , Y_ENABLE = 0x20 , XY_ENABLE = 0x30 , Z_ENABLE = 0x40 , XZ_ENABLE = 0x50 , YZ_ENABLE = 0x60 , XYZ_ENABLE = 0x70 , XYX_ENABLE = 0x80 , YXY_ENABLE = 0x90 , YZY_ENABLE = 0xA0 , XZX_ENABLE = 0xB0 } |
| Channels to enabled. More... | |
| enum class | SleepTime : uint8_t { SLEEP_1_MSEC = 0x00 , SLEEP_5_MSEC = 0x01 , SLEEP_10_MSEC = 0x02 , SLEEP_15_MSEC = 0x03 , SLEEP_20_MSEC = 0x04 , SLEEP_30_MSEC = 0x05 , SLEEP_50_MSEC = 0x06 , SLEEP_100_MSEC = 0x07 , SLEEP_500_MSEC = 0x08 , SLEEP_1000_MSEC = 0x09 , SLEEP_2000_MSEC = 0x0A , SLEEP_5000_MSEC = 0x0B , SLEEP_20000_MSEC = 0x0C } |
| Sleep time between conversions when operating mode is wake-up and sleep. More... | |
| enum class | DeviceStatus : uint8_t { INT_N_PIN_RESET_LEVEL_MASK = 0x10 , OSC_ERROR_MASK = 0x08 , INT_N_PIN_ERROR_MASK = 0x04 , OTP_CRC_ERROR_MASK = 0x02 , VCC_UV_ERROR_MASK = 0x01 } |
| Device status bit masks. More... | |
| enum class | InterruptConfig : uint8_t { CONVERSION_COMPLETE_ENABLE = 0x80 , THRESHOLD_ENABLE = 0x40 , STATE_MASK = 0x20 , MODE_NONE = (0 << 2) , MODE_INT_N = (1 << 2) , MODE_INT_N_EXCEPT_I2C_BUSY = (2 << 2) , MODE_SCL = (3 << 2) , MODE_SCL_EXCEPT_I2C_BUSY = (4 << 2) , MASK_INT_N_MASK = 0x01 } |
| Interrupt configuration. More... | |
| enum class | ConversionStatus : uint8_t { SET_COUNT_MASK = 0xE0 , SET_COUNT_SHIFT = 5 , POR_MASK = 0x10 , DIAG_STATUS_MASK = 0x02 , RESULT_STATUS_MASK = 0x01 } |
| conversion status. More... | |
| enum class | ConversionAverage : uint8_t { AVG_1 = (0 << 2) , AVG_2 = (1 << 2) , AVG_4 = (2 << 2) , AVG_8 = (3 << 2) , AVG_16 = (4 << 2) , AVG_32 = (5 << 2) } |
| Conversion oversampling average configuration. More... | |
| enum class | AngleEnable : uint8_t { OFF = (0 << 2) , XY_ENABLE = (1 << 2) , YZ_ENABLE = (2 << 2) , XZ_ENABLE = (3 << 2) } |
| Angle calculation to enable. More... | |
| enum class | OperatingMode : uint8_t { STANDBY = 0x00 , SLEEP = 0x01 , CONTINUOUS = 0x02 , WAKE_UP_AND_SLEEP = 0x03 } |
| Operating modes of the device. More... | |
Public Member Functions | |
| TMAG5273 (uint8_t address=ADDR_A) | |
| Constructor. | |
| TMAG5273 (const char *i2c_path, uint8_t address=ADDR_A) | |
| Constructor. | |
| ~TMAG5273 () | |
| Destructor. | |
| void | init (const char *i2c_path) |
| void | init (int i2c_fd) |
| Initializes the device. | |
| DeviceID | is_present () |
| Checks whether the magnetic sensor is present. | |
| DeviceStatus | get_device_status () |
| Get the device status. | |
| void | clr_device_status (DeviceStatus status) |
| Get the device status. | |
| void | enable_channels (ChannelEnable enable) |
| Enable/disable channels. | |
| void | set_sleep_time (SleepTime t) |
| Set the sleep time between channels in wake and sleep mode. | |
| void | set_interrupt_config (InterruptConfig config) |
| Set the interrupt config. | |
| void | set_operating_mode (OperatingMode mode) |
| Set the operating mode. | |
| uint8_t | get_conversion_status () |
| Get the conversion status. | |
| void | clr_por_conversion_status () |
| Clear the power on reset bit of the conversion status. | |
| void | set_oversampling (ConversionAverage avg) |
| Sets the oversampling+averaging mode. | |
| void | set_angle_en (AngleEnable angle) |
| Sets whether angle measurement should be enabled. | |
| void | set_angle_gain (uint8_t gain, bool second) |
| Sets angle gain parameters. | |
| void | set_offset_1 (int8_t offset) |
| Sets offset correction for the first axis. | |
| void | set_offset_2 (int8_t offset) |
| Sets offset correction for the second axis. | |
| DeviceID | get_device_id () |
| void | read_conversion_results (int16_t xyz[3]) |
| Read the conversion results. | |
| int16_t | read_angle_result () |
| Read the angle result. | |
Private Types | |
| enum | BitMasks { DCONF1_AVG_MASK = 0b11100 , DCONF2_OPERATING_MODE_MASK = 0b11 , SCONF1_MAG_CH_MASK = 0b11110000 , SCONF1_SLEEP_TIME_MASK = 0b00001111 , SCONF2_MAG_GAIN_CH_MASK = 1 << 4 , SCONF2_MAG_GAIN_CH_ADJ1 = 0 , SCONF2_MAG_GAIN_CH_ADJ2 = SCONF2_MAG_GAIN_CH_MASK , SCONF2_ANGLE_EN_MASK = 0b1100 , DEVICE_ID_RESERVED_MASK = 0b11111100 , DEVICE_ID_VERSION_MASK = 0b00000011 } |
| Useful bit masks. More... | |
| enum | Registers { DEVICE_CONFIG_1 = 0x0 , DEVICE_CONFIG_2 = 0x1 , SENSOR_CONFIG_1 = 0x2 , SENSOR_CONFIG_2 = 0x3 , X_THR_CONFIG = 0x4 , Y_THR_CONFIG = 0x5 , Z_THR_CONFIG = 0x6 , T_CONFIG = 0x7 , INT_CONFIG_1 = 0x8 , MAG_GAIN_CONFIG = 0x9 , MAG_OFFSET_CONFIG_1 = 0xA , MAG_OFFSET_CONFIG_2 = 0xB , I2C_ADDRESS = 0xC , DEVICE_ID = 0xD , MANUFACTURER_ID_LSB = 0xE , MANUFACTURER_ID_MSB = 0xF , T_MSB_RESULT = 0x10 , T_LSB_RESULT = 0x11 , X_MSB_RESULT = 0x12 , X_LSB_RESULT = 0x13 , Y_MSB_RESULT = 0x14 , Y_LSB_RESULT = 0x15 , Z_MSB_RESULT = 0x16 , Z_LSB_RESULT = 0x17 , CONV_STATUS = 0x18 , ANGLE_RESULT_MSB = 0x19 , ANGLE_RESULT_LSB = 0x1A , MAGNITUDE_RESULT = 0x1B , DEVICE_STATUS = 0x1C } |
| Device register address offsets. More... | |
Private Member Functions | |
| int | register_read (uint8_t reg, uint8_t *data, uint16_t len) |
| Reads one or more (sequential) registers. | |
| void | register_write (uint8_t reg, const uint8_t *data, uint16_t len) |
| Writes one or more (sequential) register. | |
| void | register_write (uint8_t reg, uint8_t value) |
| Writes one register. | |
| uint8_t | register_read (uint8_t reg) |
| Reads a single register. | |
| void | register_modify (uint8_t reg, uint8_t mask, uint8_t value) |
| Modifies a register value in place. | |
Private Member Functions inherited from Atomic | |
| void | lock () |
| void | unlock () |
Private Attributes | |
| int | fd_ = -1 |
| I2C device. | |
| const uint8_t | i2cAddress_ |
| 7-bit address, right aligned. | |
Friends | |
| class | MagSensorTest |
| class | Input |
Implementation of TMAG5273 sensor driver.
Definition at line 49 of file TMAG5273.hxx.
|
strong |
Angle calculation to enable.
| Enumerator | |
|---|---|
| OFF | angle not enabled |
| XY_ENABLE | X + Y derived angle. |
| YZ_ENABLE | Y + Z derived angle. |
| XZ_ENABLE | X + Z derived angle. |
Definition at line 170 of file TMAG5273.hxx.
|
private |
Useful bit masks.
Definition at line 398 of file TMAG5273.hxx.
|
strong |
Channels to enabled.
Definition at line 71 of file TMAG5273.hxx.
|
strong |
Conversion oversampling average configuration.
| Enumerator | |
|---|---|
| AVG_1 | average 1 saple |
| AVG_2 | average 2 saples |
| AVG_4 | average 4 saples |
| AVG_8 | average 8 saples |
| AVG_16 | average 16 saples |
| AVG_32 | average 32 saples |
Definition at line 159 of file TMAG5273.hxx.
|
strong |
conversion status.
| Enumerator | |
|---|---|
| SET_COUNT_MASK | rolling count of conversion data sets mask |
| SET_COUNT_SHIFT | rolling count of conversion data sets shift |
| POR_MASK | Device powered up, 0 = no power on reset, 1 = power on reset. |
| DIAG_STATUS_MASK | Diagnositic status, 0 = no diag fail, 1 diag fail detected. |
| RESULT_STATUS_MASK | Conversion, 0 = conversion not complete, 1 = conversion complete. |
Definition at line 144 of file TMAG5273.hxx.
|
strong |
Device Identifier.
| Enumerator | |
|---|---|
| UNKNOWN | ID unknown or device not detected. |
| MT_40_AND_80 | 40-mT and 80-mT range supported |
| MT_133_AND_266 | 133-mT and 266-mT range supported |
| ERROR | some kind of error occured |
Definition at line 62 of file TMAG5273.hxx.
|
strong |
Device status bit masks.
Definition at line 106 of file TMAG5273.hxx.
| enum TMAG5273::I2CAddress : uint8_t |
Supported I2C addresses.
| Enumerator | |
|---|---|
| ADDR_A | A1 and A2 device address. |
| ADDR_B | B1 and B2 device address. |
| ADDR_C | C1 and C2 device address. |
| ADDR_D | D1 and D2 device address. |
Definition at line 53 of file TMAG5273.hxx.
|
strong |
Interrupt configuration.
| Enumerator | |
|---|---|
| CONVERSION_COMPLETE_ENABLE | interrupt on conversion complete, 0 = disable, 1 = enable |
| THRESHOLD_ENABLE | interrupt on threshold, 0 = disabled, 1 = enabled |
| STATE_MASK | interrupt state, 0 = lached until clear, 1 = pulse for 10 usec |
| MODE_NONE | no interrupt |
| MODE_INT_N | interrupt mode through INT_N |
| MODE_INT_N_EXCEPT_I2C_BUSY | interrupt mode through INT_N, except when I2C is busy |
| MODE_SCL | interrupt mode through SCL |
| MODE_SCL_EXCEPT_I2C_BUSY | interrupt mode through SCL, except when I2C is busy |
| MASK_INT_N_MASK | mask interrupt pin when INT_N is connected to ground |
Definition at line 121 of file TMAG5273.hxx.
|
strong |
Operating modes of the device.
| Enumerator | |
|---|---|
| STANDBY | starts new conversion at trigger event |
| SLEEP | sleep |
| CONTINUOUS | continuous measurement mode |
| WAKE_UP_AND_SLEEP | wake-up and sleep mode |
Definition at line 179 of file TMAG5273.hxx.
|
private |
Device register address offsets.
Definition at line 422 of file TMAG5273.hxx.
|
strong |
Sleep time between conversions when operating mode is wake-up and sleep.
Definition at line 88 of file TMAG5273.hxx.
|
inline |
Constructor.
| address | is the 7-bit address (right aligned), typically 0x35, 0x22, 0x78 or 0x44. Default is the A1/A2 device. |
Definition at line 190 of file TMAG5273.hxx.
|
inline |
Constructor.
Can only be called from thread context.
| i2c_path | path to the i2c bus to use. |
| address | is the 7-bit address (right aligned), typically 0x35, 0x22, 0x78 or 0x44. Default is the A1/A2 device. |
Definition at line 198 of file TMAG5273.hxx.
|
inline |
Destructor.
Definition at line 205 of file TMAG5273.hxx.
|
inline |
Get the device status.
| bit | mask of DeviceStatus bits to clear |
Definition at line 267 of file TMAG5273.hxx.
|
inline |
Clear the power on reset bit of the conversion status.
Definition at line 313 of file TMAG5273.hxx.
|
inline |
Enable/disable channels.
| enable | The channel set to enable. Default is all channels off. |
Definition at line 274 of file TMAG5273.hxx.
|
inline |
Get the conversion status.
Definition at line 305 of file TMAG5273.hxx.
|
inline |
Definition at line 371 of file TMAG5273.hxx.
|
inline |
|
inline |
Definition at line 213 of file TMAG5273.hxx.
|
inline |
Initializes the device.
| i2c_fd | is the file descriptor of the i2c port to use. |
Definition at line 222 of file TMAG5273.hxx.
|
inline |
Checks whether the magnetic sensor is present.
Definition at line 233 of file TMAG5273.hxx.
|
inline |
Read the angle result.
Definition at line 389 of file TMAG5273.hxx.
|
inline |
Read the conversion results.
| xyz | results as a three item array in order of X, Y, and Z |
Definition at line 379 of file TMAG5273.hxx.
|
inlineprivate |
Modifies a register value in place.
| reg | register address to modify. |
| mask | bitmask of which bits to modify |
| value | new value of these bits (shifted to the right position). Any bits set outside the mask will be ignored. |
Definition at line 528 of file TMAG5273.hxx.
|
inlineprivate |
Reads a single register.
| reg | register number. |
Definition at line 514 of file TMAG5273.hxx.
|
inlineprivate |
Reads one or more (sequential) registers.
| reg | starting register offset. |
| data | where to write payload |
| len | number of registers to read (1 byte each). Returns when the read is complete. |
Definition at line 461 of file TMAG5273.hxx.
|
inlineprivate |
Writes one or more (sequential) register.
| reg | starting register offset. |
| data | payload to write |
| len | number of bytes to write. Returns when the write is complete. |
Definition at line 478 of file TMAG5273.hxx.
|
inlineprivate |
Writes one register.
| reg | starting register offset. |
| value | payload to write Returns when the write is complete. |
Definition at line 503 of file TMAG5273.hxx.
|
inline |
Sets whether angle measurement should be enabled.
| angle | angle measurement to enabld (or disable) |
Definition at line 329 of file TMAG5273.hxx.
|
inline |
Sets angle gain parameters.
| gain | 8-bit gain value (interpreted as 0..1) |
| second | true if the second channel is larger than the first channel |
Definition at line 340 of file TMAG5273.hxx.
|
inline |
Set the interrupt config.
| config | InterruptConfig options or'd together. |
Definition at line 290 of file TMAG5273.hxx.
|
inline |
Sets offset correction for the first axis.
The maximum correction is -2048 .. +2032.
| offset | 8-bit signed value to write to offset correction. The counts offset are offset * 16. |
Definition at line 353 of file TMAG5273.hxx.
|
inline |
Sets offset correction for the second axis.
The maximum correction is -2048 .. +2032.
| offset | 8-bit signed value to write to offset correction. |
Definition at line 363 of file TMAG5273.hxx.
|
inline |
Set the operating mode.
| mode | operating mode to set |
Definition at line 297 of file TMAG5273.hxx.
|
inline |
Sets the oversampling+averaging mode.
| avg | average oversample setting |
Definition at line 321 of file TMAG5273.hxx.
|
inline |
Set the sleep time between channels in wake and sleep mode.
| t | The sleep time between conversions. |
Definition at line 282 of file TMAG5273.hxx.
|
friend |
Definition at line 228 of file TMAG5273.hxx.
|
friend |
Definition at line 227 of file TMAG5273.hxx.
|
private |
I2C device.
Definition at line 537 of file TMAG5273.hxx.
|
private |
7-bit address, right aligned.
Definition at line 539 of file TMAG5273.hxx.