main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. //rxPacket.rxCount++;
  28. }else{
  29. DL_I2C_receiveControllerData(I2C_1_INST);
  30. }
  31. }
  32. if(rxPacket.rxCount >= rxPacket.rxLen){
  33. DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP);
  34. rxPacket.rxComplete= true;
  35. }
  36. break;
  37. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  38. /* Fill TX FIFO with bytes to send */
  39. //DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1);
  40. txPacket.txComplete= true;
  41. break;
  42. case DL_I2C_IIDX_CONTROLLER_STOP:
  43. rxPacket.rxComplete= true;
  44. txPacket.txComplete= true;
  45. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  46. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  47. break;
  48. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  49. break;
  50. case DL_I2C_IIDX_CONTROLLER_NACK:
  51. break;
  52. case DL_I2C_IIDX_TIMEOUT_A:
  53. case DL_I2C_IIDX_TIMEOUT_B:
  54. DL_I2C_flushTargetTXFIFO(I2C_1_INST);
  55. DL_I2C_flushTargetRXFIFO(I2C_1_INST);
  56. break;
  57. default:
  58. break;
  59. }
  60. }
  61. void I2C_0_INST_IRQHandler(void)
  62. {
  63. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  64. {
  65. case DL_I2C_IIDX_TARGET_START:
  66. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  67. break;
  68. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  69. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  70. return;
  71. }
  72. picommandPending = true;
  73. break;
  74. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  75. /* Fill TX FIFO with bytes to send */
  76. picommandPending = true;
  77. break;
  78. case DL_I2C_IIDX_TARGET_STOP:
  79. picommandPending = true;
  80. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  81. DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  82. break;
  83. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. int main(void)
  90. {
  91. SYSCFG_DL_init();
  92. Battery_Init();
  93. //dynamic addressing function call for Pi
  94. dynamic_gpio_addressing();
  95. //Interrupt routine for Pi
  96. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  97. //Interrupt for target mcu
  98. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  99. while(1)
  100. {
  101. if(picommandPending)
  102. { //printf("Pi Interrupt Triggered.\n");
  103. pi_i2c_mcu();
  104. picommandPending = false;
  105. }
  106. //DL_SYSCTL_getPendingNonMaskableInterrupt();
  107. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  108. //GET battery measurement from the Target
  109. getBatteryMeasurement(slot_id);
  110. //Reading battery state:
  111. Battery_StateCondition(slot_id);
  112. printf("Battery State: %d, Charging State:%d\n", battery_data[slot_id].battery_state, battery_data[slot_id].battery_charging_state);
  113. CC_CV_ControlCharging(slot_id);
  114. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  115. if(battery_data[slot_id].batteryLimitReceived){
  116. printf("Min Voltage: %d, Max Voltage: %d, Charge Fraction:%d, Capacitance: %d, CutOff Current:%d\n", battery_data[slot_id].min_voltage, battery_data[slot_id].max_voltage,
  117. battery_data[slot_id].charge_fraction, battery_data[slot_id].capacitance, battery_data[slot_id].cut_off_current);
  118. }
  119. }
  120. }
  121. }