|
@@ -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;
|
|
|
|
|
+}
|