main_target.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "src/battery_data/battery.h"
  2. #include "ti/driverlib/dl_i2c.h"
  3. #include "ti_msp_dl_config.h"
  4. //#include "ti/driverlib/dl_i2c.h"
  5. #include <stdio.h>
  6. #include "src/interfaces/i2c_target.h"
  7. #include "src/interfaces/i2c_controller.h"
  8. #include "src/config.h"
  9. int8_t handle_read_pending_slot = -1;
  10. void I2C_controller_INST_IRQHandler(void) {
  11. switch (DL_I2C_getPendingInterrupt(I2C_controller_INST)) {
  12. case DL_I2C_IIDX_CONTROLLER_START:
  13. controllerRxPackage.count = 0;
  14. DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
  15. break;
  16. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  17. /* Store bytes received from target in Rx Msg Buffer */
  18. while (DL_I2C_isControllerRXFIFOEmpty(I2C_controller_INST) != true) {
  19. if (controllerRxPackage.count < controllerRxPackage.len) {
  20. controllerRxPackage.packet[controllerRxPackage.count] =
  21. DL_I2C_receiveControllerData(I2C_controller_INST);
  22. ;
  23. controllerRxPackage.count++;
  24. } else {
  25. /* Ignore and remove from FIFO if the buffer is full */
  26. DL_I2C_receiveControllerData(I2C_controller_INST);
  27. }
  28. }
  29. if (controllerRxPackage.count >= controllerRxPackage.len) {
  30. controllerRxPackage.complete = true;
  31. DL_I2C_enableInterrupt(I2C_controller_INST,
  32. DL_I2C_INTERRUPT_CONTROLLER_STOP);
  33. }
  34. break;
  35. /*TRANSMIT data to ADC*/
  36. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  37. if (controllerTxPackage.count < controllerTxPackage.len) {
  38. DL_I2C_fillControllerTXFIFO(I2C_controller_INST,
  39. &controllerTxPackage.packet[controllerTxPackage.count],
  40. (controllerTxPackage.len - controllerTxPackage.count));
  41. controllerTxPackage.count++;
  42. } else {
  43. /*Prevent overflow and just ignore data*/
  44. DL_I2C_fillTargetTXFIFO(I2C_controller_INST, (uint8_t[]){0x00}, 1);
  45. controllerTxPackage.complete = true;
  46. }
  47. if(controllerTxPackage.count >= controllerTxPackage.len){
  48. controllerTxPackage.complete= true;
  49. }
  50. break;
  51. /*STOP condition*/
  52. case DL_I2C_IIDX_CONTROLLER_STOP:
  53. controllerTxPackage.complete = true;
  54. controllerRxPackage.complete = true;
  55. break;
  56. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  57. break;
  58. case DL_I2C_IIDX_CONTROLLER_NACK:
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. /**** Interrupt for Pi to MCU ****/
  65. void I2C_target_INST_IRQHandler(void) {
  66. uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
  67. switch (status) {
  68. case DL_I2C_IIDX_TARGET_START:
  69. break;
  70. case DL_I2C_IIDX_TARGET_STOP:
  71. break;
  72. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  73. // only use this function if we are async (filling buffers) - the rest is handled by the mainloop
  74. if (handle_read_pending_slot == -1) {
  75. handle_read_pending_slot = mcu_i2c_handle(I2C_target_INST);
  76. }
  77. break;
  78. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  79. break;
  80. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  81. break;
  82. case DL_I2C_IIDX_TIMEOUT_A:
  83. case DL_I2C_IIDX_TIMEOUT_B:
  84. DL_I2C_flushTargetRXFIFO(I2C_target_INST);
  85. DL_I2C_flushTargetTXFIFO(I2C_target_INST);
  86. default:
  87. break;
  88. }
  89. }
  90. int main(void)
  91. {
  92. SYSCFG_DL_init();
  93. NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
  94. NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
  95. battery_slotmgr.init();
  96. initialize_target_address();
  97. while (1) {
  98. for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
  99. if (handle_read_pending_slot != -1) {
  100. mcu_i2c_handle_read(I2C_target_INST, handle_read_pending_slot);
  101. handle_read_pending_slot = -1;
  102. }
  103. // step 1: update the voltage readings
  104. battery_slotmgr.read_state(slot);
  105. // step 2: control loop to adjust the dac / adc values,
  106. // but only if no error happens
  107. // (0x80 is the error flag of the state)
  108. if ((*battery_slots[slot].state & 0x80) == 0) {
  109. battery_slotmgr.adjust_current(slot);
  110. } else {
  111. battery_slotmgr.disable(slot);
  112. }
  113. }
  114. delay_cycles(MAINLOOP_DELAY);
  115. }
  116. }