INA3221.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Arduino library for INA3221 current and voltage sensor.
  3. MIT License
  4. Copyright (c) 2020 Beast Devices, Andrejs Bondarevs
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. #ifndef BEASTDEVICES_INA3221_H
  22. #define BEASTDEVICES_INA3221_H
  23. #include <ch.h>
  24. #include <hal.h>
  25. typedef enum {
  26. INA3221_ADDR40_GND = 0b1000000, // A0 pin -> GND
  27. INA3221_ADDR41_VCC = 0b1000001, // A0 pin -> VCC
  28. INA3221_ADDR42_SDA = 0b1000010, // A0 pin -> SDA
  29. INA3221_ADDR43_SCL = 0b1000011 // A0 pin -> SCL
  30. } ina3221_addr_t;
  31. // Channels
  32. typedef enum {
  33. INA3221_CH1 = 0,
  34. INA3221_CH2 = 1,
  35. INA3221_CH3 = 2,
  36. INA3221_CH_NUM = 3
  37. } ina3221_ch_t;
  38. // Registers
  39. typedef enum {
  40. INA3221_REG_CONF = 0,
  41. INA3221_REG_CH1_SHUNTV,
  42. INA3221_REG_CH1_BUSV,
  43. INA3221_REG_CH2_SHUNTV,
  44. INA3221_REG_CH2_BUSV,
  45. INA3221_REG_CH3_SHUNTV,
  46. INA3221_REG_CH3_BUSV,
  47. INA3221_REG_CH1_CRIT_ALERT_LIM,
  48. INA3221_REG_CH1_WARNING_ALERT_LIM,
  49. INA3221_REG_CH2_CRIT_ALERT_LIM,
  50. INA3221_REG_CH2_WARNING_ALERT_LIM,
  51. INA3221_REG_CH3_CRIT_ALERT_LIM,
  52. INA3221_REG_CH3_WARNING_ALERT_LIM,
  53. INA3221_REG_SHUNTV_SUM,
  54. INA3221_REG_SHUNTV_SUM_LIM,
  55. INA3221_REG_MASK_ENABLE,
  56. INA3221_REG_PWR_VALID_HI_LIM,
  57. INA3221_REG_PWR_VALID_LO_LIM,
  58. INA3221_REG_MANUF_ID = 0xFE,
  59. INA3221_REG_DIE_ID = 0xFF
  60. } ina3221_reg_t;
  61. // Conversion times
  62. typedef enum {
  63. INA3221_REG_CONF_CT_140US = 0,
  64. INA3221_REG_CONF_CT_204US,
  65. INA3221_REG_CONF_CT_332US,
  66. INA3221_REG_CONF_CT_588US,
  67. INA3221_REG_CONF_CT_1100US,
  68. INA3221_REG_CONF_CT_2116US,
  69. INA3221_REG_CONF_CT_4156US,
  70. INA3221_REG_CONF_CT_8244US
  71. } ina3221_conv_time_t;
  72. // Averaging modes
  73. typedef enum {
  74. INA3221_REG_CONF_AVG_1 = 0,
  75. INA3221_REG_CONF_AVG_4,
  76. INA3221_REG_CONF_AVG_16,
  77. INA3221_REG_CONF_AVG_64,
  78. INA3221_REG_CONF_AVG_128,
  79. INA3221_REG_CONF_AVG_256,
  80. INA3221_REG_CONF_AVG_512,
  81. INA3221_REG_CONF_AVG_1024
  82. } ina3221_avg_mode_t;
  83. typedef struct Beastdevices_INA3221 {
  84. ina3221_addr_t i2c_addr; /**< I2C address */
  85. I2CDriver *i2cd; /**< I2C driver */
  86. uint32_t shuntRes[INA3221_CH_NUM]; /**< Shunt resistance in mOhm */
  87. uint32_t filterRes[INA3221_CH_NUM]; /**< Series filter resistance in Ohm */
  88. } ina3221_t;
  89. // Gets a register value.
  90. uint16_t INA3221_getReg(ina3221_t * ina, const ina3221_reg_t reg);
  91. // Resets INA3221
  92. void INA3221_reset(ina3221_t * ina);
  93. // Sets operating mode to power-down
  94. void INA3221_setModePowerDown(ina3221_t * ina);
  95. // Sets operating mode to continious
  96. void INA3221_setModeContinious(ina3221_t * ina);
  97. // Sets operating mode to triggered (single-shot)
  98. void INA3221_setModeTriggered(ina3221_t * ina);
  99. // Enables shunt-voltage measurement
  100. void INA3221_setShuntMeasEnable(ina3221_t * ina);
  101. // Disables shunt-voltage mesurement
  102. void INA3221_setShuntMeasDisable(ina3221_t * ina);
  103. // Enables bus-voltage measurement
  104. void INA3221_setBusMeasEnable(ina3221_t * ina);
  105. // Disables bus-voltage measureement
  106. void INA3221_setBusMeasDisable(ina3221_t * ina);
  107. // Sets averaging mode. Sets number of samples that are collected
  108. // and averaged togehter.
  109. void INA3221_setAveragingMode(ina3221_t * ina, ina3221_avg_mode_t mode);
  110. // Sets bus-voltage conversion time.
  111. void INA3221_setBusConversionTime(ina3221_t * ina, ina3221_conv_time_t convTime);
  112. // Sets shunt-voltage conversion time.
  113. void INA3221_setShuntConversionTime(ina3221_t * ina, ina3221_conv_time_t convTime);
  114. // Sets power-valid upper-limit voltage. The power-valid condition
  115. // is reached when all bus-voltage channels exceed the value set.
  116. // When the powervalid condition is met, the PV alert pin asserts high.
  117. void INA3221_setPwrValidUpLimit(ina3221_t * ina, int16_t voltagemV);
  118. // Sets power-valid lower-limit voltage. If any bus-voltage channel drops
  119. // below the power-valid lower-limit, the PV alert pin pulls low.
  120. void INA3221_setPwrValidLowLimit(ina3221_t * ina, int16_t voltagemV);
  121. // Sets the value that is compared to the Shunt-Voltage Sum register value
  122. // following each completed cycle of all selected channels to detect
  123. // for system overcurrent events.
  124. void INA3221_setShuntSumAlertLimit(ina3221_t * ina, int32_t voltagemV);
  125. // Sets the current value that is compared to the sum all currents.
  126. // This function is a helper for setShuntSumAlertLim(). It onverts current
  127. // value to shunt voltage value.
  128. void INA3221_setCurrentSumAlertLimit(ina3221_t * ina, int32_t currentmA);
  129. // Enables warning alert latch.
  130. void INA3221_setWarnAlertLatchEnable(ina3221_t * ina);
  131. // Disables warning alert latch.
  132. void INA3221_setWarnAlertLatchDisable(ina3221_t * ina);
  133. // Enables critical alert latch.
  134. void INA3221_setCritAlertLatchEnable(ina3221_t * ina);
  135. // Disables critical alert latch.
  136. void INA3221_setCritAlertLatchDisable(ina3221_t * ina);
  137. // Reads flags from Mask/Enable register.
  138. // When Mask/Enable register is read, flags are cleared.
  139. // Use getTimingCtrlAlertFlag(), getPwrValidAlertFlag(),
  140. // getCurrentSumAlertFlag() and getConvReadyFlag() to get flags after
  141. // readFlags() is called.
  142. void INA3221_readFlags(ina3221_t * ina);
  143. // Gets timing-control-alert flag indicator.
  144. bool INA3221_getTimingCtrlAlertFlag(void);
  145. // Gets power-valid-alert flag indicator.
  146. bool INA3221_getPwrValidAlertFlag(void);
  147. // Gets summation-alert flag indicator.
  148. bool INA3221_getCurrentSumAlertFlag(void);
  149. // Gets Conversion-ready flag.
  150. bool INA3221_getConversionReadyFlag(void);
  151. // Gets manufacturer ID.
  152. // Should read 0x5449.
  153. uint16_t INA3221_getManufID(ina3221_t * ina);
  154. // Gets die ID.
  155. // Should read 0x3220.
  156. uint16_t INA3221_getDieID(ina3221_t * ina);
  157. // Enables channel measurements
  158. void INA3221_setChannelEnable(ina3221_t * ina, ina3221_ch_t channel);
  159. // Disables channel measurements
  160. void INA3221_setChannelDisable(ina3221_t * ina, ina3221_ch_t channel);
  161. // Sets warning alert shunt voltage limit
  162. void INA3221_setWarnAlertShuntLimit(ina3221_t * ina, ina3221_ch_t channel, int32_t voltageuV);
  163. // Sets critical alert shunt voltage limit
  164. void INA3221_setCritAlertShuntLimit(ina3221_t * ina, ina3221_ch_t channel, int32_t voltageuV);
  165. // Sets warning alert current limit
  166. void INA3221_setWarnAlertCurrentLimit(ina3221_t * ina, ina3221_ch_t channel, int32_t currentmA);
  167. // Sets critical alert current limit
  168. void INA3221_setCritAlertCurrentLimit(ina3221_t * ina, ina3221_ch_t channel, int32_t currentmA);
  169. // Includes channel to fill Shunt-Voltage Sum register.
  170. void INA3221_setCurrentSumEnable(ina3221_t * ina, ina3221_ch_t channel);
  171. // Excludes channel from filling Shunt-Voltage Sum register.
  172. void INA3221_setCurrentSumDisable(ina3221_t * ina, ina3221_ch_t channel);
  173. // Gets shunt voltage in uV.
  174. int32_t INA3221_getShuntVoltage(ina3221_t * ina, ina3221_ch_t channel);
  175. // Gets warning alert flag.
  176. bool INA3221_getWarnAlertFlag(ina3221_t * ina, ina3221_ch_t channel);
  177. // Gets critical alert flag.
  178. bool INA3221_getCritAlertFlag(ina3221_t * ina, ina3221_ch_t channel);
  179. // Estimates offset voltage added by the series filter resitors
  180. int32_t INA3221_estimateOffsetVoltage(ina3221_t * ina, ina3221_ch_t channel, uint32_t busVoltage);
  181. // Gets current in A.
  182. int32_t INA3221_getCurrent(ina3221_t * ina, ina3221_ch_t channel);
  183. // Gets current compensated with calculated offset voltage.
  184. int32_t INA3221_getCurrentCompensated(ina3221_t * ina, ina3221_ch_t channel);
  185. // Gets bus voltage in V.
  186. uint32_t INA3221_getVoltage(ina3221_t * ina, ina3221_ch_t channel);
  187. #endif