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.

128 lines
3.9 KiB

  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 follows the convention of the AVR i2c_master library.
  18. * As a result addresses are expected to be already shifted (addr << 1).
  19. * I2CD1 is the default driver which corresponds to pins B6 and B7. This
  20. * can be changed.
  21. * Please ensure that HAL_USE_I2C is TRUE in the halconf.h file and that
  22. * STM32_I2C_USE_I2C1 is TRUE in the mcuconf.h file.
  23. */
  24. #pragma once
  25. #include <ch.h>
  26. #include <hal.h>
  27. #include "gpio.h"
  28. #ifndef I2C_COUNT
  29. # define I2C_COUNT 1
  30. #endif
  31. #ifdef I2C1_BANK
  32. # define I2C1_SCL_BANK I2C1_BANK
  33. # define I2C1_SDA_BANK I2C1_BANK
  34. #endif
  35. #ifndef I2C1_SCL_BANK
  36. # define I2C1_SCL_BANK GPIOB
  37. #endif
  38. #ifndef I2C1_SDA_BANK
  39. # define I2C1_SDA_BANK GPIOB
  40. #endif
  41. #ifdef USE_I2C2
  42. # ifdef I2C2_BANK
  43. # define I2C2_SCL_BANK I2C2_BANK
  44. # define I2C2_SDA_BANK I2C2_BANK
  45. # endif
  46. # ifndef I2C2_SCL_BANK
  47. # define I2C2_SCL_BANK GPIOC
  48. # endif
  49. # ifndef I2C2_SDA_BANK
  50. # define I2C2_SDA_BANK GPIOC
  51. # endif
  52. #endif
  53. #ifndef I2C1_SCL_PIN
  54. # define I2C1_SCL_PIN 6
  55. #endif
  56. #ifndef I2C1_SDA_PIN
  57. # define I2C1_SDA_PIN 7
  58. #endif
  59. #ifdef USE_I2CV1
  60. # ifndef I2C1_OPMODE
  61. # define I2C1_OPMODE OPMODE_I2C
  62. # endif
  63. # ifndef I2C1_CLOCK_SPEED
  64. # define I2C1_CLOCK_SPEED 100000 /* 400000 */
  65. # endif
  66. # ifndef I2C1_DUTY_CYCLE
  67. # define I2C1_DUTY_CYCLE STD_DUTY_CYCLE /* FAST_DUTY_CYCLE_2 */
  68. # endif
  69. #else
  70. // The default timing values below configures the I2C clock to 400khz assuming a 72Mhz clock
  71. // For more info : https://www.st.com/en/embedded-software/stsw-stm32126.html
  72. # ifndef I2C1_TIMINGR_PRESC
  73. # define I2C1_TIMINGR_PRESC 0U
  74. # endif
  75. # ifndef I2C1_TIMINGR_SCLDEL
  76. # define I2C1_TIMINGR_SCLDEL 7U
  77. # endif
  78. # ifndef I2C1_TIMINGR_SDADEL
  79. # define I2C1_TIMINGR_SDADEL 0U
  80. # endif
  81. # ifndef I2C1_TIMINGR_SCLH
  82. # define I2C1_TIMINGR_SCLH 38U
  83. # endif
  84. # ifndef I2C1_TIMINGR_SCLL
  85. # define I2C1_TIMINGR_SCLL 129U
  86. # endif
  87. #endif
  88. #ifdef USE_GPIOV1
  89. # ifndef I2C1_SCL_PAL_MODE
  90. # define I2C1_SCL_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN
  91. # endif
  92. # ifndef I2C1_SDA_PAL_MODE
  93. # define I2C1_SDA_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN
  94. # endif
  95. #else
  96. // The default PAL alternate modes are used to signal that the pins are used for I2C
  97. # ifndef I2C1_SCL_PAL_MODE
  98. # define I2C1_SCL_PAL_MODE 4
  99. # endif
  100. # ifndef I2C1_SDA_PAL_MODE
  101. # define I2C1_SDA_PAL_MODE 4
  102. # endif
  103. #endif
  104. typedef int16_t i2c_status_t;
  105. #define I2C_STATUS_SUCCESS (0)
  106. #define I2C_STATUS_ERROR (-1)
  107. #define I2C_STATUS_TIMEOUT (-2)
  108. void i2c_init(I2CDriver *driver, ioline_t scl_pin, ioline_t sda_pin);
  109. i2c_status_t i2c_start(uint8_t index, uint8_t address);
  110. i2c_status_t i2c_transmit(uint8_t index, uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout);
  111. i2c_status_t i2c_receive(uint8_t index, uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout);
  112. i2c_status_t i2c_write_register(uint8_t index, uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout);
  113. i2c_status_t i2c_read_register(uint8_t index, uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout);
  114. void i2c_stop(uint8_t index);