i2c_master.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2025 QMK
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <stdint.h>
  5. /**
  6. * \file
  7. *
  8. * \defgroup i2c_master I2C Master API
  9. *
  10. * \brief API to communicate with I2C devices.
  11. * \{
  12. */
  13. typedef int16_t i2c_status_t;
  14. #define I2C_STATUS_SUCCESS (0)
  15. #define I2C_STATUS_ERROR (-1)
  16. #define I2C_STATUS_TIMEOUT (-2)
  17. #define I2C_TIMEOUT_IMMEDIATE (0)
  18. #define I2C_TIMEOUT_INFINITE (0xFFFF)
  19. /**
  20. * \brief Initialize the I2C driver. This function must be called only once, before any of the below functions can be called.
  21. *
  22. * This function is weakly defined, meaning it can be overridden if necessary for your particular use case.
  23. */
  24. void i2c_init(void);
  25. /**
  26. * \brief Send multiple bytes to the selected I2C device.
  27. *
  28. * \param address The 7-bit I2C address of the device.
  29. * \param data A pointer to the data to transmit.
  30. * \param length The number of bytes to write. Take care not to overrun the length of `data`.
  31. * \param timeout The time in milliseconds to wait for a response from the target device.
  32. *
  33. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  34. */
  35. i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout);
  36. #if defined(__AVR__) || defined(__DOXYGEN__)
  37. /**
  38. * \brief Send multiple bytes from PROGMEM to the selected I2C device.
  39. *
  40. * On ARM devices, this function is simply an alias for i2c_transmit(address, data, length, timeout).
  41. *
  42. * \param address The 7-bit I2C address of the device.
  43. * \param data A pointer to the data to transmit.
  44. * \param length The number of bytes to write. Take care not to overrun the length of `data`.
  45. * \param timeout The time in milliseconds to wait for a response from the target device.
  46. *
  47. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  48. */
  49. i2c_status_t i2c_transmit_P(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout);
  50. #else
  51. # define i2c_transmit_P(address, data, length, timeout) i2c_transmit(address, data, length, timeout)
  52. #endif
  53. /**
  54. * \brief Receive multiple bytes from the selected I2C device.
  55. *
  56. * \param address The 7-bit I2C address of the device.
  57. * \param data A pointer to a buffer to read into.
  58. * \param length The number of bytes to read. Take care not to overrun the length of `data`.
  59. * \param timeout The time in milliseconds to wait for a response from the target device.
  60. *
  61. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  62. */
  63. i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout);
  64. /**
  65. * \brief Send multiple bytes and then receive multiple bytes from the selected I2C device.
  66. *
  67. * \param address The 7-bit I2C address of the device.
  68. * \param tx_data A pointer to the data to transmit.
  69. * \param tx_length The number of bytes to write. Take care not to overrun the length of `tx_data`.
  70. * \param rx_data A pointer to a buffer to read into.
  71. * \param rx_length The number of bytes to read. Take care not to overrun the length of `rx_data`.
  72. * \param timeout The time in milliseconds to wait for a response from the target device.
  73. *
  74. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  75. */
  76. i2c_status_t i2c_transmit_and_receive(uint8_t address, const uint8_t* tx_data, uint16_t tx_length, uint8_t* rx_data, uint16_t rx_length, uint16_t timeout);
  77. /**
  78. * \brief Write to a register with an 8-bit address on the I2C device.
  79. *
  80. * \param devaddr The 7-bit I2C address of the device.
  81. * \param regaddr The register address to write to.
  82. * \param data A pointer to the data to transmit.
  83. * \param length The number of bytes to write. Take care not to overrun the length of `data`.
  84. * \param timeout The time in milliseconds to wait for a response from the target device.
  85. *
  86. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  87. */
  88. i2c_status_t i2c_write_register(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout);
  89. /**
  90. * \brief Write to a register with a 16-bit address (big endian) on the I2C device.
  91. *
  92. * \param devaddr The 7-bit I2C address of the device.
  93. * \param regaddr The register address to write to.
  94. * \param data A pointer to the data to transmit.
  95. * \param length The number of bytes to write. Take care not to overrun the length of `data`.
  96. * \param timeout The time in milliseconds to wait for a response from the target device.
  97. *
  98. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  99. */
  100. i2c_status_t i2c_write_register16(uint8_t devaddr, uint16_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout);
  101. /**
  102. * \brief Read from a register with an 8-bit address on the I2C device.
  103. *
  104. * \param devaddr The 7-bit I2C address of the device.
  105. * \param regaddr The register address to read from.
  106. * \param data A pointer to a buffer to read into.
  107. * \param length The number of bytes to read. Take care not to overrun the length of `data`.
  108. * \param timeout The time in milliseconds to wait for a response from the target device.
  109. *
  110. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  111. */
  112. i2c_status_t i2c_read_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout);
  113. /**
  114. * \brief Read from a register with a 16-bit address (big endian) on the I2C device.
  115. *
  116. * \param devaddr The 7-bit I2C address of the device.
  117. * \param regaddr The register address to read from.
  118. * \param data A pointer to a buffer to read into.
  119. * \param length The number of bytes to read. Take care not to overrun the length of `data`.
  120. * \param timeout The time in milliseconds to wait for a response from the target device.
  121. *
  122. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  123. */
  124. i2c_status_t i2c_read_register16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout);
  125. /**
  126. * \brief Ping the I2C bus for a specific address.
  127. *
  128. * On ChibiOS a "best effort" attempt is made by reading a single byte from register 0 at the given address. This should generally work except for I2C devices that do not not respond to a register 0 read request, which will result in a false negative result (unsuccessful response to ping attempt).
  129. *
  130. * This function is weakly defined, meaning it can be overridden if necessary for your particular use case.
  131. *
  132. * \param address The 7-bit I2C address of the device.
  133. * \param timeout The time in milliseconds to wait for a response from the target device.
  134. *
  135. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  136. */
  137. i2c_status_t i2c_ping_address(uint8_t address, uint16_t timeout);
  138. /** \} */