main_target.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "src/battery_data/battery.h"
  2. #include "ti_msp_dl_config.h"
  3. //#include "ti/driverlib/dl_i2c.h"
  4. #include <stdio.h>
  5. #include "src/peripherals/adc/adc.h"
  6. #include "src/peripherals/adc/adc_interface.h"
  7. #include "src/i2c_comm/mcu_slave_interface.h"
  8. /*
  9. DL_TimerG_startCounter(PWM_0_INST);
  10. DL_TimerA_setCaptureCompareValue(PWM_1_INST, pwm_count,
  11. DL_TIMER_CC_0_INDEX); // update ccr0 value
  12. */
  13. #define DELAY_CYCLE (10000000)
  14. volatile bool mcu_CommandPending= false;
  15. /*
  16. Scans all the addresses of the peripherals:
  17. */
  18. /*void I2C_scanBus(I2C_Regs *i2c) {
  19. printf("1");
  20. // **Step 1: Reset I2C Controller if Busy**
  21. if (DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
  22. printf("I2C Bus Busy! Resetting I2C Controller...\n");
  23. DL_I2C_disableController(i2c); // Disable I2C
  24. delay_cycles(20000);
  25. DL_I2C_enableController(i2c); // Re-enable I2C
  26. delay_cycles(20000);
  27. }
  28. uint32_t i2c_status;
  29. // **Step 2: Scan I2C Bus**
  30. for (uint8_t addr = 0x08; addr < 0x78; addr++) { // Valid I2C Address Range
  31. printf("Scanning 0x%02X\n", addr);
  32. DL_I2C_startControllerTransfer(i2c, addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
  33. delay_cycles(5000);
  34. if (addr != 0x60 && addr != 0x68) {
  35. continue;
  36. }
  37. i2c_status = DL_I2C_getControllerStatus(i2c);
  38. printf("DL_I2C_getControllerStatus(i2c): %d\n", i2c_status);
  39. printf("busy?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_BUSY));
  40. printf("error?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_ERROR));
  41. printf("addr_ack?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_ADDR_ACK));
  42. printf("data_ack?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_DATA_ACK));
  43. printf("arb_lost?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_ARBITRATION_LOST));
  44. printf("idle?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_IDLE));
  45. printf("busy_bus?: %d\n", (i2c_status & DL_I2C_CONTROLLER_STATUS_BUSY_BUS));
  46. if (!(DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_ERROR)) {
  47. printf("Device found at: 0x%02X\n", addr);
  48. DL_I2C_disableController(i2c);
  49. DL_I2C_enableController(i2c);
  50. }else {
  51. // Clear the error by resetting the I2C controller
  52. printf("Device not found...\n");
  53. DL_I2C_disableController(i2c);
  54. DL_I2C_enableController(i2c);
  55. }
  56. }
  57. //printf("I2C Scan Complete!\n");
  58. }
  59. */
  60. void I2C_controller_INST_IRQHandler(void) {
  61. // printf("I2C Interrupt Triggered to ADC!\n");
  62. switch (DL_I2C_getPendingInterrupt(I2C_controller_INST)) {
  63. case DL_I2C_IIDX_CONTROLLER_START:
  64. gRxADCcount = 0;
  65. DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
  66. break;
  67. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  68. /* Store bytes received from target in Rx Msg Buffer */
  69. while (DL_I2C_isControllerRXFIFOEmpty(I2C_controller_INST) != true) {
  70. if (gRxADCcount < gRxADClen) {
  71. gRxPacket[gRxADCcount] =
  72. DL_I2C_receiveControllerData(I2C_controller_INST);
  73. gRxADCcount++;
  74. } else {
  75. /* Ignore and remove from FIFO if the buffer is full */
  76. DL_I2C_receiveControllerData(I2C_controller_INST);
  77. }
  78. }
  79. if (gRxADCcount >= gRxADClen) {
  80. gRxComplete = true;
  81. DL_I2C_enableInterrupt(I2C_controller_INST,
  82. DL_I2C_INTERRUPT_CONTROLLER_STOP);
  83. }
  84. break;
  85. /*TRANSMIT data to ADC*/
  86. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  87. if (gTxADCcount < gTxADClen) {
  88. gTxPacket[gTxADCcount] = DL_I2C_fillControllerTXFIFO(I2C_controller_INST,
  89. &gTxPacket[gTxADCcount],
  90. (gTxADClen - gTxADCcount));
  91. gTxADCcount++;
  92. } else {
  93. /*Prevent overflow and just ignore data*/
  94. DL_I2C_fillTargetTXFIFO(I2C_controller_INST, (uint8_t[]){0x00}, 1);
  95. gTxComplete = true;
  96. }
  97. if(gTxADCcount >= gTxADClen){
  98. gTxComplete= true;
  99. }
  100. break;
  101. /*STOP condition*/
  102. case DL_I2C_IIDX_CONTROLLER_STOP:
  103. gTxComplete = true;
  104. gRxComplete = true;
  105. break;
  106. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  107. break;
  108. case DL_I2C_IIDX_CONTROLLER_NACK:
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. /**** Interrupt for Pi to MCU ****/
  115. void I2C_target_INST_IRQHandler(void) {
  116. uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
  117. switch (status) {
  118. case DL_I2C_IIDX_TARGET_START:
  119. DL_I2C_flushTargetTXFIFO(I2C_target_INST);
  120. break;
  121. case DL_I2C_IIDX_TARGET_STOP:
  122. mcu_CommandPending= true;
  123. DL_I2C_flushTargetTXFIFO(I2C_target_INST);
  124. DL_I2C_flushTargetRXFIFO(I2C_target_INST);
  125. break;
  126. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  127. if (DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)) {
  128. return;
  129. }
  130. mcu_CommandPending= true;
  131. break;
  132. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  133. mcu_CommandPending= true;
  134. break;
  135. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. int main(void)
  142. {
  143. SYSCFG_DL_init();
  144. Battery_Init();
  145. NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
  146. //NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
  147. /*printf("&gRxPacket: %p\n", (void*)gRxPacket);
  148. printf("&batteries[0]: %p\n", (void*)&batteries[0]);
  149. printf("&gRxADClen: %p\n", (void*)&gRxADClen);
  150. printf("&gTxPacket: %p\n", (void*)gTxPacket);
  151. printf("&gTxADClen: %p\n", (void*)&gTxADClen);
  152. */
  153. printf("Memory address of batteries: %p\n", &batteries[0]);
  154. while (1) {
  155. /*if(mcu_CommandPending){
  156. mcu_i2c_handle(I2C_target_INST);
  157. mcu_CommandPending= false;
  158. continue;
  159. }*/
  160. for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
  161. for(uint8_t channel= 0; channel< 2; channel++){
  162. updateADCReading_multichannel(slot, channel);
  163. }
  164. }
  165. delay_cycles(DELAY_CYCLE);
  166. }
  167. }