2 **************************************************************************
3 * @file i2c_application.c
6 * @brief the driver library of the i2c peripheral
7 **************************************************************************
8 * Copyright notice & Disclaimer
10 * The software Board Support Package (BSP) that is made available to
11 * download from Artery official website is the copyrighted work of Artery.
12 * Artery authorizes customers to use, copy, and distribute the BSP
13 * software and its related documentation for the purpose of design and
14 * development in conjunction with Artery microcontrollers. Use of the
15 * software is governed by this copyright notice and the following disclaimer.
17 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
18 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
19 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
20 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
21 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 **************************************************************************
27 #include "i2c_application.h"
29 /** @addtogroup AT32F435_437_middlewares_i2c_application_library
34 * @brief get the dma transfer direction flag through the channel
36 #define DMA_GET_REQUEST(DMA_CHANNEL) \
37 (((uint32_t)(DMA_CHANNEL) == ((uint32_t)hi2c->dma_tx_channel)) ? I2C_DMA_REQUEST_TX : I2C_DMA_REQUEST_RX)
40 * @brief get the dma transfer complete flag through the channel
42 #define DMA_GET_TC_FLAG(DMA_CHANNEL) \
43 (((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL1))? DMA1_FDT1_FLAG : \
44 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL2))? DMA1_FDT2_FLAG : \
45 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL3))? DMA1_FDT3_FLAG : \
46 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL4))? DMA1_FDT4_FLAG : \
47 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL5))? DMA1_FDT5_FLAG : \
48 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL6))? DMA1_FDT6_FLAG : \
49 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL7))? DMA1_FDT7_FLAG : \
50 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL1))? DMA2_FDT1_FLAG : \
51 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL2))? DMA2_FDT2_FLAG : \
52 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL3))? DMA2_FDT3_FLAG : \
53 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL4))? DMA2_FDT4_FLAG : \
54 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL5))? DMA2_FDT5_FLAG : \
55 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL6))? DMA2_FDT6_FLAG : \
59 * @brief get the dma half transfer flag through the channel
61 #define DMA_GET_HT_FLAG(DMA_CHANNEL) \
62 (((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL1))? DMA1_HDT1_FLAG : \
63 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL2))? DMA1_HDT2_FLAG : \
64 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL3))? DMA1_HDT3_FLAG : \
65 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL4))? DMA1_HDT4_FLAG : \
66 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL5))? DMA1_HDT5_FLAG : \
67 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL6))? DMA1_HDT6_FLAG : \
68 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL7))? DMA1_HDT7_FLAG : \
69 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL1))? DMA2_HDT1_FLAG : \
70 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL2))? DMA2_HDT2_FLAG : \
71 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL3))? DMA2_HDT3_FLAG : \
72 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL4))? DMA2_HDT4_FLAG : \
73 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL5))? DMA2_HDT5_FLAG : \
74 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL6))? DMA2_HDT6_FLAG : \
78 * @brief get the dma transfer error flag through the channel
80 #define DMA_GET_TERR_FLAG(DMA_CHANNEL) \
81 (((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL1))? DMA1_DTERR1_FLAG : \
82 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL2))? DMA1_DTERR2_FLAG : \
83 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL3))? DMA1_DTERR3_FLAG : \
84 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL4))? DMA1_DTERR4_FLAG : \
85 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL5))? DMA1_DTERR5_FLAG : \
86 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL6))? DMA1_DTERR6_FLAG : \
87 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL7))? DMA1_DTERR7_FLAG : \
88 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL1))? DMA2_DTERR1_FLAG : \
89 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL2))? DMA2_DTERR2_FLAG : \
90 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL3))? DMA2_DTERR3_FLAG : \
91 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL4))? DMA2_DTERR4_FLAG : \
92 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL5))? DMA2_DTERR5_FLAG : \
93 ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL6))? DMA2_DTERR6_FLAG : \
97 * @brief i2c transmission status
104 * @brief i2c peripheral initialization.
105 * @param hi2c: the handle points to the operation information.
108 void i2c_config(i2c_handle_type
* hi2c
)
110 /* reset i2c peripheral */
111 i2c_reset(hi2c
->i2cx
);
113 /* i2c peripheral initialization */
114 i2c_lowlevel_init(hi2c
);
116 /* i2c peripheral enable */
117 i2c_enable(hi2c
->i2cx
, TRUE
);
121 * @brief refresh i2c register.
122 * @param hi2c: the handle points to the operation information.
125 void i2c_refresh_txdt_register(i2c_handle_type
* hi2c
)
127 /* clear tdis flag */
128 if (i2c_flag_get(hi2c
->i2cx
, I2C_TDIS_FLAG
) != RESET
)
130 hi2c
->i2cx
->txdt
= 0x00;
133 /* refresh txdt register*/
134 if (i2c_flag_get(hi2c
->i2cx
, I2C_TDBE_FLAG
) == RESET
)
136 hi2c
->i2cx
->sts_bit
.tdbe
= 1;
141 * @brief reset ctrl2 register.
142 * @param hi2c: the handle points to the operation information.
145 void i2c_reset_ctrl2_register(i2c_handle_type
* hi2c
)
147 hi2c
->i2cx
->ctrl2_bit
.saddr
= 0;
148 hi2c
->i2cx
->ctrl2_bit
.readh10
= 0;
149 hi2c
->i2cx
->ctrl2_bit
.cnt
= 0;
150 hi2c
->i2cx
->ctrl2_bit
.rlden
= 0;
151 hi2c
->i2cx
->ctrl2_bit
.dir
= 0;
155 * @brief wait for the transfer to end.
156 * @param hi2c: the handle points to the operation information.
157 * @param timeout: maximum waiting time.
158 * @retval i2c status.
160 i2c_status_type
i2c_wait_end(i2c_handle_type
* hi2c
, uint32_t timeout
)
162 while(hi2c
->status
!= I2C_END
)
167 return I2C_ERR_TIMEOUT
;
171 if(hi2c
->error_code
!= I2C_OK
)
173 return hi2c
->error_code
;
180 * @brief wait for the flag to be set or reset, only BUSYF flag
181 * is waiting to be reset, and other flags are waiting to be set
182 * @param hi2c: the handle points to the operation information.
183 * @param flag: specifies the flag to check.
184 * this parameter can be one of the following values:
185 * - I2C_TDBE_FLAG: transmit data buffer empty flag.
186 * - I2C_TDIS_FLAG: send interrupt status.
187 * - I2C_RDBF_FLAG: receive data buffer full flag.
188 * - I2C_ADDRF_FLAG: 0~7 bit address match flag.
189 * - I2C_ACKFAIL_FLAG: acknowledge failure flag.
190 * - I2C_STOPF_FLAG: stop condition generation complete flag.
191 * - I2C_TDC_FLAG: transmit data complete flag.
192 * - I2C_TCRLD_FLAG: transmission is complete, waiting to load data.
193 * - I2C_BUSERR_FLAG: bus error flag.
194 * - I2C_ARLOST_FLAG: arbitration lost flag.
195 * - I2C_OUF_FLAG: overflow or underflow flag.
196 * - I2C_PECERR_FLAG: pec receive error flag.
197 * - I2C_TMOUT_FLAG: smbus timeout flag.
198 * - I2C_ALERTF_FLAG: smbus alert flag.
199 * - I2C_BUSYF_FLAG: bus busy flag transmission mode.
200 * - I2C_SDIR_FLAG: slave data transmit direction.
201 * @param event_check: check other error flags while waiting for the flag.
202 * parameter as following values:
203 * - I2C_EVENT_CHECK_NONE
204 * - I2C_EVENT_CHECK_ACKFAIL
205 * - I2C_EVENT_CHECK_STOP
206 * @param timeout: maximum waiting time.
207 * @retval i2c status.
209 i2c_status_type
i2c_wait_flag(i2c_handle_type
* hi2c
, uint32_t flag
, uint32_t event_check
, uint32_t timeout
)
211 if(flag
== I2C_BUSYF_FLAG
)
213 while(i2c_flag_get(hi2c
->i2cx
, flag
) != RESET
)
218 hi2c
->error_code
= I2C_ERR_TIMEOUT
;
220 return I2C_ERR_TIMEOUT
;
226 while(i2c_flag_get(hi2c
->i2cx
, flag
) == RESET
)
228 /* check the ack fail flag */
229 if(event_check
& I2C_EVENT_CHECK_ACKFAIL
)
231 if(i2c_flag_get(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
) != RESET
)
233 /* clear ack fail flag */
234 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
236 hi2c
->error_code
= I2C_ERR_ACKFAIL
;
238 return I2C_ERR_ACKFAIL
;
242 /* check the stop flag */
243 if(event_check
& I2C_EVENT_CHECK_STOP
)
245 if(i2c_flag_get(hi2c
->i2cx
, I2C_STOPF_FLAG
) != RESET
)
247 /* clear stop flag */
248 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
250 i2c_reset_ctrl2_register(hi2c
);
252 hi2c
->error_code
= I2C_ERR_STOP
;
261 hi2c
->error_code
= I2C_ERR_TIMEOUT
;
263 return I2C_ERR_TIMEOUT
;
272 * @brief dma transfer cofiguration.
273 * @param hi2c: the handle points to the operation information.
274 * @param dma_channelx: dma channel to be cofigured.
275 * @param pdata: data buffer.
276 * @param size: data size.
279 void i2c_dma_config(i2c_handle_type
* hi2c
, dma_channel_type
* dma_channel
, uint8_t* pdata
, uint16_t size
)
281 /* disable the dma channel */
282 dma_channel_enable(dma_channel
, FALSE
);
284 /* disable the transfer complete interrupt */
285 dma_interrupt_enable(dma_channel
, DMA_FDT_INT
, FALSE
);
287 /* configure the dma channel with the buffer address and the buffer size */
288 hi2c
->dma_init_struct
.memory_base_addr
= (uint32_t)pdata
;
289 hi2c
->dma_init_struct
.direction
= (dma_channel
== hi2c
->dma_tx_channel
) ? DMA_DIR_MEMORY_TO_PERIPHERAL
: DMA_DIR_PERIPHERAL_TO_MEMORY
;
290 hi2c
->dma_init_struct
.peripheral_base_addr
= (dma_channel
== hi2c
->dma_tx_channel
) ? (uint32_t)&hi2c
->i2cx
->txdt
: (uint32_t)&hi2c
->i2cx
->rxdt
;
291 hi2c
->dma_init_struct
.buffer_size
= (uint32_t)size
;
292 dma_init(dma_channel
, &hi2c
->dma_init_struct
);
294 /* enable the transfer complete interrupt */
295 dma_interrupt_enable(dma_channel
, DMA_FDT_INT
, TRUE
);
297 /* enable the dma channel */
298 dma_channel_enable(dma_channel
, TRUE
);
302 * @brief start transfer in poll mode or interrupt mode.
303 * @param hi2c: the handle points to the operation information.
304 * @param address: slave address.
305 * @param start: config gen start condition mode.
306 * parameter as following values:
307 * - I2C_WITHOUT_START: transfer data without start condition.
308 * - I2C_GEN_START_READ: read data and generate start.
309 * - I2C_GEN_START_WRITE: send data and generate start.
310 * @retval i2c status.
312 void i2c_start_transfer(i2c_handle_type
* hi2c
, uint16_t address
, i2c_start_mode_type start
)
314 if (hi2c
->pcount
> MAX_TRANSFER_CNT
)
316 hi2c
->psize
= MAX_TRANSFER_CNT
;
318 i2c_transmit_set(hi2c
->i2cx
, address
, hi2c
->psize
, I2C_RELOAD_MODE
, start
);
322 hi2c
->psize
= hi2c
->pcount
;
324 i2c_transmit_set(hi2c
->i2cx
, address
, hi2c
->psize
, I2C_AUTO_STOP_MODE
, start
);
329 * @brief start transfer in dma mode.
330 * @param hi2c: the handle points to the operation information.
331 * @param address: slave address.
332 * @param start: config gen start condition mode.
333 * parameter as following values:
334 * - I2C_WITHOUT_START: transfer data without start condition.
335 * - I2C_GEN_START_READ: read data and generate start.
336 * - I2C_GEN_START_WRITE: send data and generate start.
337 * @retval i2c status.
339 void i2c_start_transfer_dma(i2c_handle_type
* hi2c
, dma_channel_type
* dma_channelx
, uint16_t address
, i2c_start_mode_type start
)
341 if (hi2c
->pcount
> MAX_TRANSFER_CNT
)
343 hi2c
->psize
= MAX_TRANSFER_CNT
;
346 i2c_dma_config(hi2c
, dma_channelx
, hi2c
->pbuff
, hi2c
->psize
);
348 i2c_transmit_set(hi2c
->i2cx
, address
, hi2c
->psize
, I2C_RELOAD_MODE
, start
);
352 hi2c
->psize
= hi2c
->pcount
;
355 i2c_dma_config(hi2c
, dma_channelx
, hi2c
->pbuff
, hi2c
->psize
);
357 i2c_transmit_set(hi2c
->i2cx
, address
, hi2c
->psize
, I2C_AUTO_STOP_MODE
, start
);
362 * @brief the master transmits data through polling mode.
363 * @param hi2c: the handle points to the operation information.
364 * @param address: slave address.
365 * @param pdata: data buffer.
366 * @param size: data size.
367 * @param timeout: maximum waiting time.
368 * @retval i2c status.
370 i2c_status_type
i2c_master_transmit(i2c_handle_type
* hi2c
, uint16_t address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
372 /* initialization parameters */
376 hi2c
->error_code
= I2C_OK
;
378 /* wait for the busy flag to be reset */
379 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
381 return I2C_ERR_STEP_1
;
385 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_WRITE
);
387 while (hi2c
->pcount
> 0)
389 /* wait for the tdis flag to be set */
390 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
392 return I2C_ERR_STEP_2
;
396 i2c_data_send(hi2c
->i2cx
, *hi2c
->pbuff
++);
400 if ((hi2c
->psize
== 0) && (hi2c
->pcount
!= 0))
402 /* wait for the tcrld flag to be set */
403 if (i2c_wait_flag(hi2c
, I2C_TCRLD_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
405 return I2C_ERR_STEP_3
;
408 /* continue transfer */
409 i2c_start_transfer(hi2c
, address
, I2C_WITHOUT_START
);
413 /* wait for the stop flag to be set */
414 if(i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
416 return I2C_ERR_STEP_4
;
419 /* clear stop flag */
420 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
422 /* reset ctrl2 register */
423 i2c_reset_ctrl2_register(hi2c
);
429 * @brief the slave receive data through polling mode.
430 * @param hi2c: the handle points to the operation information.
431 * @param pdata: data buffer.
432 * @param size: data size.
433 * @param timeout: maximum waiting time.
434 * @retval i2c status.
436 i2c_status_type
i2c_slave_receive(i2c_handle_type
* hi2c
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
438 /* initialization parameters */
442 hi2c
->error_code
= I2C_OK
;
444 /* wait for the busy flag to be reset */
445 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
447 return I2C_ERR_STEP_1
;
450 /* enable acknowledge */
451 i2c_ack_enable(hi2c
->i2cx
, TRUE
);
453 /* wait for the addr flag to be set */
454 if (i2c_wait_flag(hi2c
, I2C_ADDRF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
456 return I2C_ERR_STEP_2
;
459 /* clear addr flag */
460 i2c_flag_clear(hi2c
->i2cx
, I2C_ADDRF_FLAG
);
462 while (hi2c
->pcount
> 0)
464 /* wait for the rdbf flag to be set */
465 if(i2c_wait_flag(hi2c
, I2C_RDBF_FLAG
, I2C_EVENT_CHECK_STOP
, timeout
) != I2C_OK
)
467 /* disable acknowledge */
468 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
470 /* if data is received, read data */
471 if (i2c_flag_get(hi2c
->i2cx
, I2C_RDBF_FLAG
) == SET
)
474 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
478 return I2C_ERR_STEP_4
;
482 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
486 /* wait for the stop flag to be set */
487 if(i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
489 /* disable acknowledge */
490 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
492 return I2C_ERR_STEP_5
;
495 /* clear stop flag */
496 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
498 /* wait for the busy flag to be reset */
499 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
501 /* disable acknowledge */
502 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
504 return I2C_ERR_STEP_6
;
511 * @brief the master receive data through polling mode.
512 * @param hi2c: the handle points to the operation information.
513 * @param address: slave address.
514 * @param pdata: data buffer.
515 * @param size: data size.
516 * @param timeout: maximum waiting time.
517 * @retval i2c status.
519 i2c_status_type
i2c_master_receive(i2c_handle_type
* hi2c
, uint16_t address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
521 /* initialization parameters */
525 hi2c
->error_code
= I2C_OK
;
527 /* wait for the busy flag to be reset */
528 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
530 return I2C_ERR_STEP_1
;
534 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_READ
);
536 while (hi2c
->pcount
> 0)
538 /* wait for the rdbf flag to be set */
539 if(i2c_wait_flag(hi2c
, I2C_RDBF_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
541 return I2C_ERR_STEP_2
;
545 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
549 if ((hi2c
->psize
== 0) && (hi2c
->pcount
!= 0))
551 /* wait for the tcrld flag to be set */
552 if (i2c_wait_flag(hi2c
, I2C_TCRLD_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
554 return I2C_ERR_STEP_3
;
557 /* continue transfer */
558 i2c_start_transfer(hi2c
, address
, I2C_WITHOUT_START
);
562 /* wait for the stop flag to be set */
563 if(i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
565 return I2C_ERR_STEP_4
;
568 /* clear stop flag */
569 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
571 /* reset ctrl2 register */
572 i2c_reset_ctrl2_register(hi2c
);
578 * @brief the slave transmits data through polling mode.
579 * @param hi2c: the handle points to the operation information.
580 * @param pdata: data buffer.
581 * @param size: data size.
582 * @param timeout: maximum waiting time.
583 * @retval i2c status.
585 i2c_status_type
i2c_slave_transmit(i2c_handle_type
* hi2c
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
587 /* initialization parameters */
591 hi2c
->error_code
= I2C_OK
;
593 /* wait for the busy flag to be reset */
594 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
596 return I2C_ERR_STEP_1
;
599 /* enable acknowledge */
600 i2c_ack_enable(hi2c
->i2cx
, TRUE
);
602 /* wait for the addr flag to be set */
603 if (i2c_wait_flag(hi2c
, I2C_ADDRF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
605 /* disable acknowledge */
606 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
607 return I2C_ERR_STEP_2
;
610 /* clear addr flag */
611 i2c_flag_clear(hi2c
->i2cx
, I2C_ADDRF_FLAG
);
613 /* if 10-bit address mode is used */
614 if (hi2c
->i2cx
->ctrl2_bit
.addr10
!= RESET
)
616 /* wait for the addr flag to be set */
617 if (i2c_wait_flag(hi2c
, I2C_ADDRF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
619 /* disable acknowledge */
620 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
622 return I2C_ERR_STEP_3
;
625 /* clear addr flag */
626 i2c_flag_clear(hi2c
->i2cx
, I2C_ADDRF_FLAG
);
629 while (hi2c
->pcount
> 0)
631 /* wait for the tdis flag to be set */
632 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
634 /* disable acknowledge */
635 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
637 return I2C_ERR_STEP_5
;
641 i2c_data_send(hi2c
->i2cx
, *hi2c
->pbuff
++);
645 /* wait for the ackfail flag to be set */
646 if(i2c_wait_flag(hi2c
, I2C_ACKFAIL_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
648 return I2C_ERR_STEP_6
;
651 /* clear ack fail flag */
652 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
654 /* wait for the stop flag to be set */
655 if(i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
657 /* disable acknowledge */
658 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
660 return I2C_ERR_STEP_7
;
663 /* clear stop flag */
664 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
666 /* wait for the busy flag to be reset */
667 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
669 /* disable acknowledge */
670 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
672 return I2C_ERR_STEP_8
;
675 /* refresh tx dt register */
676 i2c_refresh_txdt_register(hi2c
);
682 * @brief the master transmits data through interrupt mode.
683 * @param hi2c: the handle points to the operation information.
684 * @param address: slave address.
685 * @param pdata: data buffer.
686 * @param size: data size.
687 * @param timeout: maximum waiting time.
688 * @retval i2c status.
690 i2c_status_type
i2c_master_transmit_int(i2c_handle_type
* hi2c
, uint16_t address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
692 /* initialization parameters */
693 hi2c
->mode
= I2C_INT_MA_TX
;
694 hi2c
->status
= I2C_START
;
699 hi2c
->error_code
= I2C_OK
;
701 /* wait for the busy flag to be reset */
702 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
704 return I2C_ERR_STEP_1
;
708 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_WRITE
);
710 /* enable interrupt */
711 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_TDC_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_TD_INT
, TRUE
);
717 * @brief the slave receive data through interrupt mode.
718 * @param hi2c: the handle points to the operation information.
719 * @param pdata: data buffer.
720 * @param size: data size.
721 * @param timeout: maximum waiting time.
722 * @retval i2c status.
724 i2c_status_type
i2c_slave_receive_int(i2c_handle_type
* hi2c
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
726 /* initialization parameters */
727 hi2c
->mode
= I2C_INT_SLA_RX
;
728 hi2c
->status
= I2C_START
;
733 hi2c
->error_code
= I2C_OK
;
735 /* wait for the busy flag to be reset */
736 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
738 return I2C_ERR_STEP_1
;
741 /* enable acknowledge */
742 i2c_ack_enable(hi2c
->i2cx
, TRUE
);
744 /* enable interrupt */
745 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_TDC_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_ADDR_INT
| I2C_RD_INT
, TRUE
);
751 * @brief the master receive data through interrupt mode.
752 * @param hi2c: the handle points to the operation information.
753 * @param address: slave address.
754 * @param pdata: data buffer.
755 * @param size: data size.
756 * @param timeout: maximum waiting time.
757 * @retval i2c status.
759 i2c_status_type
i2c_master_receive_int(i2c_handle_type
* hi2c
, uint16_t address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
761 /* initialization parameters */
762 hi2c
->mode
= I2C_INT_MA_RX
;
763 hi2c
->status
= I2C_START
;
768 hi2c
->error_code
= I2C_OK
;
770 /* wait for the busy flag to be reset */
771 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
773 return I2C_ERR_STEP_1
;
777 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_READ
);
779 /* enable interrupt */
780 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_TDC_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_RD_INT
, TRUE
);
786 * @brief the slave transmits data through interrupt mode.
787 * @param hi2c: the handle points to the operation information.
788 * @param pdata: data buffer.
789 * @param size: data size.
790 * @param timeout: maximum waiting time.
791 * @retval i2c status.
793 i2c_status_type
i2c_slave_transmit_int(i2c_handle_type
* hi2c
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
795 /* initialization parameters */
796 hi2c
->mode
= I2C_INT_SLA_TX
;
797 hi2c
->status
= I2C_START
;
802 hi2c
->error_code
= I2C_OK
;
804 /* wait for the busy flag to be reset */
805 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
807 return I2C_ERR_STEP_1
;
810 /* enable acknowledge */
811 i2c_ack_enable(hi2c
->i2cx
, TRUE
);
813 /* enable interrupt */
814 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_TDC_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_ADDR_INT
| I2C_TD_INT
, TRUE
);
816 i2c_refresh_txdt_register(hi2c
);
822 * @brief the master transmits data through dma mode.
823 * @param hi2c: the handle points to the operation information.
824 * @param address: slave address.
825 * @param pdata: data buffer.
826 * @param size: data size.
827 * @param timeout: maximum waiting time.
828 * @retval i2c status.
830 i2c_status_type
i2c_master_transmit_dma(i2c_handle_type
* hi2c
, uint16_t address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
832 /* initialization parameters */
833 hi2c
->mode
= I2C_DMA_MA_TX
;
834 hi2c
->status
= I2C_START
;
839 hi2c
->error_code
= I2C_OK
;
841 /* wait for the busy flag to be reset */
842 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
844 return I2C_ERR_STEP_1
;
847 /* disable dma request */
848 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_TX
, FALSE
);
851 i2c_start_transfer_dma(hi2c
, hi2c
->dma_tx_channel
, address
, I2C_GEN_START_WRITE
);
853 /* enable i2c interrupt */
854 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_ACKFIAL_INT
, TRUE
);
856 /* enable dma request */
857 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_TX
, TRUE
);
863 * @brief the slave receive data through dma mode.
864 * @param hi2c: the handle points to the operation information.
865 * @param pdata: data buffer.
866 * @param size: data size.
867 * @param timeout: maximum waiting time.
868 * @retval i2c status.
870 i2c_status_type
i2c_slave_receive_dma(i2c_handle_type
* hi2c
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
872 /* initialization parameters */
873 hi2c
->mode
= I2C_DMA_SLA_RX
;
874 hi2c
->status
= I2C_START
;
879 hi2c
->error_code
= I2C_OK
;
881 /* wait for the busy flag to be reset */
882 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
884 return I2C_ERR_STEP_1
;
887 /* disable dma request */
888 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_RX
, FALSE
);
891 i2c_dma_config(hi2c
, hi2c
->dma_rx_channel
, hi2c
->pbuff
, size
);
893 /* enable acknowledge */
894 i2c_ack_enable(hi2c
->i2cx
, TRUE
);
896 /* enable i2c interrupt */
897 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ADDR_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_ERR_INT
, TRUE
);
899 /* enable dma request */
900 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_RX
, TRUE
);
906 * @brief the master receive data through dma mode.
907 * @param hi2c: the handle points to the operation information.
908 * @param address: slave address.
909 * @param pdata: data buffer.
910 * @param size: data size.
911 * @param timeout: maximum waiting time.
912 * @retval i2c status.
914 i2c_status_type
i2c_master_receive_dma(i2c_handle_type
* hi2c
, uint16_t address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
916 /* initialization parameters */
917 hi2c
->mode
= I2C_DMA_MA_RX
;
918 hi2c
->status
= I2C_START
;
923 hi2c
->error_code
= I2C_OK
;
925 /* wait for the busy flag to be reset */
926 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
928 return I2C_ERR_STEP_1
;
931 /* disable dma request */
932 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_RX
, FALSE
);
935 i2c_start_transfer_dma(hi2c
, hi2c
->dma_rx_channel
, address
, I2C_GEN_START_READ
);
937 /* enable i2c interrupt */
938 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_ACKFIAL_INT
, TRUE
);
940 /* enable dma request */
941 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_RX
, TRUE
);
947 * @brief the slave transmits data through dma mode.
948 * @param hi2c: the handle points to the operation information.
949 * @param pdata: data buffer.
950 * @param size: data size.
951 * @param timeout: maximum waiting time.
952 * @retval i2c status.
954 i2c_status_type
i2c_slave_transmit_dma(i2c_handle_type
* hi2c
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
956 /* initialization parameters */
957 hi2c
->mode
= I2C_DMA_SLA_TX
;
958 hi2c
->status
= I2C_START
;
963 hi2c
->error_code
= I2C_OK
;
965 /* wait for the busy flag to be reset */
966 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
968 return I2C_ERR_STEP_1
;
971 /* disable dma request */
972 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_TX
, FALSE
);
975 i2c_dma_config(hi2c
, hi2c
->dma_tx_channel
, hi2c
->pbuff
, size
);
977 /* enable acknowledge */
978 i2c_ack_enable(hi2c
->i2cx
, TRUE
);
980 /* enable i2c interrupt */
981 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ADDR_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_ERR_INT
, TRUE
);
983 /* enable dma request */
984 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_TX
, TRUE
);
990 * @brief send memory address.
991 * @param hi2c: the handle points to the operation information.
992 * @param mem_address_width: memory address width.
993 * this parameter can be one of the following values:
994 * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
995 * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
996 * @param address: memory device address.
997 * @param mem_address: memory address.
998 * @param timeout: maximum waiting time.
999 * @retval i2c status.
1001 i2c_status_type
i2c_memory_address_send(i2c_handle_type
* hi2c
, i2c_mem_address_width_type mem_address_width
, uint16_t mem_address
, int32_t timeout
)
1003 i2c_status_type err_code
;
1005 if(mem_address_width
== I2C_MEM_ADDR_WIDIH_8
)
1007 /* send memory address */
1008 i2c_data_send(hi2c
->i2cx
, mem_address
& 0xFF);
1012 /* send memory address */
1013 i2c_data_send(hi2c
->i2cx
, (mem_address
>> 8) & 0xFF);
1015 /* wait for the tdis flag to be set */
1016 err_code
= i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
);
1018 if(err_code
!= I2C_OK
)
1023 /* send memory address */
1024 i2c_data_send(hi2c
->i2cx
, mem_address
& 0xFF);
1031 * @brief write data to the memory device through polling mode.
1032 * @param hi2c: the handle points to the operation information.
1033 * @param mem_address_width: memory address width.
1034 * this parameter can be one of the following values:
1035 * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
1036 * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
1037 * @param address: memory device address.
1038 * @param mem_address: memory address.
1039 * @param pdata: data buffer.
1040 * @param size: data size.
1041 * @param timeout: maximum waiting time.
1042 * @retval i2c status.
1044 i2c_status_type
i2c_memory_write(i2c_handle_type
* hi2c
, i2c_mem_address_width_type mem_address_width
, uint16_t address
, uint16_t mem_address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1046 /* initialization parameters */
1047 hi2c
->pbuff
= pdata
;
1048 hi2c
->pcount
= size
+ mem_address_width
;
1050 hi2c
->error_code
= I2C_OK
;
1052 /* wait for the busy flag to be reset */
1053 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1055 return I2C_ERR_STEP_1
;
1058 /* start transfer */
1059 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_WRITE
);
1061 /* wait for the tdis flag to be set */
1062 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1064 return I2C_ERR_STEP_2
;
1067 /* send memory address */
1068 if(i2c_memory_address_send(hi2c
, mem_address_width
, mem_address
, timeout
) != I2C_OK
)
1070 return I2C_ERR_STEP_3
;
1073 hi2c
->psize
-= mem_address_width
;
1074 hi2c
->pcount
-= mem_address_width
;
1076 while (hi2c
->pcount
> 0)
1078 /* wait for the tdis flag to be set */
1079 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1081 return I2C_ERR_STEP_4
;
1085 i2c_data_send(hi2c
->i2cx
, *hi2c
->pbuff
++);
1089 if ((hi2c
->psize
== 0) && (hi2c
->pcount
!= 0))
1091 /* wait for the tcrld flag to be set */
1092 if (i2c_wait_flag(hi2c
, I2C_TCRLD_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1094 return I2C_ERR_STEP_5
;
1097 /* continue transfer */
1098 i2c_start_transfer(hi2c
, address
, I2C_WITHOUT_START
);
1102 /* wait for the stop flag to be set */
1103 if(i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1105 return I2C_ERR_STEP_6
;
1108 /* clear stop flag */
1109 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
1111 /* reset ctrl2 register */
1112 i2c_reset_ctrl2_register(hi2c
);
1118 * @brief read data from memory device through polling mode.
1119 * @param hi2c: the handle points to the operation information.
1120 * @param mem_address_width: memory address width.
1121 * this parameter can be one of the following values:
1122 * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
1123 * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
1124 * @param address: memory device address.
1125 * @param mem_address: memory address.
1126 * @param pdata: data buffer.
1127 * @param size: data size.
1128 * @param timeout: maximum waiting time.
1129 * @retval i2c status.
1131 i2c_status_type
i2c_memory_read(i2c_handle_type
* hi2c
, i2c_mem_address_width_type mem_address_width
, uint16_t address
, uint16_t mem_address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1133 /* initialization parameters */
1134 hi2c
->pbuff
= pdata
;
1135 hi2c
->pcount
= size
;
1137 hi2c
->error_code
= I2C_OK
;
1139 /* wait for the busy flag to be reset */
1140 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1142 return I2C_ERR_STEP_1
;
1145 /* start transfer */
1146 i2c_transmit_set(hi2c
->i2cx
, address
, mem_address_width
, I2C_SOFT_STOP_MODE
, I2C_GEN_START_WRITE
);
1148 /* wait for the tdis flag to be set */
1149 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1151 return I2C_ERR_STEP_2
;
1154 /* send memory address */
1155 if(i2c_memory_address_send(hi2c
, mem_address_width
, mem_address
, timeout
) != I2C_OK
)
1157 return I2C_ERR_STEP_3
;
1160 /* wait for the tdc flag to be set */
1161 if (i2c_wait_flag(hi2c
, I2C_TDC_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1163 return I2C_ERR_STEP_4
;
1166 /* start transfer */
1167 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_READ
);
1169 while (hi2c
->pcount
> 0)
1171 /* wait for the rdbf flag to be set */
1172 if (i2c_wait_flag(hi2c
, I2C_RDBF_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1174 return I2C_ERR_STEP_5
;
1178 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
1182 if ((hi2c
->psize
== 0) && (hi2c
->pcount
!= 0))
1184 /* wait for the tcrld flag to be set */
1185 if (i2c_wait_flag(hi2c
, I2C_TCRLD_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1187 return I2C_ERR_STEP_6
;
1190 /* continue transfer */
1191 i2c_start_transfer(hi2c
, address
, I2C_WITHOUT_START
);
1195 /* wait for the stop flag to be set */
1196 if (i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1198 return I2C_ERR_STEP_7
;
1201 /* clear stop flag */
1202 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
1204 /* reset ctrl2 register */
1205 i2c_reset_ctrl2_register(hi2c
);
1211 * @brief write data to the memory device through interrupt mode.
1212 * @param hi2c: the handle points to the operation information.
1213 * @param mem_address_width: memory address width.
1214 * this parameter can be one of the following values:
1215 * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
1216 * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
1217 * @param address: memory device address.
1218 * @param mem_address: memory address.
1219 * @param pdata: data buffer.
1220 * @param size: data size.
1221 * @param timeout: maximum waiting time.
1222 * @retval i2c status.
1224 i2c_status_type
i2c_memory_write_int(i2c_handle_type
* hi2c
, i2c_mem_address_width_type mem_address_width
, uint16_t address
, uint16_t mem_address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1226 /* initialization parameters */
1227 hi2c
->mode
= I2C_INT_MA_TX
;
1228 hi2c
->status
= I2C_START
;
1230 hi2c
->pbuff
= pdata
;
1231 hi2c
->pcount
= size
+ mem_address_width
;
1233 hi2c
->error_code
= I2C_OK
;
1235 /* wait for the busy flag to be reset */
1236 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1238 return I2C_ERR_STEP_1
;
1241 /* start transfer */
1242 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_WRITE
);
1244 /* wait for the tdis flag to be set */
1245 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1247 return I2C_ERR_STEP_2
;
1250 /* send memory address */
1251 if(i2c_memory_address_send(hi2c
, mem_address_width
, mem_address
, timeout
) != I2C_OK
)
1253 return I2C_ERR_STEP_3
;
1259 /* enable interrupt */
1260 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_TDC_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_TD_INT
, TRUE
);
1266 * @brief read data from memory device through interrupt mode.
1267 * @param hi2c: the handle points to the operation information.
1268 * @param mem_address_width: memory address width.
1269 * this parameter can be one of the following values:
1270 * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
1271 * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
1272 * @param address: memory device address.
1273 * @param mem_address: memory address.
1274 * @param pdata: data buffer.
1275 * @param size: data size.
1276 * @param timeout: maximum waiting time.
1277 * @retval i2c status.
1279 i2c_status_type
i2c_memory_read_int(i2c_handle_type
* hi2c
, i2c_mem_address_width_type mem_address_width
, uint16_t address
, uint16_t mem_address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1281 /* initialization parameters */
1282 hi2c
->mode
= I2C_INT_MA_RX
;
1283 hi2c
->status
= I2C_START
;
1285 hi2c
->pbuff
= pdata
;
1286 hi2c
->pcount
= size
;
1288 hi2c
->error_code
= I2C_OK
;
1290 /* wait for the busy flag to be reset */
1291 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1293 return I2C_ERR_STEP_1
;
1296 /* start transfer */
1297 i2c_transmit_set(hi2c
->i2cx
, address
, mem_address_width
, I2C_SOFT_STOP_MODE
, I2C_GEN_START_WRITE
);
1299 /* wait for the tdis flag to be set */
1300 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1302 return I2C_ERR_STEP_2
;
1305 /* send memory address */
1306 if(i2c_memory_address_send(hi2c
, mem_address_width
, mem_address
, timeout
) != I2C_OK
)
1308 return I2C_ERR_STEP_3
;
1311 /* wait for the tdc flag to be set */
1312 if (i2c_wait_flag(hi2c
, I2C_TDC_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1314 return I2C_ERR_STEP_4
;
1317 /* start transfer */
1318 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_READ
);
1320 /* enable i2c interrupt */
1321 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_TDC_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_RD_INT
, TRUE
);
1327 * @brief write data to the memory device through dma mode.
1328 * @param hi2c: the handle points to the operation information.
1329 * @param mem_address_width: memory address width.
1330 * this parameter can be one of the following values:
1331 * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
1332 * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
1333 * @param address: memory device address.
1334 * @param mem_address: memory address.
1335 * @param pdata: data buffer.
1336 * @param size: data size.
1337 * @param timeout: maximum waiting time.
1338 * @retval i2c status.
1340 i2c_status_type
i2c_memory_write_dma(i2c_handle_type
* hi2c
, i2c_mem_address_width_type mem_address_width
, uint16_t address
, uint16_t mem_address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1342 /* initialization parameters */
1343 hi2c
->mode
= I2C_DMA_MA_TX
;
1344 hi2c
->status
= I2C_START
;
1346 hi2c
->pbuff
= pdata
;
1347 hi2c
->pcount
= size
;
1349 hi2c
->error_code
= I2C_OK
;
1351 /* disable dma request */
1352 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_TX
, FALSE
);
1354 /* wait for the busy flag to be reset */
1355 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1357 return I2C_ERR_STEP_1
;
1360 /* transfer config */
1361 i2c_transmit_set(hi2c
->i2cx
, address
, mem_address_width
, I2C_RELOAD_MODE
, I2C_GEN_START_WRITE
);
1363 /* wait for the tdis flag to be set */
1364 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1366 return I2C_ERR_STEP_2
;
1369 /* send memory address */
1370 if(i2c_memory_address_send(hi2c
, mem_address_width
, mem_address
, timeout
) != I2C_OK
)
1372 return I2C_ERR_STEP_3
;
1375 /* wait for the tcrld flag to be set */
1376 if (i2c_wait_flag(hi2c
, I2C_TCRLD_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1378 return I2C_ERR_STEP_4
;
1381 /* start transfer */
1382 i2c_start_transfer_dma(hi2c
, hi2c
->dma_tx_channel
, address
, I2C_WITHOUT_START
);
1384 /* enable i2c interrupt */
1385 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_ACKFIAL_INT
, TRUE
);
1387 /* enable dma request */
1388 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_TX
, TRUE
);
1394 * @brief read data from memory device through polling mode.
1395 * @param hi2c: the handle points to the operation information.
1396 * @param mem_address_width: memory address width.
1397 * this parameter can be one of the following values:
1398 * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
1399 * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
1400 * @param address: memory device address.
1401 * @param mem_address: memory address.
1402 * @param pdata: data buffer.
1403 * @param size: data size.
1404 * @param timeout: maximum waiting time.
1405 * @retval i2c status.
1407 i2c_status_type
i2c_memory_read_dma(i2c_handle_type
* hi2c
, i2c_mem_address_width_type mem_address_width
, uint16_t address
, uint16_t mem_address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1409 /* initialization parameters */
1410 hi2c
->mode
= I2C_DMA_MA_RX
;
1411 hi2c
->status
= I2C_START
;
1413 hi2c
->pbuff
= pdata
;
1414 hi2c
->pcount
= size
;
1416 hi2c
->error_code
= I2C_OK
;
1418 /* wait for the busy flag to be reset */
1419 if(i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1421 return I2C_ERR_STEP_1
;
1424 /* start transfer */
1425 i2c_transmit_set(hi2c
->i2cx
, address
, mem_address_width
, I2C_SOFT_STOP_MODE
, I2C_GEN_START_WRITE
);
1427 /* wait for the tdis flag to be set */
1428 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1430 return I2C_ERR_STEP_2
;
1433 /* send memory address */
1434 if(i2c_memory_address_send(hi2c
, mem_address_width
, mem_address
, timeout
) != I2C_OK
)
1436 return I2C_ERR_STEP_3
;
1439 /* wait for the tdc flag to be set */
1440 if (i2c_wait_flag(hi2c
, I2C_TDC_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1442 return I2C_ERR_STEP_4
;
1445 /* disable dma request */
1446 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_RX
, FALSE
);
1448 /* start transfer */
1449 i2c_start_transfer_dma(hi2c
, hi2c
->dma_rx_channel
, address
, I2C_GEN_START_READ
);
1451 /* enable i2c interrupt */
1452 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_ACKFIAL_INT
, TRUE
);
1454 /* enable dma request */
1455 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_RX
, TRUE
);
1461 * @brief the master transmits data through SMBus mode.
1462 * @param hi2c: the handle points to the operation information.
1463 * @param address: slave address.
1464 * @param pdata: data buffer.
1465 * @param size: data size.
1466 * @param timeout: maximum waiting time.
1467 * @retval i2c status.
1469 i2c_status_type
i2c_smbus_master_transmit(i2c_handle_type
* hi2c
, uint16_t address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1471 /* initialization parameters */
1472 hi2c
->pbuff
= pdata
;
1473 hi2c
->pcount
= size
;
1475 hi2c
->error_code
= I2C_OK
;
1477 /* wait for the busy flag to be reset */
1478 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1480 return I2C_ERR_STEP_1
;
1483 /* enable pec calculation */
1484 i2c_pec_calculate_enable(hi2c
->i2cx
, TRUE
);
1486 /* enable pec transmit request */
1487 i2c_pec_transmit_enable(hi2c
->i2cx
, TRUE
);
1489 /* start transfer */
1490 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_WRITE
);
1494 while (hi2c
->pcount
> 0)
1496 /* wait for the tdis flag to be set */
1497 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1499 return I2C_ERR_STEP_2
;
1503 i2c_data_send(hi2c
->i2cx
, *hi2c
->pbuff
++);
1507 if ((hi2c
->psize
== 0) && (hi2c
->pcount
!= 0))
1509 /* wait for the tcrld flag to be set */
1510 if (i2c_wait_flag(hi2c
, I2C_TCRLD_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1512 return I2C_ERR_STEP_3
;
1515 /* continue transfer */
1516 i2c_start_transfer(hi2c
, address
, I2C_WITHOUT_START
);
1520 /* wait for the stop flag to be set */
1521 if(i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1523 return I2C_ERR_STEP_4
;
1526 /* clear stop flag */
1527 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
1529 /* reset ctrl2 register */
1530 i2c_reset_ctrl2_register(hi2c
);
1536 * @brief the slave receive data through SMBus mode.
1537 * @param hi2c: the handle points to the operation information.
1538 * @param pdata: data buffer.
1539 * @param size: data size.
1540 * @param timeout: maximum waiting time.
1541 * @retval i2c status.
1543 i2c_status_type
i2c_smbus_slave_receive(i2c_handle_type
* hi2c
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1545 /* initialization parameters */
1546 hi2c
->pbuff
= pdata
;
1547 hi2c
->pcount
= size
;
1549 hi2c
->error_code
= I2C_OK
;
1551 /* enable pec calculation */
1552 i2c_pec_calculate_enable(hi2c
->i2cx
, TRUE
);
1554 /* enable slave data control mode */
1555 i2c_slave_data_ctrl_enable(hi2c
->i2cx
, TRUE
);
1557 /* enable acknowledge */
1558 i2c_ack_enable(hi2c
->i2cx
, TRUE
);
1560 /* wait for the addr flag to be set */
1561 if (i2c_wait_flag(hi2c
, I2C_ADDRF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1563 return I2C_ERR_STEP_1
;
1566 /* enable pec transmit request */
1567 i2c_pec_transmit_enable(hi2c
->i2cx
, TRUE
);
1569 /* configure the number of bytes to be transmitted */
1570 i2c_cnt_set(hi2c
->i2cx
, hi2c
->pcount
);
1572 /* clear addr flag */
1573 i2c_flag_clear(hi2c
->i2cx
, I2C_ADDRF_FLAG
);
1575 while (hi2c
->pcount
> 0)
1577 /* wait for the rdbf flag to be set */
1578 if(i2c_wait_flag(hi2c
, I2C_RDBF_FLAG
, I2C_EVENT_CHECK_STOP
, timeout
) != I2C_OK
)
1580 /* disable acknowledge */
1581 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
1583 /* if data is received, read data */
1584 if (i2c_flag_get(hi2c
->i2cx
, I2C_RDBF_FLAG
) == SET
)
1587 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
1591 return I2C_ERR_STEP_3
;
1595 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
1599 /* wait for the stop flag to be set */
1600 if(i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1602 /* disable acknowledge */
1603 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
1605 return I2C_ERR_STEP_4
;
1608 /* clear stop flag */
1609 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
1611 /* wait for the busy flag to be reset */
1612 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1614 /* disable acknowledge */
1615 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
1617 return I2C_ERR_STEP_5
;
1620 /* disable slave data control mode */
1621 i2c_slave_data_ctrl_enable(hi2c
->i2cx
, FALSE
);
1627 * @brief the master receive data through SMBus mode.
1628 * @param hi2c: the handle points to the operation information.
1629 * @param address: slave address.
1630 * @param pdata: data buffer.
1631 * @param size: data size.
1632 * @param timeout: maximum waiting time.
1633 * @retval i2c status.
1635 i2c_status_type
i2c_smbus_master_receive(i2c_handle_type
* hi2c
, uint16_t address
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1637 /* initialization parameters */
1638 hi2c
->pbuff
= pdata
;
1639 hi2c
->pcount
= size
;
1641 hi2c
->error_code
= I2C_OK
;
1643 /* wait for the busy flag to be reset */
1644 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1646 return I2C_ERR_STEP_1
;
1649 /* enable pec calculation */
1650 i2c_pec_calculate_enable(hi2c
->i2cx
, TRUE
);
1652 /* enable pec transmit request */
1653 i2c_pec_transmit_enable(hi2c
->i2cx
, TRUE
);
1655 /* start transfer */
1656 i2c_start_transfer(hi2c
, address
, I2C_GEN_START_READ
);
1658 while (hi2c
->pcount
> 0)
1660 /* wait for the rdbf flag to be set */
1661 if(i2c_wait_flag(hi2c
, I2C_RDBF_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1663 return I2C_ERR_STEP_2
;
1667 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
1671 if ((hi2c
->psize
== 0) && (hi2c
->pcount
!= 0))
1673 /* wait for the tcrld flag to be set */
1674 if (i2c_wait_flag(hi2c
, I2C_TCRLD_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1676 return I2C_ERR_STEP_3
;
1679 /* continue transfer */
1680 i2c_start_transfer(hi2c
, address
, I2C_WITHOUT_START
);
1684 /* wait for the stop flag to be set */
1685 if(i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1687 return I2C_ERR_STEP_4
;
1690 /* clear stop flag */
1691 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
1693 /* reset ctrl2 register */
1694 i2c_reset_ctrl2_register(hi2c
);
1700 * @brief the slave transmits data through SMBus mode.
1701 * @param hi2c: the handle points to the operation information.
1702 * @param pdata: data buffer.
1703 * @param size: data size.
1704 * @param timeout: maximum waiting time.
1705 * @retval i2c status.
1707 i2c_status_type
i2c_smbus_slave_transmit(i2c_handle_type
* hi2c
, uint8_t* pdata
, uint16_t size
, uint32_t timeout
)
1709 /* initialization parameters */
1710 hi2c
->pbuff
= pdata
;
1711 hi2c
->pcount
= size
;
1713 hi2c
->error_code
= I2C_OK
;
1715 /* enable pec calculation */
1716 i2c_pec_calculate_enable(hi2c
->i2cx
, TRUE
);
1718 /* enable slave data control mode */
1719 i2c_slave_data_ctrl_enable(hi2c
->i2cx
, TRUE
);
1721 /* enable acknowledge */
1722 i2c_ack_enable(hi2c
->i2cx
, TRUE
);
1724 /* wait for the addr flag to be set */
1725 if (i2c_wait_flag(hi2c
, I2C_ADDRF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1727 /* disable acknowledge */
1728 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
1729 return I2C_ERR_STEP_1
;
1732 /* if 7-bit address mode is selected */
1733 if (hi2c
->i2cx
->ctrl2_bit
.addr10
== 0)
1735 /* enable pec transmit request */
1736 i2c_pec_transmit_enable(hi2c
->i2cx
, TRUE
);
1738 /* configure the number of bytes to be transmitted */
1739 i2c_cnt_set(hi2c
->i2cx
, hi2c
->pcount
);
1742 /* clear addr flag */
1743 i2c_flag_clear(hi2c
->i2cx
, I2C_ADDRF_FLAG
);
1745 /* if 10-bit address mode is used */
1746 if (hi2c
->i2cx
->ctrl2_bit
.addr10
!= RESET
)
1748 /* wait for the addr flag to be set */
1749 if (i2c_wait_flag(hi2c
, I2C_ADDRF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1751 /* disable acknowledge */
1752 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
1754 return I2C_ERR_STEP_2
;
1757 /* enable pec transmit request */
1758 i2c_pec_transmit_enable(hi2c
->i2cx
, TRUE
);
1760 /* configure the number of bytes to be transmitted */
1761 i2c_cnt_set(hi2c
->i2cx
, hi2c
->pcount
);
1763 /* clear addr flag */
1764 i2c_flag_clear(hi2c
->i2cx
, I2C_ADDRF_FLAG
);
1769 while (hi2c
->pcount
> 0)
1771 /* wait for the tdis flag to be set */
1772 if(i2c_wait_flag(hi2c
, I2C_TDIS_FLAG
, I2C_EVENT_CHECK_ACKFAIL
, timeout
) != I2C_OK
)
1774 /* disable acknowledge */
1775 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
1777 return I2C_ERR_STEP_4
;
1781 i2c_data_send(hi2c
->i2cx
, *hi2c
->pbuff
++);
1785 /* wait for the ackfail flag to be set */
1786 if(i2c_wait_flag(hi2c
, I2C_ACKFAIL_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1788 return I2C_ERR_STEP_5
;
1791 /* clear ack fail flag */
1792 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
1794 /* wait for the stop flag to be set */
1795 if(i2c_wait_flag(hi2c
, I2C_STOPF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1797 /* disable acknowledge */
1798 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
1800 return I2C_ERR_STEP_6
;
1803 /* clear stop flag */
1804 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
1806 /* wait for the busy flag to be reset */
1807 if (i2c_wait_flag(hi2c
, I2C_BUSYF_FLAG
, I2C_EVENT_CHECK_NONE
, timeout
) != I2C_OK
)
1809 /* disable acknowledge */
1810 i2c_ack_enable(hi2c
->i2cx
, FALSE
);
1812 return I2C_ERR_STEP_7
;
1815 /* reset ctrl2 register */
1816 i2c_reset_ctrl2_register(hi2c
);
1818 /* refresh tx dt register */
1819 i2c_refresh_txdt_register(hi2c
);
1821 /* disable slave data control mode */
1822 i2c_slave_data_ctrl_enable(hi2c
->i2cx
, FALSE
);
1828 * @brief master interrupt processing function in interrupt mode.
1829 * @param hi2c: the handle points to the operation information.
1830 * @retval i2c status.
1832 i2c_status_type
i2c_master_irq_handler_int(i2c_handle_type
* hi2c
)
1834 if (i2c_flag_get(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
) != RESET
)
1837 /* clear ackfail flag */
1838 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
1840 /* refresh tx register */
1841 i2c_refresh_txdt_register(hi2c
);
1843 if(hi2c
->pcount
!= 0)
1845 hi2c
->error_code
= I2C_ERR_ACKFAIL
;
1848 else if (i2c_flag_get(hi2c
->i2cx
, I2C_TDIS_FLAG
) != RESET
)
1851 i2c_data_send(hi2c
->i2cx
, *hi2c
->pbuff
++);
1855 else if (i2c_flag_get(hi2c
->i2cx
, I2C_TCRLD_FLAG
) != RESET
)
1857 if ((hi2c
->psize
== 0) && (hi2c
->pcount
!= 0))
1859 /* continue transfer */
1860 i2c_start_transfer(hi2c
, i2c_transfer_addr_get(hi2c
->i2cx
), I2C_WITHOUT_START
);
1864 return I2C_ERR_TCRLD
;
1867 else if (i2c_flag_get(hi2c
->i2cx
, I2C_RDBF_FLAG
) != RESET
)
1870 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
1874 else if (i2c_flag_get(hi2c
->i2cx
, I2C_TDC_FLAG
) != RESET
)
1876 if (hi2c
->pcount
== 0)
1878 if (hi2c
->i2cx
->ctrl2_bit
.astopen
== 0)
1880 /* generate stop condtion */
1881 i2c_stop_generate(hi2c
->i2cx
);
1889 else if (i2c_flag_get(hi2c
->i2cx
, I2C_STOPF_FLAG
) != RESET
)
1891 /* clear stop flag */
1892 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
1894 /* reset ctrl2 register */
1895 i2c_reset_ctrl2_register(hi2c
);
1897 if (i2c_flag_get(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
) != RESET
)
1899 /* clear ackfail flag */
1900 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
1903 /* refresh tx dt register */
1904 i2c_refresh_txdt_register(hi2c
);
1906 /* disable interrupts */
1907 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_TDC_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_TD_INT
| I2C_RD_INT
, FALSE
);
1909 /* transfer complete */
1910 hi2c
->status
= I2C_END
;
1917 * @brief slave interrupt processing function in interrupt mode.
1918 * @param hi2c: the handle points to the operation information.
1919 * @retval i2c status.
1921 i2c_status_type
i2c_slave_irq_handler_int(i2c_handle_type
* hi2c
)
1923 if (i2c_flag_get(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
) != RESET
)
1925 /* transfer complete */
1926 if (hi2c
->pcount
== 0)
1928 i2c_refresh_txdt_register(hi2c
);
1930 /* clear ackfail flag */
1931 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
1933 /* the transfer has not been completed */
1936 /* clear ackfail flag */
1937 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
1940 else if (i2c_flag_get(hi2c
->i2cx
, I2C_ADDRF_FLAG
) != RESET
)
1942 /* clear addr flag */
1943 i2c_flag_clear(hi2c
->i2cx
, I2C_ADDRF_FLAG
);
1945 else if (i2c_flag_get(hi2c
->i2cx
, I2C_TDIS_FLAG
) != RESET
)
1947 if (hi2c
->pcount
> 0)
1950 hi2c
->i2cx
->txdt
= (*(hi2c
->pbuff
++));
1955 else if (i2c_flag_get(hi2c
->i2cx
, I2C_RDBF_FLAG
) != RESET
)
1957 if (hi2c
->pcount
> 0)
1960 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
1965 else if (i2c_flag_get(hi2c
->i2cx
, I2C_STOPF_FLAG
) != RESET
)
1967 /* clear stop flag */
1968 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
1970 /* disable interrupts */
1971 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ADDR_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_ERR_INT
| I2C_TDC_INT
| I2C_TD_INT
| I2C_RD_INT
, FALSE
);
1973 /* reset ctrl2 register */
1974 i2c_reset_ctrl2_register(hi2c
);
1976 /* refresh tx dt register */
1977 i2c_refresh_txdt_register(hi2c
);
1979 /* if data is received, read data */
1980 if (i2c_flag_get(hi2c
->i2cx
, I2C_RDBF_FLAG
) != RESET
)
1983 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
1985 if ((hi2c
->psize
> 0))
1992 /* transfer complete */
1993 hi2c
->status
= I2C_END
;
2000 * @brief master interrupt processing function in dma mode.
2001 * @param hi2c: the handle points to the operation information.
2002 * @retval i2c status.
2004 i2c_status_type
i2c_master_irq_handler_dma(i2c_handle_type
* hi2c
)
2006 if (i2c_flag_get(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
) != RESET
)
2008 /* clear ackfail flag */
2009 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
2011 /* enable stop interrupt to wait for stop generate stop */
2012 i2c_interrupt_enable(hi2c
->i2cx
, I2C_STOP_INT
, TRUE
);
2014 /* refresh tx dt register */
2015 i2c_refresh_txdt_register(hi2c
);
2017 if(hi2c
->pcount
!= 0)
2019 hi2c
->error_code
= I2C_ERR_ACKFAIL
;
2022 else if (i2c_flag_get(hi2c
->i2cx
, I2C_TCRLD_FLAG
) != RESET
)
2024 /* disable tdc interrupt */
2025 i2c_interrupt_enable(hi2c
->i2cx
, I2C_TDC_INT
, FALSE
);
2027 if (hi2c
->pcount
!= 0)
2029 /* continue transfer */
2030 i2c_start_transfer(hi2c
, i2c_transfer_addr_get(hi2c
->i2cx
), I2C_WITHOUT_START
);
2032 /* enable dma request */
2033 if (hi2c
->dma_init_struct
.direction
== DMA_DIR_MEMORY_TO_PERIPHERAL
)
2035 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_TX
, TRUE
);
2039 i2c_dma_enable(hi2c
->i2cx
, I2C_DMA_REQUEST_RX
, TRUE
);
2044 return I2C_ERR_TCRLD
;
2047 else if (i2c_flag_get(hi2c
->i2cx
, I2C_STOPF_FLAG
) != RESET
)
2049 /* clear stop flag */
2050 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
2052 /* reset ctrl2 register */
2053 i2c_reset_ctrl2_register(hi2c
);
2055 if (i2c_flag_get(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
) != RESET
)
2057 /* clear ackfail flag */
2058 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
2061 /* refresh tx dt register */
2062 i2c_refresh_txdt_register(hi2c
);
2064 /* disable interrupts */
2065 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
| I2C_TDC_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_TD_INT
| I2C_RD_INT
, FALSE
);
2067 /* transfer complete */
2068 hi2c
->status
= I2C_END
;
2075 * @brief slave interrupt processing function in dma mode.
2076 * @param hi2c: the handle points to the operation information.
2077 * @retval i2c status.
2079 i2c_status_type
i2c_slave_irq_handler_dma(i2c_handle_type
* hi2c
)
2081 if (i2c_flag_get(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
) != RESET
)
2083 /* clear ackfail flag */
2084 i2c_flag_clear(hi2c
->i2cx
, I2C_ACKFAIL_FLAG
);
2086 else if (i2c_flag_get(hi2c
->i2cx
, I2C_ADDRF_FLAG
) != RESET
)
2088 /* clear addr flag */
2089 i2c_flag_clear(hi2c
->i2cx
, I2C_ADDRF_FLAG
);
2091 else if (i2c_flag_get(hi2c
->i2cx
, I2C_STOPF_FLAG
) != RESET
)
2093 /* clear stop flag */
2094 i2c_flag_clear(hi2c
->i2cx
, I2C_STOPF_FLAG
);
2096 /* disable interrupts */
2097 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ADDR_INT
| I2C_STOP_INT
| I2C_ACKFIAL_INT
| I2C_ERR_INT
| I2C_TDC_INT
| I2C_TD_INT
| I2C_RD_INT
, FALSE
);
2099 /* reset ctrl2 register */
2100 i2c_reset_ctrl2_register(hi2c
);
2102 /* refresh tx dt register */
2103 i2c_refresh_txdt_register(hi2c
);
2105 /* if data is received, read data */
2106 if (i2c_flag_get(hi2c
->i2cx
, I2C_RDBF_FLAG
) != RESET
)
2109 (*hi2c
->pbuff
++) = i2c_data_receive(hi2c
->i2cx
);
2111 if ((hi2c
->psize
> 0))
2118 /* transfer complete */
2119 hi2c
->status
= I2C_END
;
2126 * @brief dma processing function.
2127 * @param hi2c: the handle points to the operation information.
2130 void i2c_dma_tx_rx_irq_handler(i2c_handle_type
* hi2c
, dma_channel_type
* dma_channel
)
2132 /* transfer complete */
2133 if (dma_flag_get(DMA_GET_TC_FLAG(dma_channel
)) != RESET
)
2135 /* disable the transfer complete interrupt */
2136 dma_interrupt_enable(dma_channel
, DMA_FDT_INT
, FALSE
);
2138 /* clear the transfer complete flag */
2139 dma_flag_clear(DMA_GET_TC_FLAG(dma_channel
));
2141 /* disable dma request */
2142 i2c_dma_enable(hi2c
->i2cx
, DMA_GET_REQUEST(dma_channel
), FALSE
);
2144 /* disable dma channel */
2145 dma_channel_enable(dma_channel
, FALSE
);
2152 /* update the number of transfers */
2153 hi2c
->pcount
-= hi2c
->psize
;
2155 /* transfer complete */
2156 if (hi2c
->pcount
== 0)
2158 /* enable stop interrupt */
2159 i2c_interrupt_enable(hi2c
->i2cx
, I2C_STOP_INT
, TRUE
);
2161 /* the transfer has not been completed */
2164 /* update the buffer pointer of transfers */
2165 hi2c
->pbuff
+= hi2c
->psize
;
2167 /* set the number to be transferred */
2168 if (hi2c
->pcount
> MAX_TRANSFER_CNT
)
2170 hi2c
->psize
= MAX_TRANSFER_CNT
;
2174 hi2c
->psize
= hi2c
->pcount
;
2177 /* config dma channel, continue to transfer data */
2178 i2c_dma_config(hi2c
, dma_channel
, hi2c
->pbuff
, hi2c
->psize
);
2180 /* enable tdc interrupt */
2181 i2c_interrupt_enable(hi2c
->i2cx
, I2C_TDC_INT
, TRUE
);
2184 case I2C_DMA_SLA_TX
:
2185 case I2C_DMA_SLA_RX
:
2196 * @brief dma transmission complete interrupt function.
2197 * @param hi2c: the handle points to the operation information.
2200 void i2c_dma_tx_irq_handler(i2c_handle_type
* hi2c
)
2202 i2c_dma_tx_rx_irq_handler(hi2c
, hi2c
->dma_tx_channel
);
2206 * @brief dma reveive complete interrupt function.
2207 * @param hi2c: the handle points to the operation information.
2210 void i2c_dma_rx_irq_handler(i2c_handle_type
* hi2c
)
2212 i2c_dma_tx_rx_irq_handler(hi2c
, hi2c
->dma_rx_channel
);
2216 * @brief interrupt procession function.
2217 * @param hi2c: the handle points to the operation information.
2220 void i2c_evt_irq_handler(i2c_handle_type
* hi2c
)
2227 i2c_master_irq_handler_int(hi2c
);
2229 case I2C_INT_SLA_TX
:
2230 case I2C_INT_SLA_RX
:
2232 i2c_slave_irq_handler_int(hi2c
);
2237 i2c_master_irq_handler_dma(hi2c
);
2239 case I2C_DMA_SLA_TX
:
2240 case I2C_DMA_SLA_RX
:
2242 i2c_slave_irq_handler_dma(hi2c
);
2250 * @brief dma reveive complete interrupt function.
2251 * @param hi2c: the handle points to the operation information.
2254 void i2c_err_irq_handler(i2c_handle_type
* hi2c
)
2257 if (i2c_flag_get(hi2c
->i2cx
, I2C_BUSERR_FLAG
) != RESET
)
2259 hi2c
->error_code
= I2C_ERR_INTERRUPT
;
2262 i2c_flag_clear(hi2c
->i2cx
, I2C_BUSERR_FLAG
);
2264 /* disable interrupts */
2265 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
, FALSE
);
2269 if (i2c_flag_get(hi2c
->i2cx
, I2C_ARLOST_FLAG
) != RESET
)
2271 hi2c
->error_code
= I2C_ERR_INTERRUPT
;
2274 i2c_flag_clear(hi2c
->i2cx
, I2C_ARLOST_FLAG
);
2276 /* disable interrupts */
2277 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
, FALSE
);
2281 if (i2c_flag_get(hi2c
->i2cx
, I2C_OUF_FLAG
) != RESET
)
2283 hi2c
->error_code
= I2C_ERR_INTERRUPT
;
2286 i2c_flag_clear(hi2c
->i2cx
, I2C_OUF_FLAG
);
2288 /* disable interrupts */
2289 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
, FALSE
);
2293 if (i2c_flag_get(hi2c
->i2cx
, I2C_PECERR_FLAG
) != RESET
)
2295 hi2c
->error_code
= I2C_ERR_INTERRUPT
;
2298 i2c_flag_clear(hi2c
->i2cx
, I2C_PECERR_FLAG
);
2300 /* disable interrupts */
2301 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
, FALSE
);
2306 if (i2c_flag_get(hi2c
->i2cx
, I2C_TMOUT_FLAG
) != RESET
)
2308 hi2c
->error_code
= I2C_ERR_INTERRUPT
;
2311 i2c_flag_clear(hi2c
->i2cx
, I2C_TMOUT_FLAG
);
2313 /* disable interrupts */
2314 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
, FALSE
);
2318 if (i2c_flag_get(hi2c
->i2cx
, I2C_ALERTF_FLAG
) != RESET
)
2320 hi2c
->error_code
= I2C_ERR_INTERRUPT
;
2323 i2c_flag_clear(hi2c
->i2cx
, I2C_ALERTF_FLAG
);
2325 /* disable interrupts */
2326 i2c_interrupt_enable(hi2c
->i2cx
, I2C_ERR_INT
, FALSE
);