You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
11 KiB

  1. # I2C Master Driver :id=i2c-master-driver
  2. The I2C Master drivers used in QMK have a set of common functions to allow portability between MCUs.
  3. ## Usage :id=usage
  4. In most cases, the I2C Master driver code is automatically included if you are using a feature or driver which requires it, such as [OLED](feature_oled_driver.md).
  5. However, if you need to use the driver standalone, add the following to your `rules.mk`:
  6. ```make
  7. I2C_DRIVER_REQUIRED = yes
  8. ```
  9. You can then call the I2C API by including `i2c_master.h` in your code.
  10. ## I2C Addressing :id=note-on-i2c-addresses
  11. All of the addresses expected by this driver should be pushed to the upper 7 bits of the address byte. Setting
  12. the lower bit (indicating read/write) will be done by the respective functions. Almost all I2C addresses listed
  13. on datasheets and the internet will be represented as 7 bits occupying the lower 7 bits and will need to be
  14. shifted to the left (more significant) by one bit. This is easy to do via the bitwise shift operator `<< 1`.
  15. You can either do this on each call to the functions below, or once in your definition of the address. For example, if your device has an address of `0x18`:
  16. ```c
  17. #define MY_I2C_ADDRESS (0x18 << 1)
  18. ```
  19. See https://www.robot-electronics.co.uk/i2c-tutorial for more information about I2C addressing and other technical details.
  20. ## AVR Configuration :id=avr-configuration
  21. The following defines can be used to configure the I2C master driver:
  22. |`config.h` Override|Description |Default |
  23. |-------------------|---------------------|--------|
  24. |`F_SCL` |Clock frequency in Hz|`400000`|
  25. No further setup is required - just connect the `SDA` and `SCL` pins of your I2C devices to the matching pins on the MCU:
  26. |MCU |`SCL`|`SDA`|
  27. |------------------|-----|-----|
  28. |ATmega16/32U4 |`D0` |`D1` |
  29. |AT90USB64/128 |`D0` |`D1` |
  30. |ATmega32A |`C0` |`C1` |
  31. |ATmega328/P |`C5` |`C4` |
  32. ?> The ATmega16/32U2 does not possess I2C functionality, and so cannot use this driver.
  33. ## ChibiOS/ARM Configuration :id=arm-configuration
  34. You'll need to determine which pins can be used for I2C -- a an example, STM32 parts generally have multiple I2C peripherals, labeled I2C1, I2C2, I2C3 etc.
  35. To enable I2C, modify your board's `halconf.h` to enable I2C:
  36. ```c
  37. #define HAL_USE_I2C TRUE
  38. ```
  39. Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
  40. ```c
  41. #undef STM32_I2C_USE_I2C2
  42. #define STM32_I2C_USE_I2C2 TRUE
  43. ```
  44. |`mcuconf.h` Setting |Description |Default|
  45. |----------------------------|----------------------------------------------------------------------------------|-------|
  46. |`STM32_I2C_BUSY_TIMEOUT` |Time in milliseconds until the I2C command is aborted if no response is received |`50` |
  47. |`STM32_I2C_XXX_IRQ_PRIORITY`|Interrupt priority for hardware driver XXX (THIS IS AN EXPERT SETTING) |`10` |
  48. |`STM32_I2C_USE_DMA` |Enable/Disable the ability of the MCU to offload the data transfer to the DMA unit|`TRUE` |
  49. |`STM32_I2C_XXX_DMA_PRIORITY`|Priority of DMA unit for hardware driver XXX (THIS IS AN EXPERT SETTING) |`1` |
  50. Configuration-wise, you'll need to set up the peripheral as per your MCU's datasheet -- the defaults match the pins for a Proton-C, i.e. STM32F303.
  51. |`config.h` Overrride |Description |Default|
  52. |------------------------|--------------------------------------------------------------|-------|
  53. |`I2C_DRIVER` |I2C peripheral to use - I2C1 -> `I2CD1`, I2C2 -> `I2CD2` etc. |`I2CD1`|
  54. |`I2C1_SCL_PIN` |The pin definition for SCL |`B6` |
  55. |`I2C1_SCL_PAL_MODE` |The alternate function mode for SCL |`4` |
  56. |`I2C1_SDA_PIN` |The pin definition for SDA |`B7` |
  57. |`I2C1_SDA_PAL_MODE` |The alternate function mode for SDA |`4` |
  58. The following configuration values depend on the specific MCU in use.
  59. ### I2Cv1 :id=arm-configuration-i2cv1
  60. * STM32F1xx
  61. * STM32F2xx
  62. * STM32F4xx
  63. * STM32L0xx
  64. * STM32L1xx
  65. See [this page](https://www.playembedded.org/blog/stm32-i2c-chibios/#7_I2Cv1_configuration_structure) for the I2Cv1 configuration structure.
  66. |`config.h` Override|Default |
  67. |-------------------|----------------|
  68. |`I2C1_OPMODE` |`OPMODE_I2C` |
  69. |`I2C1_CLOCK_SPEED` |`100000` |
  70. |`I2C1_DUTY_CYCLE` |`STD_DUTY_CYCLE`|
  71. ### I2Cv2 :id=arm-configuration-i2cv2
  72. * STM32F0xx
  73. * STM32F3xx
  74. * STM32F7xx
  75. * STM32L4xx
  76. See [this page](https://www.playembedded.org/blog/stm32-i2c-chibios/#8_I2Cv2_I2Cv3_configuration_structure) for the I2Cv2 configuration structure.
  77. |`config.h` Override |Default|
  78. |---------------------|-------|
  79. |`I2C1_TIMINGR_PRESC` |`0U` |
  80. |`I2C1_TIMINGR_SCLDEL`|`7U` |
  81. |`I2C1_TIMINGR_SDADEL`|`0U` |
  82. |`I2C1_TIMINGR_SCLH` |`38U` |
  83. |`I2C1_TIMINGR_SCLL` |`129U` |
  84. ## API :id=api
  85. ### `void i2c_init(void)` :id=api-i2c-init
  86. Initialize the I2C driver. This function must be called only once, before any of the below functions can be called.
  87. This function is weakly defined, meaning it can be overridden if necessary for your particular use case:
  88. ```c
  89. void i2c_init(void) {
  90. gpio_set_pin_input(B6); // Try releasing special pins for a short time
  91. gpio_set_pin_input(B7);
  92. wait_ms(10); // Wait for the release to happen
  93. palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP); // Set B6 to I2C function
  94. palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP); // Set B7 to I2C function
  95. }
  96. ```
  97. ---
  98. ### `i2c_status_t i2c_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout)` :id=api-i2c-transmit
  99. Send multiple bytes to the selected I2C device.
  100. #### Arguments :id=api-i2c-transmit-arguments
  101. - `uint8_t address`
  102. The 7-bit I2C address of the device.
  103. - `uint8_t *data`
  104. A pointer to the data to transmit.
  105. - `uint16_t length`
  106. The number of bytes to write. Take care not to overrun the length of `data`.
  107. - `uint16_t timeout`
  108. The time in milliseconds to wait for a response from the target device.
  109. #### Return Value :id=api-i2c-transmit-return
  110. `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_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)` :id=api-i2c-receive
  113. Receive multiple bytes from the selected I2C device.
  114. #### Arguments :id=api-i2c-receive-arguments
  115. - `uint8_t address`
  116. The 7-bit I2C address of the device.
  117. - `uint8_t *data`
  118. A pointer to the buffer to read into.
  119. - `uint16_t length`
  120. The number of bytes to read. Take care not to overrun the length of `data`.
  121. - `uint16_t timeout`
  122. The time in milliseconds to wait for a response from the target device.
  123. #### Return Value :id=api-i2c-receive-return
  124. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  125. ---
  126. ### `i2c_status_t i2c_write_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)` :id=api-i2c-write-register
  127. Writes to a register with an 8-bit address on the I2C device.
  128. #### Arguments :id=api-i2c-write-register-arguments
  129. - `uint8_t devaddr`
  130. The 7-bit I2C address of the device.
  131. - `uint8_t regaddr`
  132. The register address to write to.
  133. - `uint8_t *data`
  134. A pointer to the data to transmit.
  135. - `uint16_t length`
  136. The number of bytes to write. Take care not to overrun the length of `data`.
  137. - `uint16_t timeout`
  138. The time in milliseconds to wait for a response from the target device.
  139. #### Return Value :id=api-i2c-write-register-return
  140. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  141. ---
  142. ### `i2c_status_t i2c_write_register16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)` :id=api-i2c-write-register16
  143. Writes to a register with a 16-bit address (big endian) on the I2C device.
  144. #### Arguments :id=api-i2c-write-register16-arguments
  145. - `uint8_t devaddr`
  146. The 7-bit I2C address of the device.
  147. - `uint16_t regaddr`
  148. The register address to write to.
  149. - `uint8_t *data`
  150. A pointer to the data to transmit.
  151. - `uint16_t length`
  152. The number of bytes to write. Take care not to overrun the length of `data`.
  153. - `uint16_t timeout`
  154. The time in milliseconds to wait for a response from the target device.
  155. #### Return Value :id=api-i2c-write-register16-return
  156. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  157. ---
  158. ### `i2c_status_t i2c_read_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)` :id=api-i2c-read-register
  159. Reads from a register with an 8-bit address on the I2C device.
  160. #### Arguments :id=api-i2c-read-register-arguments
  161. - `uint8_t devaddr`
  162. The 7-bit I2C address of the device.
  163. - `uint8_t regaddr`
  164. The register address to read from.
  165. - `uint16_t length`
  166. The number of bytes to read. Take care not to overrun the length of `data`.
  167. - `uint16_t timeout`
  168. The time in milliseconds to wait for a response from the target device.
  169. #### Return Value :id=api-i2c-read-register-return
  170. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  171. ---
  172. ### `i2c_status_t i2c_read_register16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)` :id=api-i2c-read-register16
  173. Reads from a register with a 16-bit address (big endian) on the I2C device.
  174. #### Arguments :id=api-i2c-read-register16-arguments
  175. - `uint8_t devaddr`
  176. The 7-bit I2C address of the device.
  177. - `uint16_t regaddr`
  178. The register address to read from.
  179. - `uint16_t length`
  180. The number of bytes to read. Take care not to overrun the length of `data`.
  181. - `uint16_t timeout`
  182. The time in milliseconds to wait for a response from the target device.
  183. #### Return Value :id=api-i2c-read-register16-return
  184. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  185. ---
  186. ### `i2c_status_t i2c_ping_address(uint8_t address, uint16_t timeout)` :id=api-i2c-ping-address
  187. Pings the I2C bus for a specific address.
  188. On ChibiOS a "best effort" attempt is made by reading a single byte from register 0 at the requested 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 (unsucessful response to ping attempt).
  189. This function is weakly defined, meaning it can be overridden if necessary for your particular use case:
  190. #### Arguments
  191. - `uint8_t address`
  192. The 7-bit I2C address of the device (ie. without the read/write bit - this will be set automatically).
  193. - `uint16_t timeout`
  194. The time in milliseconds to wait for a response from the target device.
  195. #### Return Value
  196. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.