|
@@ -27,78 +27,63 @@ void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_
|
|
|
DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-//Generic Function to send data to the target controller using I2C:
|
|
|
|
|
-//Format: command + ((slot_id) + data (optional))
|
|
|
|
|
-bool controller_GetCommandRequest(uint8_t const TARGET_ADDRESS, uint8_t slot_id, uint8_t *data, uint8_t data_len){
|
|
|
|
|
|
|
+
|
|
|
|
|
+//Get battery measurement: Voltage, Current, Temperature including slot state:
|
|
|
|
|
+bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
|
|
|
|
|
+
|
|
|
|
|
+ //Initializing BatteryMeasurement structure from battery.h of size 8
|
|
|
|
|
+ BatteryMeasurement measurement;
|
|
|
|
|
+ uint8_t rx_index=0;
|
|
|
|
|
|
|
|
//Write Command to the target
|
|
//Write Command to the target
|
|
|
- //Set the command in the tx buffer in bit masked format to the target
|
|
|
|
|
- uint8_t command= CMD_GET_MEASUREMENT|(slot_id & 0x0F); //shift slot_id to the left by 4 bits
|
|
|
|
|
|
|
+ //Set the command in the tx buffer in bit masked format to the target: Upper Nibble-> Slot and Lower Nibbel -> Command
|
|
|
|
|
+ tx_packet.txBuffer[0]= (slot_id << 4)|(CMD_GET_MEASUREMENT & 0x0F); //shift slot_id to the left by 4 bits
|
|
|
|
|
+
|
|
|
|
|
+ //Send command bytes to the target
|
|
|
|
|
+ DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
|
|
|
DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
|
|
DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
|
|
|
- DL_I2C_fillControllerTXFIFO(I2C_1_INST, &command, 1);
|
|
|
|
|
- printf("[MCU] GET Bitmasked Command:: 0x%02X\n", command);
|
|
|
|
|
|
|
+ printf("[MCU] TX Packet Sent:: 0x%02X\n", tx_packet.txBuffer[0]);
|
|
|
|
|
+ DL_I2C_fillControllerTXFIFO(I2C_1_INST, tx_packet.txBuffer, 1);
|
|
|
|
|
+
|
|
|
|
|
+ // Wait for the write transaction to complete
|
|
|
|
|
+ uint32_t timeout = 100000;
|
|
|
|
|
+ while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout > 0) {
|
|
|
|
|
+ timeout--;
|
|
|
|
|
+ }
|
|
|
|
|
+ //If I2C Bus is stuck then reset the controller:
|
|
|
if(DL_I2C_getControllerStatus(I2C_1_INST)& (DL_I2C_CONTROLLER_STATUS_ERROR)){
|
|
if(DL_I2C_getControllerStatus(I2C_1_INST)& (DL_I2C_CONTROLLER_STATUS_ERROR)){
|
|
|
printf("I2C Write Error: Bus is stuck\n");
|
|
printf("I2C Write Error: Bus is stuck\n");
|
|
|
DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ //Clear RX FIFO for any stale data and prepare for receiving data
|
|
|
|
|
+ //DL_I2C_flushControllerRXFIFO(I2C_1_INST);
|
|
|
|
|
+
|
|
|
//Receive the data from the target:
|
|
//Receive the data from the target:
|
|
|
- uint8_t rx_index=0;
|
|
|
|
|
|
|
+ //BatteryMeasurement size is 12 similar to the target side
|
|
|
DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, sizeof(BatteryMeasurement));
|
|
DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, sizeof(BatteryMeasurement));
|
|
|
- while(rx_index < data_len){
|
|
|
|
|
|
|
+ while((rx_index < sizeof(BatteryMeasurement)) && timeout > 0){
|
|
|
if(!DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST)){
|
|
if(!DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST)){
|
|
|
rx_packet.rxBuffer[rx_index]= DL_I2C_receiveTargetData(I2C_1_INST);
|
|
rx_packet.rxBuffer[rx_index]= DL_I2C_receiveTargetData(I2C_1_INST);
|
|
|
- printf("Received Bytes[%d]: 0x%02X\n", rx_index, rx_packet.rxBuffer[rx_index]);
|
|
|
|
|
|
|
+ //printf("Received Bytes[%d]: 0x%02X\n", rx_index, buffer[rx_index]);
|
|
|
rx_index++;
|
|
rx_index++;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
- printf("[I2C] Successfully read %d bytes from target 0x%02X\n", data_len, TARGET_ADDRESS);
|
|
|
|
|
- return true;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-//Get battery measurement: Voltage, Current and Temperature:
|
|
|
|
|
-bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot_id, BatteryMeasurement *measurement){
|
|
|
|
|
- //Write Command to the target
|
|
|
|
|
- //Set the command in the tx buffer in bit masked format to the target
|
|
|
|
|
- uint8_t command= CMD_GET_MEASUREMENT|(slot_id & 0x0F); //shift slot_id to the left by 4 bits
|
|
|
|
|
- DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
|
|
|
|
|
- DL_I2C_fillControllerTXFIFO(I2C_1_INST, &command, 1);
|
|
|
|
|
- printf("[MCU] GET Bitmasked Command:: 0x%02X\n", command);
|
|
|
|
|
- if(DL_I2C_getControllerStatus(I2C_1_INST)& (DL_I2C_CONTROLLER_STATUS_ERROR)){
|
|
|
|
|
- printf("I2C Write Error: Bus is stuck\n");
|
|
|
|
|
- DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- //Receive the data from the target:
|
|
|
|
|
-
|
|
|
|
|
- DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, sizeof(BatteryMeasurement));
|
|
|
|
|
- uint8_t *dest= (uint8_t *)measurement;
|
|
|
|
|
- uint8_t rx_index=0;
|
|
|
|
|
- while(rx_index < sizeof(BatteryMeasurement)){
|
|
|
|
|
- if(!DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST)){
|
|
|
|
|
- dest[rx_index++]= DL_I2C_receiveTargetData(I2C_1_INST);
|
|
|
|
|
- //printf("Received Bytes[%d]: 0x%02X\n", rx_index, rx_packet.rxBuffer[rx_index]);
|
|
|
|
|
- //rx_index++;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ printf("index:%d\n", rx_index);
|
|
|
|
|
+ //Check if all the data is received then store the battery limits in BatteryInfo struct:
|
|
|
|
|
+ if(rx_index== (sizeof(BatteryMeasurement))){
|
|
|
|
|
+ measurement.voltage= rx_packet.rxBuffer[0] | (rx_packet.rxBuffer[1] << 8);
|
|
|
|
|
+ measurement.current= rx_packet.rxBuffer[2]|(rx_packet.rxBuffer[3] << 8);
|
|
|
|
|
+ measurement.temperature = rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8);
|
|
|
|
|
+ measurement.slot_state = (SlotState)(rx_packet.rxBuffer[6]);
|
|
|
}
|
|
}
|
|
|
printf("[I2C] Successfully read %d bytes from target 0x%02X\n", sizeof(BatteryMeasurement), TARGET_ADDRESS);
|
|
printf("[I2C] Successfully read %d bytes from target 0x%02X\n", sizeof(BatteryMeasurement), TARGET_ADDRESS);
|
|
|
- printf("Voltage: %u\n", measurement->voltage);
|
|
|
|
|
- printf("Current: %d\n", measurement->current);
|
|
|
|
|
- printf("Temp: %u\n", measurement->temperature);
|
|
|
|
|
-
|
|
|
|
|
- /*measurement->voltage = rx_packet.rxBuffer[0] | (rx_packet.rxBuffer[1] << 8);
|
|
|
|
|
- measurement->current = rx_packet.rxBuffer[2] | (rx_packet.rxBuffer[3] << 8);
|
|
|
|
|
- measurement->temperature = rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8);
|
|
|
|
|
- measurement->slot_state = (SlotState)(rx_packet.rxBuffer[6]);
|
|
|
|
|
- //Debug
|
|
|
|
|
- printf("Slot ID: 0x%02X\n", slot_id);
|
|
|
|
|
- printf("Voltage:0x%04X\n", rx_packet.rxBuffer[0] | (rx_packet.rxBuffer[1] << 8));
|
|
|
|
|
- printf("Current:0x%02X\n", rx_packet.rxBuffer[2] | (rx_packet.rxBuffer[3] << 8));
|
|
|
|
|
- printf("Temperature:0x%02X\n", rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8));
|
|
|
|
|
- printf("Slot State:0x%02X\n", rx_packet.rxBuffer[6]);*/
|
|
|
|
|
|
|
+ printf("Voltage: %u\n", measurement.voltage);
|
|
|
|
|
+ printf("Current: %d\n", measurement.current);
|
|
|
|
|
+ printf("Temp: %u\n", measurement.temperature);
|
|
|
|
|
+ DL_I2C_flushControllerRXFIFO(I2C_1_INST);
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -117,7 +102,7 @@ void controller_ClearError(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
|
|
|
DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-//Logic to handle Power Burning PWM on the controller side: is there any clamping required between a certain range?
|
|
|
|
|
|
|
+//Logic to handle Power Burning PWM on the controller side: Problem of power flow back to the device which was breaking the board has been resolved
|
|
|
void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *measurement){
|
|
void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *measurement){
|
|
|
static bool slot_sov_hov_state= false;
|
|
static bool slot_sov_hov_state= false;
|
|
|
if((measurement->slot_state== SLOT_ERR_HOV) || (measurement->slot_state == SLOT_STATE_SOV)){
|
|
if((measurement->slot_state== SLOT_ERR_HOV) || (measurement->slot_state == SLOT_STATE_SOV)){
|