main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "ti/driverlib/m0p/dl_core.h"
  2. #include "ti_msp_dl_config.h"
  3. #include "src/pi/i2c_pi_target.h"
  4. #include "src/controller/controller.h"
  5. #include "ti/driverlib/dl_i2c.h"
  6. #include "src/battery_data/battery.h"
  7. #include "src/cc_cv_charging.h"
  8. #include <stdio.h>
  9. #include "src/battery_data/battery.h"
  10. #include "mock_setup.h"
  11. //define the varibales:
  12. volatile bool picommandPending = false;
  13. volatile bool watchdog_triggered= false;
  14. // Interrupt for I2C instance -> MCU to Target
  15. void I2C_1_INST_IRQHandler(void)
  16. {
  17. switch (DL_I2C_getPendingInterrupt(I2C_1_INST))
  18. {
  19. case DL_I2C_IIDX_CONTROLLER_START:
  20. break;
  21. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  22. while(DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST) != true) {
  23. if(rxPacket.rxCount < rxPacket.rxLen){
  24. //Get byte from the I2C RX FIFO of the target
  25. rxPacket.rxBuffer[rxPacket.rxCount++]= DL_I2C_receiveControllerData(I2C_1_INST);
  26. //rxPacket.rxCount++;
  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. rxPacket.rxComplete= true;
  43. txPacket.txComplete= true;
  44. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  45. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  46. break;
  47. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  48. break;
  49. case DL_I2C_IIDX_CONTROLLER_NACK:
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. void I2C_0_INST_IRQHandler(void)
  56. {
  57. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  58. {
  59. case DL_I2C_IIDX_TARGET_START:
  60. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  61. break;
  62. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  63. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  64. return;
  65. }
  66. picommandPending = true;
  67. break;
  68. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  69. /* Fill TX FIFO with bytes to send */
  70. picommandPending = true;
  71. break;
  72. case DL_I2C_IIDX_TARGET_STOP:
  73. picommandPending = true;
  74. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  75. DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  76. break;
  77. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. int main(void)
  84. {
  85. SYSCFG_DL_init();
  86. Battery_Init();
  87. //dynamic addressing function call for Pi
  88. dynamic_gpio_addressing();
  89. //Interrupt routine for Pi
  90. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  91. //Interrupt for target mcu
  92. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  93. while(1)
  94. {
  95. if(picommandPending)
  96. { //printf("Pi Interrupt Triggered.\n");
  97. pi_i2c_mcu();
  98. picommandPending = false;
  99. }
  100. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  101. //GET battery measurement from the Target
  102. getBatteryMeasurement(slot_id);
  103. //Reading battery state:
  104. Battery_StateCondition(slot_id);
  105. printf("Battery State: %d\n", battery_data[slot_id].battery_state);
  106. //If target received battery limits from Pi then start charging, else wait with state: 3::
  107. //CC_CV_ControlCharging(slot_id, 50);
  108. if(battery_data[slot_id].batteryLimitReceived){
  109. printf("Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  110. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u, Cycle Number: %u\n", slot_id, battery_data[slot_id].max_voltage,
  111. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  112. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction, battery_data[slot_id].cycle_number);
  113. CC_CV_ControlCharging(slot_id, 50);
  114. }
  115. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  116. }
  117. }
  118. }