2 **************************************************************************
3 * @file at32f435_437_i2c.c
6 * @brief contains all the functions for the i2c firmware library
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 "at32f435_437_conf.h"
29 /** @addtogroup AT32F435_437_periph_driver
34 * @brief I2C driver modules
38 #ifdef I2C_MODULE_ENABLED
40 /** @defgroup I2C_private_functions
45 * @brief reset the i2c register
46 * @param i2c_x: to select the i2c peripheral.
47 * this parameter can be one of the following values:
51 void i2c_reset(i2c_type
*i2c_x
)
55 crm_periph_reset(CRM_I2C1_PERIPH_RESET
, TRUE
);
56 crm_periph_reset(CRM_I2C1_PERIPH_RESET
, FALSE
);
58 else if(i2c_x
== I2C2
)
60 crm_periph_reset(CRM_I2C2_PERIPH_RESET
, TRUE
);
61 crm_periph_reset(CRM_I2C2_PERIPH_RESET
, FALSE
);
63 else if(i2c_x
== I2C3
)
65 crm_periph_reset(CRM_I2C3_PERIPH_RESET
, TRUE
);
66 crm_periph_reset(CRM_I2C3_PERIPH_RESET
, FALSE
);
71 * @brief init i2c digit filters and clock control register.
72 * @param i2c_x: to select the i2c peripheral.
73 * this parameter can be one of the following values:
75 * @param dfilters: number of digit filters (0x00~0x0F).
76 * @param clk: i2c clock control register (0x00000000~0xFFFFFFFF).
79 void i2c_init(i2c_type
*i2c_x
, uint8_t dfilters
, uint32_t clk
)
81 /* disable i2c peripheral */
82 i2c_x
->ctrl1_bit
.i2cen
= FALSE
;
84 /* write clkctrl register*/
87 /* write digital filter register*/
88 i2c_x
->ctrl1_bit
.dflt
= dfilters
;
92 * @brief config i2c own address 1.
93 * @param i2c_x: to select the i2c peripheral.
94 * this parameter can be one of the following values:
96 * @param mode: i2c address mode.
97 * this parameter can be one of the following values:
98 * - I2C_ADDRESS_MODE_7BIT: 7bit address.
99 * - I2C_ADDRESS_MODE_10BIT: 10bit address.
100 * @param address: own address 1, such as 0xB0.
103 void i2c_own_address1_set(i2c_type
*i2c_x
, i2c_address_mode_type mode
, uint16_t address
)
105 /* config address mode */
106 i2c_x
->oaddr1_bit
.addr1mode
= mode
;
109 i2c_x
->oaddr1_bit
.addr1
= address
& 0x03FF;
112 i2c_x
->oaddr1_bit
.addr1en
= TRUE
;
116 * @brief config i2c own address 2 and mask.
117 * @param i2c_x: to select the i2c peripheral.
118 * this parameter can be one of the following values:
120 * @param address: own address 1, such as 0xC0.
121 * @param mask: own address 2 mask.
122 * this parameter can be one of the following values:
123 * - I2C_ADDR2_NOMASK: compare bit [7:1].
124 * - I2C_ADDR2_MASK01: only compare bit [7:2].
125 * - I2C_ADDR2_MASK02: only compare bit [7:2].
126 * - I2C_ADDR2_MASK03: only compare bit [7:3].
127 * - I2C_ADDR2_MASK04: only compare bit [7:4].
128 * - I2C_ADDR2_MASK05: only compare bit [7:5].
129 * - I2C_ADDR2_MASK06: only compare bit [7:6].
130 * - I2C_ADDR2_MASK07: only compare bit [7].
133 void i2c_own_address2_set(i2c_type
*i2c_x
, uint8_t address
, i2c_addr2_mask_type mask
)
135 i2c_x
->oaddr2_bit
.addr2mask
= mask
;
137 i2c_x
->oaddr2_bit
.addr2
= (address
>> 1) & 0x7F;
141 * @brief enable or disable own address 2.
142 * @param i2c_x: to select the i2c peripheral.
143 * this parameter can be one of the following values:
145 * @param new_state (TRUE or FALSE).
148 void i2c_own_address2_enable(i2c_type
*i2c_x
, confirm_state new_state
)
150 i2c_x
->oaddr2_bit
.addr2en
= new_state
;
154 * @brief enable or disable smbus mode.
155 * @param i2c_x: to select the i2c peripheral.
156 * this parameter can be one of the following values:
158 * @param mode: smbus device mode.
159 * this parameter can be one of the following values:
160 * - I2C_SMBUS_MODE_DEVICE: smbus device.
161 * - I2C_SMBUS_MODE_HOST: smbus host.
162 * @param new_state (TRUE or FALSE).
165 void i2c_smbus_enable(i2c_type
*i2c_x
, i2c_smbus_mode_type mode
, confirm_state new_state
)
169 case I2C_SMBUS_MODE_DEVICE
:
170 i2c_x
->ctrl1_bit
.devaddren
= new_state
;
172 case I2C_SMBUS_MODE_HOST
:
173 i2c_x
->ctrl1_bit
.haddren
= new_state
;
181 * @brief enable or disable peripheral.
182 * @param i2c_x: to select the i2c peripheral.
183 * this parameter can be one of the following values:
185 * @param new_state (TRUE or FALSE).
188 void i2c_enable(i2c_type
*i2c_x
, confirm_state new_state
)
190 i2c_x
->ctrl1_bit
.i2cen
= new_state
;
194 * @brief enable or disable clock stretch.
195 * @param i2c_x: to select the i2c peripheral.
196 * this parameter can be one of the following values:
198 * @param new_state (TRUE or FALSE).
201 void i2c_clock_stretch_enable(i2c_type
*i2c_x
, confirm_state new_state
)
203 i2c_x
->ctrl1_bit
.stretch
= (!new_state
);
207 * @brief enable or disable ack.
208 * @param i2c_x: to select the i2c peripheral.
209 * this parameter can be one of the following values:
211 * @param new_state (TRUE or FALSE).
214 void i2c_ack_enable(i2c_type
*i2c_x
, confirm_state new_state
)
216 i2c_x
->ctrl2_bit
.nacken
= (!new_state
);
220 * @brief enable or disable 10-bit address mode (master transfer).
221 * @param i2c_x: to select the i2c peripheral.
222 * this parameter can be one of the following values:
224 * @param new_state (TRUE or FALSE).
227 void i2c_addr10_mode_enable(i2c_type
*i2c_x
, confirm_state new_state
)
229 i2c_x
->ctrl2_bit
.addr10
= new_state
;
233 * @brief config the slave address to be transmitted.
234 * @param i2c_x: to select the i2c peripheral.
235 * this parameter can be one of the following values:
237 * @param address: slave address.
240 void i2c_transfer_addr_set(i2c_type
*i2c_x
, uint16_t address
)
242 i2c_x
->ctrl2_bit
.saddr
= address
& 0x03FF;
246 * @brief get the slave address to be transmitted.
247 * @param i2c_x: to select the i2c peripheral.
248 * this parameter can be one of the following values:
250 * @retval slave address
252 uint16_t i2c_transfer_addr_get(i2c_type
*i2c_x
)
254 return i2c_x
->ctrl2_bit
.saddr
;
258 * @brief config the master transfer direction.
259 * @param i2c_x: to select the i2c peripheral.
260 * this parameter can be one of the following values:
262 * @param i2c_direction: transfer request direction.
263 * this parameter can be one of the following values:
264 * - I2C_DIR_TRANSMIT: master request a write transfer.
265 * - I2C_DIR_RECEIVE: master request a read transfer.
268 void i2c_transfer_dir_set(i2c_type
*i2c_x
, i2c_transfer_dir_type i2c_direction
)
270 i2c_x
->ctrl2_bit
.dir
= i2c_direction
;
274 * @brief slave get the i2c transfer direction.
275 * @param i2c_x: to select the i2c peripheral.
276 * this parameter can be one of the following values:
278 * @retval the value of the slave direction
279 * - I2C_DIR_TRANSMIT: master request a write transfer, slave enters receiver mode.
280 * - I2C_DIR_RECEIVE: master request a read transfer, slave enters transmitter mode.
282 i2c_transfer_dir_type
i2c_transfer_dir_get(i2c_type
*i2c_x
)
284 if (i2c_x
->sts_bit
.sdir
== 0)
286 return I2C_DIR_TRANSMIT
;
290 return I2C_DIR_RECEIVE
;
295 * @brief get the i2c slave matched address.
296 * @param i2c_x: to select the i2c peripheral.
297 * this parameter can be one of the following values:
299 * @retval slave matched address.
301 uint8_t i2c_matched_addr_get(i2c_type
*i2c_x
)
303 return (i2c_x
->sts_bit
.addr
<< 1);
307 * @brief enable or disable auto send stop mode.
308 * @param i2c_x: to select the i2c peripheral.
309 * this parameter can be one of the following values:
311 * @param new_state (TRUE or FALSE).
314 void i2c_auto_stop_enable(i2c_type
*i2c_x
, confirm_state new_state
)
316 i2c_x
->ctrl2_bit
.astopen
= new_state
;
320 * @brief enable or disable cnt reload mode.
321 * @param i2c_x: to select the i2c peripheral.
322 * this parameter can be one of the following values:
324 * @param new_state (TRUE or FALSE).
327 void i2c_reload_enable(i2c_type
*i2c_x
, confirm_state new_state
)
329 i2c_x
->ctrl2_bit
.rlden
= new_state
;
333 * @brief config the transfer cnt .
334 * @param i2c_x: to select the i2c peripheral.
335 * this parameter can be one of the following values:
337 * @param cnt: transfer cnt.
340 void i2c_cnt_set(i2c_type
*i2c_x
, uint8_t cnt
)
342 i2c_x
->ctrl2_bit
.cnt
= cnt
;
346 * @brief enable or disable read 10-bit header, this mode
347 * only used in 10-bit address mode read.
348 * @param i2c_x: to select the i2c peripheral.
349 * this parameter can be one of the following values:
351 * @param new_state (TRUE or FALSE).
354 void i2c_addr10_header_enable(i2c_type
*i2c_x
, confirm_state new_state
)
356 i2c_x
->ctrl2_bit
.readh10
= new_state
;
360 * @brief enable or disable general call mode.
361 * @param i2c_x: to select the i2c peripheral.
362 * this parameter can be one of the following values:
364 * @param new_state (TRUE or FALSE).
367 void i2c_general_call_enable(i2c_type
*i2c_x
, confirm_state new_state
)
369 i2c_x
->ctrl1_bit
.gcaen
= new_state
;
373 * @brief drives the smbus alert pin high or low.
374 * @param i2c_x: to select the i2c peripheral.
375 * this parameter can be one of the following values:
378 * this parameter can be one of the following values:
379 * - I2C_SMBUS_ALERT_LOW: smbus alert set low.
380 * - I2C_SMBUS_ALERT_HIGH: smbus alert set high.
383 void i2c_smbus_alert_set(i2c_type
*i2c_x
, i2c_smbus_alert_set_type level
)
385 i2c_x
->ctrl1_bit
.smbalert
= level
;
389 * @brief enable or disable slave data control.
390 * @param i2c_x: to select the i2c peripheral.
391 * this parameter can be one of the following values:
393 * @param new_state (TRUE or FALSE).
396 void i2c_slave_data_ctrl_enable(i2c_type
*i2c_x
, confirm_state new_state
)
398 i2c_x
->ctrl1_bit
.sctrl
= new_state
;
402 * @brief enable or disable pec calculate.
403 * @param i2c_x: to select the i2c peripheral.
404 * this parameter can be one of the following values:
406 * @param new_state (TRUE or FALSE).
409 void i2c_pec_calculate_enable(i2c_type
*i2c_x
, confirm_state new_state
)
411 i2c_x
->ctrl1_bit
.pecen
= new_state
;
415 * @brief enable or disable pec transfer.
416 * @param i2c_x: to select the i2c peripheral.
417 * this parameter can be one of the following values:
419 * @param new_state (TRUE or FALSE).
422 void i2c_pec_transmit_enable(i2c_type
*i2c_x
, confirm_state new_state
)
424 i2c_x
->ctrl2_bit
.pecten
= new_state
;
428 * @brief get the i2c pec value.
429 * @param i2c_x: to select the i2c peripheral.
430 * this parameter can be one of the following values:
432 * @retval the value of the pec.
434 uint8_t i2c_pec_value_get(i2c_type
*i2c_x
)
436 return (uint8_t)(i2c_x
->pec_bit
.pecval
);
440 * @brief config the i2c bus timeout.
441 * @param i2c_x: to select the i2c peripheral.
442 * this parameter can be one of the following values:
444 * @param timeout: timeout (0x0000~0x0FFF).
447 void i2c_timeout_set(i2c_type
*i2c_x
, uint16_t timeout
)
449 i2c_x
->timeout_bit
.totime
= timeout
;
453 * @brief config the bus timeout detcet level.
454 * @param i2c_x: to select the i2c peripheral.
455 * this parameter can be one of the following values:
458 * this parameter can be one of the following values:
459 * - I2C_TIMEOUT_DETCET_HIGH: detect high level timeout.
460 * - I2C_TIMEOUT_DETCET_LOW: detect low level timeout.
463 void i2c_timeout_detcet_set(i2c_type
*i2c_x
, i2c_timeout_detcet_type mode
)
465 i2c_x
->timeout_bit
.tomode
= mode
;
469 * @brief enable or disable bus timeout.
470 * @param i2c_x: to select the i2c peripheral.
471 * this parameter can be one of the following values:
473 * @param new_state (TRUE or FALSE).
476 void i2c_timeout_enable(i2c_type
*i2c_x
, confirm_state new_state
)
478 i2c_x
->timeout_bit
.toen
= new_state
;
482 * @brief config the i2c extend bus timeout.
483 * @param i2c_x: to select the i2c peripheral.
484 * this parameter can be one of the following values:
486 * @param timeout: extend timeout (0x0000~0x0FFF).
489 void i2c_ext_timeout_set(i2c_type
*i2c_x
, uint16_t timeout
)
491 i2c_x
->timeout_bit
.exttime
= timeout
;
495 * @brief enable or disable extend bus timeout.
496 * @param i2c_x: to select the i2c peripheral.
497 * this parameter can be one of the following values:
499 * @param new_state (TRUE or FALSE).
502 void i2c_ext_timeout_enable(i2c_type
*i2c_x
, confirm_state new_state
)
504 i2c_x
->timeout_bit
.exten
= new_state
;
508 * @brief enable or disable interrupts.
509 * @param i2c_x: to select the i2c peripheral.
510 * this parameter can be one of the following values:
512 * @param i2c_int: interrupts sources.
513 * this parameter can be one of the following values:
514 * - I2C_TD_INT: transmit data interrupt.
515 * - I2C_RD_INT: receive data interrupt.
516 * - I2C_ADDR_INT: address match interrupt.
517 * - I2C_ACKFIAL_INT: ack fail interrupt.
518 * - I2C_STOP_INT: stop detect interrupt.
519 * - I2C_TDC_INT: transmit data complete interrupt.
520 * - I2C_ERR_INT: bus error interrupt.
521 * @param new_state (TRUE or FALSE).
524 void i2c_interrupt_enable(i2c_type
*i2c_x
, uint32_t source
, confirm_state new_state
)
526 if (new_state
!= FALSE
)
528 i2c_x
->ctrl1
|= source
;
532 i2c_x
->ctrl1
&= (uint32_t)~source
;
537 * @brief get interrupt status
538 * @param i2c_x: to select the i2c peripheral.
539 * this parameter can be one of the following values:
542 * this parameter can be one of the following values:
543 * - I2C_TD_INT: transmit data interrupt.
544 * - I2C_RD_INT: receive data interrupt.
545 * - I2C_ADDR_INT: address match interrupt.
546 * - I2C_ACKFIAL_INT: ack fail interrupt.
547 * - I2C_STOP_INT: stop detect interrupt.
548 * - I2C_TDC_INT: transmit data complete interrupt.
549 * - I2C_ERR_INT: bus error interrupt.
550 * @retval flag_status (SET or RESET)
552 flag_status
i2c_interrupt_get(i2c_type
*i2c_x
, uint16_t source
)
554 if((i2c_x
->ctrl1
& source
) != RESET
)
565 * @brief enable or disable dma requests.
566 * @param i2c_x: to select the i2c peripheral.
567 * this parameter can be one of the following values:
569 * @param dma_req: dma transfer request.
570 * this parameter can be one of the following values:
571 * - I2C_DMA_REQUEST_TX: dma transmit request.
572 * - I2C_DMA_REQUEST_RX: dma receive request.
573 * @param new_state (TRUE or FALSE).
576 void i2c_dma_enable(i2c_type
*i2c_x
, i2c_dma_request_type dma_req
, confirm_state new_state
)
578 if(dma_req
== I2C_DMA_REQUEST_TX
)
580 i2c_x
->ctrl1_bit
.dmaten
= new_state
;
584 i2c_x
->ctrl1_bit
.dmaren
= new_state
;
589 * @brief config data transfer.
590 * @param i2c_x: to select the i2c peripheral.
591 * this parameter can be one of the following values:
593 * @param address: slave address.
594 * @param cnt: transfer conuter(0~255)
595 * @param rld_stop: config reload and gen stop condition mode.
596 * this parameter can be one of the following values:
597 * - I2C_AUTO_STOP_MODE: auto generate stop mode.
598 * - I2C_SOFT_STOP_MODE: soft generate stop mode.
599 * - I2C_RELOAD_MODE: reload mode.
600 * @param start: config gen start condition mode.
601 * this parameter can be one of the following values:
602 * - I2C_WITHOUT_START: transfer data without start condition.
603 * - I2C_GEN_START_READ: read data and generate start.
604 * - I2C_GEN_START_WRITE: send data and generate start.
607 void i2c_transmit_set(i2c_type
*i2c_x
, uint16_t address
, uint8_t cnt
, i2c_reload_stop_mode_type rld_stop
, i2c_start_mode_type start
)
611 /* copy ctrl2 value to temp */
614 /* clear ctrl2_bit specific bits */
617 /* transfer mode and address set */
618 temp
|= address
| rld_stop
| start
;
620 /* transfer counter set */
621 temp
|= (uint32_t)cnt
<< 16;
623 /* update ctrl2 value */
628 * @brief generate start condition.
629 * @param i2c_x: to select the i2c peripheral.
630 * this parameter can be one of the following values:
634 void i2c_start_generate(i2c_type
*i2c_x
)
636 i2c_x
->ctrl2_bit
.genstart
= TRUE
;
640 * @brief generate stop condition.
641 * @param i2c_x: to select the i2c peripheral.
642 * this parameter can be one of the following values:
646 void i2c_stop_generate(i2c_type
*i2c_x
)
648 i2c_x
->ctrl2_bit
.genstop
= TRUE
;
652 * @brief send a byte through the i2c periph.
653 * @param i2c_x: to select the i2c peripheral.
654 * this parameter can be one of the following values:
656 * @param data: byte to be transmitted.
659 void i2c_data_send(i2c_type
*i2c_x
, uint8_t data
)
665 * @brief receive a byte through the i2c periph.
666 * @param i2c_x: to select the i2c peripheral.
667 * this parameter can be one of the following values:
669 * @retval the value of the received data.
671 uint8_t i2c_data_receive(i2c_type
*i2c_x
)
673 return (uint8_t)i2c_x
->rxdt
;
677 * @brief get flag status.
678 * @param i2c_x: to select the i2c peripheral.
679 * this parameter can be one of the following values:
681 * @param flag: specifies the flag to check.
682 * this parameter can be one of the following values:
683 * - I2C_TDBE_FLAG: transmit data buffer empty flag.
684 * - I2C_TDIS_FLAG: send interrupt status.
685 * - I2C_RDBF_FLAG: receive data buffer full flag.
686 * - I2C_ADDRF_FLAG: 0~7 bit address match flag.
687 * - I2C_ACKFAIL_FLAG: acknowledge failure flag.
688 * - I2C_STOPF_FLAG: stop condition generation complete flag.
689 * - I2C_TDC_FLAG: transmit data complete flag.
690 * - I2C_TCRLD_FLAG: transmission is complete, waiting to load data.
691 * - I2C_BUSERR_FLAG: bus error flag.
692 * - I2C_ARLOST_FLAG: arbitration lost flag.
693 * - I2C_OUF_FLAG: overflow or underflow flag.
694 * - I2C_PECERR_FLAG: pec receive error flag.
695 * - I2C_TMOUT_FLAG: smbus timeout flag.
696 * - I2C_ALERTF_FLAG: smbus alert flag.
697 * - I2C_BUSYF_FLAG: bus busy flag transmission mode.
698 * - I2C_SDIR_FLAG: slave data transmit direction.
699 * @retval the new state of flag (SET or RESET).
701 flag_status
i2c_flag_get(i2c_type
*i2c_x
, uint32_t flag
)
703 if((i2c_x
->sts
& flag
) != RESET
)
714 * @brief clear flag status
715 * @param i2c_x: to select the i2c peripheral.
716 * this parameter can be one of the following values:
718 * @param flag: specifies the flag to clear.
719 * this parameter can be any combination of the following values:
720 * - I2C_ADDRF_FLAG: 0~7 bit address match flag.
721 * - I2C_ACKFAIL_FLAG: acknowledge failure flag.
722 * - I2C_STOPF_FLAG: stop condition generation complete flag.
723 * - I2C_BUSERR_FLAG: bus error flag.
724 * - I2C_ARLOST_FLAG: arbitration lost flag.
725 * - I2C_OUF_FLAG: overflow or underflow flag.
726 * - I2C_PECERR_FLAG: pec receive error flag.
727 * - I2C_TMOUT_FLAG: smbus timeout flag.
728 * - I2C_ALERTF_FLAG: smbus alert flag.
731 void i2c_flag_clear(i2c_type
*i2c_x
, uint32_t flag
)