|
|
@@ -28,7 +28,7 @@ |
|
|
|
#include "protocol/protocol.h" |
|
|
|
#endif |
|
|
|
|
|
|
|
//#define REF2_DEBUG |
|
|
|
#define REF2_DEBUG 0 |
|
|
|
|
|
|
|
#define INA260_ADDRESS 0x4e |
|
|
|
#define LTC4162F_ADDRESS 0x68 |
|
|
@@ -159,8 +159,12 @@ void measure_cell_voltages_and_control_discharge() { |
|
|
|
spir[1] = 0xc7; |
|
|
|
|
|
|
|
spir[2] = 0xe1; // set cdc to 2 = continuous measurement |
|
|
|
spir[3] = discharge_bits&0xff; // first 8 bits of discharge switches |
|
|
|
spir[4] = (discharge_bits&0x700)>>8; // last 4 bits of discharge switches |
|
|
|
//spir[3] = discharge_bits&0xff; // first 8 bits of discharge switches |
|
|
|
//spir[4] = (discharge_bits&0x700)>>8; // last 4 bits of discharge switches |
|
|
|
|
|
|
|
spir[3] = 0x0; |
|
|
|
spir[4] = 0x0; |
|
|
|
|
|
|
|
spir[5] = 0x0; |
|
|
|
spir[6] = 0x0; |
|
|
|
spir[7] = 0x0; |
|
|
@@ -296,84 +300,21 @@ float chg_vin; |
|
|
|
float chg_vbat; |
|
|
|
|
|
|
|
void configure_charger(int charge_current) { |
|
|
|
// disable jeita |
|
|
|
i2c_write16_be(LTC4162F_ADDRESS, 0x29, 0); |
|
|
|
|
|
|
|
// set charge current |
|
|
|
i2c_write16_be(LTC4162F_ADDRESS, 0x1a, charge_current); |
|
|
|
|
|
|
|
// disable max_cv_time |
|
|
|
i2c_write16_be(LTC4162F_ADDRESS, 0x1d, 0); |
|
|
|
|
|
|
|
// set charge voltage |
|
|
|
// 27 = 30V |
|
|
|
// default 31 = 28.8V |
|
|
|
i2c_write16_be(LTC4162F_ADDRESS, 0x1b, 31); |
|
|
|
|
|
|
|
if (charge_current > 0) { |
|
|
|
// config_bits_reg: unsuspend charger |
|
|
|
i2c_write16_be(LTC4162F_ADDRESS, 0x14, 0); |
|
|
|
} else { |
|
|
|
// config_bits_reg: suspend charger |
|
|
|
i2c_write16_be(LTC4162F_ADDRESS, 0x14, 1<<5); |
|
|
|
} |
|
|
|
|
|
|
|
// set recharge threshold |
|
|
|
// signed 16-bit number, max 32767! default 17424 |
|
|
|
i2c_write16_be(LTC4162F_ADDRESS, 0x2e, 30000); |
|
|
|
|
|
|
|
// read some charger regs |
|
|
|
charger_state = i2c_read16_be(LTC4162F_ADDRESS, 0x34); |
|
|
|
charge_status = i2c_read16_be(LTC4162F_ADDRESS, 0x35); |
|
|
|
limit_alerts = i2c_read16_be(LTC4162F_ADDRESS, 0x36); |
|
|
|
charger_alerts = i2c_read16_be(LTC4162F_ADDRESS, 0x37); |
|
|
|
status_alerts = i2c_read16_be(LTC4162F_ADDRESS, 0x38); |
|
|
|
system_status = i2c_read16_be(LTC4162F_ADDRESS, 0x39); |
|
|
|
|
|
|
|
chg_vbat = i2c_read16_be(LTC4162F_ADDRESS, 0x3a); |
|
|
|
chg_vin = i2c_read16_be(LTC4162F_ADDRESS, 0x3b); |
|
|
|
float chg_vout = i2c_read16_be(LTC4162F_ADDRESS, 0x3c); |
|
|
|
//float chg_ibat = i2c_read16_be(LTC4162F_ADDRESS, 0x3d); |
|
|
|
//float chg_iin = i2c_read16_be(LTC4162F_ADDRESS, 0x3e); |
|
|
|
//int16_t chg_chem = (i2c_read16_be(LTC4162F_ADDRESS, 0x43) >> 8) & 0xf; |
|
|
|
//int16_t chg_cell_count = (i2c_read16_be(LTC4162F_ADDRESS, 0x43)) & 0xf; |
|
|
|
|
|
|
|
//uint16_t tchargetimer = i2c_read16_be(LTC4162F_ADDRESS, 0x30); |
|
|
|
//uint16_t tcvtimer = i2c_read16_be(LTC4162F_ADDRESS, 0x31); |
|
|
|
//uint16_t tabsorbtimer = i2c_read16_be(LTC4162F_ADDRESS, 0x32); |
|
|
|
|
|
|
|
chg_vin*=1.649; |
|
|
|
chg_vbat*=(8.0*0.1924); |
|
|
|
chg_vout*=1.653; |
|
|
|
|
|
|
|
#ifdef REF2_DEBUG |
|
|
|
sprintf(uartBuffer,"ltc state: %x status: %x system: %x\r\n",charger_state,charge_status,system_status); |
|
|
|
uartSend((uint8_t *)uartBuffer, strlen(uartBuffer)); |
|
|
|
#endif |
|
|
|
|
|
|
|
//sprintf(uartBuffer,"ltc vbat: %f vin: %f vout: %f\r\n",chg_vbat,chg_vin,chg_vout); |
|
|
|
//uartSend((uint8_t *)uartBuffer, strlen(uartBuffer)); |
|
|
|
|
|
|
|
//sprintf(uartBuffer,"ltc chemistry: %d cell count: %d\r\n",chg_chem,chg_cell_count); |
|
|
|
//uartSend((uint8_t *)uartBuffer, strlen(uartBuffer)); |
|
|
|
|
|
|
|
//sprintf(uartBuffer,"config: jeita: %d thermal_reg: %d ilim_reg: %d vin_uvcl: %d iin_limit: %d\r\n",charger_config&1, !!(charge_status&16), !!(charge_status&32), !!(charge_status&8), !!(charge_status&4)); |
|
|
|
//uartSend((uint8_t *)uartBuffer, strlen(uartBuffer)); |
|
|
|
|
|
|
|
//sprintf(uartBuffer,"tchgt: %d tcvt: %d tabsorbt: %d\r\n\r\n",tchargetimer,tcvtimer,tabsorbtimer); |
|
|
|
//uartSend((uint8_t *)uartBuffer, strlen(uartBuffer)); |
|
|
|
} |
|
|
|
|
|
|
|
void turn_som_power_on(void) { |
|
|
|
LPC_GPIO->CLR[1] = (1 << 16); // 3v3, low = on |
|
|
|
LPC_GPIO->CLR[0] = (1 << 20); // PCIe, low = on |
|
|
|
LPC_GPIO->SET[1] = (1 << 16); // 3v3, high = on |
|
|
|
// FIXME this turns 1v5 off :/ |
|
|
|
LPC_GPIO->SET[0] = (1 << 20); // PCIe, low = on |
|
|
|
LPC_GPIO->SET[1] = (1 << 15); // 5v, high = on |
|
|
|
LPC_GPIO->SET[1] = (1 << 19); // 1v2, high = on |
|
|
|
} |
|
|
|
|
|
|
|
void turn_som_power_off(void) { |
|
|
|
LPC_GPIO->CLR[1] = (1 << 19); // 1v2, high = on |
|
|
|
LPC_GPIO->CLR[1] = (1 << 15); // 5v, high = on |
|
|
|
LPC_GPIO->SET[0] = (1 << 20); // PCIe, low = on |
|
|
|
LPC_GPIO->SET[1] = (1 << 16); // 3v3, low = on |
|
|
|
LPC_GPIO->CLR[0] = (1 << 20); // PCIe, low = on |
|
|
|
LPC_GPIO->CLR[1] = (1 << 16); // 3v3, high = on |
|
|
|
|
|
|
|
// FIXME experiment: temp. disable charger to reset its timers |
|
|
|
configure_charger(0); |
|
|
@@ -423,10 +364,12 @@ void boardInit(void) |
|
|
|
LPC_GPIO->DIR[1] |= (1 << 15); |
|
|
|
// 3V3 rail transistor on/off |
|
|
|
LPC_GPIO->DIR[1] |= (1 << 16); |
|
|
|
// 1V2 regulator on/off |
|
|
|
LPC_GPIO->DIR[1] |= (1 << 19); |
|
|
|
// PCIe 1 power supply transistor |
|
|
|
LPC_GPIO->DIR[0] |= (1 << 20); |
|
|
|
|
|
|
|
turn_som_power_off(); |
|
|
|
|
|
|
|
turn_som_power_on(); |
|
|
|
|
|
|
|
uartInit(CFG_UART_BAUDRATE); |
|
|
|
i2cInit(I2CMASTER); |
|
|
@@ -439,16 +382,18 @@ void boardInit(void) |
|
|
|
// SPI chip select |
|
|
|
LPC_GPIO->DIR[1] |= (1 << 23); |
|
|
|
LPC_GPIO->SET[1] = (1 << 23); // active low |
|
|
|
|
|
|
|
//sprintf(uartBuffer, "\r\nMNT Reform 2.0 MCU initialized.\r\n"); |
|
|
|
//uartSend((uint8_t*)uartBuffer, strlen(uartBuffer)); |
|
|
|
|
|
|
|
#ifdef REF2_DEBUG |
|
|
|
sprintf(uartBuffer, "\r\nMNT Reform 2.0 MCU initialized.\r\n"); |
|
|
|
uartSend((uint8_t*)uartBuffer, strlen(uartBuffer)); |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
char remote_cmd = 0; |
|
|
|
uint8_t remote_arg = 0; |
|
|
|
unsigned char cmd_state = ST_EXPECT_DIGIT_0; |
|
|
|
unsigned int cmd_number = 0; |
|
|
|
int cmd_echo = 0; |
|
|
|
int cmd_echo = 1; |
|
|
|
|
|
|
|
void handle_commands() { |
|
|
|
if (!uartRxBufferDataPending()) return; |
|
|
@@ -608,17 +553,15 @@ void handle_commands() { |
|
|
|
int main(void) |
|
|
|
{ |
|
|
|
boardInit(); |
|
|
|
reset_discharge_bits(); |
|
|
|
//reset_discharge_bits(); |
|
|
|
|
|
|
|
state = ST_CHARGE; |
|
|
|
cycles_in_state = 0; |
|
|
|
|
|
|
|
delay(500); |
|
|
|
|
|
|
|
last_second = delayGetSecondsActive(); |
|
|
|
|
|
|
|
// WIP, not yet tested |
|
|
|
watchdog_setup(); |
|
|
|
//watchdog_setup(); |
|
|
|
//sprintf(uartBuffer, "\r\nwatchdog_setup() completed.\r\n"); |
|
|
|
//uartSend((uint8_t*)uartBuffer, strlen(uartBuffer)); |
|
|
|
|
|
|
@@ -636,12 +579,12 @@ int main(void) |
|
|
|
|
|
|
|
// charge current 2: ~0.2A |
|
|
|
|
|
|
|
watchdog_feed(); |
|
|
|
//watchdog_feed(); |
|
|
|
|
|
|
|
measure_and_accumulate_current(); |
|
|
|
measure_cell_voltages_and_control_discharge(); |
|
|
|
|
|
|
|
if (state == ST_CHARGE) { |
|
|
|
/*if (state == ST_CHARGE) { |
|
|
|
configure_charger(charge_current); |
|
|
|
|
|
|
|
if (cycles_in_state > 5) { |
|
|
@@ -692,7 +635,7 @@ int main(void) |
|
|
|
cycles_in_state = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}*/ |
|
|
|
|
|
|
|
handle_commands(); |
|
|
|
cur_second = delayGetSecondsActive(); |
|
|
|