main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include "src/adc_peripheral/adc_singleConversion.h"
  13. //define the varibales:
  14. volatile bool picommandPending = false;
  15. volatile bool watchdog_triggered= false;
  16. // Interrupt for I2C instance -> MCU to Target
  17. void I2C_1_INST_IRQHandler(void)
  18. {
  19. switch (DL_I2C_getPendingInterrupt(I2C_1_INST))
  20. {
  21. case DL_I2C_IIDX_CONTROLLER_START:
  22. break;
  23. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  24. while(DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST) != true) {
  25. if(rxPacket.rxCount < rxPacket.rxLen){
  26. //Get byte from the I2C RX FIFO of the target
  27. rxPacket.rxBuffer[rxPacket.rxCount++]= DL_I2C_receiveControllerData(I2C_1_INST);
  28. //rxPacket.rxCount++;
  29. }else{
  30. DL_I2C_receiveControllerData(I2C_1_INST);
  31. }
  32. }
  33. if(rxPacket.rxCount >= rxPacket.rxLen){
  34. DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP);
  35. rxPacket.rxComplete= true;
  36. }
  37. break;
  38. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  39. /* Fill TX FIFO with bytes to send */
  40. //DL_I2C_fillControllerTXFIFO(I2C_1_INST, (uint8_t *)&txPacket.txBuffer[0], 1);
  41. txPacket.txComplete= true;
  42. break;
  43. case DL_I2C_IIDX_CONTROLLER_STOP:
  44. rxPacket.rxComplete= true;
  45. txPacket.txComplete= true;
  46. DL_I2C_flushControllerRXFIFO(I2C_1_INST);
  47. DL_I2C_flushControllerTXFIFO(I2C_1_INST);
  48. break;
  49. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  50. break;
  51. case DL_I2C_IIDX_CONTROLLER_NACK:
  52. break;
  53. case DL_I2C_IIDX_TIMEOUT_A:
  54. case DL_I2C_IIDX_TIMEOUT_B:
  55. DL_I2C_flushTargetTXFIFO(I2C_1_INST);
  56. DL_I2C_flushTargetRXFIFO(I2C_1_INST);
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. void I2C_0_INST_IRQHandler(void)
  63. {
  64. switch (DL_I2C_getPendingInterrupt(I2C_0_INST))
  65. {
  66. case DL_I2C_IIDX_TARGET_START:
  67. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  68. break;
  69. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  70. if (DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)) {
  71. return;
  72. }
  73. picommandPending = true;
  74. break;
  75. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  76. /* Fill TX FIFO with bytes to send */
  77. picommandPending = true;
  78. break;
  79. case DL_I2C_IIDX_TARGET_STOP:
  80. picommandPending = true;
  81. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  82. DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  83. break;
  84. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. void TIMER_LED_INST_IRQHandler(void) {
  91. DL_TimerA_clearInterruptStatus(TIMER_LED_INST, DL_TIMERA_INTERRUPT_ZERO_EVENT);
  92. // Toggle the LED
  93. DL_GPIO_togglePins(GPIO_ControllerBoard_PORT, GPIO_ControllerBoard_LED_Error_PIN);
  94. }
  95. int main(void)
  96. {
  97. SYSCFG_DL_init();
  98. Battery_Init();
  99. //dynamic addressing function call for Pi
  100. dynamic_gpio_addressing();
  101. //Interrupt routine for Pi
  102. NVIC_EnableIRQ(I2C_0_INST_INT_IRQN);
  103. //Interrupt for target mcu
  104. NVIC_EnableIRQ(I2C_1_INST_INT_IRQN);
  105. NVIC_EnableIRQ(ADC_Controller_INST_INT_IRQN);
  106. NVIC_EnableIRQ(TIMER_LED_INST_INT_IRQN);
  107. DL_Timer_startCounter(TIMER_LED_INST);
  108. while(1)
  109. {
  110. if(picommandPending)
  111. { //printf("Pi Interrupt Triggered.\n");
  112. pi_i2c_mcu();
  113. picommandPending = false;
  114. }
  115. //DL_SYSCTL_getPendingNonMaskableInterrupt();
  116. //PWM
  117. processPWMController();
  118. /*for(uint8_t slot_id= 0; slot_id< NUM_SLOTS; slot_id++){
  119. //GET battery measurement from the Target
  120. getBatteryMeasurement(slot_id);
  121. //Reading battery state:
  122. Battery_StateCondition(slot_id);
  123. printf("Battery State: %d, Charging State:%d\n", battery_data[slot_id].battery_state, battery_data[slot_id].battery_charging_state);
  124. CC_CV_ControlCharging(slot_id);
  125. delay_cycles(MEASUREMENT_CHECK_INTERVAL);
  126. if(battery_data[slot_id].batteryLimitReceived){
  127. 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,
  128. battery_data[slot_id].charge_fraction, battery_data[slot_id].capacitance, battery_data[slot_id].cut_off_current);
  129. }
  130. }*/
  131. }
  132. }