main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "ti_msp_dl_config.h"
  2. #include "src/pi/i2c_pi_target.h"
  3. #include "src/controller/controller.h"
  4. #include "ti/driverlib/dl_i2c.h"
  5. #include "src/battery_data/battery.h"
  6. #include "src/cc_cv_charging.h"
  7. #include <stdio.h>
  8. #include "src/battery_data/battery.h"
  9. #include "mock_setup.h"
  10. volatile bool mcuSendCommand = false;
  11. volatile bool picommandPending = false;
  12. // Interrupt for I2C instance -> MCU to Target
  13. void I2C_1_INST_IRQHandler(void)
  14. {
  15. switch (DL_I2C_getPendingInterrupt(I2C_1_INST))
  16. {
  17. case DL_I2C_IIDX_CONTROLLER_START:
  18. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  19. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  20. break;
  21. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  22. if (DL_I2C_isTargetRXFIFOEmpty(I2C_1_INST)) {
  23. return;
  24. }
  25. mcuSendCommand= true;
  26. break;
  27. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  28. /* Fill TX FIFO with bytes to send */
  29. mcuSendCommand = true;
  30. break;
  31. case DL_I2C_IIDX_CONTROLLER_STOP:
  32. mcuSendCommand = true;
  33. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  34. case DL_I2C_IIDX_CONTROLLER_NACK:
  35. break;
  36. default:
  37. break;
  38. }
  39. }
  40. void I2C_0_INST_IRQHandler(void)
  41. {
  42. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  43. {
  44. case DL_I2C_IIDX_TARGET_START:
  45. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  46. break;
  47. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  48. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  49. return;
  50. }
  51. picommandPending = true;
  52. break;
  53. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  54. /* Fill TX FIFO with bytes to send */
  55. picommandPending = true;
  56. break;
  57. case DL_I2C_IIDX_TARGET_STOP:
  58. picommandPending = true;
  59. //DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  60. //DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  61. break;
  62. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. int main(void)
  69. {
  70. SYSCFG_DL_init();
  71. Battery_Init();
  72. //Interrupt routine for Pi
  73. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  74. //Interrupt for target mcu
  75. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  76. while(1)
  77. {
  78. if(picommandPending)
  79. { printf("Pi Interrupt Triggered.\n");
  80. pi_i2c_mcu();
  81. picommandPending = false;
  82. }
  83. if(mcuSendCommand)
  84. { printf("MCU Interrupt Triggered.\n");
  85. controller_GetBatteryMeasurement(TARGET_MCU_ADDRESS, 0);
  86. //test_Controller_SetCurrent();
  87. //test_Controller_GetBatteryMeasurement();
  88. //controller_SetCurrent(TARGET_MCU_ADDRESS, 0 , 90);
  89. mcuSendCommand = false;
  90. }
  91. for(uint8_t slot_id= 0; slot_id<NUM_SLOTS; slot_id++){
  92. //Battery Measurement:
  93. printf("*** Battery Details: Slot: %u, Voltage:%u, Current: %u, Temperature:%u, Slot State:%u ***\n",
  94. slot_id, battery_data[slot_id].battery_measurement.voltage, battery_data[slot_id].battery_measurement.current, battery_data[slot_id].battery_measurement.temperature,
  95. battery_data[slot_id].battery_measurement.slot_state);
  96. //controller_EvaluateBatterySlotState(slot_id, &battery_data[slot_id].battery_measurement);
  97. Battery_ReadState(slot_id);
  98. /*printf("*** Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  99. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u ***\n", slot_id, battery_data[slot_id].max_voltage,
  100. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  101. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction);*/
  102. }
  103. }
  104. }