main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP);
  37. rxPacket.rxComplete= true;
  38. }
  39. break;
  40. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  41. /* Fill TX FIFO with bytes to send */
  42. mcuSendCommand = true;
  43. DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1);
  44. txPacket.txComplete= true;
  45. break;
  46. case DL_I2C_IIDX_CONTROLLER_STOP:
  47. mcuSendCommand = true;
  48. rxPacket.rxComplete= true;
  49. txPacket.txComplete= true;
  50. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  51. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  52. break;
  53. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  54. break;
  55. case DL_I2C_IIDX_CONTROLLER_NACK:
  56. break;
  57. default:
  58. break;
  59. }
  60. }
  61. void I2C_0_INST_IRQHandler(void)
  62. {
  63. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  64. {
  65. case DL_I2C_IIDX_TARGET_START:
  66. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  67. break;
  68. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  69. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  70. return;
  71. }
  72. picommandPending = true;
  73. break;
  74. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  75. /* Fill TX FIFO with bytes to send */
  76. picommandPending = true;
  77. break;
  78. case DL_I2C_IIDX_TARGET_STOP:
  79. picommandPending = true;
  80. //DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  81. //DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  82. break;
  83. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. int main(void)
  90. {
  91. SYSCFG_DL_init();
  92. Battery_Init();
  93. //dynamic addressing function call for Pi
  94. dynamic_gpio_addressing();
  95. //Interrupt routine for Pi
  96. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  97. //Interrupt for target mcu
  98. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  99. while(1)
  100. {
  101. if(picommandPending)
  102. { //printf("Pi Interrupt Triggered.\n");
  103. pi_i2c_mcu();
  104. picommandPending = false;
  105. }
  106. if(mcuSendCommand){
  107. //printf("MCU Interrupt Triggered.\n");
  108. //getBatteryMeasurement_test();
  109. mcuSendCommand = false;
  110. }
  111. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  112. //GET battery measurement from the Target
  113. getBatteryMeasurement_test(slot_id);
  114. //Reading battery state:
  115. Battery_StateCondition(slot_id);
  116. printf("Battery State: %d\n", battery_data[slot_id].battery_state);
  117. //If target received battery limits from Pi then start charging:
  118. if(battery_data[slot_id].batteryLimitReceived){
  119. printf("Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  120. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u, Cycle Number: %u\n", slot_id, battery_data[slot_id].max_voltage,
  121. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  122. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction, battery_data[slot_id].cycle_number);
  123. //CC_CV_ControlCharging(slot_id, 100);
  124. controller_SetCurrent(0x48, slot_id, 50);
  125. }
  126. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  127. }
  128. }
  129. }