TMAG5170 class
Class for TMAG5170.
Constructors, destructors, conversion operators
- TMAG5170(void)
- Construct a new TMAG5170::
TMAG5170 object.
Public functions
- void attachSPI(spi_inst_t* spi, uint spi_sck_pin, uint spi_mosi_pin, uint spi_miso_pin, uint spi_cs_pin, uint buadrate)
- Attach and initialise SPI on the Pico for TMAG5170.
- auto generateCRC(uint32_t data) -> uint32_t
- Generate CRC for SPI communication.
- auto checkCRC(uint32_t received_frame) -> int
- Check the CRC of the received frame.
- auto exchangeFrame(uint32_t frame) -> uint32_t
- Exchange an SPI frame with TMAG5170.
- auto readRegister(uint8_t offset, bool start_conversion_spi = false) -> uint16_t
- Read the content of the register at the offset in the argument. This function attempts to read the register until the CRC is correct.
- void writeRegister(uint8_t offset, bool start_conversion_spi = false)
- Write the register content stored in the array into the register at the offset provided in the argument. This function attempts to write the register until the returning CRC is correct.
- auto readERRORSTAT(void) -> uint16_t
- Read the ERROR_STAT bits of the previously received frame.
-
auto init(void) -> TMAG5170_
version - Initialise TMAG5170, clear the CFG_REST bit and reads the version of the device.
- void setOperatingMode(uint16_t operating_mode)
- Set the operating mode of the TMAG5170.
- void setConversionAverage(uint16_t conversion_average)
- Set the number of averages to take for each conversion.
- void enableAngleCalculation(uint16_t angle_calculation_config)
- Enable angle calculation.
- void enableMagneticChannel(bool x_enable, bool y_enable, bool z_enable)
- Enable conversion on a selection of the three magnetic axes.
- void setMagneticRange(uint16_t x_range, uint16_t y_range, uint16_t z_range)
- Set magnetic field measurement range on each axis.
- void enableAlertOutput(bool enable)
- Enable ALERT output to notify the microcontroller when the conversion is finished.
- auto readXRaw(bool start_conversion_spi = false) -> int16_t
- Read the raw 16-bit conversion result of the magnetic field on the X axis.
- auto readYRaw(bool start_conversion_spi = false) -> int16_t
- Read the raw 16-bit conversion result of the magnetic field on the Y axis.
- auto readZRaw(bool start_conversion_spi = false) -> int16_t
- Read the raw 16-bit conversion result of the magnetic field on the Z axis.
- auto readX(bool start_conversion_spi = false) -> float
- Read the conversion result in mT on the X axis.
- auto readY(bool start_conversion_spi = false) -> float
- Read the conversion result in mT on the Y axis.
- auto readZ(bool start_conversion_spi = false) -> float
- Read the conversion result in mT on the Z axis.
- auto readAngleRaw(bool start_conversion_spi = false) -> int16_t
- Read the raw 16-bit data of the angle of the magnetic field.
- auto readAngle(bool start_conversion_spi = false) -> float
- Read the angle of the magnetic field in degree.
- auto readMagnitudeRaw(bool start_conversion_spi = false) -> int16_t
- Read the raw 16-bit magnitude of the magnetic field.
Function documentation
void TMAG5170:: attachSPI(spi_inst_t* spi,
uint spi_sck_pin,
uint spi_mosi_pin,
uint spi_miso_pin,
uint spi_cs_pin,
uint buadrate)
Attach and initialise SPI on the Pico for TMAG5170.
| Parameters | |
|---|---|
| spi | SPI instance specifier, either spi0 or spi1. |
| spi_sck_pin | SPI SCK GPIO pin number. |
| spi_mosi_pin | SPI MOSI GPIO pin number. |
| spi_miso_pin | SPI MISO GPIO pin number. |
| spi_cs_pin | SPI CS GPIO pin number. |
| buadrate | Baudrate in Hz. The TMAG5170 supports up to 10 MHz SPI baudrate. |
uint32_t TMAG5170:: generateCRC(uint32_t data)
Generate CRC for SPI communication.
| Parameters | |
|---|---|
| data | 32-bit data to generate CRC for. The last four bits should be all 0's. |
| Returns | uint32_t containing the four CRC bits. |
int TMAG5170:: checkCRC(uint32_t received_frame)
Check the CRC of the received frame.
| Parameters | |
|---|---|
| received_frame | 32-bit data of the received frame. |
| Returns | Return nonzero value if CRC is incorrect. |
uint32_t TMAG5170:: exchangeFrame(uint32_t frame)
Exchange an SPI frame with TMAG5170.
| Parameters | |
|---|---|
| frame | 32-bit data of the frame to send. CRC needs to be included in this frame. |
| Returns | uint32_t containing the received frame. CRC is not checked for the received frame. |
uint16_t TMAG5170:: readRegister(uint8_t offset,
bool start_conversion_spi = false)
Read the content of the register at the offset in the argument. This function attempts to read the register until the CRC is correct.
| Parameters | |
|---|---|
| offset | Offset of the register to read. |
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | Register content read from the TMAG5170. |
void TMAG5170:: writeRegister(uint8_t offset,
bool start_conversion_spi = false)
Write the register content stored in the array into the register at the offset provided in the argument. This function attempts to write the register until the returning CRC is correct.
| Parameters | |
|---|---|
| offset | Offset of the register to write. |
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
uint16_t TMAG5170:: readERRORSTAT(void)
Read the ERROR_STAT bits of the previously received frame.
| Returns | uint16_t containing the ERROR_STAT bits. |
|---|
TMAG5170_ version TMAG5170:: init(void)
Initialise TMAG5170, clear the CFG_REST bit and reads the version of the device.
| Returns | Version of the device, A1 or A2. |
|---|
void TMAG5170:: setOperatingMode(uint16_t operating_mode)
Set the operating mode of the TMAG5170.
| Parameters | |
|---|---|
| operating_mode | Use macros to specify the operating mode. |
void TMAG5170:: setConversionAverage(uint16_t conversion_average)
Set the number of averages to take for each conversion.
| Parameters | |
|---|---|
| conversion_average | Use macros to specify the number of averages. |
void TMAG5170:: enableAngleCalculation(uint16_t angle_calculation_config)
Enable angle calculation.
| Parameters | |
|---|---|
| angle_calculation_config | Use macros to specify the two axes used for angle calculation. |
void TMAG5170:: enableMagneticChannel(bool x_enable,
bool y_enable,
bool z_enable)
Enable conversion on a selection of the three magnetic axes.
| Parameters | |
|---|---|
| x_enable | Enable X axis. |
| y_enable | Enable Y axis. |
| z_enable | Enable Z axis. |
void TMAG5170:: setMagneticRange(uint16_t x_range,
uint16_t y_range,
uint16_t z_range)
Set magnetic field measurement range on each axis.
| Parameters | |
|---|---|
| x_range | Use macros to specify the range of the X axis. |
| y_range | Use macros to specify the range of the Y axis. |
| z_range | Use macros to specify the range of the Z axis. |
void TMAG5170:: enableAlertOutput(bool enable)
Enable ALERT output to notify the microcontroller when the conversion is finished.
| Parameters | |
|---|---|
| enable | Set true to enable ALERT output. |
int16_t TMAG5170:: readXRaw(bool start_conversion_spi = false)
Read the raw 16-bit conversion result of the magnetic field on the X axis.
| Parameters | |
|---|---|
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | The raw 16-bit value in the X_CH_RESULT register. |
int16_t TMAG5170:: readYRaw(bool start_conversion_spi = false)
Read the raw 16-bit conversion result of the magnetic field on the Y axis.
| Parameters | |
|---|---|
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | The raw 16-bit value in the Y_CH_RESULT register. |
int16_t TMAG5170:: readZRaw(bool start_conversion_spi = false)
Read the raw 16-bit conversion result of the magnetic field on the Z axis.
| Parameters | |
|---|---|
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | The raw 16-bit value in the Z_CH_RESULT register. |
float TMAG5170:: readX(bool start_conversion_spi = false)
Read the conversion result in mT on the X axis.
| Parameters | |
|---|---|
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | The magnetic field on the X axis in mT. |
float TMAG5170:: readY(bool start_conversion_spi = false)
Read the conversion result in mT on the Y axis.
| Parameters | |
|---|---|
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | The magnetic field on the Y axis in mT. |
float TMAG5170:: readZ(bool start_conversion_spi = false)
Read the conversion result in mT on the Z axis.
| Parameters | |
|---|---|
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | The magnetic field on the Z axis in mT. |
int16_t TMAG5170:: readAngleRaw(bool start_conversion_spi = false)
Read the raw 16-bit data of the angle of the magnetic field.
| Parameters | |
|---|---|
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | The raw 16-bit data in the ANGLE_RESULT register. |
float TMAG5170:: readAngle(bool start_conversion_spi = false)
Read the angle of the magnetic field in degree.
| Parameters | |
|---|---|
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | The angle of the magnetic field in degree in float. |
int16_t TMAG5170:: readMagnitudeRaw(bool start_conversion_spi = false)
Read the raw 16-bit magnitude of the magnetic field.
| Parameters | |
|---|---|
| start_conversion_spi | Set true to initiate conversion when the CS line goes high after the register read. |
| Returns | The raw 16-bit data in the MAGNITUDE_RESULT register. |