|
|
@@ -11,56 +11,6 @@
|
|
|
|
|
|
volatile bool mcu_CommandPending= false;
|
|
|
|
|
|
-/*
|
|
|
-Scans all the addresses of the peripherals:
|
|
|
-*/
|
|
|
-/*void I2C_scanBus(I2C_Regs *i2c) {
|
|
|
- printf("1");
|
|
|
- // **Step 1: Reset I2C Controller if Busy**
|
|
|
- if (DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
|
|
|
- printf("I2C Bus Busy! Resetting I2C Controller...\n");
|
|
|
- DL_I2C_disableController(i2c); // Disable I2C
|
|
|
- delay_cycles(20000);
|
|
|
- DL_I2C_enableController(i2c); // Re-enable I2C
|
|
|
- delay_cycles(20000);
|
|
|
- }
|
|
|
- uint32_t i2c_status;
|
|
|
- // **Step 2: Scan I2C Bus**
|
|
|
- for (uint8_t addr = 0x08; addr < 0x78; addr++) { // Valid I2C Address Range
|
|
|
- printf("Scanning 0x%02X\n", addr);
|
|
|
- DL_I2C_startControllerTransfer(i2c, addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
|
|
|
- delay_cycles(5000);
|
|
|
-
|
|
|
- if (addr != 0x60 && addr != 0x68) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- i2c_status = DL_I2C_getControllerStatus(i2c);
|
|
|
- printf("DL_I2C_getControllerStatus(i2c): %d\n", i2c_status);
|
|
|
- printf("busy?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_BUSY));
|
|
|
- printf("error?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_ERROR));
|
|
|
- printf("addr_ack?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_ADDR_ACK));
|
|
|
- printf("data_ack?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_DATA_ACK));
|
|
|
- printf("arb_lost?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_ARBITRATION_LOST));
|
|
|
- printf("idle?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_IDLE));
|
|
|
- printf("busy_bus?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_BUSY_BUS));
|
|
|
-
|
|
|
- if (!(DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_ERROR)) {
|
|
|
- printf("Device found at: 0x%02X\n", addr);
|
|
|
- DL_I2C_disableController(i2c);
|
|
|
- DL_I2C_enableController(i2c);
|
|
|
- }else {
|
|
|
- // Clear the error by resetting the I2C controller
|
|
|
- printf("Device not found...\n");
|
|
|
- DL_I2C_disableController(i2c);
|
|
|
- DL_I2C_enableController(i2c);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //printf("I2C Scan Complete!\n");
|
|
|
-}
|
|
|
-*/
|
|
|
-
|
|
|
void I2C_controller_INST_IRQHandler(void) {
|
|
|
// printf("I2C Interrupt Triggered to ADC!\n");
|
|
|
switch (DL_I2C_getPendingInterrupt(I2C_controller_INST)) {
|
|
|
@@ -155,7 +105,6 @@ int main(void)
|
|
|
{
|
|
|
SYSCFG_DL_init();
|
|
|
battery_slotmgr.init();
|
|
|
- //I2C_scanBus(I2C_controller_INST);
|
|
|
NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
|
|
|
NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
|
|
|
|
|
|
@@ -172,8 +121,16 @@ int main(void)
|
|
|
// step 1: update the voltage readings
|
|
|
battery_slotmgr.read_state(slot);
|
|
|
|
|
|
- // step 2: control loop to adjust the dac / adc values
|
|
|
- battery_slotmgr.adjust_current(slot);
|
|
|
+ // step 2: control loop to adjust the dac / adc values,
|
|
|
+ // but only if no error happens
|
|
|
+ // (0x80 is the error flag of the state)
|
|
|
+ if (*battery_slots[i].state & 0x80 == 0) {
|
|
|
+ battery_slotmgr.adjust_current(slot);
|
|
|
+ } else {
|
|
|
+ battery_slotmgr.disable(slot);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
delay_cycles(DELAY_CYCLE);
|