stm32_i2c.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2016-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef STM32_I2C_H
  7. #define STM32_I2C_H
  8. #include <stdint.h>
  9. #include <lib/utils_def.h>
  10. /* Bit definition for I2C_CR1 register */
  11. #define I2C_CR1_PE BIT(0)
  12. #define I2C_CR1_TXIE BIT(1)
  13. #define I2C_CR1_RXIE BIT(2)
  14. #define I2C_CR1_ADDRIE BIT(3)
  15. #define I2C_CR1_NACKIE BIT(4)
  16. #define I2C_CR1_STOPIE BIT(5)
  17. #define I2C_CR1_TCIE BIT(6)
  18. #define I2C_CR1_ERRIE BIT(7)
  19. #define I2C_CR1_DNF GENMASK(11, 8)
  20. #define I2C_CR1_ANFOFF BIT(12)
  21. #define I2C_CR1_SWRST BIT(13)
  22. #define I2C_CR1_TXDMAEN BIT(14)
  23. #define I2C_CR1_RXDMAEN BIT(15)
  24. #define I2C_CR1_SBC BIT(16)
  25. #define I2C_CR1_NOSTRETCH BIT(17)
  26. #define I2C_CR1_WUPEN BIT(18)
  27. #define I2C_CR1_GCEN BIT(19)
  28. #define I2C_CR1_SMBHEN BIT(22)
  29. #define I2C_CR1_SMBDEN BIT(21)
  30. #define I2C_CR1_ALERTEN BIT(22)
  31. #define I2C_CR1_PECEN BIT(23)
  32. /* Bit definition for I2C_CR2 register */
  33. #define I2C_CR2_SADD GENMASK(9, 0)
  34. #define I2C_CR2_RD_WRN BIT(10)
  35. #define I2C_CR2_RD_WRN_OFFSET 10U
  36. #define I2C_CR2_ADD10 BIT(11)
  37. #define I2C_CR2_HEAD10R BIT(12)
  38. #define I2C_CR2_START BIT(13)
  39. #define I2C_CR2_STOP BIT(14)
  40. #define I2C_CR2_NACK BIT(15)
  41. #define I2C_CR2_NBYTES GENMASK(23, 16)
  42. #define I2C_CR2_NBYTES_OFFSET 16U
  43. #define I2C_CR2_RELOAD BIT(24)
  44. #define I2C_CR2_AUTOEND BIT(25)
  45. #define I2C_CR2_PECBYTE BIT(26)
  46. /* Bit definition for I2C_OAR1 register */
  47. #define I2C_OAR1_OA1 GENMASK(9, 0)
  48. #define I2C_OAR1_OA1MODE BIT(10)
  49. #define I2C_OAR1_OA1EN BIT(15)
  50. /* Bit definition for I2C_OAR2 register */
  51. #define I2C_OAR2_OA2 GENMASK(7, 1)
  52. #define I2C_OAR2_OA2MSK GENMASK(10, 8)
  53. #define I2C_OAR2_OA2NOMASK 0
  54. #define I2C_OAR2_OA2MASK01 BIT(8)
  55. #define I2C_OAR2_OA2MASK02 BIT(9)
  56. #define I2C_OAR2_OA2MASK03 GENMASK(9, 8)
  57. #define I2C_OAR2_OA2MASK04 BIT(10)
  58. #define I2C_OAR2_OA2MASK05 (BIT(8) | BIT(10))
  59. #define I2C_OAR2_OA2MASK06 (BIT(9) | BIT(10))
  60. #define I2C_OAR2_OA2MASK07 GENMASK(10, 8)
  61. #define I2C_OAR2_OA2EN BIT(15)
  62. /* Bit definition for I2C_TIMINGR register */
  63. #define I2C_TIMINGR_SCLL GENMASK(7, 0)
  64. #define I2C_TIMINGR_SCLH GENMASK(15, 8)
  65. #define I2C_TIMINGR_SDADEL GENMASK(19, 16)
  66. #define I2C_TIMINGR_SCLDEL GENMASK(23, 20)
  67. #define I2C_TIMINGR_PRESC GENMASK(31, 28)
  68. /* Bit definition for I2C_TIMEOUTR register */
  69. #define I2C_TIMEOUTR_TIMEOUTA GENMASK(11, 0)
  70. #define I2C_TIMEOUTR_TIDLE BIT(12)
  71. #define I2C_TIMEOUTR_TIMOUTEN BIT(15)
  72. #define I2C_TIMEOUTR_TIMEOUTB GENMASK(27, 16)
  73. #define I2C_TIMEOUTR_TEXTEN BIT(31)
  74. /* Bit definition for I2C_ISR register */
  75. #define I2C_ISR_TXE BIT(0)
  76. #define I2C_ISR_TXIS BIT(1)
  77. #define I2C_ISR_RXNE BIT(2)
  78. #define I2C_ISR_ADDR BIT(3)
  79. #define I2C_ISR_NACKF BIT(4)
  80. #define I2C_ISR_STOPF BIT(5)
  81. #define I2C_ISR_TC BIT(6)
  82. #define I2C_ISR_TCR BIT(7)
  83. #define I2C_ISR_BERR BIT(8)
  84. #define I2C_ISR_ARLO BIT(9)
  85. #define I2C_ISR_OVR BIT(10)
  86. #define I2C_ISR_PECERR BIT(11)
  87. #define I2C_ISR_TIMEOUT BIT(12)
  88. #define I2C_ISR_ALERT BIT(13)
  89. #define I2C_ISR_BUSY BIT(15)
  90. #define I2C_ISR_DIR BIT(16)
  91. #define I2C_ISR_ADDCODE GENMASK(23, 17)
  92. /* Bit definition for I2C_ICR register */
  93. #define I2C_ICR_ADDRCF BIT(3)
  94. #define I2C_ICR_NACKCF BIT(4)
  95. #define I2C_ICR_STOPCF BIT(5)
  96. #define I2C_ICR_BERRCF BIT(8)
  97. #define I2C_ICR_ARLOCF BIT(9)
  98. #define I2C_ICR_OVRCF BIT(10)
  99. #define I2C_ICR_PECCF BIT(11)
  100. #define I2C_ICR_TIMOUTCF BIT(12)
  101. #define I2C_ICR_ALERTCF BIT(13)
  102. enum i2c_speed_e {
  103. I2C_SPEED_STANDARD, /* 100 kHz */
  104. I2C_SPEED_FAST, /* 400 kHz */
  105. I2C_SPEED_FAST_PLUS, /* 1 MHz */
  106. };
  107. #define STANDARD_RATE 100000
  108. #define FAST_RATE 400000
  109. #define FAST_PLUS_RATE 1000000
  110. struct stm32_i2c_init_s {
  111. uint32_t own_address1; /*
  112. * Specifies the first device own
  113. * address. This parameter can be a
  114. * 7-bit or 10-bit address.
  115. */
  116. uint32_t addressing_mode; /*
  117. * Specifies if 7-bit or 10-bit
  118. * addressing mode is selected.
  119. * This parameter can be a value of
  120. * @ref I2C_ADDRESSING_MODE.
  121. */
  122. uint32_t dual_address_mode; /*
  123. * Specifies if dual addressing mode is
  124. * selected.
  125. * This parameter can be a value of @ref
  126. * I2C_DUAL_ADDRESSING_MODE.
  127. */
  128. uint32_t own_address2; /*
  129. * Specifies the second device own
  130. * address if dual addressing mode is
  131. * selected. This parameter can be a
  132. * 7-bit address.
  133. */
  134. uint32_t own_address2_masks; /*
  135. * Specifies the acknowledge mask
  136. * address second device own address
  137. * if dual addressing mode is selected
  138. * This parameter can be a value of @ref
  139. * I2C_OWN_ADDRESS2_MASKS.
  140. */
  141. uint32_t general_call_mode; /*
  142. * Specifies if general call mode is
  143. * selected.
  144. * This parameter can be a value of @ref
  145. * I2C_GENERAL_CALL_ADDRESSING_MODE.
  146. */
  147. uint32_t no_stretch_mode; /*
  148. * Specifies if nostretch mode is
  149. * selected.
  150. * This parameter can be a value of @ref
  151. * I2C_NOSTRETCH_MODE.
  152. */
  153. uint32_t rise_time; /*
  154. * Specifies the SCL clock pin rising
  155. * time in nanoseconds.
  156. */
  157. uint32_t fall_time; /*
  158. * Specifies the SCL clock pin falling
  159. * time in nanoseconds.
  160. */
  161. enum i2c_speed_e speed_mode; /*
  162. * Specifies the I2C clock source
  163. * frequency mode.
  164. * This parameter can be a value of @ref
  165. * i2c_speed_mode_e.
  166. */
  167. int analog_filter; /*
  168. * Specifies if the I2C analog noise
  169. * filter is selected.
  170. * This parameter can be 0 (filter
  171. * off), all other values mean filter
  172. * on.
  173. */
  174. uint8_t digital_filter_coef; /*
  175. * Specifies the I2C digital noise
  176. * filter coefficient.
  177. * This parameter can be a value
  178. * between 0 and
  179. * STM32_I2C_DIGITAL_FILTER_MAX.
  180. */
  181. };
  182. enum i2c_state_e {
  183. I2C_STATE_RESET = 0x00U, /* Not yet initialized */
  184. I2C_STATE_READY = 0x20U, /* Ready for use */
  185. I2C_STATE_BUSY = 0x24U, /* Internal process ongoing */
  186. I2C_STATE_BUSY_TX = 0x21U, /* Data Transmission ongoing */
  187. I2C_STATE_BUSY_RX = 0x22U, /* Data Reception ongoing */
  188. };
  189. enum i2c_mode_e {
  190. I2C_MODE_NONE = 0x00U, /* No active communication */
  191. I2C_MODE_MASTER = 0x10U, /* Communication in Master Mode */
  192. I2C_MODE_SLAVE = 0x20U, /* Communication in Slave Mode */
  193. I2C_MODE_MEM = 0x40U /* Communication in Memory Mode */
  194. };
  195. #define I2C_ERROR_NONE 0x00000000U /* No error */
  196. #define I2C_ERROR_BERR 0x00000001U /* BERR error */
  197. #define I2C_ERROR_ARLO 0x00000002U /* ARLO error */
  198. #define I2C_ERROR_AF 0x00000004U /* ACKF error */
  199. #define I2C_ERROR_OVR 0x00000008U /* OVR error */
  200. #define I2C_ERROR_DMA 0x00000010U /* DMA transfer error */
  201. #define I2C_ERROR_TIMEOUT 0x00000020U /* Timeout error */
  202. #define I2C_ERROR_SIZE 0x00000040U /* Size Management error */
  203. struct i2c_handle_s {
  204. uint32_t i2c_base_addr; /* Registers base address */
  205. unsigned int dt_status; /* DT nsec/sec status */
  206. unsigned int clock; /* Clock reference */
  207. uint8_t lock; /* Locking object */
  208. enum i2c_state_e i2c_state; /* Communication state */
  209. enum i2c_mode_e i2c_mode; /* Communication mode */
  210. uint32_t i2c_err; /* Error code */
  211. };
  212. #define I2C_ADDRESSINGMODE_7BIT 0x00000001U
  213. #define I2C_ADDRESSINGMODE_10BIT 0x00000002U
  214. #define I2C_DUALADDRESS_DISABLE 0x00000000U
  215. #define I2C_DUALADDRESS_ENABLE I2C_OAR2_OA2EN
  216. #define I2C_GENERALCALL_DISABLE 0x00000000U
  217. #define I2C_GENERALCALL_ENABLE I2C_CR1_GCEN
  218. #define I2C_NOSTRETCH_DISABLE 0x00000000U
  219. #define I2C_NOSTRETCH_ENABLE I2C_CR1_NOSTRETCH
  220. #define I2C_MEMADD_SIZE_8BIT 0x00000001U
  221. #define I2C_MEMADD_SIZE_16BIT 0x00000002U
  222. #define I2C_RELOAD_MODE I2C_CR2_RELOAD
  223. #define I2C_AUTOEND_MODE I2C_CR2_AUTOEND
  224. #define I2C_SOFTEND_MODE 0x00000000U
  225. #define I2C_NO_STARTSTOP 0x00000000U
  226. #define I2C_GENERATE_STOP (BIT(31) | I2C_CR2_STOP)
  227. #define I2C_GENERATE_START_READ (BIT(31) | I2C_CR2_START | \
  228. I2C_CR2_RD_WRN)
  229. #define I2C_GENERATE_START_WRITE (BIT(31) | I2C_CR2_START)
  230. #define I2C_FLAG_TXE I2C_ISR_TXE
  231. #define I2C_FLAG_TXIS I2C_ISR_TXIS
  232. #define I2C_FLAG_RXNE I2C_ISR_RXNE
  233. #define I2C_FLAG_ADDR I2C_ISR_ADDR
  234. #define I2C_FLAG_AF I2C_ISR_NACKF
  235. #define I2C_FLAG_STOPF I2C_ISR_STOPF
  236. #define I2C_FLAG_TC I2C_ISR_TC
  237. #define I2C_FLAG_TCR I2C_ISR_TCR
  238. #define I2C_FLAG_BERR I2C_ISR_BERR
  239. #define I2C_FLAG_ARLO I2C_ISR_ARLO
  240. #define I2C_FLAG_OVR I2C_ISR_OVR
  241. #define I2C_FLAG_PECERR I2C_ISR_PECERR
  242. #define I2C_FLAG_TIMEOUT I2C_ISR_TIMEOUT
  243. #define I2C_FLAG_ALERT I2C_ISR_ALERT
  244. #define I2C_FLAG_BUSY I2C_ISR_BUSY
  245. #define I2C_FLAG_DIR I2C_ISR_DIR
  246. #define I2C_RESET_CR2 (I2C_CR2_SADD | I2C_CR2_HEAD10R | \
  247. I2C_CR2_NBYTES | I2C_CR2_RELOAD | \
  248. I2C_CR2_RD_WRN)
  249. #define I2C_TIMEOUT_BUSY_MS 25U
  250. #define I2C_ANALOGFILTER_ENABLE 0x00000000U
  251. #define I2C_ANALOGFILTER_DISABLE I2C_CR1_ANFOFF
  252. /* STM32 specific defines */
  253. #define STM32_I2C_RISE_TIME_DEFAULT 25 /* ns */
  254. #define STM32_I2C_FALL_TIME_DEFAULT 10 /* ns */
  255. #define STM32_I2C_ANALOG_FILTER_DELAY_MIN 50 /* ns */
  256. #define STM32_I2C_ANALOG_FILTER_DELAY_MAX 260 /* ns */
  257. #define STM32_I2C_DIGITAL_FILTER_MAX 16
  258. int stm32_i2c_get_setup_from_fdt(void *fdt, int node,
  259. struct stm32_i2c_init_s *init);
  260. int stm32_i2c_init(struct i2c_handle_s *hi2c,
  261. struct stm32_i2c_init_s *init_data);
  262. int stm32_i2c_mem_write(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  263. uint16_t mem_addr, uint16_t mem_add_size,
  264. uint8_t *p_data, uint16_t size, uint32_t timeout_ms);
  265. int stm32_i2c_mem_read(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  266. uint16_t mem_addr, uint16_t mem_add_size,
  267. uint8_t *p_data, uint16_t size, uint32_t timeout_ms);
  268. int stm32_i2c_master_transmit(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  269. uint8_t *p_data, uint16_t size,
  270. uint32_t timeout_ms);
  271. int stm32_i2c_master_receive(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  272. uint8_t *p_data, uint16_t size,
  273. uint32_t timeout_ms);
  274. bool stm32_i2c_is_device_ready(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  275. uint32_t trials, uint32_t timeout_ms);
  276. #endif /* STM32_I2C_H */