main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "ti/driverlib/m0p/dl_core.h"
  2. #include "ti_msp_dl_config.h"
  3. #include "src/pi/i2c_pi_target.h"
  4. #include "src/controller/controller.h"
  5. #include "ti/driverlib/dl_i2c.h"
  6. #include "src/battery_data/battery.h"
  7. #include "src/cc_cv_charging.h"
  8. #include <stdio.h>
  9. #include "src/battery_data/battery.h"
  10. #include "mock_setup.h"
  11. //define the varibales:
  12. volatile bool mcuSendCommand = false;
  13. volatile bool picommandPending = false;
  14. volatile bool watchdog_triggered= false;
  15. volatile uint8_t current_command= 0x06;
  16. volatile bool txCommandSent= false;
  17. // Interrupt for I2C instance -> MCU to Target
  18. void I2C_1_INST_IRQHandler(void)
  19. {
  20. switch (DL_I2C_getPendingInterrupt(I2C_1_INST))
  21. {
  22. case DL_I2C_IIDX_CONTROLLER_START:
  23. break;
  24. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  25. mcuSendCommand= true;
  26. while(DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST) != true) {
  27. if(rxPacket.rxCount < rxPacket.rxLen){
  28. //Get byte from the I2C RX FIFO of the target
  29. rxPacket.rxBuffer[rxPacket.rxCount++]= DL_I2C_receiveControllerData(I2C_1_INST);
  30. //rxPacket.rxCount++;
  31. }else{
  32. DL_I2C_receiveControllerData(I2C_1_INST);
  33. }
  34. }
  35. if(rxPacket.rxCount >= rxPacket.rxLen){
  36. //rxPacket.rxComplete= true;
  37. DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP);
  38. rxPacket.rxComplete= true;
  39. }
  40. break;
  41. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  42. /* Fill TX FIFO with bytes to send */
  43. mcuSendCommand = true;
  44. DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1);
  45. txPacket.txComplete= true;
  46. break;
  47. case DL_I2C_IIDX_CONTROLLER_STOP:
  48. mcuSendCommand = true;
  49. rxPacket.rxComplete= true;
  50. txPacket.txComplete= true;
  51. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  52. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  53. break;
  54. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  55. break;
  56. case DL_I2C_IIDX_CONTROLLER_NACK:
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. void I2C_0_INST_IRQHandler(void)
  63. {
  64. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  65. {
  66. case DL_I2C_IIDX_TARGET_START:
  67. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  68. break;
  69. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  70. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  71. return;
  72. }
  73. picommandPending = true;
  74. break;
  75. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  76. /* Fill TX FIFO with bytes to send */
  77. picommandPending = true;
  78. break;
  79. case DL_I2C_IIDX_TARGET_STOP:
  80. picommandPending = true;
  81. //DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  82. //DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  83. break;
  84. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. int main(void)
  91. {
  92. SYSCFG_DL_init();
  93. Battery_Init();
  94. //dynamic addressing function call for Pi
  95. dynamic_gpio_addressing();
  96. //Interrupt routine for Pi
  97. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  98. //Interrupt for target mcu
  99. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  100. while(1)
  101. {
  102. if(picommandPending)
  103. { //printf("Pi Interrupt Triggered.\n");
  104. pi_i2c_mcu();
  105. picommandPending = false;
  106. }
  107. if(mcuSendCommand){
  108. //printf("MCU Interrupt Triggered.\n");
  109. //getBatteryMeasurement_test();
  110. mcuSendCommand = false;
  111. }
  112. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  113. //GET battery measurement from the Target
  114. getBatteryMeasurement_test(slot_id);
  115. //Reading battery state:
  116. Battery_StateCondition(slot_id);
  117. printf("Battery State: %d\n", battery_data[slot_id].battery_state);
  118. //If target received battery limits from Pi then start charging:
  119. if(battery_data[slot_id].batteryLimitReceived){
  120. printf("Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  121. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u, Cycle Number: %u\n", slot_id, battery_data[slot_id].max_voltage,
  122. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  123. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction, battery_data[slot_id].cycle_number);
  124. CC_CV_ControlCharging(slot_id, 50);
  125. }
  126. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  127. }
  128. }
  129. }