Parcourir la source

Refactored setCurrent and getBatteryMeasurement with the i2c_hal helper functions of read and write

namrota ghosh il y a 6 mois
Parent
commit
219a270bc8
1 fichiers modifiés avec 49 ajouts et 26 suppressions
  1. 49 26
      src/controller/controller.c

+ 49 - 26
src/controller/controller.c

@@ -1,5 +1,5 @@
 #include "src/controller/controller.h"
-//#include "src/i2c_comm/i2c_hal.h"
+#include "src/i2c_comm/i2c_hal.h"
 #include "ti/driverlib/dl_i2c.h"
 #include "ti/driverlib/m0p/dl_core.h"
 #include "ti_msp_dl_config.h"
@@ -13,17 +13,23 @@ Format: command + ((slot_id) + data (optional))
 */
 //Send command to set charge and discharge current to the target
 void controller_SetCurrent(uint8_t slot_id, int16_t current_mA){
-    
+
+    //For dynamic addressing:
     uint8_t target_address= TARGET_BASE_ADDRESS + ((slot_id & 0b00000100) >> 2);
     printf("Target Address 0x%02X \n", target_address);
+
     //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*)(&txBuffer_setCurrent[1])) = current_mA;
     
+    
+    //Write function from Controller MCU to target MCU:
+    i2c_hal.write(target_address, txBuffer_setCurrent, 3);
+
     // Wait for the bus to be not IDLE WITH a timeout
-    uint32_t timeout = 10000; 
+    /*uint32_t timeout = 10000; 
     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");
@@ -37,8 +43,9 @@ void controller_SetCurrent(uint8_t slot_id, int16_t current_mA){
     delay_cycles(1000);
     //start the transfer
     DL_I2C_startControllerTransfer(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_TX, 3);
-    
-    timeout= 32000;
+    */
+
+    uint32_t timeout= 32000;
     while((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY) && timeout--);
     if(timeout==0){
         printf("Tx transmit not completed");
@@ -50,16 +57,21 @@ void controller_SetCurrent(uint8_t slot_id, int16_t current_mA){
 //Clear error flag to the target to change it back to SLOT_STATE_OK
 //Format: command + ((slot_id) + data (optional))
 void controller_ClearError(uint8_t slot_id){
+
+    //For dynamic addressing:
     uint8_t target_address= TARGET_BASE_ADDRESS + ((slot_id & 0b00000100) >> 2);
     printf("Target Address 0x%02X \n", target_address);
+
+    //Tx Buffer:
     uint8_t command= CMD_CLEAR_ERR| (slot_id<<4); //shift slot_id to the left by 4 bits
     printf("[MCU] Clear Error Bitmasked Command:: 0x%02X\n", command);
     txPacket.txBuffer[0]= command;
     txPacket.txBuffer[1]= slot_id;
     txPacket.txLen= sizeof(txPacket.txBuffer);
+
     while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
     DL_I2C_startControllerTransfer(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_TX, txPacket.txLen);
-    DL_I2C_fillControllerTXFIFO(I2C_1_INST, txPacket.txBuffer, txPacket.txLen);
+    DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer, txPacket.txLen);
     while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
     DL_I2C_flushControllerTXFIFO(I2C_1_INST);
 }
@@ -93,66 +105,75 @@ bool getBatteryMeasurement(uint8_t slot_id){
     BatteryMeasurement measurement;
 
     //Dynamic addressing:
-
     uint8_t target_address= TARGET_BASE_ADDRESS + ((slot_id & 0b00000100) >> 2);
     printf("Target Address 0x%02X \n", target_address);
     
     //Enable the Interrupt:
     DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
 
-
+    //Preparing Tx Buffer:
     txPacket.txComplete= false;
     txPacket.txLen= 1;
     txPacket.txBuffer[0] = (slot_id<<4) | (CMD_GET_MEASUREMENT & 0x0F);  
 
-    DL_I2C_flushControllerTXFIFO(I2C_1_INST);
+    //Calling helper function for i2c write:
+    i2c_hal.write(target_address, (uint8_t *)&txPacket.txBuffer[0], 1);
+
+    printf("[I2C] TX Packet Sent:: 0x%02X\n", txPacket.txBuffer[0]);
+
+    /*DL_I2C_flushControllerTXFIFO(I2C_1_INST);
     DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1); 
+    */
 
     /* Wait for I2C to be Idle */
-    uint32_t timeout = 10000;
+    /*uint32_t timeout = 10000;
     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); 
         return false;
     }
+    */
     
-    
-    
+
     /* Send the packet to the controller.
      * This function will send Start + Stop automatically.
      */
-    DL_I2C_startControllerTransferAdvanced(I2C_1_INST,
+    /*DL_I2C_startControllerTransferAdvanced(I2C_1_INST,
                                            target_address,
                                            DL_I2C_CONTROLLER_DIRECTION_TX,
                                            txPacket.txLen,
                                            DL_I2C_CONTROLLER_START_ENABLE,
                                            DL_I2C_CONTROLLER_STOP_ENABLE,
                                            DL_I2C_CONTROLLER_ACK_ENABLE);
+    */
     
-    printf("[I2C] TX Packet Sent:: 0x%02X\n", txPacket.txBuffer[0]);
-
     /* Poll until the Controller writes all bytes */
-    timeout= 32000;
+    uint32_t timeout= 32000;
     while(!(txPacket.txComplete) && timeout--);
     if(timeout == 0){
         printf("Tx Transmit not completed.\n");
         DL_I2C_resetControllerTransfer(I2C_1_INST);
         return false;
     }
-           
-    /* Add delay between transfers */
-    //delay_cycles(32000);
+        
+    //Preparing Rx Buffer:
     rxPacket.rxCount= 0;
     rxPacket.rxLen= 8;
     rxPacket.rxComplete= false;
+    
+    //Re-initialize timeout for the Rx
     timeout= 32000;
 
+
+    //Calling helper function for i2c read:
+    i2c_hal.read(target_address);
+
     //Make RX Buffer ready to receive the data
     //DL_I2C_flushControllerRXFIFO(I2C_1_INST);
     
     // Clear any garbage
-    memset(rxPacket.rxBuffer, 0, sizeof(rxPacket.rxBuffer));
+    /*memset(rxPacket.rxBuffer, 0, sizeof(rxPacket.rxBuffer));
     
     DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
     
@@ -161,23 +182,25 @@ bool getBatteryMeasurement(uint8_t slot_id){
     
 
     //while (DL_I2C_getControllerStatus(I2C_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
+    */
 
     /* Send a read request to Target */
-    DL_I2C_startControllerTransferAdvanced(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen, DL_I2C_CONTROLLER_START_ENABLE,
+    /*DL_I2C_startControllerTransferAdvanced(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen, DL_I2C_CONTROLLER_START_ENABLE,
                                            DL_I2C_CONTROLLER_STOP_ENABLE,
                                            DL_I2C_CONTROLLER_ACK_ENABLE);
-
-    while(rxPacket.rxCount < 8 && timeout--);
+    */
+    
+    while(rxPacket.rxCount < rxPacket.rxLen && timeout--);
     if(timeout==0){
         printf("Rx transmit not completed");
     }
-    
-    
-    
+    //To avoid race conditions:
+    //__disable_irq();
     measurement.voltage= rxPacket.rxBuffer[0] | (rxPacket.rxBuffer[1] << 8);
     measurement.current= rxPacket.rxBuffer[2]|(rxPacket.rxBuffer[3] << 8);
     measurement.temperature = rxPacket.rxBuffer[4] | (rxPacket.rxBuffer[5] << 8);
     measurement.slot_state = (SlotState)(rxPacket.rxBuffer[6]);
+    //__enable_irq();
     //DL_I2C_flushControllerRXFIFO(I2C_1_INST);
     //Storing the measurements in Battery structure:
     battery_data[slot_id].battery_measurement= measurement;