main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. volatile bool mcuSendCommand = false;
  9. volatile bool picommandPending = false;
  10. // Interrupt for I2C instance -> MCU to Target
  11. void I2C_1_INST_IRQHandler(void)
  12. {
  13. switch (DL_I2C_getPendingInterrupt(I2C_1_INST))
  14. {
  15. case DL_I2C_IIDX_CONTROLLER_START:
  16. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  17. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  18. break;
  19. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  20. mcuSendCommand= true;
  21. /* Store bytes received from target in Rx Msg Buffer */
  22. while (DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST) != true) {
  23. if (rx_packet.rxCount < rx_packet.rxLen) {
  24. rx_packet.rxBuffer[rx_packet.rxCount] = DL_I2C_receiveControllerData(I2C_1_INST);
  25. rx_packet.rxCount++;
  26. } else {
  27. /* Ignore and remove from FIFO if the buffer is full */
  28. DL_I2C_receiveControllerData(I2C_1_INST);
  29. }
  30. }
  31. break;
  32. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  33. /* Fill TX FIFO with bytes to send */
  34. mcuSendCommand = true;
  35. break;
  36. case DL_I2C_IIDX_CONTROLLER_STOP:
  37. mcuSendCommand = false;
  38. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  39. case DL_I2C_IIDX_CONTROLLER_NACK:
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. void I2C_0_INST_IRQHandler(void)
  46. {
  47. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  48. {
  49. case DL_I2C_IIDX_TARGET_START:
  50. DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  51. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  52. break;
  53. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  54. picommandPending = true;
  55. //printf("Rx Interrupt Triggered \n");
  56. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  57. return;
  58. }
  59. break;
  60. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  61. /* Fill TX FIFO with bytes to send */
  62. picommandPending = true;
  63. break;
  64. case DL_I2C_IIDX_TARGET_STOP:
  65. picommandPending = false;
  66. break;
  67. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. int main(void)
  74. {
  75. SYSCFG_DL_init();
  76. Battery_Init();
  77. //Interrupt routine for Pi
  78. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  79. //Interrupt for target mcu
  80. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  81. while(1)
  82. {
  83. if(picommandPending)
  84. { printf("Pi Interrupt Triggered.\n");
  85. pi_i2c_mcu();
  86. picommandPending = false;
  87. }
  88. if(mcuSendCommand)
  89. { printf("MCU Interrupt Triggered.\n");
  90. //GET command from the target: target_address, slot, data:
  91. controller_GetBatteryMeasurement(0x48, 0, &battery_data[0].battery_measurement);
  92. //Set the current to the target: command, slot, current_value
  93. controller_SetCurrent(0x48,0,1000);
  94. mcuSendCommand = false;
  95. }
  96. /*for(uint8_t slot=0; slot<NUM_SLOTS; slot++)
  97. {
  98. Battery_ReadState(slot);
  99. CC_CV_ControlCharging(slot, );
  100. }*/
  101. }
  102. }