max17043.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /******************************************************************************
  2. * 2016 ideasX (Tyler Berezowsky)
  3. *
  4. * FileName: MAX17043.h
  5. *
  6. * Description: i2c driver for MAX17042 gas gauge IC
  7. * Needs to implement code to resent/re-request data based on
  8. * ack. or nack.
  9. *
  10. * Modification history:
  11. * 2016/3/15, v1.0 created this file
  12. *******************************************************************************/
  13. #include "ets_sys.h"
  14. #include "hal/max17043.h"
  15. #include "osapi.h"
  16. #include "driver/i2c_master.h"
  17. /******************************************************************************
  18. * FunctionName : max17042_get_voltage()
  19. * Description : get 12-bit ADC voltage value from gas gauge IC
  20. VCELL FORMAT (MSB first)
  21. [d|d|d|d|d|d|d|d] [d|d|d|d|0|0|0|0]
  22. d = vaild data
  23. 0 = nothing
  24. resolution = 1.25mV
  25. NOTE: 500ms after POR or sleep for vaild value
  26. * Parameters : NONE
  27. * Returns : uint16_t, 0 if failed
  28. *******************************************************************************/
  29. uint16_t ICACHE_FLASH_ATTR
  30. max17043_getVoltage(void)
  31. {
  32. max17043_i2c_init();
  33. uint16_t voltage;
  34. //i2c_master_init();
  35. i2c_master_start();
  36. i2c_master_writeByte(WRITE_ADDRESS);
  37. if (!i2c_master_checkAck()) {
  38. i2c_master_stop();
  39. return 0;
  40. }
  41. i2c_master_writeByte(VCELL_H);
  42. if (!i2c_master_checkAck()) {
  43. i2c_master_stop();
  44. return 0;
  45. }
  46. i2c_master_start();
  47. i2c_master_writeByte(READ_ADDRESS);
  48. if (!i2c_master_checkAck()) {
  49. i2c_master_stop();
  50. return 0;
  51. }
  52. voltage = (i2c_master_readByte() << 4);
  53. i2c_master_send_ack();
  54. voltage |= (i2c_master_readByte() >> 4);
  55. i2c_master_send_nack();
  56. i2c_master_stop();
  57. #if MAX17043_DEBUG
  58. os_printf("Voltage: %d\r\n", voltage);
  59. #endif
  60. return voltage;
  61. }
  62. /******************************************************************************
  63. * FunctionName : max17042_getSOC
  64. * Description : get 16-bit SOC value from gas gauge IC
  65. SOC FORMAT
  66. [2^7|2^6|2^5|2^4|...|2^0][2^-1|2^-2|...|2^-8]
  67. Resolution 1.0%
  68. * Parameters : NONE
  69. * Returns : uint16_t, 0 if failed
  70. *******************************************************************************/
  71. uint16_t ICACHE_FLASH_ATTR
  72. max17043_getSOC(void)
  73. {
  74. max17043_i2c_init();
  75. uint16_t soc;
  76. i2c_master_start(); // sent srt. cond.
  77. i2c_master_writeByte(WRITE_ADDRESS); // send w. addr.
  78. if (!i2c_master_checkAck()) {
  79. i2c_master_stop();
  80. return 0;
  81. }
  82. i2c_master_writeByte(SOC_H); // sent reg. addr. to be read
  83. if (!i2c_master_checkAck()) {
  84. i2c_master_stop();
  85. return 0;
  86. }
  87. i2c_master_start();
  88. i2c_master_writeByte(READ_ADDRESS); // send r. addr.
  89. if (!i2c_master_checkAck()) {
  90. i2c_master_stop();
  91. return 0;
  92. }
  93. soc = (i2c_master_readByte()<<8); // read VCELL_H into voltage
  94. i2c_master_send_ack(); // sent ack.
  95. soc |= i2c_master_readByte(); // read VCELL_L into voltage
  96. i2c_master_send_nack(); // send nack.
  97. i2c_master_stop(); // send stop cond.
  98. #if MAX17043_DEBUG
  99. os_printf("SOC: %d\r\n", soc);
  100. #endif
  101. return soc;
  102. }
  103. /******************************************************************************
  104. * FunctionName : max17042_sleep
  105. * Description : sets gas gauge IC to sleep
  106. * Parameters : uint8_t enable, true: set to sleep, false: wake from sleep
  107. * Returns : Success 1, Fail 0
  108. *******************************************************************************/
  109. bool ICACHE_FLASH_ATTR
  110. max17043_sleep(uint8_t enable)
  111. {
  112. max17043_i2c_init();
  113. i2c_master_start();
  114. i2c_master_writeByte(WRITE_ADDRESS);
  115. if (!i2c_master_checkAck()) {
  116. i2c_master_stop();
  117. return 0;
  118. }
  119. i2c_master_writeByte(CONFIG_L);
  120. if (!i2c_master_checkAck()) {
  121. i2c_master_stop();
  122. return 0;
  123. }
  124. if (enable) {
  125. i2c_master_writeByte(0x80);
  126. }
  127. else {
  128. i2c_master_writeByte(0x00);
  129. }
  130. if (!i2c_master_checkAck()) {
  131. i2c_master_stop();
  132. return 0;
  133. }
  134. i2c_master_stop();
  135. return 1;
  136. }
  137. /******************************************************************************
  138. * FunctionName : max17042_quick_start
  139. * Description : soft reset gas guage IC to fix battery calculation
  140. * Parameters : NONE
  141. * Returns : Success 1, Failed o
  142. *******************************************************************************/
  143. bool ICACHE_FLASH_ATTR
  144. max17043_quickStart(void)
  145. {
  146. i2c_master_start();
  147. i2c_master_writeByte(WRITE_ADDRESS);
  148. if (!i2c_master_checkAck()) {
  149. i2c_master_stop();
  150. return 0;
  151. }
  152. i2c_master_writeByte(MODE_H);
  153. if (!i2c_master_checkAck()) {
  154. i2c_master_stop();
  155. return 0;
  156. }
  157. i2c_master_writeByte(0x40);
  158. if (!i2c_master_checkAck()){
  159. i2c_master_stop();
  160. }
  161. i2c_master_writeByte(0x00);
  162. if (!i2c_master_checkAck()) {
  163. i2c_master_stop();
  164. return 0;
  165. }
  166. i2c_master_stop();
  167. return 1;
  168. }