From c67b187a3559c9c9241b1887ef9ced9ed5ee3069 Mon Sep 17 00:00:00 2001 From: MadisonC-SparkFun Date: Fri, 3 Nov 2023 11:54:00 -0600 Subject: [PATCH] Added further funcionality for Flux drivers Need to test files to ensure the library is still ready to go --- README.md | 14 ++ src/SparkFun_TMAG5273_Arduino_Library.cpp | 144 ++++++++++++++++++- src/SparkFun_TMAG5273_Arduino_Library.h | 11 +- src/SparkFun_TMAG5273_Arduino_Library_Defs.h | 2 +- 4 files changed, 164 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index fb96050..596c2ce 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,20 @@ Documentation * **[Hookup Guide](https://docs.sparkfun.com/SparkFun_IoT_Brushless_Motor_Driver)** - Basic hookup guide for the SparkFun IoT Motor Driver Breakout (ESP32, TMC6300). * **[Product Repository](https://github.com/sparkfun/SparkFun_IoT_Brushless_Motor_Driver)** - Main repository for the IoT Motor Driver (including hardware files) + +Products that use this Library +--------------------------------- + +* [*ROB-22132*](https://www.sparkfun.com/products/22132) + +Version History +--------------- + +* [V_1.0.0](https://github.com/sparkfun/SparkFun_TMAG5273_Arduino_Library/tree/v1.0.0) - Public release. +* [V_1.0.1](https://github.com/sparkfun/SparkFun_TMAG5273_Arduino_Library/tree/v1.0.1) - Fix Angle Calculations from Pull Request. +* [V_1.0.2](https://github.com/sparkfun/SparkFun_TMAG5273_Arduino_Library/tree/v1.0.2) - Adjusted LSB/MSB math from Pull Request. + + License Information ------------------- diff --git a/src/SparkFun_TMAG5273_Arduino_Library.cpp b/src/SparkFun_TMAG5273_Arduino_Library.cpp index e76443f..4ccf92b 100644 --- a/src/SparkFun_TMAG5273_Arduino_Library.cpp +++ b/src/SparkFun_TMAG5273_Arduino_Library.cpp @@ -852,6 +852,33 @@ int8_t TMAG5273::setSleeptime(uint8_t sleepTime) return getError(); } +/// @brief Sets the number of threshold crossings +/// @param cross Value to set the number of threshold crossings +/// 0x0 = TMAG5273_THRESHOLD_1 (1 crossing) +/// 0x1 = TMAG5273_THRESHOLD_4 (4 crossings) +/// @return Error code (0 is success, negative is failure, positive is warning) +int8_t TMAG5273::setThresholdCross(uint8_t cross) +{ + uint16_t mode = 0; + mode = readRegister(TMAG5273_REG_SENSOR_CONFIG_2); + + // If-Else statement for writing values to the register, bit by bit + if (cross == 0X0) // 0b0 + { + // Write 0 to bit 6 of the register value + bitWrite(mode, 6, 0); + // Writes mode to the CONFIG_2 register + writeRegister(TMAG5273_REG_SENSOR_CONFIG_2, mode); + } + else if (threshDir == 0X1) // 0b1 + { + bitWrite(cross, 6, 1); + writeRegister(TMAG5273_REG_SENSOR_CONFIG_2, mode); + } + + return getError(); +} + /// @brief Sets the direction of threshold check. This bit /// is ignored when THR_HYST > 001b /// @param threshDir value to set the direction of threshold @@ -910,6 +937,34 @@ int8_t TMAG5273::setMagnitudeGain(uint8_t gainAdjust) return getError(); } +/// @brief This function sets the axis for the magnitude gain correction +/// value entered in the MAG_GAIN_CONFIG register. +/// @param select Selects which channel +/// 0x0 = 1st channel (XY) +/// 0x1 = 2nd channel (Z) +/// @return Error code (0 is success, negative is failure, positive is warning) +int8_t TMAG5273::setMagChannelSelect(uint8_t select) +{ + uint8_t channelReg = 0; + channelReg = readRegister(TMAG5273_REG_SENSOR_CONFIG_2); + + // If-Else statement for writing values to the register, bit by bit + if (select == 0) // 0b0 + { + // Write 0 to bit 4 of the register value + bitWrite(channelReg, 4, 0); + // Writes mode to the CONFIG_2 register + writeRegister(TMAG5273_REG_SENSOR_CONFIG_2, channelReg); + } + else if (select == 1) // 0b1 + { + bitWrite(channelReg, 4, 1); + writeRegister(TMAG5273_REG_SENSOR_CONFIG_2, channelReg); + } + + return getError(); +} + /// @brief This function sets an 8-bit gain value determined by a /// primary to adjust a Hall axis gain. The particular axis is selected /// based off the settings of MAG_GAIN_CH and ANGLE_EN register bits. @@ -1404,6 +1459,34 @@ int8_t TMAG5273::setI2CAddress(uint8_t address) return getError(); } +/// @brief Clears the POR bit of the CONV_STATUS register. +/// POR = Power-On-Reset +/// TMAG5273_NO_POR = 0x0 +/// TMAG5273_YES_POR = 0x1 +/// @param por Bit to clear the register once POR happens +/// @return Error code (0 is success, negative is failure, positive is warning) +int8_t TMAG5273::setPOR(bool por) +{ + uint16_t mode = 0; + mode = readRegister(TMAG5273_REG_CONV_STATUS); + + // If-Else statement for writing values to the register, bit by bit + if (por == 0) // 0b0 + { + // Write 0 to bit 1 of the register value + bitWrite(mode, 4, 0); + // Writes mode to the CONFIG_2 register + writeRegister(TMAG5273_REG_CONV_STATUS, mode); + } + else if (por == 1) // 0b1 + { + bitWrite(mode, 4, 1); + writeRegister(TMAG5273_REG_CONV_STATUS, mode); + } + + return getError(); +} + /// @brief Writes to the I2C_ADDRESS_UPDATE_EN bit to enable /// a new user defined I2C address. /// @param addressEnable Value to determine if the user can write a new address. @@ -1653,7 +1736,7 @@ uint8_t TMAG5273::getLowPower() /// 0X0 = Glitch filter OFF /// TMAG5273_REG_DEVICE_CONFIG_2 - bit 3 /// @return I2C filter ON (0) or OFF (1) -uint8_t TMAG5273::getGlitchFiler() +uint8_t TMAG5273::getGlitchFilter() { uint8_t glitchMode = 0; glitchMode = readRegister(TMAG5273_REG_DEVICE_CONFIG_2); @@ -1885,6 +1968,20 @@ uint8_t TMAG5273::getSleeptime() } } +/// @brief Returns the number of threshold crossings +/// 0x0 = 1 threshold cross +/// 0x1 = 4 threshold crossings +/// @return Number of threshold crossings +uint8_t TMAG5273::getThresholdCross() +{ + uint8_t crossReg = 0; + crossReg = readRegister(TMAG5273_REG_SENSOR_CONFIG_2); + + uint8_t cross6 = bitRead(crossReg, 6); + + return cross6; +} + /// @brief Returns the direction of threshold check. This bit is /// ignored when THR_HYST > 001b. /// 0X0 = sets interrupt for field above the threshold @@ -1901,13 +1998,23 @@ uint8_t TMAG5273::getMagDir() return magDirection5; } +uint8_t TMAG5273::getMagnitudeGain() +{ + uint8_t magGainReg = 0; + magGainReg = readRegister(TMAG5273_REG_SENSOR_CONFIG_2); + + uint8_t magnitudeGain = bitRead(magGainReg, 4); + + return magnitudeGain; +} + /// @brief Returns the axis for magnitude gain correction value /// entered in MAG_GAIN_CONFIG register /// 0X0 = 1st channel is selected for gain adjustment /// 0X1 = 2nd channel is selected for gain adjustment /// TMAG5273_REG_SENSOR_CONFIG_2 - bit 4 /// @return First (0) or Second (0) channel selected for gain adjustment -uint8_t TMAG5273::getMagnitudeChannelSelect() +uint8_t TMAG5273::getMagChannelSelect() { uint8_t magGainReg = 0; magGainReg = readRegister(TMAG5273_REG_SENSOR_CONFIG_2); @@ -2351,6 +2458,21 @@ uint8_t TMAG5273::getPOR() return PORBit; } +/// @brief Returns if the device experienced an oscillator error. +/// 0X0 = No Oscillator error detected +/// 0X1 = Oscillator error detected +/// TMAG5273_REG_DEVICE_STATUS - bit 3 +/// @return Device is powered up or experienced POR. +uint8_t TMAG5273::getOscillatorError() +{ + uint8_t statusReg = 0; + statusReg = readRegister(TMAG5273_REG_DEVICE_STATUS); + + uint8_t oscBit = bitRead(statusReg, 3); + + return oscBit; +} + /// @brief Returns if there was a detection of any internal /// diagnostics fail which include VCC UV, internal memory CRC /// error, !INT pin error and internal clock error. Ignore @@ -2385,6 +2507,20 @@ uint8_t TMAG5273::getResultStatus() return resultBit; } +/// @brief Returns the status of the I2C_ADDRESS_UPDATE_EN bit to enable +/// a new user defined I2C address. +/// @param addressEnable Value to determine if the user can write a new address. +/// 0X0 = Disable update of I2C address +/// 0X1 = Enable update of I2C address with bits (7:1) +/// @return Error code (0 is success, negative is failure, positive is warning) +bool TMAG5273::getI2CAddressEN() +{ + uint8_t i2cReg = readRegister(TMAG5273_REG_I2C_ADDRESS); + bool addressBit = bitRead(i2cReg, 0); + + return addressBit; +} + /// @brief Returns the I2C address of the device. There is a 7-bit /// default factory address is loaded from OTP during first power /// up. Change these bits to a new setting if a new I2C address is @@ -2614,7 +2750,7 @@ float TMAG5273::getZData() // Variable to store full X data int16_t zData = 0; // Combines the two in one register where the MSB is shifted to the correct location - zData = zLSB + (zMSB << 8); + zData = zLSB + (zMSB << 8); // Reads to see if the range is set to 40mT or 80mT uint8_t rangeValZ = getZAxisRange(); @@ -2661,7 +2797,7 @@ float TMAG5273::getAngleResult() float finalVal = 0; // Combining the register value - angleReg = angleLSB + (angleMSB << 8); + angleReg = angleLSB + (angleMSB << 8); // Removing the uneeded bits for the fraction value decValue = float(angleLSB & 0b1111) / 16; diff --git a/src/SparkFun_TMAG5273_Arduino_Library.h b/src/SparkFun_TMAG5273_Arduino_Library.h index 2a7746d..32e2fa5 100644 --- a/src/SparkFun_TMAG5273_Arduino_Library.h +++ b/src/SparkFun_TMAG5273_Arduino_Library.h @@ -51,8 +51,10 @@ class TMAG5273 int8_t setOperatingMode(uint8_t opMode); // Selects operating mode and updates values based on operating mode int8_t setMagneticChannel(uint8_t channelMode); // Sets the data acquisition from magnetic axis channels int8_t setSleeptime(uint8_t sleepTime); // Sets the time spent in low power mode between conversions + int8_t setThresholdCross(uint8_t cross); // Sets the number of threshold crossings int8_t setMagDir(uint8_t threshDir); // Sets the direction of threshold check int8_t setMagnitudeGain(uint8_t gainAdjust); // Sets the axis for magnitude gain correction value + int8_t setMagChannelSelect(uint8_t select); // Sets the axis for magnitude gain correction value int8_t setMagneticGain(float magneticGain); // Sets the 8-bit gain value to adjust a Hall axis gain int8_t setMagneticOffset1(float offset1); // Sets the offset value determined by a primary for the first axis int8_t setMagneticOffset2(float offset2); // Sets the offset value determined by a primary for the second axis @@ -74,6 +76,7 @@ class TMAG5273 int8_t setI2CAddress(uint8_t address); // Change these bits to a new I2C address if required int8_t setI2CAddressEN( bool addressEnable); // Enables/disables bit to allow the user to change the I2C address of the devices + int8_t setPOR(bool por); // Clears the bit once POR happens to the device int8_t setOscillatorError(bool oscError); // Clear the Oscillator Error pin accordingly // Get Device Configuration Register Settings @@ -83,13 +86,15 @@ class TMAG5273 uint8_t getReadMode(); // Returns the I2C read mode uint8_t getIntThreshold(); // Returns the threshold for the interrupt function uint8_t getLowPower(); // Returns if the device is operating in low power or noise mode - uint8_t getGlitchFiler(); // Returns I2C glitch filter on or off + uint8_t getGlitchFilter(); // Returns I2C glitch filter on or off uint8_t getTriggerMode(); // Returns if trigger is set to I2C command or INT pin uint8_t getOperatingMode(); // Returns the operating mode from 1 of the 4 modes. uint8_t getMagneticChannel(); // Returns data acquisiton from the list of mag axis channels uint8_t getSleeptime(); // Returns the time spent in low power mode uint8_t getMagDir(); // Returns the direction of threshold check - uint8_t getMagnitudeChannelSelect(); // Returns the axis for magnitude gain correct XYAxisRange section value + uint8_t getThresholdCross(); // Returns the number of threshold crossings (1 or 4) + uint8_t getMagnitudeGain(); // Returns the axis for magnitude gain correction value + uint8_t getMagChannelSelect(); // Returns the axis for magnitude gain correct XYAxisRange section value uint8_t getMagneticGain(); // Returns the value determined to adjust a Hall axis gain int8_t getMagneticOffset1(); // Returns the offset value determined by a primary for the first axis int8_t getMagneticOffset2(); // Returns the offset value determined by a primary for the second axis @@ -108,8 +113,10 @@ class TMAG5273 uint8_t getMaskInt(); // Returns the Mask !INT pin when !INT is connected to GND uint8_t getSetCount(); // Returns the rolling count of conversion data sets uint8_t getPOR(); // Returns if the device is powered up or expereinced POR + uint8_t getOscillatorError(); // Returns if there was an oscillator error uint8_t getDiagStatus(); // Returns if there was a detection of any internal diagnostics fail uint8_t getResultStatus(); // Returns the conversion data buffer status (Data complete or not) + uint8_t getI2CAddressEN(); // Returns if the device is enabled to rewrite the I2C Address uint8_t getI2CAddress(); // Returns the I2C address of the device uint8_t getDeviceID(); // Returns the device version indicator uint16_t getManufacturerID(); // Returns the manufacturer ID diff --git a/src/SparkFun_TMAG5273_Arduino_Library_Defs.h b/src/SparkFun_TMAG5273_Arduino_Library_Defs.h index 68b56a7..fc2cb03 100644 --- a/src/SparkFun_TMAG5273_Arduino_Library_Defs.h +++ b/src/SparkFun_TMAG5273_Arduino_Library_Defs.h @@ -108,7 +108,7 @@ Features as per datasheet #define TMAG5273_TEMPERATURE_DISABLE 0X0 // Temperature channel disabled #define TMAG5273_TEMPERATURE_ENABLE 0X1 // Temperature channel enabled -#define TMAG5273_INTERRUPT_NOT_ASSERTED 0X0 // Interrupt is not asserved when set +#define TMAG5273_INTERRUPT_NOT_ASSERTED 0X0 // Interrupt is not asserted when set #define TMAG5273_INTERRUPT_ASSERTED 0X1 // Interrupt is asserted #define TMAG5273_NO_INTERRUPT 0X0 // No interrupt