main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. break;
  23. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  24. mcuSendCommand= true;
  25. while(DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST) != true) {
  26. if(rxPacket.rxCount < rxPacket.rxLen){
  27. //Get byte from the I2C RX FIFO of the target
  28. rxPacket.rxBuffer[rxPacket.rxCount]= DL_I2C_receiveControllerData(I2C_1_INST);
  29. rxPacket.rxCount++;
  30. }else{
  31. DL_I2C_receiveControllerData(I2C_1_INST);
  32. }
  33. }
  34. if(rxPacket.rxCount >= rxPacket.rxLen){
  35. rxPacket.rxComplete= true;
  36. }
  37. break;
  38. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  39. /* Fill TX FIFO with bytes to send */
  40. mcuSendCommand = true;
  41. txPacket.txComplete= true;
  42. break;
  43. case DL_I2C_IIDX_CONTROLLER_STOP:
  44. mcuSendCommand = true;
  45. rxPacket.rxComplete= true;
  46. break;
  47. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  48. case DL_I2C_IIDX_CONTROLLER_NACK:
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. void I2C_0_INST_IRQHandler(void)
  55. {
  56. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  57. {
  58. case DL_I2C_IIDX_TARGET_START:
  59. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  60. break;
  61. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  62. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  63. return;
  64. }
  65. picommandPending = true;
  66. break;
  67. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  68. /* Fill TX FIFO with bytes to send */
  69. picommandPending = true;
  70. break;
  71. case DL_I2C_IIDX_TARGET_STOP:
  72. picommandPending = true;
  73. //DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  74. //DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  75. break;
  76. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. //interrupt added for Windows Watchdog Timer:
  83. /*void GROUP0_IRQHandler(void)
  84. {
  85. switch (DL_Interrupt_getPendingGroup(DL_INTERRUPT_GROUP_0)) {
  86. case DL_INTERRUPT_GROUP0_IIDX_WWDT0:
  87. if (DL_WWDT_getPendingInterrupt(WWDT0)) {
  88. //Clears the interrupt
  89. DL_WWDT_clearInterruptStatus(WWDT0);
  90. //Resets the timer:
  91. DL_WWDT_reset(WWDT0);
  92. //Set the flag to True
  93. watchdog_triggered= true;
  94. //how to handle in the case of failure?
  95. }
  96. default:
  97. break;
  98. }
  99. }*/
  100. int main(void)
  101. {
  102. SYSCFG_DL_init();
  103. Battery_Init();
  104. //dynamic addressing function call for Pi
  105. dynamic_gpio_addressing();
  106. /* Enable WWDT interrupts on device */
  107. //NVIC_EnableIRQ(WWDT0_INT_IRQN);
  108. //Interrupt routine for Pi
  109. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  110. //Interrupt for target mcu
  111. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  112. //DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
  113. bool was_true = false;
  114. while(1)
  115. {
  116. /*if(watchdog_triggered){
  117. printf("ERROR: ***WATCHDOG TRIGGERED***\n");
  118. //Resetting the flags to its original state
  119. picommandPending= false;
  120. mcuSendCommand= false;
  121. watchdog_triggered= false;
  122. //Reinitialize the system
  123. }*/
  124. if(picommandPending)
  125. { printf("Pi Interrupt Triggered.\n");
  126. pi_i2c_mcu();
  127. picommandPending = false;
  128. }
  129. if(mcuSendCommand){
  130. printf("MCU Interrupt Triggered.\n");
  131. mcuSendCommand = false;
  132. }
  133. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  134. //Reading the battery measurement:
  135. printf("mainloop slot?\n");
  136. if (!was_true) {
  137. printf("executing?\n");
  138. was_true = controller_GetBatteryMeasurement(slot_id);
  139. printf("true? %d\n", was_true);
  140. }
  141. //Reading battery state:
  142. Battery_ReadState(slot_id);
  143. //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,
  144. //battery_data[slot_id].battery_measurement.current, battery_data[slot_id].battery_measurement.temperature, battery_data[slot_id].battery_measurement.slot_state);
  145. //If target received battery limits from Pi then start charging:
  146. if(battery_data[slot_id].batteryLimitReceived){
  147. printf("Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  148. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u\n", slot_id, battery_data[slot_id].max_voltage,
  149. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  150. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction);
  151. CC_CV_ControlCharging(slot_id, 50);
  152. }
  153. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  154. }
  155. }
  156. }