logo

qmk_firmware

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

i2c_master.c (7617B)


  1. /* Copyright 2018 Jack Humbert
  2. * Copyright 2018 Yiancar
  3. * Copyright 2023 customMK
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* This library is only valid for STM32 processors.
  19. * This library follows the convention of the AVR i2c_master library.
  20. * As a result addresses are expected to be already shifted (addr << 1).
  21. * I2CD1 is the default driver which corresponds to pins B6 and B7. This
  22. * can be changed.
  23. * Please ensure that HAL_USE_I2C is TRUE in the halconf.h file and that
  24. * STM32_I2C_USE_I2C1 is TRUE in the mcuconf.h file. Pins B6 and B7 are used
  25. * but using any other I2C pins should be trivial.
  26. */
  27. #include "i2c_master.h"
  28. #include "gpio.h"
  29. #include "chibios_config.h"
  30. #include <ch.h>
  31. #include <hal.h>
  32. #ifndef I2C_DRIVER
  33. # define I2C_DRIVER I2CD1
  34. #endif
  35. #ifndef I2C1_SCL_PIN
  36. # define I2C1_SCL_PIN B6
  37. #endif
  38. #ifndef I2C1_SCL_PAL_MODE
  39. # ifdef USE_GPIOV1
  40. # define I2C1_SCL_PAL_MODE PAL_MODE_ALTERNATE_OPENDRAIN
  41. # else
  42. # define I2C1_SCL_PAL_MODE 4
  43. # endif
  44. #endif
  45. #ifndef I2C1_SDA_PIN
  46. # define I2C1_SDA_PIN B7
  47. #endif
  48. #ifndef I2C1_SDA_PAL_MODE
  49. # ifdef USE_GPIOV1
  50. # define I2C1_SDA_PAL_MODE PAL_MODE_ALTERNATE_OPENDRAIN
  51. # else
  52. # define I2C1_SDA_PAL_MODE 4
  53. # endif
  54. #endif
  55. #ifdef USE_I2CV1
  56. # ifndef I2C1_OPMODE
  57. # define I2C1_OPMODE OPMODE_I2C
  58. # endif
  59. # ifndef I2C1_CLOCK_SPEED
  60. # define I2C1_CLOCK_SPEED 100000 /* 400000 */
  61. # endif
  62. # ifndef I2C1_DUTY_CYCLE
  63. # define I2C1_DUTY_CYCLE STD_DUTY_CYCLE /* FAST_DUTY_CYCLE_2 */
  64. # endif
  65. #else
  66. // The default timing values below configures the I2C clock to 400khz assuming a 72Mhz clock
  67. // For more info : https://www.st.com/en/embedded-software/stsw-stm32126.html
  68. # ifndef I2C1_TIMINGR_PRESC
  69. # define I2C1_TIMINGR_PRESC 0U
  70. # endif
  71. # ifndef I2C1_TIMINGR_SCLDEL
  72. # define I2C1_TIMINGR_SCLDEL 7U
  73. # endif
  74. # ifndef I2C1_TIMINGR_SDADEL
  75. # define I2C1_TIMINGR_SDADEL 0U
  76. # endif
  77. # ifndef I2C1_TIMINGR_SCLH
  78. # define I2C1_TIMINGR_SCLH 38U
  79. # endif
  80. # ifndef I2C1_TIMINGR_SCLL
  81. # define I2C1_TIMINGR_SCLL 129U
  82. # endif
  83. #endif
  84. static const I2CConfig i2cconfig = {
  85. #if defined(USE_I2CV1_CONTRIB)
  86. I2C1_CLOCK_SPEED,
  87. #elif defined(USE_I2CV1)
  88. I2C1_OPMODE,
  89. I2C1_CLOCK_SPEED,
  90. I2C1_DUTY_CYCLE,
  91. #elif defined(WB32F3G71xx) || defined(WB32FQ95xx)
  92. I2C1_OPMODE,
  93. I2C1_CLOCK_SPEED,
  94. #else
  95. // This configures the I2C clock to 400khz assuming a 72Mhz clock
  96. // For more info : https://www.st.com/en/embedded-software/stsw-stm32126.html
  97. STM32_TIMINGR_PRESC(I2C1_TIMINGR_PRESC) | STM32_TIMINGR_SCLDEL(I2C1_TIMINGR_SCLDEL) | STM32_TIMINGR_SDADEL(I2C1_TIMINGR_SDADEL) | STM32_TIMINGR_SCLH(I2C1_TIMINGR_SCLH) | STM32_TIMINGR_SCLL(I2C1_TIMINGR_SCLL), 0, 0
  98. #endif
  99. };
  100. /**
  101. * @brief Handles any I2C error condition by stopping the I2C peripheral and
  102. * aborting any ongoing transactions. Furthermore ChibiOS status codes are
  103. * converted into QMK codes.
  104. *
  105. * @param status ChibiOS specific I2C status code
  106. * @return i2c_status_t QMK specific I2C status code
  107. */
  108. static i2c_status_t i2c_epilogue(const msg_t status) {
  109. if (status == MSG_OK) {
  110. return I2C_STATUS_SUCCESS;
  111. }
  112. // From ChibiOS HAL: "After a timeout the driver must be stopped and
  113. // restarted because the bus is in an uncertain state." We also issue that
  114. // hard stop in case of any error.
  115. i2cStop(&I2C_DRIVER);
  116. return status == MSG_TIMEOUT ? I2C_STATUS_TIMEOUT : I2C_STATUS_ERROR;
  117. }
  118. __attribute__((weak)) void i2c_init(void) {
  119. static bool is_initialised = false;
  120. if (!is_initialised) {
  121. is_initialised = true;
  122. // Try releasing special pins for a short time
  123. palSetLineMode(I2C1_SCL_PIN, PAL_MODE_INPUT);
  124. palSetLineMode(I2C1_SDA_PIN, PAL_MODE_INPUT);
  125. chThdSleepMilliseconds(10);
  126. #if defined(USE_GPIOV1)
  127. palSetLineMode(I2C1_SCL_PIN, I2C1_SCL_PAL_MODE);
  128. palSetLineMode(I2C1_SDA_PIN, I2C1_SDA_PAL_MODE);
  129. #else
  130. palSetLineMode(I2C1_SCL_PIN, PAL_MODE_ALTERNATE(I2C1_SCL_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
  131. palSetLineMode(I2C1_SDA_PIN, PAL_MODE_ALTERNATE(I2C1_SDA_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
  132. #endif
  133. }
  134. }
  135. i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
  136. i2cStart(&I2C_DRIVER, &i2cconfig);
  137. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (address >> 1), data, length, 0, 0, TIME_MS2I(timeout));
  138. return i2c_epilogue(status);
  139. }
  140. i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) {
  141. i2cStart(&I2C_DRIVER, &i2cconfig);
  142. msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (address >> 1), data, length, TIME_MS2I(timeout));
  143. return i2c_epilogue(status);
  144. }
  145. i2c_status_t i2c_write_register(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
  146. i2cStart(&I2C_DRIVER, &i2cconfig);
  147. uint8_t complete_packet[length + 1];
  148. for (uint16_t i = 0; i < length; i++) {
  149. complete_packet[i + 1] = data[i];
  150. }
  151. complete_packet[0] = regaddr;
  152. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), complete_packet, length + 1, 0, 0, TIME_MS2I(timeout));
  153. return i2c_epilogue(status);
  154. }
  155. i2c_status_t i2c_write_register16(uint8_t devaddr, uint16_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
  156. i2cStart(&I2C_DRIVER, &i2cconfig);
  157. uint8_t complete_packet[length + 2];
  158. for (uint16_t i = 0; i < length; i++) {
  159. complete_packet[i + 2] = data[i];
  160. }
  161. complete_packet[0] = regaddr >> 8;
  162. complete_packet[1] = regaddr & 0xFF;
  163. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), complete_packet, length + 2, 0, 0, TIME_MS2I(timeout));
  164. return i2c_epilogue(status);
  165. }
  166. i2c_status_t i2c_read_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
  167. i2cStart(&I2C_DRIVER, &i2cconfig);
  168. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), &regaddr, 1, data, length, TIME_MS2I(timeout));
  169. return i2c_epilogue(status);
  170. }
  171. i2c_status_t i2c_read_register16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
  172. i2cStart(&I2C_DRIVER, &i2cconfig);
  173. uint8_t register_packet[2] = {regaddr >> 8, regaddr & 0xFF};
  174. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), register_packet, 2, data, length, TIME_MS2I(timeout));
  175. return i2c_epilogue(status);
  176. }
  177. __attribute__((weak)) i2c_status_t i2c_ping_address(uint8_t address, uint16_t timeout) {
  178. // ChibiOS does not provide low level enough control to check for an ack.
  179. // Best effort instead tries reading register 0 which will either succeed or timeout.
  180. // This approach may produce false negative results for I2C devices that do not respond to a register 0 read request.
  181. uint8_t data = 0;
  182. return i2c_read_register(address, 0, &data, sizeof(data), timeout);
  183. }