main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /*Timer feeds the WWDT. the initialization of the Timer is done in the config.h file
  74. Timer is configured to reset the watchdog timer periodically.
  75. LFCLK is sourced from the internal low frequency oscilloscope.
  76. The device remains in STANDBY mode while waiting for an interrupt
  77. */
  78. //interrupt added for Windows Watchdog Timer:
  79. void TIMER_0_INST_IRQHandler(void)
  80. {
  81. switch (DL_TimerG_getPendingInterrupt(TIMER_0_INST)) {
  82. case DL_TIMER_IIDX_ZERO:
  83. if (DL_WWDT_getPendingInterrupt(WWDT0)) {
  84. //Restart the WWDT peripheral:
  85. DL_WWDT_restart(WWDT0);
  86. //Set the flag to True
  87. watchdog_triggered= true;
  88. }
  89. default:
  90. break;
  91. }
  92. }
  93. int main(void)
  94. {
  95. SYSCFG_DL_init();
  96. Battery_Init();
  97. //dynamic addressing function call for Pi
  98. dynamic_gpio_addressing();
  99. /* Enable WWDT interrupts on device */
  100. NVIC_EnableIRQ(WWDT0_INT_IRQN);
  101. //Interrupt routine for Pi
  102. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  103. //Interrupt for target mcu
  104. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  105. /*
  106. * Configures timer to be halted if CPU is halted. This ensures
  107. * that timer is always aligned with the WWDT so it is never serviced
  108. * too late or too early.
  109. */
  110. DL_Timer_setCoreHaltBehavior(TIMER_0_INST, DL_TIMER_CORE_HALT_IMMEDIATE);
  111. /* Start TimerG counter */
  112. DL_TimerG_startCounter(TIMER_0_INST);
  113. while(1)
  114. {
  115. if(picommandPending)
  116. { printf("Pi Interrupt Triggered.\n");
  117. pi_i2c_mcu();
  118. picommandPending = false;
  119. }
  120. if(mcuSendCommand){
  121. printf("MCU Interrupt Triggered.\n");
  122. for(uint8_t i=0; i<NUM_SLOTS; i++){
  123. controller_GetBatteryMeasurement(TARGET_BASE_ADDRESS, i);
  124. }
  125. mcuSendCommand = false;
  126. }
  127. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  128. //Reading battery state:
  129. Battery_ReadState(slot_id);
  130. 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,
  131. battery_data[slot_id].battery_measurement.current, battery_data[slot_id].battery_measurement.temperature, battery_data[slot_id].battery_measurement.slot_state);
  132. //If target received battery limits from Pi then start charging:
  133. if(battery_data[slot_id].batteryLimitReceived){
  134. printf("STATUS ***Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  135. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u***\n", slot_id, battery_data[slot_id].max_voltage,
  136. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  137. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction);
  138. CC_CV_ControlCharging(slot_id, 50);
  139. }
  140. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  141. }
  142. }
  143. }