Browse Source

TX FIFO buffer was for testing initialized inside the interrupt, that is now changed and has been initialized inside the function; set current otherwise won't work

namrota ghosh 7 tháng trước cách đây
mục cha
commit
22003209bf
2 tập tin đã thay đổi với 20 bổ sung4 xóa
  1. 19 3
      src/controller/controller.c
  2. 1 1
      src/controller/controller.h

+ 19 - 3
src/controller/controller.c

@@ -24,7 +24,9 @@ void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_
     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);
 
@@ -235,11 +237,14 @@ void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *me
 }*/
 
 // Following code worked with ADC as the target with now clocking issues:
-bool getBatteryMeasurement_test(){
-
+bool getBatteryMeasurement_test(uint8_t slot_id){
+    BatteryMeasurement measurement;
     //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--);
@@ -255,7 +260,7 @@ bool getBatteryMeasurement_test(){
     DL_I2C_startControllerTransferAdvanced(I2C_1_INST,
                                            TARGET_BASE_ADDRESS,
                                            DL_I2C_CONTROLLER_DIRECTION_TX,
-                                           1,
+                                           txPacket.txLen,
                                            DL_I2C_CONTROLLER_START_ENABLE,
                                            DL_I2C_CONTROLLER_STOP_ENABLE,
                                            DL_I2C_CONTROLLER_ACK_ENABLE);
@@ -297,6 +302,17 @@ bool getBatteryMeasurement_test(){
     while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY)
     ;
     
+    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]);
+    battery_data[slot_id].battery_measurement= measurement;
 
+    //DEBUG
+    printf("[I2C] Successfully read %d bytes from target 0x%02X\n", rxPacket.rxCount, TARGET_BASE_ADDRESS);
+    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;
 }

+ 1 - 1
src/controller/controller.h

@@ -21,5 +21,5 @@ uint8_t detect_target_address();
 void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_t current_mA);
 bool controller_GetBatteryMeasurement(uint8_t slot_id);
 void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *measurement);
-bool getBatteryMeasurement_test();
+bool getBatteryMeasurement_test(uint8_t slot_id);
 #endif