controller.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "src/controller/controller.h"
  2. //#include "src/i2c_comm/i2c_hal.h"
  3. #include "ti/driverlib/dl_i2c.h"
  4. #include "ti/driverlib/m0p/dl_core.h"
  5. #include "ti_msp_dl_config.h"
  6. #include <stdio.h>
  7. //#include <string.h>
  8. /*
  9. * Generic Function to send data to the target controller using I2C:
  10. Format: command + ((slot_id) + data (optional))
  11. */
  12. //Send command to set charge and discharge current to the target
  13. void controller_SetCurrent(uint8_t const TARGET_ADDRESS, uint8_t slot_id, int16_t current_mA){
  14. //Bitmasked: Slot id + Command
  15. txPacket.txBuffer[0]= (slot_id<<4) | (CMD_SET_CURRENT & 0x0F);
  16. //Filling the buffer with current value
  17. *((int16_t*)(&txPacket.txBuffer[1])) = current_mA;
  18. /*I2C Communication for transmitting Charging/Discharging current for the slots*/
  19. //Length is calculated as 1 byte for the bitmasked slot and command+ 2 bytes of current + 1 byte of padding
  20. txPacket.txLen= 4;
  21. DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
  22. DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, txPacket.txLen);
  23. 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]);
  24. DL_I2C_fillControllerTXFIFO(I2C_1_INST, txPacket.txBuffer, txPacket.txLen);
  25. // Wait for the bus to become not busy WITH a timeout
  26. uint32_t timeout = 10000;
  27. while ((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && timeout > 0) {
  28. timeout--;
  29. }
  30. printf("STATUS ***SetCurrent successful for slot %d with current %d mA***\n", slot_id, current_mA);
  31. // Clean up and exit
  32. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  33. }
  34. //Get battery measurement: Voltage, Current, Temperature including slot state:
  35. bool controller_GetBatteryMeasurement(uint8_t slot_id){
  36. uint8_t target_address= TARGET_BASE_ADDRESS + ((slot_id & 0b00000100) >> 2);
  37. printf("Target Address 0x%02X \n", target_address);
  38. //Initializing BatteryMeasurement structure from battery.h of size 8
  39. BatteryMeasurement measurement;
  40. //Flush the TX FIFO and make the buffer ready to transmit data:
  41. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  42. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  43. //Write Command to the target
  44. //Set the command in the tx buffer in bit masked format to the target: Upper Nibble-> Slot and Lower Nibble -> Command
  45. txPacket.txBuffer[0]= (slot_id << 4)|(CMD_GET_MEASUREMENT & 0x0F); //shift slot_id to the left by 4 bits
  46. txPacket.txLen= 1;
  47. txPacket.txCount= 0;
  48. txPacket.txComplete= false;
  49. DL_I2C_fillControllerTXFIFO(I2C_1_INST, &txPacket.txBuffer[0], txPacket.txLen);
  50. // Wait for the bus to be idle
  51. uint32_t timeout = 10000;
  52. while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
  53. if(timeout == 0){
  54. printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
  55. DL_I2C_resetControllerTransfer(I2C_1_INST);
  56. return false;
  57. }
  58. //Send command bytes to the target
  59. 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);
  60. printf("[I2C] TX Packet Sent:: 0x%02X\n", txPacket.txBuffer[0]);
  61. //If I2C Bus is stuck then reset the controller:
  62. if(DL_I2C_getControllerStatus(I2C_1_INST)&(DL_I2C_CONTROLLER_STATUS_ERROR)){
  63. printf("ERROR ***I2C Write Error: Bus is stuck***\n");
  64. DL_I2C_resetControllerTransfer(I2C_1_INST);
  65. return false;
  66. }
  67. // Wait until TX FIFO is empty (TX complete)
  68. timeout = 10000;
  69. while ((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && timeout--);
  70. if (timeout == 0) {
  71. printf("Bus stuck during TX.\n");
  72. DL_I2C_resetControllerTransfer(I2C_1_INST);
  73. return false;
  74. }
  75. //delay_cycles(1000);
  76. //Re-initializing te timeout value for Rx:
  77. timeout= 10000;
  78. rxPacket.rxLen= sizeof(BatteryMeasurement);
  79. rxPacket.rxCount= 0;
  80. rxPacket.rxComplete= false;
  81. DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
  82. //BatteryMeasurement size is 8 similar to the target side
  83. //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);
  84. DL_I2C_startControllerTransfer(I2C_1_INST, target_address, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen);
  85. while((DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && timeout--);
  86. if(timeout == 0 || (DL_I2C_getSCLStatus(I2C_1_INST)== DL_I2C_CONTROLLER_SCL_LOW)){
  87. printf("Bus stuck during Rx transmit or SCL held LOW.\n");
  88. DL_I2C_resetControllerTransfer(I2C_1_INST);
  89. return false;
  90. }
  91. //DEBUG
  92. //printf("Rx Count: %d\n", rxPacket.rxCount);
  93. printf("Total Received Bytes out of total length of [0x%02X]: 0x%02X\n", rxPacket.rxLen, sizeof(rxPacket.rxBuffer[rxPacket.rxCount]));
  94. printf("Rx Complete: %d\n", rxPacket.rxComplete);
  95. //Check if all the data is received then store the battery limits in BatteryInfo struct:
  96. if(rxPacket.rxCount== (sizeof(BatteryMeasurement))){
  97. measurement.voltage= rxPacket.rxBuffer[0] | (rxPacket.rxBuffer[1] << 8);
  98. measurement.current= rxPacket.rxBuffer[2]|(rxPacket.rxBuffer[3] << 8);
  99. measurement.temperature = rxPacket.rxBuffer[4] | (rxPacket.rxBuffer[5] << 8);
  100. measurement.slot_state = (SlotState)(rxPacket.rxBuffer[6]);
  101. battery_data[slot_id].battery_measurement= measurement;
  102. //DEBUG
  103. printf("[I2C] Successfully read %d bytes from target 0x%02X\n", rxPacket.rxCount, target_address);
  104. printf("Voltage: %u\n", battery_data[slot_id].battery_measurement.voltage);
  105. printf("Current: %d\n", battery_data[slot_id].battery_measurement.current);
  106. printf("Temp: %u\n", battery_data[slot_id].battery_measurement.temperature);
  107. printf("Slot state: %u\n", battery_data[slot_id].battery_measurement.slot_state);
  108. return true;
  109. }
  110. return false;
  111. }
  112. //Clear error flag to the target to change it back to SLOT_STATE_OK
  113. //Format: command + ((slot_id) + data (optional))
  114. void controller_ClearError(uint8_t const TARGET_ADDRESS, uint8_t slot_id){
  115. uint8_t command= CMD_CLEAR_ERR| (slot_id<<4); //shift slot_id to the left by 4 bits
  116. printf("[MCU] Clear Error Bitmasked Command:: 0x%02X\n", command);
  117. txPacket.txBuffer[0]= command;
  118. txPacket.txBuffer[1]= slot_id;
  119. txPacket.txLen= sizeof(txPacket.txBuffer);
  120. while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  121. DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, txPacket.txLen);
  122. DL_I2C_fillControllerTXFIFO(I2C_1_INST, txPacket.txBuffer, txPacket.txLen);
  123. while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  124. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  125. }
  126. //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
  127. void controller_EvaluateBatterySlotState(uint8_t slot_id, BatteryMeasurement *measurement){
  128. static bool slot_sov_hov_state= false;
  129. if((measurement->slot_state== SLOT_ERR_HOV) || (measurement->slot_state == SLOT_STATE_SOV)){
  130. slot_sov_hov_state= true;
  131. if(battery_data[slot_id].pwm_value < (PWM_MAX_VALUE - PWM_INCREMENT_VALUE)){
  132. battery_data[slot_id].pwm_value+= PWM_INCREMENT_VALUE;
  133. printf("[Power Burning PWM] SOV/HOV state: Increased power burn PWM to %d\n", battery_data[slot_id].pwm_value);
  134. DL_TimerG_setCaptureCompareValue(PWM_0_INST, battery_data[slot_id].pwm_value, DL_TIMER_CC_0_INDEX);
  135. }
  136. }else if(measurement->slot_state== SLOT_STATE_OK && slot_sov_hov_state== true){
  137. if(battery_data[slot_id].pwm_value >= (INITIAL_PWM_VALUE + PWM_DECREMENT_VALUE)){
  138. battery_data[slot_id].pwm_value-= PWM_DECREMENT_VALUE;
  139. printf("[Power Burning PWM] OK state: Decreased power burn PWM to %d\n", battery_data[slot_id].pwm_value);
  140. DL_TimerG_setCaptureCompareValue(PWM_0_INST, battery_data[slot_id].pwm_value, DL_TIMER_CC_0_INDEX);
  141. }
  142. }else{
  143. printf("[Power Burning PWM] initial state: %d\n", battery_data[slot_id].pwm_value);
  144. DL_TimerG_setCaptureCompareValue(PWM_0_INST, battery_data[slot_id].pwm_value, DL_TIMER_CC_0_INDEX);
  145. }
  146. }