|
|
@@ -16,15 +16,20 @@ void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_
|
|
|
*((int16_t*)(&tx_packet.txBuffer[1])) = current_mA;
|
|
|
//Length is calculated as 1 byte for the bitmasked slot and command+ 2 bytes of current + 1 byte of padding
|
|
|
tx_packet.txLen= 4;
|
|
|
- //printf("[MCU] SET Command:: 0x%02X\n", tx_packet.txBuffer[0]);
|
|
|
- //printf("Tx Buffer Length is 0x%02X\n", tx_packet.txLen);
|
|
|
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, tx_packet.txLen);
|
|
|
printf("Packet Sent:: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X \n", TARGET_ADDRESS, tx_packet.txBuffer[0], tx_packet.txBuffer[1],tx_packet.txBuffer[2], tx_packet.txBuffer[3]);
|
|
|
DL_I2C_fillControllerTXFIFO(I2C_1_INST, tx_packet.txBuffer, tx_packet.txLen);
|
|
|
-
|
|
|
- while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
|
|
|
+
|
|
|
+ // Wait for the bus to become not busy WITH a timeout
|
|
|
+ uint32_t timeout = 100000; // Adjust timeout value as needed
|
|
|
+ while ((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && timeout > 0) {
|
|
|
+ timeout--;
|
|
|
+ }
|
|
|
+ printf("SetCurrent successful for slot %d with current %d mA\n", slot_id, current_mA);
|
|
|
+ // Clean up and exit
|
|
|
DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -46,9 +51,9 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
|
|
|
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--;
|
|
|
+ uint32_t tx_timeout = 100000;
|
|
|
+ while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && tx_timeout > 0) {
|
|
|
+ tx_timeout--;
|
|
|
}
|
|
|
//If I2C Bus is stuck then reset the controller:
|
|
|
if(DL_I2C_getControllerStatus(I2C_1_INST)& (DL_I2C_CONTROLLER_STATUS_ERROR)){
|
|
|
@@ -59,22 +64,25 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
|
|
|
|
|
|
//Clear RX FIFO for any stale data and prepare for receiving data
|
|
|
//DL_I2C_flushControllerRXFIFO(I2C_1_INST);
|
|
|
-
|
|
|
+ //Add a small delax between Read and Write
|
|
|
+ for (volatile int d = 0; d < 500; d++);
|
|
|
//Receive the data from the target:
|
|
|
//BatteryMeasurement size is 12 similar to the target side
|
|
|
DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, sizeof(BatteryMeasurement));
|
|
|
|
|
|
-
|
|
|
- while((rx_index < sizeof(BatteryMeasurement)) && (timeout > 0)){
|
|
|
+ uint32_t rx_timeout = 20000;
|
|
|
+ while((rx_index < sizeof(BatteryMeasurement)) && (rx_timeout)){
|
|
|
if(!DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST)){
|
|
|
//Get byte from the I2C RX FIFO of the target
|
|
|
rx_packet.rxBuffer[rx_index]= DL_I2C_receiveControllerData(I2C_1_INST);
|
|
|
- //printf("Received Bytes[%d]: 0x%02X\n", rx_index, buffer[rx_index]);
|
|
|
+ printf("Received Bytes[%d]: 0x%02X\n", rx_index, rx_packet.rxBuffer[rx_index]);
|
|
|
rx_index++;
|
|
|
- }
|
|
|
-
|
|
|
+ rx_timeout= 20000; //reset on successful read
|
|
|
+ }/*else{
|
|
|
+ rx_timeout--;
|
|
|
+ }*/
|
|
|
}
|
|
|
- printf("index:%d\n", 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);
|
|
|
@@ -89,6 +97,7 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
|
|
|
printf("Temp: %u\n", measurement.temperature);
|
|
|
printf("Slot state: %u\n", measurement.slot_state);
|
|
|
DL_I2C_flushControllerRXFIFO(I2C_1_INST);
|
|
|
+ DL_I2C_flushControllerTXFIFO(I2C_1_INST);
|
|
|
return true;
|
|
|
}
|
|
|
|