Browse Source

bring back 3v3 power switching; fix and enable watchdog reset; tweak OV and UV; minor cleanups

master
mntmn 2 months ago
parent
commit
cf670c3ec3
1 changed files with 57 additions and 68 deletions
  1. 57
    68
      src/boards/reform2/board_reform2.c

+ 57
- 68
src/boards/reform2/board_reform2.c View File

@@ -145,14 +145,13 @@ uint16_t discharge_bits = 0;
uint16_t overvoltage_bits = 0;
uint16_t undervoltage_bits = 0;
uint16_t missing_bits = 0;
uint8_t spir[64];

#define OVERVOLTAGE_VALUE 3.5
#define UNDERVOLTAGE_VALUE 2.7
#define OVERVOLTAGE_VALUE 3.6
#define UNDERVOLTAGE_VALUE 2.5
#define MISSING_VALUE 5

void measure_cell_voltages_and_control_discharge() {
uint8_t spir[64];

// delay is measured in "ticks", which are basically ms?

// 0x10: WRCFG, write config
@@ -173,60 +172,28 @@ void measure_cell_voltages_and_control_discharge() {
spir[8] = pec;
LPC_GPIO->CLR[1] = (1 << 23);
//delay(2);
ssp1Send(spir, 9);
//delay(2);
LPC_GPIO->SET[1] = (1 << 23);

// 0x10: STCVDC, start adc
spir[0] = 0x60;
spir[1] = 0xe7;
LPC_GPIO->CLR[1] = (1 << 23);
//delay(2);
ssp1Send(spir, 2);
//delay(2);
LPC_GPIO->SET[1] = (1 << 23);

delay(50); // FIXME tunable

// 0x4: PLADC, poll adc converter status
/*spir[0] = 0x40;
spir[1] = 0x07;
LPC_GPIO->CLR[1] = (1 << 23);
delay(2);
ssp1Send(spir, 2);
memset(spir, 0, 32);
ssp1Receive(spir, 2);
delay(2);
LPC_GPIO->SET[1] = (1 << 23);
sprintf(uartBuffer,"PLADC %02x %02x\r\n",
spir[0], spir[1]);
uartSend((uint8_t *)uartBuffer, strlen(uartBuffer));*/

//delay(20);
// 0x4: RDCV, read all cell voltages
spir[0] = 0x4;
spir[1] = 0xdc;
LPC_GPIO->CLR[1] = (1 << 23);
//delay(2);
ssp1Send(spir, 2);
memset(spir, 0, 32);
ssp1Receive(spir, 19);
//delay(2);
LPC_GPIO->SET[1] = (1 << 23);

/*sprintf(uartBuffer,"CVR00 %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
spir[0], spir[1], spir[2], spir[3], spir[4], spir[5], spir[6], spir[7]);
uartSend((uint8_t *)uartBuffer, strlen(uartBuffer));
sprintf(uartBuffer,"CVR08 %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
spir[8], spir[9], spir[10], spir[11], spir[12], spir[13], spir[14], spir[15]);
uartSend((uint8_t *)uartBuffer, strlen(uartBuffer));
sprintf(uartBuffer,"CVR10 %02x %02x %02x %02x %02x %02x %02x %02x\r\n\r\n",
spir[16], spir[17], spir[18], spir[19], spir[20], spir[21], spir[22], spir[23]);
uartSend((uint8_t *)uartBuffer, strlen(uartBuffer));*/

int j=0;
for (int i=0; i<8; i+=2) {
cells_v[i] = ((float)((spir[j]|((spir[j+1]&0xf)<<8))-512)) * 1.5 / 1000.0;
@@ -345,22 +312,22 @@ void configure_charger(int charge_current) {
i2c_write16_be(LTC4162F_ADDRESS, 0x2e, 40000);
// read some charger regs
uint16_t vcharge_setting = i2c_read16_be(LTC4162F_ADDRESS, 0x1b) & 0x1f; // [4:0]
//uint16_t vcharge_setting = i2c_read16_be(LTC4162F_ADDRESS, 0x1b) & 0x1f; // [4:0]
charger_state = i2c_read16_be(LTC4162F_ADDRESS, 0x34);
charge_status = i2c_read16_be(LTC4162F_ADDRESS, 0x35);
system_status = i2c_read16_be(LTC4162F_ADDRESS, 0x39);
uint16_t charger_config = i2c_read16_be(LTC4162F_ADDRESS, 0x29);
//uint16_t charger_config = i2c_read16_be(LTC4162F_ADDRESS, 0x29);
float chg_vbat = i2c_read16_be(LTC4162F_ADDRESS, 0x3a);
float 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);
//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);
@@ -371,27 +338,35 @@ void configure_charger(int charge_current) {
uartSend((uint8_t *)uartBuffer, strlen(uartBuffer));
#endif
sprintf(uartBuffer,"ltc vbat: %f vin: %f vout: %f\r\n",chg_vbat,chg_vin,chg_vout);
//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);
//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));
//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);
//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->SET[1] = (1 << 15); // high = on
LPC_GPIO->CLR[0] = (1 << 20); // low = on
LPC_GPIO->SET[1] = (1 << 15); // 5v, high = on
LPC_GPIO->CLR[0] = (1 << 20); // PCIe, low = on
LPC_GPIO->CLR[1] = (1 << 16); // 3v3, low = on
}

void turn_som_power_off(void) {
LPC_GPIO->CLR[1] = (1 << 15); // high = on
LPC_GPIO->SET[0] = (1 << 20); // low = 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
}

void brownout_setup(void) {
// Set brownout threshold to 2.63-2.71V (highest level)
// and enable brownout reset
LPC_SYSCON->BODCTRL = 0x3 | (1<<4);
}

void watchdog_feed(void) {
@@ -399,19 +374,27 @@ void watchdog_feed(void) {
LPC_WWDT->FEED = 0x55;
}

#define WWDT_WDMOD_WDEN ((uint32_t) (1 << 0))
#define WWDT_WDMOD_WDRESET ((uint32_t) (1 << 1))

void watchdog_setup(void) {
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<15); // WWDT enable
LPC_SYSCON->WDTOSCCTRL =
(0x1<<5) | // FREQSEL 0.6MHz
0; // DIVSEL 2 (0+1)*2
(1<<5) | // FREQSEL 0.6MHz
31; // DIVSEL 64 (31+1)*2
LPC_SYSCON->PDRUNCFG &= ~(1<<6); // WDTOSC_PD disable
LPC_WWDT->CLKSEL = 1; // WDOSC
LPC_SYSCON->PDRUNCFG &= ~(6<<15); // WDTOSC_PD disable

LPC_SYSCON->SYSAHBCLKCTRL |= (1<<15); // WWDT enable
LPC_WWDT->TC = 0xffff; // timeout counter

LPC_WWDT->MOD = 0;
LPC_WWDT->MOD |= WWDT_WDMOD_WDRESET; // enable WDRESET (watchdog resets system)
LPC_WWDT->MOD |= WWDT_WDMOD_WDEN; // watchdog enable
LPC_WWDT->COUNT = 0xffff; // timeout counter
LPC_WWDT->MOD |= (1<<1); // enable WDRESET (watchdog resets system)
LPC_WWDT->MOD |= 1; // watchdog enable
watchdog_feed();
}

void boardInit(void)
@@ -434,20 +417,20 @@ void boardInit(void)

// 5V regulator on/off
LPC_GPIO->DIR[1] |= (1 << 15);
turn_som_power_on(); // FIXME
//LPC_GPIO->SET[1] = (1 << 15); // high = on

// FIXME
// 3V3 rail transistor on/off
LPC_GPIO->DIR[1] |= (1 << 16);
LPC_GPIO->CLR[1] = (1 << 16); // low = on

// PCIe 1 power supply transistor
LPC_GPIO->DIR[0] |= (1 << 20);
LPC_GPIO->CLR[0] = (1 << 20); // low = on
//LPC_GPIO->CLR[0] = (1 << 20); // low = on

turn_som_power_off(); // FIXME

sprintf(uartBuffer, "\r\nMNT Reform 2.0 MCU initialized.\r\n");
uartSend((uint8_t*)uartBuffer, strlen(uartBuffer));
//sprintf(uartBuffer, "\r\nMNT Reform 2.0 MCU initialized.\r\n");
//uartSend((uint8_t*)uartBuffer, strlen(uartBuffer));
}

char remote_cmd = 0;
@@ -532,8 +515,12 @@ void handle_commands() {
// toggle system 5V power
if (cmd_number == 0) {
turn_som_power_off();
sprintf(uartBuffer,"system: off\r\n");
uartSend((uint8_t*)uartBuffer, strlen(uartBuffer));
} else {
turn_som_power_on();
sprintf(uartBuffer,"system: on\r\n");
uartSend((uint8_t*)uartBuffer, strlen(uartBuffer));
}
}
else if (remote_cmd == 'a') {
@@ -620,7 +607,9 @@ int main(void)
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));
while (1)
{
@@ -636,7 +625,7 @@ int main(void)

// charge current 2: ~0.2A

//watchdog_feed();
watchdog_feed();

measure_and_accumulate_current();
measure_cell_voltages_and_control_discharge();

Loading…
Cancel
Save