main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "ti/driverlib/m0p/dl_core.h"
  2. #include "ti/driverlib/m0p/sysctl/dl_sysctl_mspm0g1x0x_g3x0x.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 picommandPending = false;
  14. volatile bool watchdog_triggered= false;
  15. // Interrupt for I2C instance -> MCU to Target
  16. void I2C_1_INST_IRQHandler(void)
  17. {
  18. switch (DL_I2C_getPendingInterrupt(I2C_1_INST))
  19. {
  20. case DL_I2C_IIDX_CONTROLLER_START:
  21. break;
  22. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  23. while(DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST) != true) {
  24. if(rxPacket.rxCount < rxPacket.rxLen){
  25. //Get byte from the I2C RX FIFO of the target
  26. rxPacket.rxBuffer[rxPacket.rxCount++]= DL_I2C_receiveControllerData(I2C_1_INST);
  27. }else{
  28. DL_I2C_receiveControllerData(I2C_1_INST);
  29. }
  30. }
  31. if(rxPacket.rxCount >= rxPacket.rxLen){
  32. DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP);
  33. rxPacket.rxComplete= true;
  34. }
  35. break;
  36. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  37. /* Fill TX FIFO with bytes to send */
  38. //DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1);
  39. txPacket.txComplete= true;
  40. break;
  41. case DL_I2C_IIDX_CONTROLLER_STOP:
  42. break;
  43. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  44. break;
  45. case DL_I2C_IIDX_CONTROLLER_NACK:
  46. break;
  47. case DL_I2C_IIDX_TIMEOUT_A:
  48. case DL_I2C_IIDX_TIMEOUT_B:
  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. int main(void)
  83. {
  84. SYSCFG_DL_init();
  85. Battery_Init();
  86. //dynamic addressing function call for Pi
  87. dynamic_gpio_addressing();
  88. //Interrupt routine for Pi
  89. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  90. //Interrupt for target mcu
  91. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  92. while(1)
  93. {
  94. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  95. if(picommandPending)
  96. { //printf("Pi Interrupt Triggered.\n");
  97. pi_i2c_mcu();
  98. picommandPending = false;
  99. }
  100. //GET battery measurement from the Target
  101. controllerGetBatteryMeasurement(slot_id);
  102. //Reading battery state:
  103. Battery_StateCondition(slot_id);
  104. //printf("Battery State: %d, Charging State:%d\n", battery_data[slot_id].battery_state, battery_data[slot_id].battery_charging_state);
  105. //function for CC-CV Charging cycle
  106. CC_CV_ControlCharging(slot_id);
  107. if(battery_data[slot_id].batteryLimitReceived){
  108. printf("Slot Id: %d, Min Voltage: %d, Max Voltage: %d, Charge Fraction:%d, Capacitance: %d, CutOff Current:%d\n",slot_id, battery_data[slot_id].min_voltage, battery_data[slot_id].max_voltage,
  109. battery_data[slot_id].charge_fraction, battery_data[slot_id].capacitance, battery_data[slot_id].cut_off_current);
  110. }
  111. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  112. }
  113. }
  114. }