|
@@ -1,4 +1,7 @@
|
|
|
#include "src/controller/controller.h"
|
|
#include "src/controller/controller.h"
|
|
|
|
|
+#include "src/i2c_comm/i2c_hal.h"
|
|
|
|
|
+#include "src/pi/i2c_pi_target.h"
|
|
|
|
|
+#include "ti/driverlib/dl_i2c.h"
|
|
|
#include "ti_msp_dl_config.h"
|
|
#include "ti_msp_dl_config.h"
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
|
#include <string.h>
|
|
#include <string.h>
|
|
@@ -18,36 +21,38 @@ bool controller_SetCommandRequest(uint8_t const TARGET_ADDRESS, uint8_t command,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
tx_packet.txLen = data_len + 1; // +1 for command
|
|
tx_packet.txLen = data_len + 1; // +1 for command
|
|
|
- //return i2c_hal.write(TARGET_ADDRESS);
|
|
|
|
|
- return i2c_mock_interface.write(TARGET_ADDRESS); //Testing:: mock test: comment it out in Production
|
|
|
|
|
|
|
+ return i2c_hal.write(TARGET_ADDRESS);
|
|
|
|
|
+ //return i2c_mock_interface.write(TARGET_ADDRESS); //Testing:: mock test: comment it out in Production
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Generic Function to send data to the target controller using I2C:
|
|
//Generic Function to send data to the target controller using I2C:
|
|
|
//Format: command + ((slot_id) + data (optional))
|
|
//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){
|
|
|
|
|
|
|
+bool controller_GetCommandRequest(uint8_t const TARGET_ADDRESS, uint8_t slot_id, uint8_t *data, uint8_t data_len){
|
|
|
|
|
+
|
|
|
|
|
+ //Write Command to the target
|
|
|
//Set the command in the tx buffer in bit masked format to the target
|
|
//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("[MCU] GET 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.read(TARGET_ADDRESS)){
|
|
|
|
|
- return false;
|
|
|
|
|
- }*/
|
|
|
|
|
- bool result= i2c_mock_interface.read(TARGET_ADDRESS); //Testing:: mock test: Comment it out in Production
|
|
|
|
|
- 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);
|
|
|
|
|
|
|
+ uint8_t command= CMD_GET_MEASUREMENT|(slot_id & 0x0F); //shift slot_id to the left by 4 bits
|
|
|
|
|
+ DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
|
|
|
|
|
+ DL_I2C_fillControllerTXFIFO(I2C_1_INST, &command, 1);
|
|
|
|
|
+ printf("[MCU] GET Bitmasked Command:: 0x%02X\n", command);
|
|
|
|
|
+ if(DL_I2C_getControllerStatus(I2C_1_INST)& (DL_I2C_CONTROLLER_STATUS_ERROR)){
|
|
|
|
|
+ printf("I2C Write Error: Bus is stuck\n");
|
|
|
|
|
+ DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ //Receive the data from the target:
|
|
|
|
|
+ uint8_t rx_index=0;
|
|
|
|
|
+ DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, sizeof(BatteryMeasurement));
|
|
|
|
|
+ while(rx_index < data_len){
|
|
|
|
|
+ if(!DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST)){
|
|
|
|
|
+ rx_packet.rxBuffer[rx_index]= DL_I2C_receiveTargetData(I2C_1_INST);
|
|
|
|
|
+ printf("Received Bytes[%d]: 0x%02X\n", rx_index, rx_packet.rxBuffer[rx_index]);
|
|
|
|
|
+ rx_index++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ printf("[I2C] Successfully read %d bytes from target 0x%02X\n", data_len, TARGET_ADDRESS);
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Send command to set charge and discharge current to the target
|
|
//Send command to set charge and discharge current to the target
|
|
@@ -62,22 +67,50 @@ void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_
|
|
|
|
|
|
|
|
//Get battery measurement: Voltage, Current and Temperature:
|
|
//Get battery measurement: Voltage, Current and Temperature:
|
|
|
bool controller_GetBatteryMeasurement(uint8_t const TARGET_ADDRESS, uint8_t slot_id, BatteryMeasurement *measurement){
|
|
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);
|
|
|
|
|
|
|
+ //Write Command to the target
|
|
|
|
|
+ //Set the command in the tx buffer in bit masked format to the target
|
|
|
|
|
+ uint8_t command= CMD_GET_MEASUREMENT|(slot_id & 0x0F); //shift slot_id to the left by 4 bits
|
|
|
|
|
+ DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
|
|
|
|
|
+ DL_I2C_fillControllerTXFIFO(I2C_1_INST, &command, 1);
|
|
|
|
|
+ printf("[MCU] GET Bitmasked Command:: 0x%02X\n", command);
|
|
|
|
|
+ if(DL_I2C_getControllerStatus(I2C_1_INST)& (DL_I2C_CONTROLLER_STATUS_ERROR)){
|
|
|
|
|
+ printf("I2C Write Error: Bus is stuck\n");
|
|
|
|
|
+ DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //Receive the data from the target:
|
|
|
|
|
+
|
|
|
|
|
+ DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, sizeof(BatteryMeasurement));
|
|
|
|
|
+ uint8_t *dest= (uint8_t *)measurement;
|
|
|
|
|
+ uint8_t rx_index=0;
|
|
|
|
|
+ while(rx_index < sizeof(BatteryMeasurement)){
|
|
|
|
|
+ if(!DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST)){
|
|
|
|
|
+ dest[rx_index++]= DL_I2C_receiveTargetData(I2C_1_INST);
|
|
|
|
|
+ //printf("Received Bytes[%d]: 0x%02X\n", rx_index, rx_packet.rxBuffer[rx_index]);
|
|
|
|
|
+ //rx_index++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ printf("[I2C] Successfully read %d bytes from target 0x%02X\n", sizeof(BatteryMeasurement), TARGET_ADDRESS);
|
|
|
|
|
+ printf("Voltage: %u\n", measurement->voltage);
|
|
|
|
|
+ printf("Current: %d\n", measurement->current);
|
|
|
|
|
+ printf("Temp: %u\n", measurement->temperature);
|
|
|
|
|
+
|
|
|
|
|
+ /*measurement->voltage = rx_packet.rxBuffer[0] | (rx_packet.rxBuffer[1] << 8);
|
|
|
measurement->current = rx_packet.rxBuffer[2] | (rx_packet.rxBuffer[3] << 8);
|
|
measurement->current = rx_packet.rxBuffer[2] | (rx_packet.rxBuffer[3] << 8);
|
|
|
measurement->temperature = rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8);
|
|
measurement->temperature = rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8);
|
|
|
measurement->slot_state = (SlotState)(rx_packet.rxBuffer[6]);
|
|
measurement->slot_state = (SlotState)(rx_packet.rxBuffer[6]);
|
|
|
//Debug
|
|
//Debug
|
|
|
printf("Slot ID: 0x%02X\n", slot_id);
|
|
printf("Slot ID: 0x%02X\n", slot_id);
|
|
|
- printf("Voltage:0x%02X\n", rx_packet.rxBuffer[0] | (rx_packet.rxBuffer[1] << 8));
|
|
|
|
|
|
|
+ printf("Voltage:0x%04X\n", rx_packet.rxBuffer[0] | (rx_packet.rxBuffer[1] << 8));
|
|
|
printf("Current:0x%02X\n", rx_packet.rxBuffer[2] | (rx_packet.rxBuffer[3] << 8));
|
|
printf("Current:0x%02X\n", rx_packet.rxBuffer[2] | (rx_packet.rxBuffer[3] << 8));
|
|
|
printf("Temperature:0x%02X\n", rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8));
|
|
printf("Temperature:0x%02X\n", rx_packet.rxBuffer[4] | (rx_packet.rxBuffer[5] << 8));
|
|
|
- printf("Slot State:0x%02X\n", rx_packet.rxBuffer[6]);
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ printf("Slot State:0x%02X\n", rx_packet.rxBuffer[6]);*/
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
//Clear error flag to the target
|
|
//Clear error flag to the target
|
|
|
//Format: command + ((slot_id) + data (optional))
|
|
//Format: command + ((slot_id) + data (optional))
|
|
|
void controller_ClearError(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
|
|
void controller_ClearError(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
|
|
@@ -88,16 +121,20 @@ void controller_ClearError(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
|
|
|
controller_SetCommandRequest(TARGET_ADDRESS, CMD_CLEAR_ERR, data, sizeof(data));
|
|
controller_SetCommandRequest(TARGET_ADDRESS, CMD_CLEAR_ERR, data, sizeof(data));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
//Logic to handle Power Burning PWM on the controller side: is there any clamping required between a certain range?
|
|
//Logic to handle Power Burning PWM on the controller side: is there any clamping required between a certain range?
|
|
|
void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *measurement){
|
|
void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *measurement){
|
|
|
|
|
+ static bool slot_sov_hov_state= false;
|
|
|
if((measurement->slot_state== SLOT_ERR_HOV) || (measurement->slot_state == SLOT_STATE_SOV)){
|
|
if((measurement->slot_state== SLOT_ERR_HOV) || (measurement->slot_state == SLOT_STATE_SOV)){
|
|
|
|
|
+ slot_sov_hov_state= true;
|
|
|
if(battery_data[slot_id].pwm_value < (PWM_MAX_VALUE - PWM_INCREMENT_VALUE)){
|
|
if(battery_data[slot_id].pwm_value < (PWM_MAX_VALUE - PWM_INCREMENT_VALUE)){
|
|
|
battery_data[slot_id].pwm_value+= PWM_INCREMENT_VALUE;
|
|
battery_data[slot_id].pwm_value+= PWM_INCREMENT_VALUE;
|
|
|
printf("[Power Burning PWM] SOV/HOV state: Increased power burn PWM to %d\n", battery_data[slot_id].pwm_value);
|
|
printf("[Power Burning PWM] SOV/HOV state: Increased power burn PWM to %d\n", battery_data[slot_id].pwm_value);
|
|
|
DL_TimerG_setCaptureCompareValue(PWM_0_INST, battery_data[slot_id].pwm_value, DL_TIMER_CC_0_INDEX);
|
|
DL_TimerG_setCaptureCompareValue(PWM_0_INST, battery_data[slot_id].pwm_value, DL_TIMER_CC_0_INDEX);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- }else if(measurement->slot_state== SLOT_STATE_OK){
|
|
|
|
|
|
|
+ }else if(measurement->slot_state== SLOT_STATE_OK && slot_sov_hov_state== true){
|
|
|
if(battery_data[slot_id].pwm_value >= (INITIAL_PWM_VALUE + PWM_DECREMENT_VALUE)){
|
|
if(battery_data[slot_id].pwm_value >= (INITIAL_PWM_VALUE + PWM_DECREMENT_VALUE)){
|
|
|
battery_data[slot_id].pwm_value-= PWM_DECREMENT_VALUE;
|
|
battery_data[slot_id].pwm_value-= PWM_DECREMENT_VALUE;
|
|
|
printf("[Power Burning PWM] OK state: Decreased power burn PWM to %d\n", battery_data[slot_id].pwm_value);
|
|
printf("[Power Burning PWM] OK state: Decreased power burn PWM to %d\n", battery_data[slot_id].pwm_value);
|
|
@@ -105,6 +142,6 @@ void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *me
|
|
|
}
|
|
}
|
|
|
}else{
|
|
}else{
|
|
|
printf("[Power Burning PWM] initial state: %d\n", battery_data[slot_id].pwm_value);
|
|
printf("[Power Burning PWM] initial state: %d\n", battery_data[slot_id].pwm_value);
|
|
|
- DL_TimerG_setCaptureCompareValue(PWM_0_INST, INITIAL_PWM_VALUE, DL_TIMER_CC_0_INDEX);
|
|
|
|
|
|
|
+ DL_TimerG_setCaptureCompareValue(PWM_0_INST, battery_data[slot_id].pwm_value, DL_TIMER_CC_0_INDEX);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|