123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #include "ets_sys.h"
- #include "hal/max17043.h"
- #include "osapi.h"
- #include "driver/i2c_master.h"
- uint16_t ICACHE_FLASH_ATTR
- max17043_getVoltage(void)
- {
- max17043_i2c_init();
- uint16_t voltage;
-
- i2c_master_start();
- i2c_master_writeByte(WRITE_ADDRESS);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- i2c_master_writeByte(VCELL_H);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- i2c_master_start();
- i2c_master_writeByte(READ_ADDRESS);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- voltage = (i2c_master_readByte() << 4);
- i2c_master_send_ack();
- voltage |= (i2c_master_readByte() >> 4);
- i2c_master_send_nack();
- i2c_master_stop();
- #if MAX17043_DEBUG
- os_printf("Voltage: %d\r\n", voltage);
- #endif
- return voltage;
- }
- uint16_t ICACHE_FLASH_ATTR
- max17043_getSOC(void)
- {
- max17043_i2c_init();
- uint16_t soc;
- i2c_master_start();
- i2c_master_writeByte(WRITE_ADDRESS);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- i2c_master_writeByte(SOC_H);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- i2c_master_start();
- i2c_master_writeByte(READ_ADDRESS);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- soc = (i2c_master_readByte()<<8);
- i2c_master_send_ack();
- soc |= i2c_master_readByte();
- i2c_master_send_nack();
- i2c_master_stop();
- #if MAX17043_DEBUG
- os_printf("SOC: %d\r\n", soc);
- #endif
- return soc;
- }
- bool ICACHE_FLASH_ATTR
- max17043_sleep(uint8_t enable)
- {
- max17043_i2c_init();
- i2c_master_start();
- i2c_master_writeByte(WRITE_ADDRESS);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- i2c_master_writeByte(CONFIG_L);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- if (enable) {
- i2c_master_writeByte(0x80);
- }
- else {
- i2c_master_writeByte(0x00);
- }
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- i2c_master_stop();
- return 1;
- }
- bool ICACHE_FLASH_ATTR
- max17043_quickStart(void)
- {
- i2c_master_start();
- i2c_master_writeByte(WRITE_ADDRESS);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- i2c_master_writeByte(MODE_H);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- i2c_master_writeByte(0x40);
- if (!i2c_master_checkAck()){
- i2c_master_stop();
- }
- i2c_master_writeByte(0x00);
- if (!i2c_master_checkAck()) {
- i2c_master_stop();
- return 0;
- }
- i2c_master_stop();
- return 1;
- }
|