#include "i2c_target.h" #include "battery.h" #include "adc.h" #include "ti/driverlib/dl_i2c.h" #include "ti_msp_dl_config.h" #include #include //Global buffer for I2C BatteryData battery_data; BatteryLimitMsg battery_limits; //Flag for Pi INTERRUPTS: volatile bool piRxComplete; volatile bool piTxComplete; uint8_t piTxPacket[I2C_TX_MAX_PACKET_SIZE_PI]; uint8_t piRxPacket[I2C_RX_MAX_PACKET_SIZE_PI]; uint32_t piTxLen, piTxCount; uint32_t piRxLen, piRxCount; /* - command: as defined in the Wiki for Pi 0x01, 0x02, 0x03, refer to i2c_target.h file - slot_id: battery slot numner from 0 to NUM_SLOTS-1 - data: pointer to SET battery limits - len: length of the data */ void Battery_StateUpdate(uint8_t slot_id){ uint16_t voltage_mv= batteries[slot_id].voltage; uint16_t min_voltage= batteries[slot_id].min_voltage; uint16_t max_voltage= batteries[slot_id].max_voltage; //Testing: if(voltage_mv< 500){ batteries[slot_id].state= STATE_EMPTY; } else if(voltage_mv>=500 && voltage_mv< 3000){ batteries[slot_id].state= STATE_BATTERY_DETECTED; } else if(voltage_mv >=3000 && voltage_mv< 4200){ batteries[slot_id].state= STATE_MEASUREMENT_IN_PROGRESS; } // once MCU is done reading ADC: else if(!(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS)) { batteries[slot_id].state= STATE_MEASUREMENT_DONE; } else{ batteries[slot_id].state= STATE_OVERCHARGING; } } void ProcessI2CCommand(uint8_t command, uint8_t slot_id, ADC_PARAMS params) { switch(command) { case CMD_GET_BATTERY_STATUS: for(uint8_t slot=0; slot< NUM_SLOTS; slot++){ Battery_StateUpdate(slot); DL_I2C_enableInterrupt(I2C_target_INST, DL_I2C_INTERRUPT_TARGET_TXFIFO_TRIGGER); while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY); } break; case CMD_GET_BATTERY_DATA: piTxCount= 0; piTxComplete= false; // Create a data message and transmit it Battery_UpdateCurrentVoltage(params); battery_data.slot_id= slot_id; battery_data.voltage= batteries[slot_id].voltage; battery_data.current= 0; battery_data.temperature=0; printf("Sending Battery Data: Slot %d | Voltage %dmV| Current %d| Temperature %d.\n", slot_id, battery_data.voltage, battery_data.current, battery_data.temperature); while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY); //Casting the struct pointer to byte array: (uint8_t*) DL_I2C_enableInterrupt(I2C_target_INST, DL_I2C_INTERRUPT_TARGET_TXFIFO_TRIGGER); DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t*)&battery_data, sizeof(BatteryData)); while(!piTxComplete); while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY); break; case CMD_SET_BATTERY_LIMIT: batteries[slot_id].min_voltage= battery_limits.min_voltage; batteries[slot_id].max_voltage= battery_limits.max_voltage; batteries[slot_id].capacitance= battery_limits.capacitance; batteries[slot_id].cut_off_current= battery_limits.cut_off_current; break; } }