|
|
@@ -0,0 +1,72 @@
|
|
|
+#include "src/interfaces/mcu_master_interface.h"
|
|
|
+#include "ti/driverlib/dl_i2c.h"
|
|
|
+#include "ti_msp_dl_config.h"
|
|
|
+#include "src/interfaces/i2c_controller_interface.h"
|
|
|
+#include <stdint.h>
|
|
|
+#include <stdbool.h>
|
|
|
+#include <string.h>
|
|
|
+#include <stdio.h>
|
|
|
+
|
|
|
+/*
|
|
|
+* Function to send data to the target: command + (slot_id + data(optional)) -> in a struct
|
|
|
+*/
|
|
|
+
|
|
|
+bool controller_SetCommandRequest(I2C_Regs *i2c, uint8_t target_address, uint8_t command, void *data, uint8_t data_len){
|
|
|
+ uint8_t tx_buffer[16];
|
|
|
+ // added a conditional statement so that the data to be sent + command (1 byte) does not exceed the tx_buffer length
|
|
|
+ if((data_len + 1) > sizeof(tx_buffer)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ tx_buffer[0]= command;
|
|
|
+ //Conditional check to ensure that data sent to the Target is not NULL and the length of the sent data is greater than 0
|
|
|
+ if((data> 0) && (data_len > 0)){
|
|
|
+ memcpy(&tx_buffer[1], data, data_len);
|
|
|
+ }
|
|
|
+ bool result= i2c_hal.write(target_address, tx_buffer, (data_len+1));
|
|
|
+ if(result){
|
|
|
+ printf("[I2C] Sent command 0x%02X to 0x%02X (%d bytes)\n", command, target_address, data_len);
|
|
|
+ }else{
|
|
|
+ printf("[ERROR] Failed to send command 0x%02X to 0x%02X\n", command, target_address);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+* Function to receive data from target:(command + slot_id) (bitmasked)
|
|
|
+*/
|
|
|
+
|
|
|
+bool controller_GetCommandRequest(I2C_Regs *i2c, uint8_t target_address, uint8_t command, uint8_t slot_id, void *data, uint8_t data_len){
|
|
|
+ // flush Rx buffer for stale data
|
|
|
+ //DL_I2C_flushTargetRXFIFO(i2c);
|
|
|
+ // send the command in bit-masked format to the target:
|
|
|
+ uint8_t bitmasked_cmd= command|(slot_id & 0x0F);
|
|
|
+ printf("Bitmasked Command:: 0x%02X\n", bitmasked_cmd);
|
|
|
+ if(!i2c_hal.write(target_address, &bitmasked_cmd, 1)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool result= i2c_hal.read(target_address, data_len);
|
|
|
+ if(result){
|
|
|
+ printf("[I2C] Successfully read %d bytes from 0x%02X\n", data_len, target_address);
|
|
|
+ }else{
|
|
|
+ printf("[ERROR] Failed to read from 0x%02X\n", target_address);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//Send command to set charge and discharge current to Target:
|
|
|
+void master_send_setcurrent(I2C_Regs *i2c, uint8_t target_address, uint8_t slot_id, int16_t current_mA){
|
|
|
+ SetChargeDischargeCurrent packet;
|
|
|
+ packet.slot_id= slot_id;
|
|
|
+ packet.current= current_mA;
|
|
|
+ controller_SetCommandRequest(i2c, target_address, CMD_SET_CURRENT, &packet, sizeof(packet));
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// GET Battery measurement data from Target
|
|
|
+bool master_getBatteryMeasurement(I2C_Regs *i2c, uint8_t target_address, uint8_t command, uint8_t slot_id, BatteryMeasurementData *battery_data){
|
|
|
+ return controller_GetCommandRequest(i2c, target_address, command, slot_id, battery_data, sizeof(BatteryMeasurementData));
|
|
|
+}
|
|
|
+
|
|
|
+
|