main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 mcuSendCommand = false;
  13. volatile bool picommandPending = false;
  14. volatile bool watchdog_triggered= false;
  15. volatile uint8_t current_command= 0x06;
  16. volatile bool txCommandSent= false;
  17. // Interrupt for I2C instance -> MCU to Target
  18. void I2C_1_INST_IRQHandler(void)
  19. {
  20. switch (DL_I2C_getPendingInterrupt(I2C_1_INST))
  21. {
  22. case DL_I2C_IIDX_CONTROLLER_START:
  23. break;
  24. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  25. mcuSendCommand= true;
  26. while(DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST) != true) {
  27. if(rxPacket.rxCount < rxPacket.rxLen){
  28. //Get byte from the I2C RX FIFO of the target
  29. rxPacket.rxBuffer[rxPacket.rxCount++]= DL_I2C_receiveControllerData(I2C_1_INST);
  30. //rxPacket.rxCount++;
  31. }else{
  32. DL_I2C_receiveControllerData(I2C_1_INST);
  33. }
  34. }
  35. if(rxPacket.rxCount >= rxPacket.rxLen){
  36. //rxPacket.rxComplete= true;
  37. DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP);
  38. rxPacket.rxComplete= true;
  39. }
  40. break;
  41. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  42. /* Fill TX FIFO with bytes to send */
  43. mcuSendCommand = true;
  44. txPacket.txBuffer[0] = (0<<4) | (CMD_GET_MEASUREMENT & 0x0F);
  45. DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1);
  46. txPacket.txComplete= true;
  47. break;
  48. case DL_I2C_IIDX_CONTROLLER_STOP:
  49. mcuSendCommand = true;
  50. rxPacket.rxComplete= true;
  51. txPacket.txComplete= true;
  52. DL_I2C_flushControllerRXFIFO(I2C_INST);
  53. DL_I2C_flushControllerTXFIFO(I2C_INST);
  54. break;
  55. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  56. break;
  57. case DL_I2C_IIDX_CONTROLLER_NACK:
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. void I2C_0_INST_IRQHandler(void)
  64. {
  65. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  66. {
  67. case DL_I2C_IIDX_TARGET_START:
  68. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  69. break;
  70. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  71. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  72. return;
  73. }
  74. picommandPending = true;
  75. break;
  76. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  77. /* Fill TX FIFO with bytes to send */
  78. picommandPending = true;
  79. break;
  80. case DL_I2C_IIDX_TARGET_STOP:
  81. picommandPending = true;
  82. //DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  83. //DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  84. break;
  85. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. int main(void)
  92. {
  93. SYSCFG_DL_init();
  94. Battery_Init();
  95. //dynamic addressing function call for Pi
  96. dynamic_gpio_addressing();
  97. //Interrupt routine for Pi
  98. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  99. //Interrupt for target mcu
  100. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  101. while(1)
  102. {
  103. if(picommandPending)
  104. { //printf("Pi Interrupt Triggered.\n");
  105. pi_i2c_mcu();
  106. picommandPending = false;
  107. }
  108. if(mcuSendCommand){
  109. //printf("MCU Interrupt Triggered.\n");
  110. //getBatteryMeasurement_test();
  111. mcuSendCommand = false;
  112. }
  113. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  114. getBatteryMeasurement_test();
  115. //controller_GetBatteryMeasurement(slot_id);
  116. //Reading the battery measurement:
  117. //printf("mainloop slot?\n");
  118. /*if (!was_true) {
  119. printf("executing?\n");
  120. controller_GetBatteryMeasurement(slot_id);
  121. was_true =
  122. printf("true? %d\n", was_true);
  123. }*/
  124. //Reading battery state:
  125. Battery_StateCondition(slot_id);
  126. //Reading Charging state condition
  127. //CC_CV_ControlCharging(slot_id, 50);
  128. //printf("Battery Charging State: %u\n", battery_data[slot_id].battery_charging_state);
  129. //printf("Battery Limit Received: %u\n", battery_data[slot_id].batteryLimitReceived);
  130. //printf("STATUS ***Reading Battery Measurement for Slot ID %u:: Battery State: %u, Voltage: %u, Current: %u, Temperature: %u, Slot state: %u***\n", slot_id, battery_data[slot_id].battery_state, battery_data[slot_id].battery_measurement.voltage,
  131. //battery_data[slot_id].battery_measurement.current, battery_data[slot_id].battery_measurement.temperature, battery_data[slot_id].battery_measurement.slot_state);
  132. //If target received battery limits from Pi then start charging:
  133. if(battery_data[slot_id].batteryLimitReceived){
  134. printf("Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  135. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u, Cycle Number: %u\n", slot_id, battery_data[slot_id].max_voltage,
  136. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  137. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction, battery_data[slot_id].cycle_number);
  138. }
  139. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  140. }
  141. }
  142. }