main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "ti/driverlib/dl_wwdt.h"
  2. #include "ti/driverlib/m0p/dl_core.h"
  3. #include "ti_msp_dl_config.h"
  4. #include "src/pi/i2c_pi_target.h"
  5. #include "src/controller/controller.h"
  6. #include "ti/driverlib/dl_i2c.h"
  7. #include "src/battery_data/battery.h"
  8. #include "src/cc_cv_charging.h"
  9. #include <stdio.h>
  10. #include "src/battery_data/battery.h"
  11. #include "mock_setup.h"
  12. //define the varibales:
  13. volatile bool mcuSendCommand = false;
  14. volatile bool picommandPending = false;
  15. volatile bool watchdog_triggered= false;
  16. // Interrupt for I2C instance -> MCU to Target
  17. void I2C_1_INST_IRQHandler(void)
  18. {
  19. switch (DL_I2C_getPendingInterrupt(I2C_1_INST))
  20. {
  21. case DL_I2C_IIDX_CONTROLLER_START:
  22. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  23. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  24. break;
  25. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  26. if (DL_I2C_isTargetRXFIFOEmpty(I2C_1_INST)) {
  27. return;
  28. }
  29. mcuSendCommand= true;
  30. break;
  31. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  32. /* Fill TX FIFO with bytes to send */
  33. mcuSendCommand = true;
  34. break;
  35. case DL_I2C_IIDX_CONTROLLER_STOP:
  36. mcuSendCommand = true;
  37. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  38. case DL_I2C_IIDX_CONTROLLER_NACK:
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. void I2C_0_INST_IRQHandler(void)
  45. {
  46. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  47. {
  48. case DL_I2C_IIDX_TARGET_START:
  49. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  50. break;
  51. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  52. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  53. return;
  54. }
  55. picommandPending = true;
  56. break;
  57. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  58. /* Fill TX FIFO with bytes to send */
  59. picommandPending = true;
  60. break;
  61. case DL_I2C_IIDX_TARGET_STOP:
  62. picommandPending = true;
  63. //DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  64. //DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  65. break;
  66. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. //interrupt added for Windows Watchdog Timer:
  73. void GROUP0_IRQHandler(void)
  74. {
  75. switch (DL_Interrupt_getPendingGroup(DL_INTERRUPT_GROUP_0)) {
  76. case DL_INTERRUPT_GROUP0_IIDX_WWDT0:
  77. if (DL_WWDT_getPendingInterrupt(WWDT0)) {
  78. //Clears the interrupt
  79. DL_WWDT_clearInterruptStatus(WWDT0);
  80. //Resets the timer:
  81. DL_WWDT_reset(WWDT0);
  82. //Set the flag to True
  83. watchdog_triggered= true;
  84. //how to handle in the case of failure?
  85. }
  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. /* Enable WWDT interrupts on device */
  97. NVIC_EnableIRQ(WWDT0_INT_IRQN);
  98. //Interrupt routine for Pi
  99. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  100. //Interrupt for target mcu
  101. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  102. //DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
  103. while(1)
  104. {
  105. if(watchdog_triggered){
  106. printf("ERROR: ***WATCHDOG TRIGGERED***\n");
  107. //Resetting the flags to its original state
  108. picommandPending= false;
  109. mcuSendCommand= false;
  110. watchdog_triggered= false;
  111. //Reinitialize the system
  112. }
  113. if(picommandPending)
  114. { printf("Pi Interrupt Triggered.\n");
  115. pi_i2c_mcu();
  116. picommandPending = false;
  117. }
  118. if(mcuSendCommand){
  119. printf("MCU Interrupt Triggered.\n");
  120. for(uint8_t i=0; i<NUM_SLOTS; i++){
  121. controller_GetBatteryMeasurement(TARGET_MCU_ADDRESS, i);
  122. }
  123. mcuSendCommand = false;
  124. }
  125. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  126. //Reading battery state:
  127. Battery_ReadState(slot_id);
  128. 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,
  129. battery_data[slot_id].battery_measurement.current, battery_data[slot_id].battery_measurement.temperature, battery_data[slot_id].battery_measurement.slot_state);
  130. //If target received battery limits from Pi then start charging:
  131. if(battery_data[slot_id].batteryLimitReceived){
  132. printf("STATUS ***Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  133. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u***\n", slot_id, battery_data[slot_id].max_voltage,
  134. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  135. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction);
  136. CC_CV_ControlCharging(slot_id, 50);
  137. }
  138. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  139. }
  140. }
  141. }