main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  38. break;
  39. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  40. /* Fill TX FIFO with bytes to send */
  41. mcuSendCommand = true;
  42. txPacket.txBuffer[0] = (0<<4) | (CMD_GET_MEASUREMENT & 0x0F);
  43. DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1);
  44. /*if(!txCommandSent){
  45. txPacket.txBuffer[0]= (0<<4) | (CMD_GET_MEASUREMENT & 0x0F);
  46. DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1);
  47. txPacket.txComplete= true;
  48. mcuSendCommand = true;
  49. txCommandSent= true;
  50. }*/
  51. /*else{
  52. while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY);
  53. DL_I2C_startControllerTransfer(I2C_1_INST, 0x49, DL_I2C_CONTROLLER_DIRECTION_RX, 8);
  54. txCommandSent= false;
  55. } */
  56. break;
  57. case DL_I2C_IIDX_CONTROLLER_STOP:
  58. mcuSendCommand = true;
  59. rxPacket.rxComplete= true;
  60. txPacket.txComplete= true;
  61. break;
  62. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  63. case DL_I2C_IIDX_CONTROLLER_NACK:
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. void I2C_0_INST_IRQHandler(void)
  70. {
  71. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  72. {
  73. case DL_I2C_IIDX_TARGET_START:
  74. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  75. break;
  76. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  77. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  78. return;
  79. }
  80. picommandPending = true;
  81. break;
  82. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  83. /* Fill TX FIFO with bytes to send */
  84. picommandPending = true;
  85. break;
  86. case DL_I2C_IIDX_TARGET_STOP:
  87. picommandPending = true;
  88. //DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  89. //DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  90. break;
  91. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. int main(void)
  98. {
  99. SYSCFG_DL_init();
  100. Battery_Init();
  101. //dynamic addressing function call for Pi
  102. dynamic_gpio_addressing();
  103. //Interrupt routine for Pi
  104. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  105. //Interrupt for target mcu
  106. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  107. while(1)
  108. {
  109. if(picommandPending)
  110. { //printf("Pi Interrupt Triggered.\n");
  111. pi_i2c_mcu();
  112. picommandPending = false;
  113. }
  114. if(mcuSendCommand){
  115. //printf("MCU Interrupt Triggered.\n");
  116. //getBatteryMeasurement_test();
  117. mcuSendCommand = false;
  118. }
  119. for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  120. getBatteryMeasurement_test();
  121. //controller_GetBatteryMeasurement(slot_id);
  122. //Reading the battery measurement:
  123. //printf("mainloop slot?\n");
  124. /*if (!was_true) {
  125. printf("executing?\n");
  126. controller_GetBatteryMeasurement(slot_id);
  127. was_true =
  128. printf("true? %d\n", was_true);
  129. }*/
  130. //Reading battery state:
  131. Battery_StateCondition(slot_id);
  132. //Reading Charging state condition
  133. //CC_CV_ControlCharging(slot_id, 50);
  134. //printf("Battery Charging State: %u\n", battery_data[slot_id].battery_charging_state);
  135. //printf("Battery Limit Received: %u\n", battery_data[slot_id].batteryLimitReceived);
  136. //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,
  137. //battery_data[slot_id].battery_measurement.current, battery_data[slot_id].battery_measurement.temperature, battery_data[slot_id].battery_measurement.slot_state);
  138. //If target received battery limits from Pi then start charging:
  139. if(battery_data[slot_id].batteryLimitReceived){
  140. printf("Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
  141. "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u, Cycle Number: %u\n", slot_id, battery_data[slot_id].max_voltage,
  142. battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
  143. battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction, battery_data[slot_id].cycle_number);
  144. }
  145. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  146. }
  147. }
  148. }