Sfoglia il codice sorgente

Updated the controller_SetCurrent(), it is communicating with the Target

namrota ghosh 7 mesi fa
parent
commit
4c60e7889c
1 ha cambiato i file con 23 aggiunte e 29 eliminazioni
  1. 23 29
      src/controller/controller.c

+ 23 - 29
src/controller/controller.c

@@ -10,19 +10,21 @@
 * Generic Function to send data to the target controller using I2C:
 Format: command + ((slot_id) + data (optional))
 */
-
-bool controller_SetCommandRequest(uint8_t const TARGET_ADDRESS, uint8_t command, uint8_t *data, uint8_t data_len){
-    //Set the command in the tx buffer
-    tx_packet.txBuffer[0] = command;
-    printf("[MCU] SET Command:: 0x%02X\n", tx_packet.txBuffer[0]);
-    if(data != NULL && data_len > 0){
-        for(uint8_t i=0; i<data_len; i++){
-            tx_packet.txBuffer[i+1] = data[i];
-        }
-    }
-    tx_packet.txLen = data_len + 1; // +1 for command
-    return i2c_hal.write(TARGET_ADDRESS);
-    //return i2c_mock_interface.write(TARGET_ADDRESS); //Testing:: mock test: comment it out in Production
+//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){
+    tx_packet.txBuffer[0]= (slot_id<<4) | (CMD_SET_CURRENT & 0x0F);
+    *((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);
+    DL_I2C_flushControllerTXFIFO(I2C_1_INST);
 }
 
 //Generic Function to send data to the target controller using I2C:
@@ -55,15 +57,6 @@ bool controller_GetCommandRequest(uint8_t const TARGET_ADDRESS, uint8_t slot_id,
     return true;
 }
 
-//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){
-    uint8_t data[3];
-    data[0] = slot_id;
-    data[1] = current_mA & 0xFF; //Current LSB
-    data[2] = (current_mA >> 8) & 0xFF; //Current MSB
-    //battery_data[slot_id].charge_discharge= current_mA;
-    controller_SetCommandRequest(TARGET_ADDRESS, CMD_SET_CURRENT, data, sizeof(data));
-}
 
 //Get battery measurement: Voltage, Current and Temperature:
 bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot_id, BatteryMeasurement *measurement){
@@ -109,20 +102,21 @@ bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot
     return true;
 }
 
-
-
 //Clear error flag to the target
 //Format: command + ((slot_id) + data (optional))
 void controller_ClearError(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
     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);
-    uint8_t data[1];
-    data[0] = slot_id;
-    controller_SetCommandRequest(TARGET_ADDRESS, CMD_CLEAR_ERR, data, sizeof(data));
+    tx_packet.txBuffer[0]= command;
+    tx_packet.txBuffer[1]= slot_id;
+    tx_packet.txLen= sizeof(tx_packet.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, tx_packet.txLen);
+    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);
+    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?
 void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *measurement){
     static bool slot_sov_hov_state= false;