main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /*Timer feeds the WWDT. the initialization of the Timer is done in the config.h file
  73. Timer is configured to reset the watchdog timer periodically.
  74. LFCLK is sourced from the internal low frequency oscilloscope.
  75. The device remains in STANDBY mode while waiting for an interrupt
  76. */
  77. //interrupt added for Windows Watchdog Timer:
  78. void GROUP0_IRQHandler(void)
  79. {
  80. switch (DL_Interrupt_getPendingGroup(DL_INTERRUPT_GROUP_0)) {
  81. case DL_INTERRUPT_GROUP0_IIDX_WWDT0:
  82. if (DL_WWDT_getPendingInterrupt(WWDT0)) {
  83. //Clears the interrupt
  84. DL_WWDT_clearInterruptStatus(WWDT0);
  85. //Resets the timer:
  86. DL_WWDT_reset(WWDT0);
  87. //Set the flag to True
  88. watchdog_triggered= true;
  89. //how to handle in the case of failure?
  90. }
  91. default:
  92. break;
  93. }
  94. }
  95. int main(void)
  96. {
  97. SYSCFG_DL_init();
  98. Battery_Init();
  99. //dynamic addressing function call for Pi
  100. dynamic_gpio_addressing();
  101. /* Enable WWDT interrupts on device */
  102. NVIC_EnableIRQ(WWDT0_INT_IRQN);
  103. //Interrupt routine for Pi
  104. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  105. //Interrupt for target mcu
  106. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  107. //DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
  108. while(1)
  109. {
  110. if(watchdog_triggered){
  111. printf("ERROR: ***WATCHDOG TRIGGERED***\n");
  112. //Resetting the flags to its original state
  113. picommandPending= false;
  114. mcuSendCommand= false;
  115. watchdog_triggered= false;
  116. //Reinitialize the system
  117. }
  118. if(picommandPending)
  119. { printf("Pi Interrupt Triggered.\n");
  120. pi_i2c_mcu();
  121. picommandPending = false;
  122. }
  123. if(mcuSendCommand){
  124. printf("MCU Interrupt Triggered.\n");
  125. for(uint8_t i=0; i<NUM_SLOTS; i++){
  126. controller_GetBatteryMeasurement(TARGET_MCU_ADDRESS, i);
  127. }
  128. mcuSendCommand = false;
  129. }
  130. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  131. //Reading battery state:
  132. Battery_ReadState(slot_id);
  133. 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,
  134. battery_data[slot_id].battery_measurement.current, battery_data[slot_id].battery_measurement.temperature, battery_data[slot_id].battery_measurement.slot_state);
  135. //If target received battery limits from Pi then start charging:
  136. if(battery_data[slot_id].batteryLimitReceived){
  137. printf("STATUS ***Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  138. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u***\n", slot_id, battery_data[slot_id].max_voltage,
  139. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  140. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction);
  141. CC_CV_ControlCharging(slot_id, 50);
  142. }
  143. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  144. }
  145. }
  146. }