i2c_controller.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * This file implements Hardware Abstraction Layer (HAL) to make the I2C
  3. * communication of MSPM0 SDK compatible with ADC (MCP3426/7/8) and DAC
  4. * (MCP34728)
  5. */
  6. #include "src/interfaces/i2c_controller.h"
  7. #include "ti/driverlib/dl_i2c.h"
  8. #include "ti_msp_dl_config.h"
  9. #include <stdio.h>
  10. /*
  11. static function is for implementing Data Hiding, access to the static function
  12. is restricted to the file where they are declared const keyword for
  13. 'i2c_addr' and 'Data_length' makes the variable immutable. const uint8_t *
  14. const Data: means the pointer to the variable and the value of Data is immutable
  15. */
  16. I2CRxPackage controllerRxPackage;
  17. I2CTxPackage controllerTxPackage;
  18. static bool msp_i2c_write(uint8_t const i2c_addr) {
  19. // Flush any stale data in TX FIFO:
  20. DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
  21. // **Check if the I2C bus is stuck before WRITE
  22. if (DL_I2C_getControllerStatus(I2C_controller_INST) &
  23. DL_I2C_CONTROLLER_STATUS_ERROR) {
  24. #ifdef DEBUG_I2C_ERR
  25. printf("I2C Communication: Bus is stuck!\n");
  26. #endif
  27. DL_I2C_resetControllerTransfer(I2C_controller_INST);
  28. return false;
  29. }
  30. // **Wait for I2C Bus to be Free**
  31. uint32_t n_cycles = 0;
  32. while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
  33. DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
  34. ;
  35. if (n_cycles == MAX_I2C_WAIT_RX) {
  36. printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
  37. DL_I2C_resetControllerTransfer(I2C_controller_INST);
  38. return false;
  39. }
  40. // **Start I2C Write Transaction**
  41. DL_I2C_startControllerTransfer(I2C_controller_INST, i2c_addr,
  42. DL_I2C_CONTROLLER_DIRECTION_TX, controllerTxPackage.len);
  43. // **Load Configuration Byte into TX FIFO**
  44. DL_I2C_fillControllerTXFIFO(I2C_controller_INST, controllerTxPackage.packet, controllerTxPackage.len);
  45. #ifdef DEBUG_I2C_TX
  46. for (uint8_t i = 0; i < controllerTxPackage.len; i++) {
  47. printf("Sending 0x%02X\n", controllerTxPackage.packet[i]);
  48. }
  49. #endif
  50. n_cycles = 0;
  51. while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
  52. DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
  53. ;
  54. if (n_cycles == MAX_I2C_WAIT_RX) {
  55. printf("Error in reading from I2C Bus: Bus is not getting ready after transmit data\n");
  56. DL_I2C_resetControllerTransfer(I2C_controller_INST);
  57. return false;
  58. }
  59. // **Check if the target address is incorrect
  60. if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ADDR_ACK) {
  61. #ifdef DEBUG_I2C_ERR
  62. printf("I2C Write Error: Target Address not acknowledged!\n");
  63. #endif
  64. return false;
  65. }
  66. // **Check for any WRITE error
  67. if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) {
  68. #ifdef DEBUG_I2C_ERR
  69. printf("I2C Write Error: Bus error after sending data.\n");
  70. #endif
  71. return false;
  72. }
  73. return true;
  74. }
  75. static bool msp_i2c_read(uint8_t const i2c_addr) {
  76. // Flush any stale data in TX FIFO:
  77. DL_I2C_flushControllerRXFIFO(I2C_controller_INST);
  78. uint32_t n_cycles = 0;
  79. while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
  80. DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
  81. ;
  82. if (n_cycles == MAX_I2C_WAIT_RX) {
  83. printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
  84. }
  85. DL_I2C_startControllerTransfer(I2C_controller_INST, i2c_addr,
  86. DL_I2C_CONTROLLER_DIRECTION_RX, controllerRxPackage.len);
  87. n_cycles = 0;
  88. while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
  89. DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
  90. ;
  91. if (n_cycles == MAX_I2C_WAIT_RX) {
  92. printf("Error in reading from I2C Bus: Bus is not getting ready after transmit start\n");
  93. }
  94. DL_I2C_enableInterrupt(I2C_controller_INST,
  95. DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
  96. return true;
  97. }
  98. /**
  99. * this function discovers on the i2c bus the devices from the address
  100. * to ensure all relevant bus participants are present
  101. */
  102. bool i2c_discover(uint8_t i2c_addr) {
  103. if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
  104. DL_I2C_disableController(I2C_controller_INST); // Disable I2C
  105. DL_I2C_enableController(I2C_controller_INST); // Re-enable I2C
  106. }
  107. #ifdef DEBUG_I2C_TX
  108. printf("Scanning 0x%02X\n", i2c_addr);
  109. #endif
  110. DL_I2C_startControllerTransfer(I2C_controller_INST, i2c_addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
  111. delay_cycles(5000);
  112. uint32_t i2c_status = DL_I2C_getControllerStatus(I2C_controller_INST);
  113. bool found = !(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR);
  114. DL_I2C_disableController(I2C_controller_INST);
  115. DL_I2C_enableController(I2C_controller_INST);
  116. return found;
  117. }
  118. I2C_Interface i2c_hal = {
  119. .write = msp_i2c_write,
  120. .read = msp_i2c_read,
  121. };