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