i2c_controller.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. I2C_Instance gI2C;
  35. volatile I2C_CommandInfo gCommand;
  36. I2C_ResponseInfo gResponse;
  37. volatile bool gSendCommand = false;
  38. /* Data to send to target */
  39. uint8_t gTxData[MAX_DATA_SIZE] = {0xAA,0xBB,0xCC,0xDD,0xEE,0xFF, \
  40. 0x00,0x11,0x22,0x33,0x44,0x55, \
  41. 0x66,0x77,0x88,0x99,0x12,0x23, \
  42. 0x34,0x45,0x56,0x67,0x78,0x89};
  43. int main(void)
  44. {
  45. SYSCFG_DL_init();
  46. NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
  47. I2C_init(&gI2C);
  48. /* Default CommandInfo Object */
  49. I2C_CommandInfo CommandInfo_Obj;
  50. CommandInfo_Obj.targetAddr = DEF_TARGET_ADDR;
  51. CommandInfo_Obj.commandType = READ_COMMAND;
  52. CommandInfo_Obj.addr = 0x20207C00;
  53. CommandInfo_Obj.dataArray = &gTxData[0];
  54. CommandInfo_Obj.dataSize = 4;
  55. CommandInfo_Obj.crcEnable = false;
  56. gCommand = CommandInfo_Obj;
  57. while(1)
  58. {
  59. if(gSendCommand == true)
  60. {
  61. /* Frames a command packet and sends it to Target through I2C Write Command */
  62. I2C_sendCommand(&gI2C, (I2C_CommandInfo *) &gCommand);
  63. gI2C.status = I2C_STATUS_RX_STARTED;
  64. gI2C.rxMsg.ptr = 0;
  65. /* Issues I2C Read Command to get the response from Target */
  66. I2C_getResponse(&gI2C,gCommand.targetAddr);
  67. gSendCommand = false;
  68. }
  69. }
  70. }
  71. void I2C_INST_IRQHandler(void)
  72. {
  73. switch (DL_I2C_getPendingInterrupt(I2C_controller_INST))
  74. {
  75. case DL_I2C_IIDX_CONTROLLER_RX_DONE:
  76. gI2C.rxMsg.len = gI2C.rxMsg.ptr;
  77. I2C_decodeResponse(&gI2C,&gResponse);
  78. gI2C.status = I2C_STATUS_RX_COMPLETE;
  79. break;
  80. case DL_I2C_IIDX_CONTROLLER_TX_DONE:
  81. DL_I2C_disableInterrupt(
  82. I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
  83. gI2C.status = I2C_STATUS_TX_COMPLETE;
  84. break;
  85. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  86. gI2C.status = I2C_STATUS_RX_INPROGRESS;
  87. /* Store bytes received from target in Rx Msg Buffer */
  88. while (DL_I2C_isControllerRXFIFOEmpty(I2C_controller_INST) != true) {
  89. if (gI2C.rxMsg.ptr < MAX_BUFFER_SIZE) {
  90. gI2C.rxMsg.buffer[gI2C.rxMsg.ptr++] =
  91. DL_I2C_receiveControllerData(I2C_controller_INST);
  92. } else {
  93. /* Ignore and remove from FIFO if the buffer is full */
  94. DL_I2C_receiveControllerData(I2C_controller_INST);
  95. }
  96. }
  97. break;
  98. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  99. gI2C.status = I2C_STATUS_TX_INPROGRESS;
  100. /* Fill TX FIFO with bytes to send */
  101. if (gI2C.txMsg.ptr < gI2C.txMsg.len) {
  102. gI2C.txMsg.ptr += DL_I2C_fillControllerTXFIFO(
  103. I2C_controller_INST, &gI2C.txMsg.buffer[gI2C.txMsg.ptr], gI2C.txMsg.len - gI2C.txMsg.ptr);
  104. }
  105. break;
  106. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  107. case DL_I2C_IIDX_CONTROLLER_NACK:
  108. gI2C.status = I2C_STATUS_ERROR;
  109. break;
  110. default:
  111. break;
  112. }
  113. }