#include "ti/driverlib/m0p/dl_core.h" #include "ti/driverlib/m0p/sysctl/dl_sysctl_mspm0g1x0x_g3x0x.h" #include "ti_msp_dl_config.h" #include "src/pi/i2c_pi_target.h" #include "src/controller/controller.h" #include "ti/driverlib/dl_i2c.h" #include "src/battery_data/battery.h" #include "src/cc_cv_charging.h" #include #include "src/battery_data/battery.h" #include "mock_setup.h" //define the varibales: volatile bool picommandPending = false; volatile bool watchdog_triggered= false; // Interrupt for I2C instance -> MCU to Target void I2C_1_INST_IRQHandler(void) { switch (DL_I2C_getPendingInterrupt(I2C_1_INST)) { case DL_I2C_IIDX_CONTROLLER_START: break; case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER: while(DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST) != true) { if(rxPacket.rxCount < rxPacket.rxLen){ //Get byte from the I2C RX FIFO of the target rxPacket.rxBuffer[rxPacket.rxCount++]= DL_I2C_receiveControllerData(I2C_1_INST); //rxPacket.rxCount++; }else{ DL_I2C_receiveControllerData(I2C_1_INST); } } if(rxPacket.rxCount >= rxPacket.rxLen){ DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP); rxPacket.rxComplete= true; } break; case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER: /* Fill TX FIFO with bytes to send */ //DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1); txPacket.txComplete= true; break; case DL_I2C_IIDX_CONTROLLER_STOP: rxPacket.rxComplete= true; txPacket.txComplete= true; DL_I2C_flushControllerRXFIFO(I2C_1_INST); DL_I2C_flushControllerTXFIFO(I2C_1_INST); break; case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST: break; case DL_I2C_IIDX_CONTROLLER_NACK: break; case DL_I2C_IIDX_TIMEOUT_A: case DL_I2C_IIDX_TIMEOUT_B: DL_I2C_flushTargetTXFIFO(I2C_1_INST); DL_I2C_flushTargetRXFIFO(I2C_1_INST); break; default: break; } } void I2C_0_INST_IRQHandler(void) { switch (DL_I2C_getPendingInterrupt(I2C_0_INST)) { case DL_I2C_IIDX_TARGET_START: DL_I2C_flushTargetTXFIFO(I2C_0_INST); break; case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER: if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) { return; } picommandPending = true; break; case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER: /* Fill TX FIFO with bytes to send */ picommandPending = true; break; case DL_I2C_IIDX_TARGET_STOP: picommandPending = true; DL_I2C_flushTargetTXFIFO(I2C_0_INST); DL_I2C_flushTargetRXFIFO(I2C_0_INST); break; case DL_I2C_IIDX_TARGET_ARBITRATION_LOST: break; default: break; } } int main(void) { SYSCFG_DL_init(); Battery_Init(); //dynamic addressing function call for Pi dynamic_gpio_addressing(); //Interrupt routine for Pi NVIC_EnableIRQ(I2C_0_INST_INT_IRQN); //Interrupt for target mcu NVIC_EnableIRQ(I2C_1_INST_INT_IRQN); while(1) { if(picommandPending) { //printf("Pi Interrupt Triggered.\n"); pi_i2c_mcu(); picommandPending = false; } //DL_SYSCTL_getPendingNonMaskableInterrupt(); for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){ //GET battery measurement from the Target getBatteryMeasurement(slot_id); //Reading battery state: Battery_StateCondition(slot_id); printf("Battery State: %d, Charging State:%d\n", battery_data[slot_id].battery_state, battery_data[slot_id].battery_charging_state); CC_CV_ControlCharging(slot_id); //If target received battery limits from Pi then start charging, else wait with state: 3:: /*if(battery_data[slot_id].batteryLimitReceived){ printf("Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, " "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u, Cycle Number: %u\n", slot_id, battery_data[slot_id].max_voltage, battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current, battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction, battery_data[slot_id].cycle_number); }*/ delay_cycles(MEASUREMENT_CHECK_INTERVAL); } } }