i2c_master.c 7.5 KB

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