فهرست منبع

Logic updated for Pi and MCU communication, bitshifting removed since it is more error prone, handling structured data transfer using memcpy

namrota ghosh 10 ماه پیش
والد
کامیت
a452f9e95a
1فایلهای تغییر یافته به همراه25 افزوده شده و 20 حذف شده
  1. 25 20
      src/pi/i2c_pi_target.c

+ 25 - 20
src/pi/i2c_pi_target.c

@@ -5,33 +5,32 @@
 #include "ti_msp_dl_config.h"
 #include <stdint.h>
 #include <stdio.h>
+#include <string.h>
 // 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);
     if(receivedCommand == CMD_GET_BATTERY_STATUS){
+        uint8_t status_buffer[NUM_SLOTS];
         //GET battery state from battery.c file
         for(uint8_t slot=0; slot<NUM_SLOTS; slot++){
-            rx_packet.rxBuffer[slot] = battery_data[slot].battery_state;
+            status_buffer[slot] = battery_data[slot].battery_state;
         }
-        DL_I2C_fillTargetTXFIFO(I2C_0_INST, rx_packet.rxBuffer, NUM_SLOTS);
+        DL_I2C_fillTargetTXFIFO(I2C_0_INST, status_buffer, NUM_SLOTS);
         while(DL_I2C_transmitTargetDataCheck(I2C_0_INST, 0x00)!= false);
     }
     //bitmasked GET command:
     else if((receivedCommand & 0xF0)== 0x20){
         //Get Battery Measurement data: Voltage, Current, Tempertaure
         uint8_t requestedSlot = receivedCommand & 0x0F;
-        tx_packet.txLen = sizeof(BatteryMeasurement);
         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;
-        DL_I2C_fillTargetTXFIFO(I2C_0_INST, tx_packet.txBuffer, tx_packet.txLen);
+        BatteryMeasurement *measurement = &battery_data[requestedSlot].battery_measurement;
+        //memcpy(tx_packet.txBuffer, &battery->battery_measurement, sizeof(BatteryMeasurement));
+        DL_I2C_fillTargetTXFIFO(I2C_0_INST, (uint8_t *)measurement, sizeof(BatteryMeasurement));
         DL_I2C_flushTargetTXFIFO(I2C_0_INST);
 
     }
@@ -44,19 +43,25 @@ void pi_i2c_mcu(){
             }
         }
         //Check if all the data is received then store the battery limits in BatteryInfo struct:
-        if(rx_index== sizeof(BatteryLimits)){
-            /*rxBuffer[0]: slot_id
-            */
+        if(rx_index== (sizeof(BatteryLimits)+1)){
             BatteryInfo *battery = &battery_data[rx_packet.rxBuffer[0]];
-            printf("Battery slot: %u\n", rx_packet.rxBuffer[0]);
-            battery->battery_limits.min_voltage = rx_packet.rxBuffer[1];
-            printf("Battery min voltage: %u\n", rx_packet.rxBuffer[1]);
-            battery->battery_limits.max_voltage = rx_packet.rxBuffer[2];
-            printf("Battery max voltage: %u\n", rx_packet.rxBuffer[1]);
-            battery->battery_limits.cut_off_current = rx_packet.rxBuffer[3];
-            battery->battery_limits.capacitance = rx_packet.rxBuffer[4];
-            battery->battery_limits.charge_fraction = rx_packet.rxBuffer[5];
-            battery->batteryLimitRecieved = true;
+            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;
+            }    
         }
         rx_index= 0;
         DL_I2C_flushTargetRXFIFO(I2C_0_INST);