main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //#include "ti/driverlib/dl_timer.h"
  2. //#include "ti/driverlib/dl_wwdt.h"
  3. #include "ti/driverlib/m0p/dl_core.h"
  4. #include "ti_msp_dl_config.h"
  5. #include "src/pi/i2c_pi_target.h"
  6. #include "src/controller/controller.h"
  7. #include "ti/driverlib/dl_i2c.h"
  8. #include "src/battery_data/battery.h"
  9. #include "src/cc_cv_charging.h"
  10. #include <stdio.h>
  11. #include "src/battery_data/battery.h"
  12. #include "mock_setup.h"
  13. //define the varibales:
  14. volatile bool mcuSendCommand = false;
  15. volatile bool picommandPending = false;
  16. volatile bool watchdog_triggered= 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. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  24. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  25. break;
  26. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  27. if (DL_I2C_isTargetRXFIFOEmpty(I2C_1_INST)) {
  28. return;
  29. }
  30. mcuSendCommand= true;
  31. break;
  32. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  33. /* Fill TX FIFO with bytes to send */
  34. mcuSendCommand = true;
  35. break;
  36. case DL_I2C_IIDX_CONTROLLER_STOP:
  37. mcuSendCommand = true;
  38. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  39. case DL_I2C_IIDX_CONTROLLER_NACK:
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. void I2C_0_INST_IRQHandler(void)
  46. {
  47. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  48. {
  49. case DL_I2C_IIDX_TARGET_START:
  50. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  51. break;
  52. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  53. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  54. return;
  55. }
  56. picommandPending = true;
  57. break;
  58. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  59. /* Fill TX FIFO with bytes to send */
  60. picommandPending = true;
  61. break;
  62. case DL_I2C_IIDX_TARGET_STOP:
  63. picommandPending = true;
  64. //DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  65. //DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  66. break;
  67. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. //interrupt added for Windows Watchdog Timer:
  74. void TIMER_0_INST_IRQHandler(void)
  75. {
  76. switch (DL_TimerG_getPendingInterrupt(TIMER_0_INST)) {
  77. case DL_TIMER_IIDX_ZERO:
  78. if (DL_WWDT_getPendingInterrupt(WWDT0)) {
  79. //Restart the WWDT peripheral:
  80. DL_WWDT_restart(WWDT0);
  81. //Set the flag to True
  82. watchdog_triggered= true;
  83. }
  84. default:
  85. break;
  86. }
  87. }
  88. int main(void)
  89. {
  90. SYSCFG_DL_init();
  91. Battery_Init();
  92. //dynamic addressing function call for Pi
  93. dynamic_gpio_addressing();
  94. /* Enable WWDT interrupts on device */
  95. NVIC_EnableIRQ(WWDT0_INT_IRQN);
  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. /*
  101. * Configures timer to be halted if CPU is halted. This ensures
  102. * that timer is always aligned with the WWDT so it is never serviced
  103. * too late or too early.
  104. */
  105. DL_Timer_setCoreHaltBehavior(TIMER_0_INST, DL_TIMER_CORE_HALT_IMMEDIATE);
  106. /* Start TimerG counter */
  107. DL_TimerG_startCounter(TIMER_0_INST);
  108. while(1)
  109. {
  110. if(picommandPending)
  111. { printf("Pi Interrupt Triggered.\n");
  112. pi_i2c_mcu();
  113. picommandPending = false;
  114. }
  115. if(mcuSendCommand){
  116. printf("MCU Interrupt Triggered.\n");
  117. for(uint8_t i=0; i<NUM_SLOTS; i++){
  118. controller_GetBatteryMeasurement(TARGET_BASE_ADDRESS, i);
  119. }
  120. mcuSendCommand = false;
  121. }
  122. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  123. //Reading battery state:
  124. Battery_ReadState(slot_id);
  125. printf("STATUS ***Reading Battery Measurement for Slot ID %u:: Battery State: %u, Voltage: %u, Current: %u, Temperature: %u, Slot state: %u***\n", slot_id, battery_data[slot_id].battery_state, battery_data[slot_id].battery_measurement.voltage,
  126. battery_data[slot_id].battery_measurement.current, battery_data[slot_id].battery_measurement.temperature, battery_data[slot_id].battery_measurement.slot_state);
  127. //If target received battery limits from Pi then start charging:
  128. if(battery_data[slot_id].batteryLimitReceived){
  129. printf("STATUS ***Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  130. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u***\n", slot_id, battery_data[slot_id].max_voltage,
  131. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  132. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction);
  133. CC_CV_ControlCharging(slot_id, 50);
  134. }
  135. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  136. }
  137. }
  138. }