ソースを参照

Functioning Pi Code for all commands

namrota ghosh 7 ヶ月 前
コミット
955f4bbdfd
2 ファイル変更34 行追加27 行削除
  1. 1 1
      src/controller/controller.h
  2. 33 26
      src/pi/i2c_pi_target.c

+ 1 - 1
src/controller/controller.h

@@ -18,6 +18,6 @@ typedef enum{
 }mcu_I2C_command;
 
 void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_t current_mA);
-bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot_id, BatteryMeasurement *measurement);
+bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot_id);
 void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *measurement);
 #endif

+ 33 - 26
src/pi/i2c_pi_target.c

@@ -9,8 +9,11 @@
 // Global extern variable for buffers are defined in config.h and declared in i2c_hal.c:
 
 void pi_i2c_mcu(){
+
     uint8_t receivedCommand= DL_I2C_receiveTargetData(I2C_0_INST);
+    printf("Received Command: 0x%02X\n", receivedCommand);
     if(receivedCommand == CMD_GET_BATTERY_STATUS){
+        //Example: i2cget -y 1 0x10 0x01 i 4
         uint8_t status_buffer[NUM_SLOTS];
         //GET battery state from battery.c file
         for(uint8_t slot=0; slot<NUM_SLOTS; slot++){
@@ -21,21 +24,17 @@ void pi_i2c_mcu(){
     }
     //bitmasked GET command:
     else if((receivedCommand & 0xF0)== 0x20){
-        
-        //Get Battery Measurement data: Voltage, Current, Tempertaure
+        //I2Ctools command: i2cget -y 1 0x10 0x20 i 7
+        //Get Battery Measurement data for slot_id: Voltage, Current, Tempertaure
         uint8_t requestedSlot = receivedCommand & 0x0F;
+        printf("Requested slot:%d\n", requestedSlot);
         BatteryData battery_measure;
-
         if(requestedSlot >= NUM_SLOTS){
             DL_I2C_flushTargetRXFIFO(I2C_0_INST);
             return;
         }
         //Check for the requested slot and get the battery measurement data
         BatteryInfo *battery = &battery_data[requestedSlot];
-        /*tx_packet.txBuffer[0] = battery->battery_measurement.voltage;
-        tx_packet.txBuffer[1] = battery->battery_measurement.current;
-        tx_packet.txBuffer[2] = battery->battery_measurement.temperature;
-        tx_packet.txBuffer[3] = battery->battery_measurement.slot_state;*/
         battery_measure.slot_id= battery->slot_id;
         battery_measure.voltage= battery->battery_measurement.voltage;
         battery_measure.current= battery->battery_measurement.current;
@@ -45,33 +44,40 @@ void pi_i2c_mcu(){
         DL_I2C_flushTargetTXFIFO(I2C_0_INST);
 
     }
+
     else if(receivedCommand== CMD_SET_BATTERY_LIMIT){
+
+        //slot_id is another element
+        //Example i2ctools: i2cset -y 1 0x10 0x03 0x00 0x2C 0x01 0x68 0x10 0xFA 0xD0 0x07 0x19 i
+        /*
+        * min_voltage: 300 (2C 01)
+        * max_voltage: 4200 (68 10)
+        * cut off current: 250 (FA)
+        * Capacitance:  2000 (D0 07)
+        * charge fraction: 25 (19)
+        */
         uint8_t rx_index= 0;
-        while(rx_index < sizeof(BatteryLimits)){
+
+        while(rx_index < (sizeof(BatteryLimitMsg))){
             if(!DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)){
                 rx_packet.rxBuffer[rx_index] = DL_I2C_receiveTargetData(I2C_0_INST);
+                //printf("Received Bytes[%d]: 0x%02X\n", rx_index, rx_packet.rxBuffer[rx_index]);
                 rx_index++;
             }
         }
+        printf("index:%d\n", rx_index);
         //Check if all the data is received then store the battery limits in BatteryInfo struct:
-        if(rx_index== (sizeof(BatteryLimits)+1)){
-            BatteryInfo *battery = &battery_data[rx_packet.rxBuffer[0]];
-            uint8_t slot_id= rx_packet.rxBuffer[0];
-            printf("Battery slot: %u\n", slot_id);
-            if(slot_id <= NUM_SLOTS){
-                //2bytes of max and min voltage: uint16_t
-                /*battery->battery_limits.min_voltage = rx_packet.rxBuffer[1] | (rx_packet.rxBuffer[2] << 8);
-                battery->battery_limits.max_voltage = rx_packet.rxBuffer[3]|(rx_packet.rxBuffer[4] << 8);
-                //1 byte of cut off current: uint8_t
-                battery->battery_limits.cut_off_current = rx_packet.rxBuffer[5];
-                //2 bytes of capacitance:uint16_t
-                battery->battery_limits.capacitance = rx_packet.rxBuffer[6] | (rx_packet.rxBuffer[7] << 8);
-                //1 byte of charge fraction: uint8_t
-                battery->battery_limits.charge_fraction = rx_packet.rxBuffer[8];
-                //Set the battery state to "STATE_BATTERY_DETECTED"
-                battery->batteryLimitRecieved = true;*/
-                memcpy(&battery_data[slot_id].battery_limits, &rx_packet.rxBuffer[1], sizeof(BatteryLimits));
-                battery_data[slot_id].batteryLimitRecieved= true;
+        if(rx_index== (sizeof(BatteryLimitMsg))){
+            BatteryLimitMsg battery_limit_received;
+            memcpy(&battery_limit_received, (const uint8_t*)rx_packet.rxBuffer, sizeof(BatteryLimitMsg));
+            if(battery_limit_received.slot_id < NUM_SLOTS){
+                BatteryInfo *battery = &battery_data[battery_limit_received.slot_id];
+                battery->min_voltage = battery_limit_received.min_voltage;
+                battery->max_voltage = battery_limit_received.max_voltage;
+                battery->cut_off_current = battery_limit_received.cut_off_current;
+                battery->capacitance = battery_limit_received.capacitance;
+                battery->charge_fraction = battery_limit_received.charge_fraction;
+                battery->batteryLimitReceived= true;
             }    
         }
         rx_index= 0;
@@ -79,3 +85,4 @@ void pi_i2c_mcu(){
     }
 }
 
+