Browse Source

Added logic for Pi to communicate with MCU for GET and SET

namrota ghosh 7 months ago
parent
commit
80039d4705
2 changed files with 77 additions and 0 deletions
  1. 62 0
      src/pi/i2c_pi_target.c
  2. 15 0
      src/pi/i2c_pi_target.h

+ 62 - 0
src/pi/i2c_pi_target.c

@@ -0,0 +1,62 @@
+#include "i2c_pi_target.h"
+#include "src/config.h"
+#include "ti/driverlib/dl_i2c.h"
+#include "src/battery_data/battery.h"
+#include "ti_msp_dl_config.h"
+#include <stdint.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){
+        //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;
+        }
+        DL_I2C_fillTargetTXFIFO(I2C_0_INST, rx_packet.rxBuffer, 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);
+        DL_I2C_flushTargetTXFIFO(I2C_0_INST);
+
+    }
+    else if(receivedCommand== CMD_SET_BATTERY_LIMIT){
+        uint8_t rx_index= 0;
+        while(rx_index < sizeof(BatteryLimits)){
+            if(!DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)){
+                rx_packet.rxBuffer[rx_index] = DL_I2C_receiveTargetData(I2C_0_INST);
+                rx_index++;
+            }
+        }
+        //Check if all the data is received then store the battery limits in BatteryInfo struct:
+        if(rx_index== sizeof(BatteryLimits)){
+            /*rxBuffer[0]: slot_id
+            */
+            BatteryInfo *battery = &battery_data[rx_packet.rxBuffer[0]];
+            battery->battery_limits.min_voltage = rx_packet.rxBuffer[1];
+            battery->battery_limits.max_voltage = rx_packet.rxBuffer[2];
+            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;
+        }
+        rx_index= 0;
+        DL_I2C_flushTargetRXFIFO(I2C_0_INST);
+    }
+}
+

+ 15 - 0
src/pi/i2c_pi_target.h

@@ -0,0 +1,15 @@
+#ifndef I2C_PI_TARGET_H
+#define I2C_PI_TARGET_H
+
+#include <stdbool.h>
+
+
+// I2C command codes
+#define CMD_GET_BATTERY_STATUS        0x01       // Request battery state
+#define CMD_GET_BATTERY_DATA          0x02       // Request battery data
+#define CMD_SET_BATTERY_LIMIT         0x03       // SET min/max voltage, cutoff current limits
+
+
+void pi_i2c_mcu();
+
+#endif