i2c_controller.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2024, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Texas Instruments Incorporated nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "ti_msp_dl_config.h"
  33. #include "ti/comm_modules/i2c/controller/i2c_comm_controller.h"
  34. #include<stdint.h>
  35. #include<stdio.h>
  36. I2C_Instance gI2C;
  37. volatile I2C_CommandInfo gCommand;
  38. I2C_ResponseInfo gResponse;
  39. volatile bool gSendCommand = true;
  40. /* Data to send to target */
  41. uint8_t gTxData[MAX_DATA_SIZE] = {0xAA,0xBB,0xCC,0xDD,0xEE,0xFF, \
  42. 0x00,0x11,0x22,0x33,0x44,0x55, \
  43. 0x66,0x77,0x88,0x99,0x12,0x23, \
  44. 0x34,0x45,0x56,0x67,0x78,0x89};
  45. int main(void)
  46. {
  47. SYSCFG_DL_init();
  48. printf("System Configuration Enabled\n");
  49. NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
  50. I2C_init(&gI2C);
  51. /* Default CommandInfo Object */
  52. I2C_CommandInfo CommandInfo_Obj;
  53. CommandInfo_Obj.targetAddr = DEF_TARGET_ADDR;
  54. CommandInfo_Obj.commandType = READ_COMMAND;
  55. CommandInfo_Obj.addr = 0x20207C00;
  56. CommandInfo_Obj.dataArray = &gTxData[0];
  57. CommandInfo_Obj.dataSize = 0;
  58. CommandInfo_Obj.crcEnable = false;
  59. gCommand = CommandInfo_Obj;
  60. while(1)
  61. {
  62. if(gSendCommand)
  63. {
  64. /* Frames a command packet and sends it to Target through I2C Write Command */
  65. I2C_sendCommand(&gI2C, (I2C_CommandInfo *) &gCommand);
  66. printf("Sending data to I2C address: 0x%X\n", gCommand.targetAddr);
  67. gI2C.status = I2C_STATUS_RX_STARTED;
  68. gI2C.rxMsg.ptr = 0;
  69. gI2C.dataLen = 4;
  70. /* Issues I2C Read Command to get the response from Target */
  71. I2C_getResponse(&gI2C,gCommand.targetAddr);
  72. printf("Reading data to I2C address: 0x%X\n", gCommand.targetAddr);
  73. gSendCommand = false;
  74. }
  75. }
  76. }
  77. void I2C_controller_INST_IRQHandler(void)
  78. { printf("I2C Interrupt Triggered!\n");
  79. switch (DL_I2C_getPendingInterrupt(I2C_controller_INST))
  80. {
  81. case DL_I2C_IIDX_CONTROLLER_RX_DONE:
  82. gI2C.rxMsg.len = gI2C.rxMsg.ptr;
  83. I2C_decodeResponse(&gI2C,&gResponse);
  84. gI2C.status = I2C_STATUS_RX_COMPLETE;
  85. break;
  86. case DL_I2C_IIDX_CONTROLLER_TX_DONE:
  87. DL_I2C_disableInterrupt(
  88. I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
  89. gI2C.status = I2C_STATUS_TX_COMPLETE;
  90. break;
  91. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  92. gI2C.status = I2C_STATUS_RX_INPROGRESS;
  93. /* Store bytes received from target in Rx Msg Buffer */
  94. while (DL_I2C_isControllerRXFIFOEmpty(I2C_controller_INST) != true) {
  95. if (gI2C.rxMsg.ptr < MAX_BUFFER_SIZE) {
  96. gI2C.rxMsg.buffer[gI2C.rxMsg.ptr++] =
  97. DL_I2C_receiveControllerData(I2C_controller_INST);
  98. } else {
  99. /* Ignore and remove from FIFO if the buffer is full */
  100. DL_I2C_receiveControllerData(I2C_controller_INST);
  101. }
  102. }
  103. break;
  104. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  105. printf("TX FIFO with data!\n");
  106. gI2C.status = I2C_STATUS_TX_INPROGRESS;
  107. /* Fill TX FIFO with bytes to send */
  108. if (gI2C.txMsg.ptr < gI2C.txMsg.len) {
  109. gI2C.txMsg.ptr += DL_I2C_fillControllerTXFIFO(
  110. I2C_controller_INST, &gI2C.txMsg.buffer[gI2C.txMsg.ptr], gI2C.txMsg.len - gI2C.txMsg.ptr);
  111. }
  112. break;
  113. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  114. printf("Interrupt index for I2C controller Arbitration Lost!\n");
  115. case DL_I2C_IIDX_CONTROLLER_NACK:
  116. printf("Interrupt index for Address/Data NACK!\n");
  117. gI2C.status = I2C_STATUS_ERROR;
  118. break;
  119. default:
  120. break;
  121. }
  122. }