logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

i2c_master.h (6408B)


  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 Write to a register with an 8-bit address on the I2C device.
  66. *
  67. * \param devaddr The 7-bit I2C address of the device.
  68. * \param regaddr The register address to write to.
  69. * \param data A pointer to the data to transmit.
  70. * \param length The number of bytes to write. Take care not to overrun the length of `data`.
  71. * \param timeout The time in milliseconds to wait for a response from the target device.
  72. *
  73. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  74. */
  75. i2c_status_t i2c_write_register(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout);
  76. /**
  77. * \brief Write to a register with a 16-bit address (big endian) on the I2C device.
  78. *
  79. * \param devaddr The 7-bit I2C address of the device.
  80. * \param regaddr The register address to write to.
  81. * \param data A pointer to the data to transmit.
  82. * \param length The number of bytes to write. Take care not to overrun the length of `data`.
  83. * \param timeout The time in milliseconds to wait for a response from the target device.
  84. *
  85. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  86. */
  87. i2c_status_t i2c_write_register16(uint8_t devaddr, uint16_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout);
  88. /**
  89. * \brief Read from a register with an 8-bit address on the I2C device.
  90. *
  91. * \param devaddr The 7-bit I2C address of the device.
  92. * \param regaddr The register address to read from.
  93. * \param data A pointer to a buffer to read into.
  94. * \param length The number of bytes to read. Take care not to overrun the length of `data`.
  95. * \param timeout The time in milliseconds to wait for a response from the target device.
  96. *
  97. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  98. */
  99. i2c_status_t i2c_read_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout);
  100. /**
  101. * \brief Read from a register with a 16-bit address (big endian) on the I2C device.
  102. *
  103. * \param devaddr The 7-bit I2C address of the device.
  104. * \param regaddr The register address to read from.
  105. * \param data A pointer to a buffer to read into.
  106. * \param length The number of bytes to read. Take care not to overrun the length of `data`.
  107. * \param timeout The time in milliseconds to wait for a response from the target device.
  108. *
  109. * \return `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  110. */
  111. i2c_status_t i2c_read_register16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout);
  112. /**
  113. * \brief Ping the I2C bus for a specific address.
  114. *
  115. * 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).
  116. *
  117. * This function is weakly defined, meaning it can be overridden if necessary for your particular use case.
  118. *
  119. * \param address The 7-bit I2C address of the device.
  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_ping_address(uint8_t address, uint16_t timeout);
  125. /** \} */