main.c 4.8 KB

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