Skip to content

Commit

Permalink
fix:Add bq2722 reset
Browse files Browse the repository at this point in the history
  • Loading branch information
ShallowGreen123 committed Dec 11, 2024
1 parent 31fa72a commit a2da937
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
2 changes: 2 additions & 0 deletions examples/factory_test/factory_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ void setup(void)
PPM.enableADCMeasure();
PPM.enableCharge();
}

bq27220.reset();

if(lora_ret)
lora_init();
Expand Down
89 changes: 86 additions & 3 deletions examples/factory_test/peripheral/bq27220.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Arduino.h"
#include <Wire.h>
#include "bq27220.h"
#include "bq27220_reg.h"

#define DEFAULT_SCL 18
#define DEFAULT_SDA 8
Expand All @@ -15,7 +16,17 @@
#define BQ27220_I2C_ADDRESS 0x55

// device id
#define BQ27220_DEVICE_ID 0x0220
#define BQ27220_ID (0x0220u)

/** Timeout for common operations. */
#define BQ27220_TIMEOUT_COMMON_US (2000000u)

/** Timeout cycle interval */
#define BQ27220_TIMEOUT_CYCLE_INTERVAL_US (1000u)

/** Timeout cycles count helper */
#define BQ27220_TIMEOUT(timeout_us) ((timeout_us) / (BQ27220_TIMEOUT_CYCLE_INTERVAL_US))


// commands
#define BQ27220_COMMAND_CONTROL 0X00 // Control()
Expand All @@ -38,8 +49,6 @@
#define BQ27220_COMMAND_RAW_CURR 0X7A
#define BQ27220_COMMAND_RAW_VOLT 0X7C

// sub-command of control


enum CURR_MODE{
CURR_RAW,
Expand Down Expand Up @@ -78,6 +87,25 @@ union battery_state {
uint16_t full;
};

typedef union OperationStatus{
struct __reg
{
// Low byte, Low bit first
bool CALMD : 1; /**< Calibration mode enabled */
uint8_t SEC : 2; /**< Current security access */
bool EDV2 : 1; /**< EDV2 threshold exceeded */
bool VDQ : 1; /**< Indicates if Current discharge cycle is NOT qualified or qualified for an FCC updated */
bool INITCOMP : 1; /**< gauge initialization is complete */
bool SMTH : 1; /**< RemainingCapacity is scaled by smooth engine */
bool BTPINT : 1; /**< BTP threshold has been crossed */
// High byte, Low bit first
uint8_t RSVD1 : 2; /**< Reserved */
bool CFGUPDATE : 1; /**< Gauge is in CONFIG UPDATE mode */
uint8_t RSVD0 : 5; /**< Reserved */
} reg;
uint16_t full;
} BQ27220OperationStatus;

class BQ27220{
public:
BQ27220() : addr{BQ27220_I2C_ADDRESS}, wire(&Wire), scl(DEFAULT_SCL), sda(DEFAULT_SDA)
Expand Down Expand Up @@ -165,6 +193,61 @@ class BQ27220{
return 0;
}

bool getOperationStatus(BQ27220OperationStatus *oper_sta)
{
bool result = false;
uint16_t data = ReadRegU16(CommandOperationStatus);
if(data != 0)
{
(*oper_sta).full = data;
result = true;
}
return result;
}

bool reset(void)
{
bool result = false;
BQ27220OperationStatus operat = {0};
do{
controlSubCmd(Control_RESET);
delay(10);

uint32_t timeout = BQ27220_TIMEOUT(BQ27220_TIMEOUT_COMMON_US);
while (--timeout)
{
if(!getOperationStatus(&operat)){
Serial.printf("Failed to get operation status, retries left %lu\n", timeout);
}else if(operat.reg.INITCOMP == true){
break;
}
delay(2);
}
if(timeout == 0) {
Serial.println("INITCOMP timeout after reset");
break;
}
Serial.printf("Cycles left: %lu\n", timeout);
result = true;
} while(0);
return result;
}

bool controlSubCmd(uint16_t sub_cmd)
{
uint8_t msb = (sub_cmd >> 8);
uint8_t lsb = (sub_cmd & 0x00FF);
uint8_t buf[2] = { lsb, msb };
i2cWriteBytes(CommandControl, buf, 2);
return true;
}

uint16_t ReadRegU16(uint16_t subAddress) {
uint8_t data[2];
i2cReadBytes(subAddress, data, 2);
return ((uint16_t) data[1] << 8) | data[0];
}

uint16_t readCtrlWord(uint16_t fun) {
uint8_t msb = (fun >> 8);
uint8_t lsb = (fun & 0x00FF);
Expand Down
2 changes: 1 addition & 1 deletion examples/factory_test/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ void batt_timer_event(lv_timer_t *t)
lv_label_set_text(batt_label, "BQ25896");

lv_label_set_text_fmt(batt_line[0], "VBUS --- %3.2f | VSYS --- %3.2f", (PPM.getVbusVoltage() *1.0 / 1000.0), (PPM.getSystemVoltage() * 1.0 / 1000.0));
lv_label_set_text_fmt(batt_line[1], "VBAT --- %3.2f | ICHG --- %3.2f", (PPM.getBattVoltage() * 1.0 / 1000.0), (PPM.getChargeCurrent()));
lv_label_set_text_fmt(batt_line[1], "VBAT --- %3.2f | ICHG --- %3.1f", (PPM.getBattVoltage() *1.0 / 1000.0), (PPM.getChargeCurrent() * 1.0));

lv_snprintf(buf, 16, "%.2f", (PPM.getChargeTargetVoltage() * 1.0 / 1000.0));
battery_set_line(batt_line[2], "VBAT Target:", buf);
Expand Down
2 changes: 1 addition & 1 deletion examples/factory_test/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "peripheral/peripheral.h"


#define T_EMBED_CC1101_SF_VER "v1.0 24.12.03"
#define T_EMBED_CC1101_SF_VER "v1.0 20241211"

// The default is landscape screen, HEIGHT and WIDTH swap
#define DISPALY_WIDTH TFT_HEIGHT
Expand Down

0 comments on commit a2da937

Please sign in to comment.