Tlv493d.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Tlv493d.h - Library for Arduino to control the TLV493D-A1B6 3D magnetic sensor.
  3. *
  4. * The 3D magnetic sensor TLV493D-A1B6 offers accurate three dimensional sensing with extremely low power consumption
  5. * in a small 6-pin package. With an opportunity to detect the magnetic field in x, y, and z-direction the sensor is
  6. * ideally suited for the measurement of 3D movements, linear movements and rotation movements.
  7. *
  8. * Have a look at the application note/reference manual for more information.
  9. *
  10. * Copyright (c) 2018 Infineon Technologies AG
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  13. * following conditions are met:
  14. *
  15. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided with the distribution.
  20. *
  21. * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
  22. * products derived from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  25. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef TLV493D_H_INCLUDED
  33. #define TLV493D_H_INCLUDED
  34. #include <Arduino.h>
  35. #include <Wire.h>
  36. #include "./util/BusInterface.h"
  37. #include "./util/Tlv493d_conf.h"
  38. typedef enum Tlv493d_Address
  39. {
  40. TLV493D_ADDRESS1 = 0x5E,
  41. TLV493D_ADDRESS2 = 0x1F
  42. }Tlv493d_Address_t;
  43. typedef enum Tlv493d_Error
  44. {
  45. TLV493D_NO_ERROR = 0,
  46. TLV493D_BUS_ERROR = 1,
  47. TLV493D_FRAME_ERROR = 2
  48. }Tlv493d_Error_t;
  49. /*
  50. * TLV493D_ACCELERATE_READOUT lets the controller just read out the first 3 bytes when in fast mode.
  51. * This makes the readout faster (half of usual transfer duration), but there is no way to get
  52. * temperature, current channel or high precision (only 8 instead of 12 bits for x, y, z)
  53. * It is necessary for slow I2C busses to read the last result before the new measurement is completed.
  54. * It only takes effect in FASTMODE, not in other modes.
  55. *
  56. * Feel free to undefine this and increase your I2C bus speed if you need to.
  57. */
  58. // SBEZEK
  59. // #define TLV493D_ACCELERATE_READOUT
  60. class Tlv493d
  61. {
  62. public:
  63. Tlv493d(void);
  64. ~Tlv493d(void);
  65. void begin(void);
  66. void begin(TwoWire &bus);
  67. void begin(TwoWire &bus, Tlv493d_Address_t slaveAddress, bool reset);
  68. void end(void);
  69. // sensor configuration
  70. /* sets the data access mode for TLE493D
  71. * Tle493d is initially in POWERDOWNMODE
  72. * use POWERDOWNMODE for rare and infrequent measurements
  73. * Tle493d will automatically switch to MASTERCONTROLLEDMODE for one measurement if on a readout
  74. * measurements are quite slow in this mode. The power consumption is very low between measurements.
  75. * use MASTERCONTROLLEDMODE for low measurement frequencies where results do not have to be up-to-date
  76. * In this mode a new measurement starts directly after the last result has been read out.
  77. * use LOWPOWERMODE and ULTRALOWPOWERMODE for continuous measurements
  78. * each readout returns the latest measurement results
  79. * use FASTMODE for for continuous measurements on high frequencies
  80. * measurement time might be higher than the time necessary for I2C-readouts in this mode.
  81. * Note: Thus, this mode requires a non-standard 1MHz I2C clock to be used to read the data fast enough.
  82. */
  83. enum AccessMode_e
  84. {
  85. POWERDOWNMODE = 0,
  86. FASTMODE,
  87. LOWPOWERMODE,
  88. ULTRALOWPOWERMODE,
  89. MASTERCONTROLLEDMODE,
  90. };
  91. bool setAccessMode(AccessMode_e mode);
  92. // interrupt is disabled by default
  93. // it is recommended for FASTMODE, LOWPOWERMODE and ULTRALOWPOWERMODE
  94. // the interrupt is indicated with a short(1.5 us) low pulse on SCL
  95. // you need to capture and react(read the new results) to it by yourself
  96. void enableInterrupt(void);
  97. void disableInterrupt(void);
  98. // temperature measurement is enabled by default
  99. // it can be disabled to reduce power consumption
  100. void enableTemp(void);
  101. void disableTemp(void);
  102. // returns the recommended time between two readouts for the sensor's current configuration
  103. uint16_t getMeasurementDelay(void);
  104. // read measurement results from sensor
  105. Tlv493d_Error_t updateData(void);
  106. // fieldvector in Cartesian coordinates
  107. float getX(void);
  108. float getY(void);
  109. float getZ(void);
  110. // fieldvector in spherical coordinates
  111. float getAmount(void);
  112. float getAzimuth(void);
  113. float getPolar(void);
  114. // temperature
  115. float getTemp(void);
  116. // SBEZEK
  117. uint8_t getExpectedFrameCount(void);
  118. private:
  119. tlv493d::BusInterface_t mInterface;
  120. AccessMode_e mMode;
  121. int16_t mXdata;
  122. int16_t mYdata;
  123. int16_t mZdata;
  124. int16_t mTempdata;
  125. uint8_t mExpectedFrameCount;
  126. void resetSensor(uint8_t adr);
  127. void setRegBits(uint8_t regMaskIndex, uint8_t data);
  128. uint8_t getRegBits(uint8_t regMaskIndex);
  129. void calcParity(void);
  130. int16_t concatResults(uint8_t upperByte, uint8_t lowerByte, bool upperFull);
  131. };
  132. #endif /* TLV493D_H_INCLUDED */