Explorar el Código

added a TIMEOUT while loop logic before reading the battery measurement

namrota ghosh hace 7 meses
padre
commit
478194f20f
Se han modificado 1 ficheros con 34 adiciones y 25 borrados
  1. 34 25
      src/controller/controller.c

+ 34 - 25
src/controller/controller.c

@@ -12,21 +12,25 @@ Format: command + ((slot_id) + data (optional))
 */
 //Send command to set charge and discharge current to the target
 void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_t current_mA){
+    //Bitmasked: Slot id + Command
     tx_packet.txBuffer[0]= (slot_id<<4) | (CMD_SET_CURRENT & 0x0F);
+    //Filling the buffer with current value
     *((int16_t*)(&tx_packet.txBuffer[1])) = current_mA;
+
+    /*I2C Communication for transmitting Charging/Discharging current for the slots*/
     //Length is calculated as 1 byte for the bitmasked slot and command+ 2 bytes of current + 1 byte of padding
     tx_packet.txLen= 4;
     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);
+    /*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);*/
 
     // Wait for the bus to become not busy WITH a timeout
-    uint32_t timeout = 100000; // Adjust timeout value as needed
+    uint32_t timeout = 100000; 
     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);
+    printf("STATUS ***SetCurrent successful for slot %d with current %d mA***\n", slot_id, current_mA);
     // Clean up and exit
     DL_I2C_flushControllerTXFIFO(I2C_1_INST);
 
@@ -39,7 +43,10 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
     //Initializing BatteryMeasurement structure from battery.h of size 8
     BatteryMeasurement measurement;
     uint8_t rx_index=0;
-    
+
+    //Flush the TX FIFO and make the buffer ready to transmit data:
+    DL_I2C_flushControllerTXFIFO(I2C_1_INST);
+
     //Write Command to the target
     //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
@@ -47,7 +54,7 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
     //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);
-    printf("[MCU] TX Packet Sent:: 0x%02X\n", tx_packet.txBuffer[0]);
+    printf("[I2C] 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
@@ -57,32 +64,33 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
     }
     //If I2C Bus is stuck then reset the controller:
     if(DL_I2C_getControllerStatus(I2C_1_INST)& (DL_I2C_CONTROLLER_STATUS_ERROR)){
-        printf("I2C Write Error: Bus is stuck\n");
+        printf("ERROR ***I2C Write Error: Bus is stuck***\n");
         DL_I2C_resetControllerTransfer(I2C_1_INST);
         return false;
     }
 
     //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:
+    DL_I2C_flushControllerRXFIFO(I2C_1_INST);
+
+    //Adding a while wait loop
+    uint32_t rx_timeout = 20000;
+    while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && rx_timeout > 0) {
+        rx_timeout--;
+    }
+
     //BatteryMeasurement size is 12 similar to the target side
     DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, sizeof(BatteryMeasurement));
 
-    uint32_t rx_timeout = 20000;
-    while((rx_index < sizeof(BatteryMeasurement)) && (rx_timeout)){
+    while(rx_index < sizeof(BatteryMeasurement)){
         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, rx_packet.rxBuffer[rx_index]);
             rx_index++;
-            rx_timeout= 20000; //reset on successful read
-        }/*else{
-            rx_timeout--;
-        }*/
+        }
     }
-    //printf("index:%d\n", rx_index);
+    //DEBUG
+    printf("RX 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);
@@ -90,14 +98,15 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
            measurement.temperature = rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8);
            measurement.slot_state = (SlotState)(rx_packet.rxBuffer[6]);
            battery_data[slot_id].battery_measurement= measurement;
+           //DEBUG
+           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);
-    printf("Slot state:    %u\n", measurement.slot_state);
-    DL_I2C_flushControllerRXFIFO(I2C_1_INST);
-    DL_I2C_flushControllerTXFIFO(I2C_1_INST);
+    DL_I2C_flushTargetRXFIFO(I2C_1_INST);
+    //DEBUG
+    printf("Voltage: %u\n", battery_data[slot_id].battery_measurement.voltage);
+    printf("Current: %d\n", battery_data[slot_id].battery_measurement.current);
+    printf("Temp: %u\n", battery_data[slot_id].battery_measurement.temperature);
+    printf("Slot state: %u\n", battery_data[slot_id].battery_measurement.slot_state);
     return true;
 }