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.

237 lines
8.9 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. ## I2C Addressing :id=note-on-i2c-addresses
  4. All of the addresses expected by this driver should be pushed to the upper 7 bits of the address byte. Setting
  5. the lower bit (indicating read/write) will be done by the respective functions. Almost all I2C addresses listed
  6. on datasheets and the internet will be represented as 7 bits occupying the lower 7 bits and will need to be
  7. shifted to the left (more significant) by one bit. This is easy to do via the bitwise shift operator `<< 1`.
  8. 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`:
  9. ```c
  10. #define MY_I2C_ADDRESS (0x18 << 1)
  11. ```
  12. See https://www.robot-electronics.co.uk/i2c-tutorial for more information about I2C addressing and other technical details.
  13. ## AVR Configuration :id=avr-configuration
  14. The following defines can be used to configure the I2C master driver:
  15. |`config.h` Override|Description |Default |
  16. |-------------------|---------------------|--------|
  17. |`F_SCL` |Clock frequency in Hz|`400000`|
  18. No further setup is required - just connect the `SDA` and `SCL` pins of your I2C devices to the matching pins on the MCU:
  19. |MCU |`SCL`|`SDA`|
  20. |------------------|-----|-----|
  21. |ATmega16/32U4 |`D0` |`D1` |
  22. |AT90USB64/128 |`D0` |`D1` |
  23. |ATmega32A |`C0` |`C1` |
  24. |ATmega328/P |`C5` |`C4` |
  25. ?> The ATmega16/32U2 does not possess I2C functionality, and so cannot use this driver.
  26. ## ChibiOS/ARM Configuration :id=arm-configuration
  27. 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.
  28. To enable I2C, modify your board's `halconf.h` to enable I2C:
  29. ```c
  30. #define HAL_USE_I2C TRUE
  31. ```
  32. Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
  33. ```c
  34. #undef STM32_I2C_USE_I2C2
  35. #define STM32_I2C_USE_I2C2 TRUE
  36. ```
  37. |`mcuconf.h` Setting |Description |Default|
  38. |----------------------------|----------------------------------------------------------------------------------|-------|
  39. |`STM32_I2C_BUSY_TIMEOUT` |Time in milliseconds until the I2C command is aborted if no response is received |`50` |
  40. |`STM32_I2C_XXX_IRQ_PRIORITY`|Interrupt priority for hardware driver XXX (THIS IS AN EXPERT SETTING) |`10` |
  41. |`STM32_I2C_USE_DMA` |Enable/Disable the ability of the MCU to offload the data transfer to the DMA unit|`TRUE` |
  42. |`STM32_I2C_XXX_DMA_PRIORITY`|Priority of DMA unit for hardware driver XXX (THIS IS AN EXPERT SETTING) |`1` |
  43. 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.
  44. |`config.h` Overrride |Description |Default|
  45. |------------------------|-------------------------------------------------------------------------------------------|-------|
  46. |`I2C_DRIVER` |I2C peripheral to use - I2C1 -> `I2CD1`, I2C2 -> `I2CD2` etc. |`I2CD1`|
  47. |`I2C1_BANK` (deprecated)|The bank of pins (`GPIOA`, `GPIOB`, `GPIOC`), superseded by `I2C1_SCL_BANK`/`I2C1_SDA_BANK`|`GPIOB`|
  48. |`I2C1_SCL_BANK` |The bank of pins (`GPIOA`, `GPIOB`, `GPIOC`) to use for SCL |`GPIOB`|
  49. |`I2C1_SCL` |The pin number for SCL (0-15) |`6` |
  50. |`I2C1_SCL_PAL_MODE` |The alternate function mode for SCL |`4` |
  51. |`I2C1_SDA_BANK` |The bank of pins (`GPIOA`, `GPIOB`, `GPIOC`) to use for SDA |`GPIOB`|
  52. |`I2C1_SDA` |The pin number for SDA (0-15) |`7` |
  53. |`I2C1_SDA_PAL_MODE` |The alternate function mode for SDA |`4` |
  54. The following configuration values depend on the specific MCU in use.
  55. ### I2Cv1 :id=i2cv1
  56. * STM32F1xx
  57. * STM32F2xx
  58. * STM32F4xx
  59. * STM32L0xx
  60. * STM32L1xx
  61. See [this page](https://www.playembedded.org/blog/stm32-i2c-chibios/#7_I2Cv1_configuration_structure) for the I2Cv1 configuration structure.
  62. |`config.h` Override|Default |
  63. |-------------------|----------------|
  64. |`I2C1_OPMODE` |`OPMODE_I2C` |
  65. |`I2C1_CLOCK_SPEED` |`100000` |
  66. |`I2C1_DUTY_CYCLE` |`STD_DUTY_CYCLE`|
  67. ### I2Cv2 :id=i2cv2
  68. * STM32F0xx
  69. * STM32F3xx
  70. * STM32F7xx
  71. * STM32L4xx
  72. See [this page](https://www.playembedded.org/blog/stm32-i2c-chibios/#8_I2Cv2_I2Cv3_configuration_structure) for the I2Cv2 configuration structure.
  73. |`config.h` Override |Default|
  74. |---------------------|-------|
  75. |`I2C1_TIMINGR_PRESC` |`0U` |
  76. |`I2C1_TIMINGR_SCLDEL`|`7U` |
  77. |`I2C1_TIMINGR_SDADEL`|`0U` |
  78. |`I2C1_TIMINGR_SCLH` |`38U` |
  79. |`I2C1_TIMINGR_SCLL` |`129U` |
  80. ## Functions :id=functions
  81. ### `void i2c_init(void)`
  82. Initialize the I2C driver. This function must be called only once, before any of the below functions can be called.
  83. This function is weakly defined, meaning it can be overridden if necessary for your particular use case:
  84. ```c
  85. void i2c_init(void) {
  86. setPinInput(B6); // Try releasing special pins for a short time
  87. setPinInput(B7);
  88. wait_ms(10); // Wait for the release to happen
  89. palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP); // Set B6 to I2C function
  90. palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP); // Set B7 to I2C function
  91. }
  92. ```
  93. ---
  94. ### `i2c_status_t i2c_start(uint8_t address, uint16_t timeout)`
  95. Start an I2C transaction.
  96. #### Arguments
  97. - `uint8_t address`
  98. The 7-bit I2C address of the device (ie. without the read/write bit - this will be set automatically).
  99. - `uint16_t timeout`
  100. The time in milliseconds to wait for a response from the target device.
  101. #### Return Value
  102. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  103. ---
  104. ### `i2c_status_t i2c_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout)`
  105. Send multiple bytes to the selected I2C device.
  106. #### Arguments
  107. - `uint8_t address`
  108. The 7-bit I2C address of the device.
  109. - `uint8_t *data`
  110. A pointer to the data to transmit.
  111. - `uint16_t length`
  112. The number of bytes to write. Take care not to overrun the length of `data`.
  113. - `uint16_t timeout`
  114. The time in milliseconds to wait for a response from the target device.
  115. #### Return Value
  116. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  117. ---
  118. ### `i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)`
  119. Receive multiple bytes from the selected SPI device.
  120. #### Arguments
  121. - `uint8_t address`
  122. The 7-bit I2C address of the device.
  123. - `uint8_t *data`
  124. A pointer to the buffer to read into.
  125. - `uint16_t length`
  126. The number of bytes to read. Take care not to overrun the length of `data`.
  127. - `uint16_t timeout`
  128. The time in milliseconds to wait for a response from the target device.
  129. #### Return Value
  130. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  131. ---
  132. ### `i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)`
  133. Writes to a register on the I2C device.
  134. #### Arguments
  135. - `uint8_t devaddr`
  136. The 7-bit I2C address of the device.
  137. - `uint8_t regaddr`
  138. The register address to write to.
  139. - `uint8_t *data`
  140. A pointer to the data to transmit.
  141. - `uint16_t length`
  142. The number of bytes to write. Take care not to overrun the length of `data`.
  143. - `uint16_t timeout`
  144. The time in milliseconds to wait for a response from the target device.
  145. #### Return Value
  146. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  147. ---
  148. ### `i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)`
  149. Reads from a register on the I2C device.
  150. #### Arguments
  151. - `uint8_t devaddr`
  152. The 7-bit I2C address of the device.
  153. - `uint8_t regaddr`
  154. The register address to read from.
  155. - `uint16_t length`
  156. The number of bytes to read. Take care not to overrun the length of `data`.
  157. - `uint16_t timeout`
  158. The time in milliseconds to wait for a response from the target device.
  159. #### Return Value
  160. `I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
  161. ---
  162. ### `i2c_status_t i2c_stop(void)`
  163. Stop the current I2C transaction.