فهرست منبع

Created a new TX Buffer for SET Charging Current since the txBuffer was falsely getting overlapped between GET and SET commands

namrota ghosh 10 ماه پیش
والد
کامیت
7645dd29d4
1فایلهای تغییر یافته به همراه31 افزوده شده و 20 حذف شده
  1. 31 20
      src/controller/controller.c

+ 31 - 20
src/controller/controller.c

@@ -13,29 +13,34 @@ 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
 
-    txPacket.txBuffer[0]= (slot_id<<4) | (CMD_SET_CURRENT & 0x0F);
+    //Bitmasked: Slot id + Command
+    uint8_t txBuffer_setCurrent[3]; 
+    txBuffer_setCurrent[0]= (slot_id<<4) | (CMD_SET_CURRENT & 0x0F);
     //Filling the buffer with current value
-    *((int16_t*)(&txPacket.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
-    txPacket.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, txPacket.txLen);
-
-    printf("Packet Sent:: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X \n", TARGET_ADDRESS, txPacket.txBuffer[0], txPacket.txBuffer[1],txPacket.txBuffer[2], txPacket.txBuffer[3]);
-    DL_I2C_fillControllerTXFIFO(I2C_1_INST, txPacket.txBuffer, txPacket.txLen);
-
-    // Wait for the bus to become not busy WITH a timeout
+    *((int16_t*)(&txBuffer_setCurrent[1])) = current_mA;
+    
+    // Wait for the bus to be not IDLE WITH a timeout
     uint32_t timeout = 10000; 
-    while ((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && timeout > 0) {
-        timeout--;
+    while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
+    if(timeout == 0){
+        printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
+        DL_I2C_resetControllerTransfer(I2C_1_INST); 
+    }
+    //Flush the TX FIFO and make the buffer ready for the transmit
+    DL_I2C_flushControllerTXFIFO(I2C_1_INST);
+    //Fill the txBuffer with Command (1 byte) and Current (2 bytes)
+    DL_I2C_fillControllerTXFIFO(I2C_1_INST, txBuffer_setCurrent, 3);
+    // Add delays
+    delay_cycles(1000);
+    //start the transfer
+    DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 3);
+    
+    timeout= 32000;
+    while((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY) && timeout--);
+    if(timeout==0){
+        printf("Tx transmit not completed");
     }
-
     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);
@@ -239,12 +244,17 @@ void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *me
 // Following code worked with ADC as the target with now clocking issues:
 bool getBatteryMeasurement_test(uint8_t slot_id){
     BatteryMeasurement measurement;
-    //Enable the interrupt:
+
+    //Enable the Interrupt:
     DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
+
+
     txPacket.txComplete= false;
     txPacket.txLen= 1;
     txPacket.txBuffer[0] = (0<<4) | (CMD_GET_MEASUREMENT & 0x0F);  
+        
     
+
     /* Wait for I2C to be Idle */
     uint32_t timeout = 10000;
     while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
@@ -283,6 +293,7 @@ bool getBatteryMeasurement_test(uint8_t slot_id){
     rxPacket.rxComplete= false;
     timeout= 32000;
 
+    //DL_I2C_flushControllerTXFIFO(I2C_1_INST);
     DL_I2C_flushControllerRXFIFO(I2C_1_INST);
     
     DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);