stm32_i2c.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * Copyright (c) 2016-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <stdbool.h>
  8. #include <stdlib.h>
  9. #include <common/debug.h>
  10. #include <common/fdt_wrappers.h>
  11. #include <drivers/clk.h>
  12. #include <drivers/delay_timer.h>
  13. #include <drivers/st/stm32_gpio.h>
  14. #include <drivers/st/stm32_i2c.h>
  15. #include <lib/mmio.h>
  16. #include <lib/utils.h>
  17. #include <libfdt.h>
  18. #include <platform_def.h>
  19. /* STM32 I2C registers offsets */
  20. #define I2C_CR1 0x00U
  21. #define I2C_CR2 0x04U
  22. #define I2C_OAR1 0x08U
  23. #define I2C_OAR2 0x0CU
  24. #define I2C_TIMINGR 0x10U
  25. #define I2C_TIMEOUTR 0x14U
  26. #define I2C_ISR 0x18U
  27. #define I2C_ICR 0x1CU
  28. #define I2C_PECR 0x20U
  29. #define I2C_RXDR 0x24U
  30. #define I2C_TXDR 0x28U
  31. #define TIMINGR_CLEAR_MASK 0xF0FFFFFFU
  32. #define MAX_NBYTE_SIZE 255U
  33. #define I2C_NSEC_PER_SEC 1000000000L
  34. /* I2C Timing hard-coded value, for I2C clock source is HSI at 64MHz */
  35. #define I2C_TIMING 0x10D07DB5
  36. static void notif_i2c_timeout(struct i2c_handle_s *hi2c)
  37. {
  38. hi2c->i2c_err |= I2C_ERROR_TIMEOUT;
  39. hi2c->i2c_mode = I2C_MODE_NONE;
  40. hi2c->i2c_state = I2C_STATE_READY;
  41. }
  42. /*
  43. * @brief Configure I2C Analog noise filter.
  44. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  45. * the configuration information for the specified I2C peripheral.
  46. * @param analog_filter: New state of the Analog filter
  47. * @retval 0 if OK, negative value else
  48. */
  49. static int i2c_config_analog_filter(struct i2c_handle_s *hi2c,
  50. uint32_t analog_filter)
  51. {
  52. if ((hi2c->i2c_state != I2C_STATE_READY) || (hi2c->lock != 0U)) {
  53. return -EBUSY;
  54. }
  55. hi2c->lock = 1;
  56. hi2c->i2c_state = I2C_STATE_BUSY;
  57. /* Disable the selected I2C peripheral */
  58. mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_PE);
  59. /* Reset I2Cx ANOFF bit */
  60. mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_ANFOFF);
  61. /* Set analog filter bit*/
  62. mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR1, analog_filter);
  63. /* Enable the selected I2C peripheral */
  64. mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_PE);
  65. hi2c->i2c_state = I2C_STATE_READY;
  66. hi2c->lock = 0;
  67. return 0;
  68. }
  69. /*
  70. * @brief Get I2C setup information from the device tree and set pinctrl
  71. * configuration.
  72. * @param fdt: Pointer to the device tree
  73. * @param node: I2C node offset
  74. * @param init: Ref to the initialization configuration structure
  75. * @retval 0 if OK, negative value else
  76. */
  77. int stm32_i2c_get_setup_from_fdt(void *fdt, int node,
  78. struct stm32_i2c_init_s *init)
  79. {
  80. uint32_t read_val;
  81. init->rise_time = fdt_read_uint32_default(fdt, node,
  82. "i2c-scl-rising-time-ns",
  83. STM32_I2C_RISE_TIME_DEFAULT);
  84. init->fall_time = fdt_read_uint32_default(fdt, node,
  85. "i2c-scl-falling-time-ns",
  86. STM32_I2C_FALL_TIME_DEFAULT);
  87. read_val = fdt_read_uint32_default(fdt, node, "clock-frequency",
  88. STANDARD_RATE);
  89. switch (read_val) {
  90. case FAST_PLUS_RATE:
  91. init->speed_mode = I2C_SPEED_FAST_PLUS;
  92. break;
  93. case FAST_RATE:
  94. init->speed_mode = I2C_SPEED_FAST;
  95. break;
  96. case STANDARD_RATE:
  97. default:
  98. init->speed_mode = I2C_SPEED_STANDARD;
  99. break;
  100. }
  101. return dt_set_pinctrl_config(node);
  102. }
  103. /*
  104. * @brief Initialize the I2C device.
  105. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  106. * the configuration information for the specified I2C.
  107. * @param init_data: Initialization configuration structure
  108. * @retval 0 if OK, negative value else
  109. */
  110. int stm32_i2c_init(struct i2c_handle_s *hi2c,
  111. struct stm32_i2c_init_s *init_data)
  112. {
  113. int rc = 0;
  114. uint32_t timing = I2C_TIMING;
  115. if (hi2c == NULL) {
  116. return -ENOENT;
  117. }
  118. if (hi2c->i2c_state == I2C_STATE_RESET) {
  119. hi2c->lock = 0;
  120. }
  121. hi2c->i2c_state = I2C_STATE_BUSY;
  122. clk_enable(hi2c->clock);
  123. /* Disable the selected I2C peripheral */
  124. mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_PE);
  125. /* Configure I2Cx: Frequency range */
  126. mmio_write_32(hi2c->i2c_base_addr + I2C_TIMINGR,
  127. timing & TIMINGR_CLEAR_MASK);
  128. /* Disable Own Address1 before set the Own Address1 configuration */
  129. mmio_clrbits_32(hi2c->i2c_base_addr + I2C_OAR1, I2C_OAR1_OA1EN);
  130. /* Configure I2Cx: Own Address1 and ack own address1 mode */
  131. if (init_data->addressing_mode == I2C_ADDRESSINGMODE_7BIT) {
  132. mmio_write_32(hi2c->i2c_base_addr + I2C_OAR1,
  133. I2C_OAR1_OA1EN | init_data->own_address1);
  134. } else { /* I2C_ADDRESSINGMODE_10BIT */
  135. mmio_write_32(hi2c->i2c_base_addr + I2C_OAR1,
  136. I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE |
  137. init_data->own_address1);
  138. }
  139. mmio_write_32(hi2c->i2c_base_addr + I2C_CR2, 0);
  140. /* Configure I2Cx: Addressing Master mode */
  141. if (init_data->addressing_mode == I2C_ADDRESSINGMODE_10BIT) {
  142. mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR2, I2C_CR2_ADD10);
  143. }
  144. /*
  145. * Enable the AUTOEND by default, and enable NACK
  146. * (should be disabled only during Slave process).
  147. */
  148. mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR2,
  149. I2C_CR2_AUTOEND | I2C_CR2_NACK);
  150. /* Disable Own Address2 before set the Own Address2 configuration */
  151. mmio_clrbits_32(hi2c->i2c_base_addr + I2C_OAR2, I2C_DUALADDRESS_ENABLE);
  152. /* Configure I2Cx: Dual mode and Own Address2 */
  153. mmio_write_32(hi2c->i2c_base_addr + I2C_OAR2,
  154. init_data->dual_address_mode |
  155. init_data->own_address2 |
  156. (init_data->own_address2_masks << 8));
  157. /* Configure I2Cx: Generalcall and NoStretch mode */
  158. mmio_write_32(hi2c->i2c_base_addr + I2C_CR1,
  159. init_data->general_call_mode |
  160. init_data->no_stretch_mode);
  161. /* Enable the selected I2C peripheral */
  162. mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_PE);
  163. hi2c->i2c_err = I2C_ERROR_NONE;
  164. hi2c->i2c_state = I2C_STATE_READY;
  165. hi2c->i2c_mode = I2C_MODE_NONE;
  166. rc = i2c_config_analog_filter(hi2c, init_data->analog_filter ?
  167. I2C_ANALOGFILTER_ENABLE :
  168. I2C_ANALOGFILTER_DISABLE);
  169. if (rc != 0) {
  170. ERROR("Cannot initialize I2C analog filter (%d)\n", rc);
  171. clk_disable(hi2c->clock);
  172. return rc;
  173. }
  174. clk_disable(hi2c->clock);
  175. return rc;
  176. }
  177. /*
  178. * @brief I2C Tx data register flush process.
  179. * @param hi2c: I2C handle
  180. * @retval None
  181. */
  182. static void i2c_flush_txdr(struct i2c_handle_s *hi2c)
  183. {
  184. /*
  185. * If a pending TXIS flag is set,
  186. * write a dummy data in TXDR to clear it.
  187. */
  188. if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) & I2C_FLAG_TXIS) !=
  189. 0U) {
  190. mmio_write_32(hi2c->i2c_base_addr + I2C_TXDR, 0);
  191. }
  192. /* Flush TX register if not empty */
  193. if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) & I2C_FLAG_TXE) ==
  194. 0U) {
  195. mmio_setbits_32(hi2c->i2c_base_addr + I2C_ISR,
  196. I2C_FLAG_TXE);
  197. }
  198. }
  199. /*
  200. * @brief This function handles I2C Communication timeout.
  201. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  202. * the configuration information for the specified I2C.
  203. * @param flag: Specifies the I2C flag to check
  204. * @param awaited_value: The awaited bit value for the flag (0 or 1)
  205. * @param timeout_ref: Reference to target timeout
  206. * @retval 0 if OK, negative value else
  207. */
  208. static int i2c_wait_flag(struct i2c_handle_s *hi2c, uint32_t flag,
  209. uint8_t awaited_value, uint64_t timeout_ref)
  210. {
  211. for ( ; ; ) {
  212. uint32_t isr = mmio_read_32(hi2c->i2c_base_addr + I2C_ISR);
  213. if (!!(isr & flag) != !!awaited_value) {
  214. return 0;
  215. }
  216. if (timeout_elapsed(timeout_ref)) {
  217. notif_i2c_timeout(hi2c);
  218. hi2c->lock = 0;
  219. return -EIO;
  220. }
  221. }
  222. }
  223. /*
  224. * @brief This function handles Acknowledge failed detection during
  225. * an I2C Communication.
  226. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  227. * the configuration information for the specified I2C.
  228. * @param timeout_ref: Reference to target timeout
  229. * @retval 0 if OK, negative value else
  230. */
  231. static int i2c_ack_failed(struct i2c_handle_s *hi2c, uint64_t timeout_ref)
  232. {
  233. if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) & I2C_FLAG_AF) == 0U) {
  234. return 0;
  235. }
  236. /*
  237. * Wait until STOP Flag is reset.
  238. * AutoEnd should be initiate after AF.
  239. */
  240. while ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
  241. I2C_FLAG_STOPF) == 0U) {
  242. if (timeout_elapsed(timeout_ref)) {
  243. notif_i2c_timeout(hi2c);
  244. hi2c->lock = 0;
  245. return -EIO;
  246. }
  247. }
  248. mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_AF);
  249. mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_STOPF);
  250. i2c_flush_txdr(hi2c);
  251. mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR2, I2C_RESET_CR2);
  252. hi2c->i2c_err |= I2C_ERROR_AF;
  253. hi2c->i2c_state = I2C_STATE_READY;
  254. hi2c->i2c_mode = I2C_MODE_NONE;
  255. hi2c->lock = 0;
  256. return -EIO;
  257. }
  258. /*
  259. * @brief This function handles I2C Communication timeout for specific usage
  260. * of TXIS flag.
  261. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  262. * the configuration information for the specified I2C.
  263. * @param timeout_ref: Reference to target timeout
  264. * @retval 0 if OK, negative value else
  265. */
  266. static int i2c_wait_txis(struct i2c_handle_s *hi2c, uint64_t timeout_ref)
  267. {
  268. while ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
  269. I2C_FLAG_TXIS) == 0U) {
  270. if (i2c_ack_failed(hi2c, timeout_ref) != 0) {
  271. return -EIO;
  272. }
  273. if (timeout_elapsed(timeout_ref)) {
  274. notif_i2c_timeout(hi2c);
  275. hi2c->lock = 0;
  276. return -EIO;
  277. }
  278. }
  279. return 0;
  280. }
  281. /*
  282. * @brief This function handles I2C Communication timeout for specific
  283. * usage of STOP flag.
  284. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  285. * the configuration information for the specified I2C.
  286. * @param timeout_ref: Reference to target timeout
  287. * @retval 0 if OK, negative value else
  288. */
  289. static int i2c_wait_stop(struct i2c_handle_s *hi2c, uint64_t timeout_ref)
  290. {
  291. while ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
  292. I2C_FLAG_STOPF) == 0U) {
  293. if (i2c_ack_failed(hi2c, timeout_ref) != 0) {
  294. return -EIO;
  295. }
  296. if (timeout_elapsed(timeout_ref)) {
  297. notif_i2c_timeout(hi2c);
  298. hi2c->lock = 0;
  299. return -EIO;
  300. }
  301. }
  302. return 0;
  303. }
  304. /*
  305. * @brief Handles I2Cx communication when starting transfer or during transfer
  306. * (TC or TCR flag are set).
  307. * @param hi2c: I2C handle
  308. * @param dev_addr: Specifies the slave address to be programmed
  309. * @param size: Specifies the number of bytes to be programmed.
  310. * This parameter must be a value between 0 and 255.
  311. * @param i2c_mode: New state of the I2C START condition generation.
  312. * This parameter can be one of the following values:
  313. * @arg @ref I2C_RELOAD_MODE: Enable Reload mode.
  314. * @arg @ref I2C_AUTOEND_MODE: Enable Automatic end mode.
  315. * @arg @ref I2C_SOFTEND_MODE: Enable Software end mode.
  316. * @param request: New state of the I2C START condition generation.
  317. * This parameter can be one of the following values:
  318. * @arg @ref I2C_NO_STARTSTOP: Don't Generate stop and start condition.
  319. * @arg @ref I2C_GENERATE_STOP: Generate stop condition
  320. * (size should be set to 0).
  321. * @arg @ref I2C_GENERATE_START_READ: Generate Restart for read request.
  322. * @arg @ref I2C_GENERATE_START_WRITE: Generate Restart for write request.
  323. * @retval None
  324. */
  325. static void i2c_transfer_config(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  326. uint16_t size, uint32_t i2c_mode,
  327. uint32_t request)
  328. {
  329. uint32_t clr_value, set_value;
  330. clr_value = (I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD |
  331. I2C_CR2_AUTOEND | I2C_CR2_START | I2C_CR2_STOP) |
  332. (I2C_CR2_RD_WRN & (request >> (31U - I2C_CR2_RD_WRN_OFFSET)));
  333. set_value = ((uint32_t)dev_addr & I2C_CR2_SADD) |
  334. (((uint32_t)size << I2C_CR2_NBYTES_OFFSET) & I2C_CR2_NBYTES) |
  335. i2c_mode | request;
  336. mmio_clrsetbits_32(hi2c->i2c_base_addr + I2C_CR2, clr_value, set_value);
  337. }
  338. /*
  339. * @brief Master sends target device address followed by internal memory
  340. * address for write request.
  341. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  342. * the configuration information for the specified I2C.
  343. * @param dev_addr: Target device address
  344. * @param mem_addr: Internal memory address
  345. * @param mem_add_size: Size of internal memory address
  346. * @param timeout_ref: Reference to target timeout
  347. * @retval 0 if OK, negative value else
  348. */
  349. static int i2c_request_memory_write(struct i2c_handle_s *hi2c,
  350. uint16_t dev_addr, uint16_t mem_addr,
  351. uint16_t mem_add_size, uint64_t timeout_ref)
  352. {
  353. i2c_transfer_config(hi2c, dev_addr, mem_add_size, I2C_RELOAD_MODE,
  354. I2C_GENERATE_START_WRITE);
  355. if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
  356. return -EIO;
  357. }
  358. if (mem_add_size == I2C_MEMADD_SIZE_8BIT) {
  359. /* Send Memory Address */
  360. mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
  361. (uint8_t)(mem_addr & 0x00FFU));
  362. } else {
  363. /* Send MSB of Memory Address */
  364. mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
  365. (uint8_t)((mem_addr & 0xFF00U) >> 8));
  366. if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
  367. return -EIO;
  368. }
  369. /* Send LSB of Memory Address */
  370. mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
  371. (uint8_t)(mem_addr & 0x00FFU));
  372. }
  373. if (i2c_wait_flag(hi2c, I2C_FLAG_TCR, 0, timeout_ref) != 0) {
  374. return -EIO;
  375. }
  376. return 0;
  377. }
  378. /*
  379. * @brief Master sends target device address followed by internal memory
  380. * address for read request.
  381. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  382. * the configuration information for the specified I2C.
  383. * @param dev_addr: Target device address
  384. * @param mem_addr: Internal memory address
  385. * @param mem_add_size: Size of internal memory address
  386. * @param timeout_ref: Reference to target timeout
  387. * @retval 0 if OK, negative value else
  388. */
  389. static int i2c_request_memory_read(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  390. uint16_t mem_addr, uint16_t mem_add_size,
  391. uint64_t timeout_ref)
  392. {
  393. i2c_transfer_config(hi2c, dev_addr, mem_add_size, I2C_SOFTEND_MODE,
  394. I2C_GENERATE_START_WRITE);
  395. if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
  396. return -EIO;
  397. }
  398. if (mem_add_size == I2C_MEMADD_SIZE_8BIT) {
  399. /* Send Memory Address */
  400. mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
  401. (uint8_t)(mem_addr & 0x00FFU));
  402. } else {
  403. /* Send MSB of Memory Address */
  404. mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
  405. (uint8_t)((mem_addr & 0xFF00U) >> 8));
  406. if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
  407. return -EIO;
  408. }
  409. /* Send LSB of Memory Address */
  410. mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
  411. (uint8_t)(mem_addr & 0x00FFU));
  412. }
  413. if (i2c_wait_flag(hi2c, I2C_FLAG_TC, 0, timeout_ref) != 0) {
  414. return -EIO;
  415. }
  416. return 0;
  417. }
  418. /*
  419. * @brief Generic function to write an amount of data in blocking mode
  420. * (for Memory Mode and Master Mode)
  421. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  422. * the configuration information for the specified I2C.
  423. * @param dev_addr: Target device address
  424. * @param mem_addr: Internal memory address (if Memory Mode)
  425. * @param mem_add_size: Size of internal memory address (if Memory Mode)
  426. * @param p_data: Pointer to data buffer
  427. * @param size: Amount of data to be sent
  428. * @param timeout_ms: Timeout duration in milliseconds
  429. * @param mode: Communication mode
  430. * @retval 0 if OK, negative value else
  431. */
  432. static int i2c_write(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  433. uint16_t mem_addr, uint16_t mem_add_size,
  434. uint8_t *p_data, uint16_t size, uint32_t timeout_ms,
  435. enum i2c_mode_e mode)
  436. {
  437. uint64_t timeout_ref;
  438. int rc = -EIO;
  439. uint8_t *p_buff = p_data;
  440. uint32_t xfer_size;
  441. uint32_t xfer_count = size;
  442. if ((mode != I2C_MODE_MASTER) && (mode != I2C_MODE_MEM)) {
  443. return -1;
  444. }
  445. if ((hi2c->i2c_state != I2C_STATE_READY) || (hi2c->lock != 0U)) {
  446. return -EBUSY;
  447. }
  448. if ((p_data == NULL) || (size == 0U)) {
  449. return -EINVAL;
  450. }
  451. clk_enable(hi2c->clock);
  452. hi2c->lock = 1;
  453. timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_MS * 1000);
  454. if (i2c_wait_flag(hi2c, I2C_FLAG_BUSY, 1, timeout_ref) != 0) {
  455. goto bail;
  456. }
  457. hi2c->i2c_state = I2C_STATE_BUSY_TX;
  458. hi2c->i2c_mode = mode;
  459. hi2c->i2c_err = I2C_ERROR_NONE;
  460. timeout_ref = timeout_init_us(timeout_ms * 1000);
  461. if (mode == I2C_MODE_MEM) {
  462. /* In Memory Mode, Send Slave Address and Memory Address */
  463. if (i2c_request_memory_write(hi2c, dev_addr, mem_addr,
  464. mem_add_size, timeout_ref) != 0) {
  465. goto bail;
  466. }
  467. if (xfer_count > MAX_NBYTE_SIZE) {
  468. xfer_size = MAX_NBYTE_SIZE;
  469. i2c_transfer_config(hi2c, dev_addr, xfer_size,
  470. I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  471. } else {
  472. xfer_size = xfer_count;
  473. i2c_transfer_config(hi2c, dev_addr, xfer_size,
  474. I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  475. }
  476. } else {
  477. /* In Master Mode, Send Slave Address */
  478. if (xfer_count > MAX_NBYTE_SIZE) {
  479. xfer_size = MAX_NBYTE_SIZE;
  480. i2c_transfer_config(hi2c, dev_addr, xfer_size,
  481. I2C_RELOAD_MODE,
  482. I2C_GENERATE_START_WRITE);
  483. } else {
  484. xfer_size = xfer_count;
  485. i2c_transfer_config(hi2c, dev_addr, xfer_size,
  486. I2C_AUTOEND_MODE,
  487. I2C_GENERATE_START_WRITE);
  488. }
  489. }
  490. do {
  491. if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
  492. goto bail;
  493. }
  494. mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR, *p_buff);
  495. p_buff++;
  496. xfer_count--;
  497. xfer_size--;
  498. if ((xfer_count != 0U) && (xfer_size == 0U)) {
  499. /* Wait until TCR flag is set */
  500. if (i2c_wait_flag(hi2c, I2C_FLAG_TCR, 0,
  501. timeout_ref) != 0) {
  502. goto bail;
  503. }
  504. if (xfer_count > MAX_NBYTE_SIZE) {
  505. xfer_size = MAX_NBYTE_SIZE;
  506. i2c_transfer_config(hi2c, dev_addr,
  507. xfer_size,
  508. I2C_RELOAD_MODE,
  509. I2C_NO_STARTSTOP);
  510. } else {
  511. xfer_size = xfer_count;
  512. i2c_transfer_config(hi2c, dev_addr,
  513. xfer_size,
  514. I2C_AUTOEND_MODE,
  515. I2C_NO_STARTSTOP);
  516. }
  517. }
  518. } while (xfer_count > 0U);
  519. /*
  520. * No need to Check TC flag, with AUTOEND mode the stop
  521. * is automatically generated.
  522. * Wait until STOPF flag is reset.
  523. */
  524. if (i2c_wait_stop(hi2c, timeout_ref) != 0) {
  525. goto bail;
  526. }
  527. mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_STOPF);
  528. mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR2, I2C_RESET_CR2);
  529. hi2c->i2c_state = I2C_STATE_READY;
  530. hi2c->i2c_mode = I2C_MODE_NONE;
  531. rc = 0;
  532. bail:
  533. hi2c->lock = 0;
  534. clk_disable(hi2c->clock);
  535. return rc;
  536. }
  537. /*
  538. * @brief Write an amount of data in blocking mode to a specific memory
  539. * address.
  540. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  541. * the configuration information for the specified I2C.
  542. * @param dev_addr: Target device address
  543. * @param mem_addr: Internal memory address
  544. * @param mem_add_size: Size of internal memory address
  545. * @param p_data: Pointer to data buffer
  546. * @param size: Amount of data to be sent
  547. * @param timeout_ms: Timeout duration in milliseconds
  548. * @retval 0 if OK, negative value else
  549. */
  550. int stm32_i2c_mem_write(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  551. uint16_t mem_addr, uint16_t mem_add_size,
  552. uint8_t *p_data, uint16_t size, uint32_t timeout_ms)
  553. {
  554. return i2c_write(hi2c, dev_addr, mem_addr, mem_add_size,
  555. p_data, size, timeout_ms, I2C_MODE_MEM);
  556. }
  557. /*
  558. * @brief Transmits in master mode an amount of data in blocking mode.
  559. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  560. * the configuration information for the specified I2C.
  561. * @param dev_addr: Target device address
  562. * @param p_data: Pointer to data buffer
  563. * @param size: Amount of data to be sent
  564. * @param timeout_ms: Timeout duration in milliseconds
  565. * @retval 0 if OK, negative value else
  566. */
  567. int stm32_i2c_master_transmit(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  568. uint8_t *p_data, uint16_t size,
  569. uint32_t timeout_ms)
  570. {
  571. return i2c_write(hi2c, dev_addr, 0, 0,
  572. p_data, size, timeout_ms, I2C_MODE_MASTER);
  573. }
  574. /*
  575. * @brief Generic function to read an amount of data in blocking mode
  576. * (for Memory Mode and Master Mode)
  577. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  578. * the configuration information for the specified I2C.
  579. * @param dev_addr: Target device address
  580. * @param mem_addr: Internal memory address (if Memory Mode)
  581. * @param mem_add_size: Size of internal memory address (if Memory Mode)
  582. * @param p_data: Pointer to data buffer
  583. * @param size: Amount of data to be sent
  584. * @param timeout_ms: Timeout duration in milliseconds
  585. * @param mode: Communication mode
  586. * @retval 0 if OK, negative value else
  587. */
  588. static int i2c_read(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  589. uint16_t mem_addr, uint16_t mem_add_size,
  590. uint8_t *p_data, uint16_t size, uint32_t timeout_ms,
  591. enum i2c_mode_e mode)
  592. {
  593. uint64_t timeout_ref;
  594. int rc = -EIO;
  595. uint8_t *p_buff = p_data;
  596. uint32_t xfer_count = size;
  597. uint32_t xfer_size;
  598. if ((mode != I2C_MODE_MASTER) && (mode != I2C_MODE_MEM)) {
  599. return -1;
  600. }
  601. if ((hi2c->i2c_state != I2C_STATE_READY) || (hi2c->lock != 0U)) {
  602. return -EBUSY;
  603. }
  604. if ((p_data == NULL) || (size == 0U)) {
  605. return -EINVAL;
  606. }
  607. clk_enable(hi2c->clock);
  608. hi2c->lock = 1;
  609. timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_MS * 1000);
  610. if (i2c_wait_flag(hi2c, I2C_FLAG_BUSY, 1, timeout_ref) != 0) {
  611. goto bail;
  612. }
  613. hi2c->i2c_state = I2C_STATE_BUSY_RX;
  614. hi2c->i2c_mode = mode;
  615. hi2c->i2c_err = I2C_ERROR_NONE;
  616. if (mode == I2C_MODE_MEM) {
  617. /* Send Memory Address */
  618. if (i2c_request_memory_read(hi2c, dev_addr, mem_addr,
  619. mem_add_size, timeout_ref) != 0) {
  620. goto bail;
  621. }
  622. }
  623. /*
  624. * Send Slave Address.
  625. * Set NBYTES to write and reload if xfer_count > MAX_NBYTE_SIZE
  626. * and generate RESTART.
  627. */
  628. if (xfer_count > MAX_NBYTE_SIZE) {
  629. xfer_size = MAX_NBYTE_SIZE;
  630. i2c_transfer_config(hi2c, dev_addr, xfer_size,
  631. I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  632. } else {
  633. xfer_size = xfer_count;
  634. i2c_transfer_config(hi2c, dev_addr, xfer_size,
  635. I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  636. }
  637. do {
  638. if (i2c_wait_flag(hi2c, I2C_FLAG_RXNE, 0, timeout_ref) != 0) {
  639. goto bail;
  640. }
  641. *p_buff = mmio_read_8(hi2c->i2c_base_addr + I2C_RXDR);
  642. p_buff++;
  643. xfer_size--;
  644. xfer_count--;
  645. if ((xfer_count != 0U) && (xfer_size == 0U)) {
  646. if (i2c_wait_flag(hi2c, I2C_FLAG_TCR, 0,
  647. timeout_ref) != 0) {
  648. goto bail;
  649. }
  650. if (xfer_count > MAX_NBYTE_SIZE) {
  651. xfer_size = MAX_NBYTE_SIZE;
  652. i2c_transfer_config(hi2c, dev_addr,
  653. xfer_size,
  654. I2C_RELOAD_MODE,
  655. I2C_NO_STARTSTOP);
  656. } else {
  657. xfer_size = xfer_count;
  658. i2c_transfer_config(hi2c, dev_addr,
  659. xfer_size,
  660. I2C_AUTOEND_MODE,
  661. I2C_NO_STARTSTOP);
  662. }
  663. }
  664. } while (xfer_count > 0U);
  665. /*
  666. * No need to Check TC flag, with AUTOEND mode the stop
  667. * is automatically generated.
  668. * Wait until STOPF flag is reset.
  669. */
  670. if (i2c_wait_stop(hi2c, timeout_ref) != 0) {
  671. goto bail;
  672. }
  673. mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_STOPF);
  674. mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR2, I2C_RESET_CR2);
  675. hi2c->i2c_state = I2C_STATE_READY;
  676. hi2c->i2c_mode = I2C_MODE_NONE;
  677. rc = 0;
  678. bail:
  679. hi2c->lock = 0;
  680. clk_disable(hi2c->clock);
  681. return rc;
  682. }
  683. /*
  684. * @brief Read an amount of data in blocking mode from a specific memory
  685. * address.
  686. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  687. * the configuration information for the specified I2C.
  688. * @param dev_addr: Target device address
  689. * @param mem_addr: Internal memory address
  690. * @param mem_add_size: Size of internal memory address
  691. * @param p_data: Pointer to data buffer
  692. * @param size: Amount of data to be sent
  693. * @param timeout_ms: Timeout duration in milliseconds
  694. * @retval 0 if OK, negative value else
  695. */
  696. int stm32_i2c_mem_read(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  697. uint16_t mem_addr, uint16_t mem_add_size,
  698. uint8_t *p_data, uint16_t size, uint32_t timeout_ms)
  699. {
  700. return i2c_read(hi2c, dev_addr, mem_addr, mem_add_size,
  701. p_data, size, timeout_ms, I2C_MODE_MEM);
  702. }
  703. /*
  704. * @brief Receives in master mode an amount of data in blocking mode.
  705. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  706. * the configuration information for the specified I2C.
  707. * @param dev_addr: Target device address
  708. * @param p_data: Pointer to data buffer
  709. * @param size: Amount of data to be sent
  710. * @param timeout_ms: Timeout duration in milliseconds
  711. * @retval 0 if OK, negative value else
  712. */
  713. int stm32_i2c_master_receive(struct i2c_handle_s *hi2c, uint16_t dev_addr,
  714. uint8_t *p_data, uint16_t size,
  715. uint32_t timeout_ms)
  716. {
  717. return i2c_read(hi2c, dev_addr, 0, 0,
  718. p_data, size, timeout_ms, I2C_MODE_MASTER);
  719. }
  720. /*
  721. * @brief Checks if target device is ready for communication.
  722. * @note This function is used with Memory devices
  723. * @param hi2c: Pointer to a struct i2c_handle_s structure that contains
  724. * the configuration information for the specified I2C.
  725. * @param dev_addr: Target device address
  726. * @param trials: Number of trials
  727. * @param timeout_ms: Timeout duration in milliseconds
  728. * @retval True if device is ready, false else
  729. */
  730. bool stm32_i2c_is_device_ready(struct i2c_handle_s *hi2c,
  731. uint16_t dev_addr, uint32_t trials,
  732. uint32_t timeout_ms)
  733. {
  734. uint32_t i2c_trials = 0U;
  735. bool rc = false;
  736. if ((hi2c->i2c_state != I2C_STATE_READY) || (hi2c->lock != 0U)) {
  737. return rc;
  738. }
  739. clk_enable(hi2c->clock);
  740. hi2c->lock = 1;
  741. hi2c->i2c_mode = I2C_MODE_NONE;
  742. if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) & I2C_FLAG_BUSY) !=
  743. 0U) {
  744. goto bail;
  745. }
  746. hi2c->i2c_state = I2C_STATE_BUSY;
  747. hi2c->i2c_err = I2C_ERROR_NONE;
  748. do {
  749. uint64_t timeout_ref;
  750. /* Generate Start */
  751. if ((mmio_read_32(hi2c->i2c_base_addr + I2C_OAR1) &
  752. I2C_OAR1_OA1MODE) == 0) {
  753. mmio_write_32(hi2c->i2c_base_addr + I2C_CR2,
  754. (((uint32_t)dev_addr & I2C_CR2_SADD) |
  755. I2C_CR2_START | I2C_CR2_AUTOEND) &
  756. ~I2C_CR2_RD_WRN);
  757. } else {
  758. mmio_write_32(hi2c->i2c_base_addr + I2C_CR2,
  759. (((uint32_t)dev_addr & I2C_CR2_SADD) |
  760. I2C_CR2_START | I2C_CR2_ADD10) &
  761. ~I2C_CR2_RD_WRN);
  762. }
  763. /*
  764. * No need to Check TC flag, with AUTOEND mode the stop
  765. * is automatically generated.
  766. * Wait until STOPF flag is set or a NACK flag is set.
  767. */
  768. timeout_ref = timeout_init_us(timeout_ms * 1000);
  769. do {
  770. if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
  771. (I2C_FLAG_STOPF | I2C_FLAG_AF)) != 0U) {
  772. break;
  773. }
  774. if (timeout_elapsed(timeout_ref)) {
  775. notif_i2c_timeout(hi2c);
  776. goto bail;
  777. }
  778. } while (true);
  779. if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
  780. I2C_FLAG_AF) == 0U) {
  781. if (i2c_wait_flag(hi2c, I2C_FLAG_STOPF, 0,
  782. timeout_ref) != 0) {
  783. goto bail;
  784. }
  785. mmio_write_32(hi2c->i2c_base_addr + I2C_ICR,
  786. I2C_FLAG_STOPF);
  787. hi2c->i2c_state = I2C_STATE_READY;
  788. rc = true;
  789. goto bail;
  790. }
  791. if (i2c_wait_flag(hi2c, I2C_FLAG_STOPF, 0, timeout_ref) != 0) {
  792. goto bail;
  793. }
  794. mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_AF);
  795. mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_STOPF);
  796. if (i2c_trials == trials) {
  797. mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR2,
  798. I2C_CR2_STOP);
  799. if (i2c_wait_flag(hi2c, I2C_FLAG_STOPF, 0,
  800. timeout_ref) != 0) {
  801. goto bail;
  802. }
  803. mmio_write_32(hi2c->i2c_base_addr + I2C_ICR,
  804. I2C_FLAG_STOPF);
  805. }
  806. i2c_trials++;
  807. } while (i2c_trials < trials);
  808. notif_i2c_timeout(hi2c);
  809. bail:
  810. hi2c->lock = 0;
  811. clk_disable(hi2c->clock);
  812. return rc;
  813. }