瀏覽代碼

added logic to communicate with target mcu for battery measurement and charging/discharging current

namrota ghosh 7 月之前
父節點
當前提交
4a463a5aa2
共有 2 個文件被更改,包括 91 次插入0 次删除
  1. 69 0
      src/controller/controller.c
  2. 22 0
      src/controller/controller.h

+ 69 - 0
src/controller/controller.c

@@ -0,0 +1,69 @@
+#include "src/controller/controller.h"
+#include "ti_msp_dl_config.h"
+#include <stdio.h>
+#include <string.h>
+
+/*
+* 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;
+    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);
+}
+
+//Generic Function to send data to the target controller using I2C:
+//Format: command + ((slot_id) + data (optional))
+bool controller_GetCommandRequest(uint8_t const TARGET_ADDRESS, uint8_t command, uint8_t slot_id, uint8_t *data, uint8_t data_len){
+    //Set the command in the tx buffer in bit masked format to the target
+    tx_packet.txBuffer[0] = command| (slot_id<<4); //shift slot_id to the left by 4 bits
+    printf("Bitmasked Command:: 0x%02X\n", tx_packet.txBuffer[0]);
+    //Conditional check to check if the data is not null and data length is greater than 0
+    if(data != NULL && data_len > 0){
+        //when data is sent along with the command, copying the data to the tx buffer
+       memcpy(&tx_packet.txBuffer[1], data, data_len);
+       tx_packet.txLen = data_len + 1; // +1 for command
+    }
+    else{
+        tx_packet.txLen = 1; // Only command
+    }
+    if(!i2c_hal.write(TARGET_ADDRESS)){
+        return false;
+    }
+    bool result= i2c_hal.read(TARGET_ADDRESS);
+    if(result){
+        printf("[I2C] Successfully read %d bytes from target 0x%02X\n", data_len, TARGET_ADDRESS);
+        return true;
+    }else{
+        printf("[ERROR] Failed to read from target 0x%02X\n", TARGET_ADDRESS);
+        return false;
+    }
+}
+
+//Send command to set charge and discharge current to the target
+void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, uint8_t current_mA){
+    uint8_t data[2];
+    data[0] = slot_id;
+    data[1] = 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){
+    
+    if(controller_GetCommandRequest(TARGET_ADDRESS, CMD_GET_MEASUREMENT, slot_id, (uint8_t *)measurement, sizeof(BatteryMeasurement))){
+        measurement->voltage = rx_packet.rxBuffer[0] | (rx_packet.rxBuffer[1] << 8);
+        measurement->current = rx_packet.rxBuffer[2] | (rx_packet.rxBuffer[3] << 8);
+        measurement->temperature = rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8);
+        return true;
+    }
+    return false;
+}

+ 22 - 0
src/controller/controller.h

@@ -0,0 +1,22 @@
+
+#include <stdint.h>
+#include <stdbool.h>
+#include "src/config.h"
+#include "src/i2c_comm/i2c_hal.h"
+#include "src/battery_data/battery.h"
+
+#ifndef CONTROLLER_H
+#define CONTROLLER_H
+
+
+typedef enum{
+    CMD_SET_CURRENT= 0x05,
+    CMD_GET_MEASUREMENT= 0x06, 
+    CMD_GET_BATTERY_STATE= 0x07, 
+    CMD_CELAR_ERR= 0x08
+}mcu_I2C_command;
+
+void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, uint8_t current_mA);
+bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot_id, BatteryMeasurement *measurement);
+
+#endif