| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- #include "src/controller/controller.h"
- //#include "src/i2c_comm/i2c_hal.h"
- #include "ti/driverlib/dl_i2c.h"
- #include "ti/driverlib/m0p/dl_core.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))
- */
- //Send command to set charge and discharge current to the target
- void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_t current_mA){
- //Bitmasked: Slot id + Command
- txPacket.txBuffer[0]= (slot_id<<4) | (CMD_SET_CURRENT & 0x0F);
- //Filling the buffer with current value
- *((int16_t*)(&txPacket.txBuffer[1])) = current_mA;
- /*I2C Communication for transmitting Charging/Discharging current for the slots*/
- //Length is calculated as 1 byte for the bitmasked slot and command+ 2 bytes of current + 1 byte of padding
- txPacket.txLen= 4;
- DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
- DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, txPacket.txLen);
- printf("Packet Sent:: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X \n", TARGET_ADDRESS, txPacket.txBuffer[0], txPacket.txBuffer[1],txPacket.txBuffer[2], txPacket.txBuffer[3]);
- DL_I2C_fillControllerTXFIFO(I2C_1_INST, txPacket.txBuffer, txPacket.txLen);
- // Wait for the bus to become not busy WITH a timeout
- uint32_t timeout = 10000;
- while ((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && timeout > 0) {
- timeout--;
- }
- printf("STATUS ***SetCurrent successful for slot %d with current %d mA***\n", slot_id, current_mA);
- // Clean up and exit
- DL_I2C_flushControllerTXFIFO(I2C_1_INST);
- }
- //Get battery measurement: Voltage, Current, Temperature including slot state:
- bool controller_GetBatteryMeasurement(uint8_t slot_id){
-
- //I2cControllerStatus= I2C_STATUS_IDLE;
- uint8_t target_address= TARGET_BASE_ADDRESS + ((slot_id & 0b00000100) >> 2);
- printf("Target Address 0x%02X \n", target_address);
- //Initializing BatteryMeasurement structure from battery.h of size 8
- BatteryMeasurement measurement;
- //Flush the TX FIFO and make the buffer ready to transmit data:
- //DL_I2C_flushControllerTXFIFO(I2C_1_INST);
- //DL_I2C_flushControllerRXFIFO(I2C_1_INST);
- //Write Command to the target
- //Set the command in the tx buffer in bit masked format to the target: Upper Nibble-> Slot and Lower Nibble -> Command
- //txPacket.txBuffer[0]= (slot_id << 4)|(CMD_GET_MEASUREMENT & 0x0F); //shift slot_id to the left by 4 bits
- txPacket.txLen= 1;
- //Number of bytes to be written to TX FIFO:
- txPacket.txCount= DL_I2C_fillControllerTXFIFO(I2C_1_INST, &txPacket.txBuffer[0] , txPacket.txLen);
- txPacket.txComplete= false;
-
- //DL_I2C_fillControllerTXFIFO(I2C_1_INST, &txPacket.txBuffer[0], txPacket.txLen);
- //Enable the interrupt:
- DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
- // Wait for the bus to be idle
- uint32_t timeout = 32000;
- while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
- if(timeout == 0){
- printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
- DL_I2C_resetControllerTransfer(I2C_1_INST);
- return false;
- }
- //Send command bytes to the target
- //delay_cycles(10000000);
-
- DL_I2C_startControllerTransferAdvanced(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_TX, txPacket.txLen, DL_I2C_CONTROLLER_START_ENABLE, DL_I2C_CONTROLLER_STOP_DISABLE, DL_I2C_CONTROLLER_ACK_ENABLE);
- printf("[I2C] TX Packet Sent:: 0x%02X\n", txPacket.txBuffer[0]);
-
- //If I2C Bus is stuck then reset the controller:
- if(DL_I2C_getControllerStatus(I2C_1_INST)&(DL_I2C_CONTROLLER_STATUS_ERROR)){
- printf("ERROR ***I2C Write Error: Bus is stuck***\n");
- DL_I2C_resetControllerTransfer(I2C_1_INST);
- return false;
- }
- // Wait until TX FIFO is empty (TX complete)
- timeout = 32000;
- while ((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY) && timeout--);
- if (timeout == 0) {
- printf("Bus stuck during TX.\n");
- DL_I2C_resetControllerTransfer(I2C_1_INST);
- return false;
- }
- //Re-initializing te timeout value for Rx:
- timeout= 32000;
- rxPacket.rxLen= sizeof(BatteryMeasurement);
- rxPacket.rxComplete= false;
- rxPacket.rxCount= 0;
-
- //BatteryMeasurement size is 8 similar to the target side
- //DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
- //DL_I2C_startControllerTransferAdvanced(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen, DL_I2C_CONTROLLER_START_ENABLE, DL_I2C_CONTROLLER_STOP_ENABLE, DL_I2C_CONTROLLER_ACK_DISABLE);
- //DL_I2C_startControllerTransfer(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen);
-
- while((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY) && timeout--);
- if(timeout == 0 || (DL_I2C_getSCLStatus(I2C_1_INST)== DL_I2C_CONTROLLER_SCL_LOW)){
- printf("Bus stuck during Rx transmit or SCL held LOW.\n");
- DL_I2C_resetControllerTransfer(I2C_1_INST);
- return false;
- }
- //while(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
- //DEBUG
- printf("Rx Count: %d\n", rxPacket.rxCount);
- printf("Rx Complete: %d\n", rxPacket.rxComplete);
- //Check if all the data is received then store the battery limits in BatteryInfo struct:
- if(rxPacket.rxCount== (sizeof(BatteryMeasurement))){
- measurement.voltage= rxPacket.rxBuffer[0] | (rxPacket.rxBuffer[1] << 8);
- measurement.current= rxPacket.rxBuffer[2]|(rxPacket.rxBuffer[3] << 8);
- measurement.temperature = rxPacket.rxBuffer[4] | (rxPacket.rxBuffer[5] << 8);
- measurement.slot_state = (SlotState)(rxPacket.rxBuffer[6]);
- battery_data[slot_id].battery_measurement= measurement;
- //DEBUG
- printf("[I2C] Successfully read %d bytes from target 0x%02X\n", rxPacket.rxCount, target_address);
- printf("Voltage: %u\n", battery_data[slot_id].battery_measurement.voltage);
- printf("Current: %d\n", battery_data[slot_id].battery_measurement.current);
- printf("Temp: %u\n", battery_data[slot_id].battery_measurement.temperature);
- printf("Slot state: %u\n", battery_data[slot_id].battery_measurement.slot_state);
- return true;
- }
-
-
- return false;
- }
- //Clear error flag to the target to change it back to SLOT_STATE_OK
- //Format: command + ((slot_id) + data (optional))
- void controller_ClearError(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
- uint8_t command= CMD_CLEAR_ERR| (slot_id<<4); //shift slot_id to the left by 4 bits
- printf("[MCU] Clear Error Bitmasked Command:: 0x%02X\n", command);
- txPacket.txBuffer[0]= command;
- txPacket.txBuffer[1]= slot_id;
- txPacket.txLen= sizeof(txPacket.txBuffer);
- while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
- DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, txPacket.txLen);
- DL_I2C_fillControllerTXFIFO(I2C_1_INST, txPacket.txBuffer, txPacket.txLen);
- while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
- DL_I2C_flushControllerTXFIFO(I2C_1_INST);
- }
- //Logic to handle Power Burning PWM on the controller side: Problem of power flow back to the device which was breaking the board has been resolved
- 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)){
- slot_sov_hov_state= true;
- if(battery_data[slot_id].pwm_value < (PWM_MAX_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);
- 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 && slot_sov_hov_state== true){
- if(battery_data[slot_id].pwm_value >= (INITIAL_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);
- DL_TimerG_setCaptureCompareValue(PWM_0_INST, battery_data[slot_id].pwm_value, DL_TIMER_CC_0_INDEX);
- }
- }else{
- printf("[Power Burning PWM] initial state: %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);
- }
- }
- /*bool getBatteryMeasurement_test(){
- //DL_I2C_flushControllerTXFIFO(I2C_1_INST);
- //Enable the interrupt:
- DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
- //Wait for I2C to be Idle
- uint32_t timeout = 10000;
- while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
- if(timeout == 0){
- printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
- DL_I2C_resetControllerTransfer(I2C_1_INST);
- return false;
- }
-
- //Send the packet to the controller.
- //This function will send Start + Stop automatically.
-
- DL_I2C_startControllerTransferAdvanced(I2C_1_INST,
- 0x49,
- DL_I2C_CONTROLLER_DIRECTION_TX,
- 1,
- DL_I2C_CONTROLLER_START_ENABLE,
- DL_I2C_CONTROLLER_STOP_ENABLE,
- DL_I2C_CONTROLLER_ACK_ENABLE);
-
- printf("[I2C] TX Packet Sent:: 0x%02X\n", txPacket.txBuffer[0]);
-
- while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY)
- ;
-
- // Add delay between transfers
- //delay_cycles(320000);
- rxPacket.rxCount= 0;
- rxPacket.rxLen= 8;
-
- DL_I2C_flushControllerRXFIFO(I2C_1_INST);
-
- DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
-
- // Send a read request to Targe
- DL_I2C_startControllerTransferAdvanced(I2C_1_INST, 0x49, DL_I2C_CONTROLLER_DIRECTION_RX, 8, DL_I2C_CONTROLLER_START_ENABLE,
- DL_I2C_CONTROLLER_STOP_ENABLE,
- DL_I2C_CONTROLLER_ACK_ENABLE);
- while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY);
-
-
- printf("Received Bytes[%d]: 0x%02X\n", rxPacket.rxCount, rxPacket.rxBuffer[rxPacket.rxCount]);
- return true;
- }*/
- // Following code worked with ADC as the target with now clocking issues:
- bool getBatteryMeasurement_test(uint8_t slot_id){
- BatteryMeasurement measurement;
- //Enable the interrupt:
- DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
- txPacket.txComplete= false;
- txPacket.txLen= 1;
- txPacket.txBuffer[0] = (0<<4) | (CMD_GET_MEASUREMENT & 0x0F);
-
- /* Wait for I2C to be Idle */
- uint32_t timeout = 10000;
- while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
- if(timeout == 0){
- printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
- DL_I2C_resetControllerTransfer(I2C_1_INST);
- return false;
- }
-
- /* Send the packet to the controller.
- * This function will send Start + Stop automatically.
- */
- DL_I2C_startControllerTransferAdvanced(I2C_1_INST,
- TARGET_BASE_ADDRESS,
- DL_I2C_CONTROLLER_DIRECTION_TX,
- txPacket.txLen,
- DL_I2C_CONTROLLER_START_ENABLE,
- DL_I2C_CONTROLLER_STOP_ENABLE,
- DL_I2C_CONTROLLER_ACK_ENABLE);
-
- printf("[I2C] TX Packet Sent:: 0x%02X\n", txPacket.txBuffer[0]);
- /* Poll until the Controller writes all bytes */
- timeout= 32000;
- while(!(txPacket.txComplete) && timeout--);
- if(timeout == 0){
- printf("Tx Transmit not completed.\n");
- DL_I2C_resetControllerTransfer(I2C_1_INST);
- return false;
- }
-
- /* Add delay between transfers */
- //delay_cycles(32000);
- rxPacket.rxCount= 0;
- rxPacket.rxLen= 8;
- rxPacket.rxComplete= false;
- timeout= 32000;
- DL_I2C_flushControllerRXFIFO(I2C_1_INST);
-
- DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
- while(!(rxPacket.rxComplete) && timeout--);
- if(timeout==0){
- printf("Rx transmit not completed");
- }
- //while (DL_I2C_getControllerStatus(I2C_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
- /* Send a read request to Target */
- DL_I2C_startControllerTransferAdvanced(I2C_1_INST, TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen, DL_I2C_CONTROLLER_START_ENABLE,
- DL_I2C_CONTROLLER_STOP_ENABLE,
- DL_I2C_CONTROLLER_ACK_ENABLE);
- while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY)
- ;
-
- measurement.voltage= rxPacket.rxBuffer[0] | (rxPacket.rxBuffer[1] << 8);
- measurement.current= rxPacket.rxBuffer[2]|(rxPacket.rxBuffer[3] << 8);
- measurement.temperature = rxPacket.rxBuffer[4] | (rxPacket.rxBuffer[5] << 8);
- measurement.slot_state = (SlotState)(rxPacket.rxBuffer[6]);
- battery_data[slot_id].battery_measurement= measurement;
- //DEBUG
- printf("[I2C] Successfully read %d bytes from target 0x%02X\n", rxPacket.rxCount, TARGET_BASE_ADDRESS);
- printf("Voltage: %u\n", battery_data[slot_id].battery_measurement.voltage);
- printf("Current: %d\n", battery_data[slot_id].battery_measurement.current);
- printf("Temp: %u\n", battery_data[slot_id].battery_measurement.temperature);
- printf("Slot state: %u\n", battery_data[slot_id].battery_measurement.slot_state);
- return true;
- }
|