i2c_controller.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 = true;
  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_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 = 0x18;
  52. CommandInfo_Obj.addr = 0x20207C00;
  53. CommandInfo_Obj.dataArray = &gTxData[0];
  54. CommandInfo_Obj.dataSize = 0;
  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. gI2C.dataLen = 4;
  66. /* Issues I2C Read Command to get the response from Target */
  67. I2C_getResponse(&gI2C,gCommand.targetAddr);
  68. gSendCommand = false;
  69. }
  70. }
  71. }
  72. void I2C_INST_IRQHandler(void)
  73. {
  74. switch (DL_I2C_getPendingInterrupt(I2C_INST))
  75. {
  76. case DL_I2C_IIDX_CONTROLLER_RX_DONE:
  77. gI2C.rxMsg.len = gI2C.rxMsg.ptr;
  78. I2C_decodeResponse(&gI2C,&gResponse);
  79. gI2C.status = I2C_STATUS_RX_COMPLETE;
  80. break;
  81. case DL_I2C_IIDX_CONTROLLER_TX_DONE:
  82. DL_I2C_disableInterrupt(
  83. I2C_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
  84. gI2C.status = I2C_STATUS_TX_COMPLETE;
  85. break;
  86. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  87. gI2C.status = I2C_STATUS_RX_INPROGRESS;
  88. /* Store bytes received from target in Rx Msg Buffer */
  89. while (DL_I2C_isControllerRXFIFOEmpty(I2C_INST) != true) {
  90. if (gI2C.rxMsg.ptr < MAX_BUFFER_SIZE) {
  91. gI2C.rxMsg.buffer[gI2C.rxMsg.ptr++] =
  92. DL_I2C_receiveControllerData(I2C_INST);
  93. } else {
  94. /* Ignore and remove from FIFO if the buffer is full */
  95. DL_I2C_receiveControllerData(I2C_INST);
  96. }
  97. }
  98. break;
  99. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  100. gI2C.status = I2C_STATUS_TX_INPROGRESS;
  101. /* Fill TX FIFO with bytes to send */
  102. if (gI2C.txMsg.ptr < gI2C.txMsg.len) {
  103. gI2C.txMsg.ptr += DL_I2C_fillControllerTXFIFO(
  104. I2C_INST, &gI2C.txMsg.buffer[gI2C.txMsg.ptr], gI2C.txMsg.len - gI2C.txMsg.ptr);
  105. }
  106. break;
  107. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  108. case DL_I2C_IIDX_CONTROLLER_NACK:
  109. gI2C.status = I2C_STATUS_ERROR;
  110. break;
  111. default:
  112. break;
  113. }
  114. }