【AT32A403A 车规MCU开发板】 硬件I2C驱动OLED显示屏
[复制链接]
测试下使用AT32A403A开发硬件I2C驱动OLED显示屏。
一、I2C主要特点
I2C 总线
― 主机和从机模式
― 多主机功能
― 标准模式(100kHz)和快速模式(400kHz)
― 7-bit和10-bit地址模式
― 广播呼叫模式
― 状态标志
― 错误标志
― 时钟延展功能
― 通讯事件中断
― 错误中断
支持 DMA 传输
支持部分 SMBus2.0 协议
― PEC产生及检查
― SMBus提醒功能
― ARP(地址解析协议)
― 超时机制
PMBus
I2C 总线频率可以最高增加到 1 MHz。
二、硬件部分
2.1、通过AT32 Work Bench软件配置I2C接口
看到芯片有三路I2C接口,测试使用I2C1接口,对应的管脚是PB6和PB7
2.2、电路图硬件
电路图上对应引脚的位置
三、程序部分
3.1、i2c_application.c
- /**
- **************************************************************************
- * [url=home.php?mod=space&uid=1307177]@File[/url] i2c_application.c
- * [url=home.php?mod=space&uid=159083]@brief[/url] the driver library of the i2c peripheral
- **************************************************************************
- * Copyright notice & Disclaimer
- *
- * The software Board Support Package (BSP) that is made available to
- * download from Artery official website is the copyrighted work of Artery.
- * Artery authorizes customers to use, copy, and distribute the BSP
- * software and its related documentation for the purpose of design and
- * development in conjunction with Artery microcontrollers. Use of the
- * software is governed by this copyright notice and the following disclaimer.
- *
- * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
- * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
- * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
- * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
- * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
- *
- **************************************************************************
- */
-
- #include "i2c_application.h"
-
- /** @addtogroup AT32A403A_middlewares_i2c_application_library
- * @{
- */
-
-
- /**
- * @brief get the dma transfer complete flag through the channel
- */
- #define DMA_GET_TC_FLAG(DMA_CHANNEL) \
- (((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL1))? DMA1_FDT1_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL2))? DMA1_FDT2_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL3))? DMA1_FDT3_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL4))? DMA1_FDT4_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL5))? DMA1_FDT5_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL6))? DMA1_FDT6_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL7))? DMA1_FDT7_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL1))? DMA2_FDT1_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL2))? DMA2_FDT2_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL3))? DMA2_FDT3_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL4))? DMA2_FDT4_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL5))? DMA2_FDT5_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL6))? DMA2_FDT6_FLAG : \
- DMA2_FDT7_FLAG)
-
- /**
- * @brief get the dma half transfer flag through the channel
- */
- #define DMA_GET_HT_FLAG(DMA_CHANNEL) \
- (((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL1))? DMA1_HDT1_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL2))? DMA1_HDT2_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL3))? DMA1_HDT3_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL4))? DMA1_HDT4_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL5))? DMA1_HDT5_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL6))? DMA1_HDT6_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL7))? DMA1_HDT7_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL1))? DMA2_HDT1_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL2))? DMA2_HDT2_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL3))? DMA2_HDT3_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL4))? DMA2_HDT4_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL5))? DMA2_HDT5_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL6))? DMA2_HDT6_FLAG : \
- DMA2_HDT7_FLAG)
-
- /**
- * @brief get the dma transfer error flag through the channel
- */
- #define DMA_GET_TERR_FLAG(DMA_CHANNEL) \
- (((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL1))? DMA1_DTERR1_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL2))? DMA1_DTERR2_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL3))? DMA1_DTERR3_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL4))? DMA1_DTERR4_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL5))? DMA1_DTERR5_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL6))? DMA1_DTERR6_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA1_CHANNEL7))? DMA1_DTERR7_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL1))? DMA2_DTERR1_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL2))? DMA2_DTERR2_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL3))? DMA2_DTERR3_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL4))? DMA2_DTERR4_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL5))? DMA2_DTERR5_FLAG : \
- ((uint32_t)(DMA_CHANNEL) == ((uint32_t)DMA2_CHANNEL6))? DMA2_DTERR6_FLAG : \
- DMA2_DTERR7_FLAG)
-
- /**
- * @brief i2c transmission status
- */
- #define I2C_START 0
- #define I2C_END 1
-
- /**
- * @brief initializes peripherals used by the i2c.
- * @param none
- * @retval none
- */
- __WEAK void i2c_lowlevel_init(i2c_handle_type* hi2c)
- {
-
- }
-
- /**
- * @brief i2c peripheral initialization.
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_config(i2c_handle_type* hi2c)
- {
-
- i2c_reset(hi2c->i2cx);
-
-
- i2c_lowlevel_init(hi2c);
-
-
- i2c_enable(hi2c->i2cx, TRUE);
- }
-
- /**
- * @brief wait for the transfer to end.
- * @param hi2c: the handle points to the operation information.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_wait_end(i2c_handle_type* hi2c, uint32_t timeout)
- {
- while(hi2c->status != I2C_END)
- {
-
- if((timeout--) == 0)
- {
- return I2C_ERR_TIMEOUT;
- }
- }
-
- if(hi2c->error_code != I2C_OK)
- {
- return hi2c->error_code;
- }
- else
- {
- return I2C_OK;
- }
- }
-
- /**
- * @brief wait for the flag to be set or reset, only BUSYF flag
- * is waiting to be reset, and other flags are waiting to be set
- * @param hi2c: the handle points to the operation information.
- * @param flag: specifies the flag to check.
- * this parameter can be one of the following values:
- * - I2C_STARTF_FLAG: start condition generation complete flag.
- * - I2C_ADDR7F_FLAG: 0~7 bit address match flag.
- * - I2C_TDC_FLAG: transmit data complete flag.
- * - I2C_ADDRHF_FLAG: master 9~8 bit address header match flag.
- * - I2C_STOPF_FLAG: stop condition generation complete flag.
- * - I2C_RDBF_FLAG: receive data buffer full flag.
- * - I2C_TDBE_FLAG: transmit data buffer empty flag.
- * - I2C_BUSERR_FLAG: bus error flag.
- * - I2C_ARLOST_FLAG: arbitration lost flag.
- * - I2C_ACKFAIL_FLAG: acknowledge failure flag.
- * - I2C_OUF_FLAG: overflow or underflow flag.
- * - I2C_PECERR_FLAG: pec receive error flag.
- * - I2C_TMOUT_FLAG: smbus timeout flag.
- * - I2C_ALERTF_FLAG: smbus alert flag.
- * - I2C_TRMODE_FLAG: transmission mode.
- * - I2C_BUSYF_FLAG: bus busy flag transmission mode.
- * - I2C_DIRF_FLAG: transmission direction flag.
- * - I2C_GCADDRF_FLAG: general call address received flag.
- * - I2C_DEVADDRF_FLAG: smbus device address received flag.
- * - I2C_HOSTADDRF_FLAG: smbus host address received flag.
- * - I2C_ADDR2_FLAG: own address 2 received flag.
- * @param event_check: check other error flags while waiting for the flag.
- * parameter as following values:
- * - I2C_EVENT_CHECK_NONE
- * - I2C_EVENT_CHECK_ACKFAIL
- * - I2C_EVENT_CHECK_STOP
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_wait_flag(i2c_handle_type* hi2c, uint32_t flag, uint32_t event_check, uint32_t timeout)
- {
- if(flag == I2C_BUSYF_FLAG)
- {
- while(i2c_flag_get(hi2c->i2cx, flag) != RESET)
- {
-
- if((timeout--) == 0)
- {
- hi2c->error_code = I2C_ERR_TIMEOUT;
-
- return I2C_ERR_TIMEOUT;
- }
- }
- }
- else
- {
- while(i2c_flag_get(hi2c->i2cx, flag) == RESET)
- {
-
- if(event_check & I2C_EVENT_CHECK_ACKFAIL)
- {
- if(i2c_flag_get(hi2c->i2cx, I2C_ACKFAIL_FLAG) != RESET)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ACKFAIL_FLAG);
-
- hi2c->error_code = I2C_ERR_ACKFAIL;
-
- return I2C_ERR_ACKFAIL;
- }
- }
-
-
- if(event_check & I2C_EVENT_CHECK_STOP)
- {
- if(i2c_flag_get(hi2c->i2cx, I2C_STOPF_FLAG) != RESET)
- {
-
- i2c_flag_clear(hi2c->i2cx, I2C_STOPF_FLAG);
-
- hi2c->error_code = I2C_ERR_STOP;
-
- return I2C_ERR_STOP;
- }
- }
-
-
- if((timeout--) == 0)
- {
- hi2c->error_code = I2C_ERR_TIMEOUT;
-
- return I2C_ERR_TIMEOUT;
- }
- }
- }
-
- return I2C_OK;
- }
-
- /**
- * @brief dma transfer cofiguration.
- * @param hi2c: the handle points to the operation information.
- * @param dma_channelx: dma channel to be cofigured.
- * @param pdata: data buffer.
- * @param size: data size.
- * @retval none.
- */
- void i2c_dma_config(i2c_handle_type* hi2c, dma_channel_type* dma_channelx, uint8_t* pdata, uint16_t size)
- {
-
- dma_channel_enable(dma_channelx, FALSE);
-
-
- dma_interrupt_enable(dma_channelx, DMA_FDT_INT , FALSE);
-
-
- hi2c->dma_init_struct.memory_base_addr = (uint32_t)pdata;
- hi2c->dma_init_struct.direction = (dma_channelx == hi2c->dma_tx_channel) ? DMA_DIR_MEMORY_TO_PERIPHERAL : DMA_DIR_PERIPHERAL_TO_MEMORY;
- hi2c->dma_init_struct.peripheral_base_addr = (uint32_t)&hi2c->i2cx->dt;
- hi2c->dma_init_struct.buffer_size = (uint32_t)size;
- dma_init(dma_channelx, &hi2c->dma_init_struct);
-
-
- dma_interrupt_enable(dma_channelx, DMA_FDT_INT , TRUE);
-
-
- dma_channel_enable(dma_channelx, TRUE);
- }
-
- /**
- * @brief send address in master transmits mode.
- * @param hi2c: the handle points to the operation information.
- * @param address: slave address.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_master_write_addr(i2c_handle_type *hi2c, uint16_t address, uint32_t timeout)
- {
-
- i2c_start_generate(hi2c->i2cx);
-
-
- if(i2c_wait_flag(hi2c, I2C_STARTF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- hi2c->error_code = I2C_ERR_START;
-
- return I2C_ERR_START;
- }
-
- if(hi2c->i2cx->oaddr1_bit.addr1mode == I2C_ADDRESS_MODE_7BIT)
- {
-
- i2c_7bit_address_send(hi2c->i2cx, address, I2C_DIRECTION_TRANSMIT);
- }
- else
- {
-
- i2c_data_send(hi2c->i2cx, (uint8_t)((address & 0x0300) >> 7) | 0xF0);
-
-
- if(i2c_wait_flag(hi2c, I2C_ADDRHF_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
- hi2c->error_code = I2C_ERR_ADDR10;
-
- return I2C_ERR_ADDR10;
- }
-
-
- i2c_data_send(hi2c->i2cx, (uint8_t)(address & 0x00FF));
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
- hi2c->error_code = I2C_ERR_ADDR;
-
- return I2C_ERR_ADDR;
- }
-
- return I2C_OK;
- }
-
- /**
- * @brief send address in master receive mode.
- * @param hi2c: the handle points to the operation information.
- * @param address: slave address.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_master_read_addr(i2c_handle_type *hi2c, uint16_t address, uint32_t timeout)
- {
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_start_generate(hi2c->i2cx);
-
-
- if(i2c_wait_flag(hi2c, I2C_STARTF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- hi2c->error_code = I2C_ERR_START;
-
- return I2C_ERR_START;
- }
-
- if(hi2c->i2cx->oaddr1_bit.addr1mode == I2C_ADDRESS_MODE_7BIT)
- {
-
- i2c_7bit_address_send(hi2c->i2cx, address, I2C_DIRECTION_RECEIVE);
- }
- else
- {
-
- i2c_data_send(hi2c->i2cx, (uint8_t)((address & 0x0300) >> 7) | 0xF0);
-
-
- if(i2c_wait_flag(hi2c, I2C_ADDRHF_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
- hi2c->error_code = I2C_ERR_ADDR10;
-
- return I2C_ERR_ADDR10;
- }
-
-
- i2c_data_send(hi2c->i2cx, (uint8_t)(address & 0x00FF));
-
-
- if(i2c_wait_flag(hi2c, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
- hi2c->error_code = I2C_ERR_ADDR;
-
- return I2C_ERR_ADDR;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_start_generate(hi2c->i2cx);
-
-
- if(i2c_wait_flag(hi2c, I2C_STARTF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- hi2c->error_code = I2C_ERR_START;
-
- return I2C_ERR_START;
- }
-
-
- i2c_data_send(hi2c->i2cx, (uint8_t)((address & 0x0300) >> 7) | 0xF1);
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
- hi2c->error_code = I2C_ERR_ADDR;
-
- return I2C_ERR_ADDR;
- }
-
- return I2C_OK;
- }
-
- /**
- * @brief the master transmits data through polling mode.
- * @param hi2c: the handle points to the operation information.
- * @param address: slave address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_master_transmit(i2c_handle_type* hi2c, uint16_t address, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
- while(size > 0)
- {
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_3;
- }
-
-
- i2c_data_send(hi2c->i2cx, (*pdata++));
- size--;
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_4;
- }
-
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_OK;
- }
-
- /**
- * @brief the slave receive data through polling mode.
- * @param hi2c: the handle points to the operation information.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_slave_receive(i2c_handle_type* hi2c, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- if(i2c_wait_flag(hi2c, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
- while(size > 0)
- {
-
- if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_STOP, timeout) != I2C_OK)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
- return I2C_ERR_STEP_3;
- }
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_STOPF_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
- return I2C_ERR_STEP_4;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_STOPF_FLAG);
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
- return I2C_OK;
- }
-
- /**
- * @brief the master receive data through polling mode.
- * @param hi2c: the handle points to the operation information.
- * @param address: slave address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_master_receive(i2c_handle_type* hi2c, uint16_t address, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
- if(size == 1)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_stop_generate(hi2c->i2cx);
- }
- else if(size == 2)
- {
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_NEXT);
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
- else
- {
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
- while(size > 0)
- {
- if(size <= 3)
- {
-
- if(size == 1)
- {
-
- if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_3;
- }
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
- }
-
- else if(size == 2)
- {
-
- if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_4;
- }
-
-
- i2c_stop_generate(hi2c->i2cx);
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
- }
-
- else
- {
-
- if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_5;
- }
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
-
-
- if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_6;
- }
-
-
- i2c_stop_generate(hi2c->i2cx);
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
- }
- }
- else
- {
-
- if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_7;
- }
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
- }
- }
-
- return I2C_OK;
- }
-
- /**
- * @brief the slave transmits data through polling mode.
- * @param hi2c: the handle points to the operation information.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_slave_transmit(i2c_handle_type* hi2c, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- if(i2c_wait_flag(hi2c, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
- if(hi2c->i2cx->oaddr1_bit.addr1mode == I2C_ADDRESS_MODE_10BIT)
- {
-
- if(i2c_wait_flag(hi2c, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
- return I2C_ERR_STEP_3;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
- while(size > 0)
- {
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
- return I2C_ERR_STEP_4;
- }
-
-
- i2c_data_send(hi2c->i2cx, *pdata++);
- size--;
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_ACKFAIL_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_5;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ACKFAIL_FLAG);
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
- return I2C_OK;
- }
-
- /**
- * @brief the master transmits data through interrupt mode.
- * @param hi2c: the handle points to the operation information.
- * @param address: slave address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_master_transmit_int(i2c_handle_type* hi2c, uint16_t address, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->mode = I2C_INT_MA_TX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief the slave receive data through interrupt mode.
- * @param hi2c: the handle points to the operation information.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_slave_receive_int(i2c_handle_type* hi2c, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->mode = I2C_INT_SLA_RX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief the master receive data through interrupt mode.
- * @param hi2c: the handle points to the operation information.
- * @param address: slave address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_master_receive_int(i2c_handle_type* hi2c, uint16_t address, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->mode = I2C_INT_MA_RX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
- if(hi2c->pcount == 1)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_stop_generate(hi2c->i2cx);
- }
- else if(hi2c->pcount == 2)
- {
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_NEXT);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
- }
- else
- {
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief the slave transmits data through interrupt mode.
- * @param hi2c: the handle points to the operation information.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_slave_transmit_int(i2c_handle_type* hi2c, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->mode = I2C_INT_SLA_TX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief the master transmits data through dma mode.
- * @param hi2c: the handle points to the operation information.
- * @param address: slave address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_master_transmit_dma(i2c_handle_type* hi2c, uint16_t address, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->mode = I2C_DMA_MA_TX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_dma_enable(hi2c->i2cx, FALSE);
-
-
- i2c_dma_config(hi2c, hi2c->dma_tx_channel, pdata, size);
-
-
- if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_dma_enable(hi2c->i2cx, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief the slave receive data through dma mode.
- * @param hi2c: the handle points to the operation information.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_slave_receive_dma(i2c_handle_type* hi2c, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->mode = I2C_DMA_SLA_RX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_dma_enable(hi2c->i2cx, FALSE);
-
-
- i2c_dma_config(hi2c, hi2c->dma_rx_channel, pdata, size);
-
-
- i2c_dma_enable(hi2c->i2cx, TRUE);
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief the master receive data through dma mode.
- * @param hi2c: the handle points to the operation information.
- * @param address: slave address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_master_receive_dma(i2c_handle_type* hi2c, uint16_t address, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->mode = I2C_DMA_MA_RX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_dma_enable(hi2c->i2cx, FALSE);
-
-
- i2c_dma_config(hi2c, hi2c->dma_rx_channel, pdata, size);
-
-
- if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
- if(size == 1)
- {
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- i2c_stop_generate(hi2c->i2cx);
-
-
- i2c_dma_enable(hi2c->i2cx, TRUE);
- }
- else
- {
-
- i2c_dma_end_transfer_set(hi2c->i2cx, TRUE);
-
-
- i2c_dma_enable(hi2c->i2cx, TRUE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
- return I2C_OK;
- }
-
- /**
- * @brief the slave transmits data through dma mode.
- * @param hi2c: the handle points to the operation information.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type i2c_slave_transmit_dma(i2c_handle_type* hi2c, uint8_t* pdata, uint16_t size, uint32_t timeout)
- {
-
- hi2c->mode = I2C_DMA_SLA_TX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_dma_enable(hi2c->i2cx, FALSE);
-
-
- i2c_dma_config(hi2c, hi2c->dma_tx_channel, pdata, size);
-
-
- i2c_dma_enable(hi2c->i2cx, TRUE);
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief send memory address.
- * @param hi2c: the handle points to the operation information.
- * @param mem_address_width: memory address width.
- * this parameter can be one of the following values:
- * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
- * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
- * @param address: memory device address.
- * @param mem_address: memory address.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- 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)
- {
- i2c_status_type err_code;
-
- if(mem_address_width == I2C_MEM_ADDR_WIDIH_8)
- {
-
- i2c_data_send(hi2c->i2cx, mem_address & 0xFF);
- }
- else
- {
-
- i2c_data_send(hi2c->i2cx, (mem_address >> 8) & 0xFF);
-
-
- err_code = i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout);
-
- if(err_code != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return err_code;
- }
-
-
- i2c_data_send(hi2c->i2cx, mem_address & 0xFF);
- }
-
- return I2C_OK;
- }
-
- /**
- * @brief write data to the memory device through polling mode.
- * @param hi2c: the handle points to the operation information.
- * @param mem_address_width: memory address width.
- * this parameter can be one of the following values:
- * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
- * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
- * @param address: memory device address.
- * @param mem_address: memory address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- 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)
- {
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_3;
- }
-
-
- if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_4;
- }
-
- while(size > 0)
- {
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_5;
- }
-
-
- i2c_data_send(hi2c->i2cx, (*pdata++));
- size--;
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_6;
- }
-
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_OK;
- }
-
- /**
- * @brief read data from memory device through polling mode.
- * @param hi2c: the handle points to the operation information.
- * @param mem_address_width: memory address width.
- * this parameter can be one of the following values:
- * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
- * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
- * @param address: memory device address.
- * @param mem_address: memory address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- 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)
- {
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_3;
- }
-
-
- if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_4;
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_5;
- }
-
-
- if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_6;
- }
-
- if(size == 1)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_stop_generate(hi2c->i2cx);
- }
- else if(size == 2)
- {
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_NEXT);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
- }
- else
- {
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
- while(size > 0)
- {
- if(size <= 3)
- {
-
- if(size == 1)
- {
-
- if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_7;
- }
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
- }
-
- else if(size == 2)
- {
-
- if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_8;
- }
-
-
- i2c_stop_generate(hi2c->i2cx);
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
- }
-
- else
- {
-
- if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_9;
- }
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
-
-
- if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_10;
- }
-
-
- i2c_stop_generate(hi2c->i2cx);
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
- }
- }
- else
- {
-
- if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_11;
- }
-
-
- (*pdata++) = i2c_data_receive(hi2c->i2cx);
- size--;
- }
- }
-
- return I2C_OK;
- }
-
- /**
- * @brief write data to the memory device through interrupt mode.
- * @param hi2c: the handle points to the operation information.
- * @param mem_address_width: memory address width.
- * this parameter can be one of the following values:
- * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
- * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
- * @param address: memory device address.
- * @param mem_address: memory address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- 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)
- {
-
- hi2c->mode = I2C_INT_MA_TX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_3;
- }
-
-
- if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_4;
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_5;
- }
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief read data from memory device through interrupt mode.
- * @param hi2c: the handle points to the operation information.
- * @param mem_address_width: memory address width.
- * this parameter can be one of the following values:
- * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
- * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
- * @param address: memory device address.
- * @param mem_address: memory address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- 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)
- {
-
- hi2c->mode = I2C_INT_MA_RX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_3;
- }
-
-
- if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_4;
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_5;
- }
-
-
- if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_6;
- }
-
- if(hi2c->pcount == 1)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_stop_generate(hi2c->i2cx);
- }
- else if(hi2c->pcount == 2)
- {
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_NEXT);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
- }
- else
- {
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief write data to the memory device through dma mode.
- * @param hi2c: the handle points to the operation information.
- * @param mem_address_width: memory address width.
- * this parameter can be one of the following values:
- * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
- * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
- * @param address: memory device address.
- * @param mem_address: memory address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- 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)
- {
-
- hi2c->mode = I2C_DMA_MA_TX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_dma_enable(hi2c->i2cx, FALSE);
-
-
- i2c_dma_config(hi2c, hi2c->dma_tx_channel, pdata, size);
-
-
- if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_3;
- }
-
-
- if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_4;
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_5;
- }
-
-
- i2c_dma_enable(hi2c->i2cx, TRUE);
-
- return I2C_OK;
- }
-
- /**
- * @brief read data from memory device through polling mode.
- * @param hi2c: the handle points to the operation information.
- * @param mem_address_width: memory address width.
- * this parameter can be one of the following values:
- * - I2C_MEM_ADDR_WIDIH_8: memory address is 8 bit
- * - I2C_MEM_ADDR_WIDIH_16: memory address is 16 bit
- * @param address: memory device address.
- * @param mem_address: memory address.
- * @param pdata: data buffer.
- * @param size: data size.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- 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)
- {
-
- hi2c->mode = I2C_DMA_MA_RX;
- hi2c->status = I2C_START;
-
- hi2c->pbuff = pdata;
- hi2c->pcount = size;
-
- hi2c->timeout = timeout;
- hi2c->error_code = I2C_OK;
-
-
- if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_1;
- }
-
-
- i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- i2c_dma_enable(hi2c->i2cx, FALSE);
-
-
- i2c_dma_config(hi2c, hi2c->dma_rx_channel, pdata, size);
-
-
- if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_2;
- }
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_3;
- }
-
-
- if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
- {
- return I2C_ERR_STEP_4;
- }
-
-
- if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_5;
- }
-
-
- if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
- return I2C_ERR_STEP_6;
- }
-
- if(size == 1)
- {
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
-
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- i2c_stop_generate(hi2c->i2cx);
-
-
- i2c_dma_enable(hi2c->i2cx, TRUE);
- }
- else
- {
-
- i2c_dma_end_transfer_set(hi2c->i2cx, TRUE);
-
-
- i2c_dma_enable(hi2c->i2cx, TRUE);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
- return I2C_OK;
- }
-
- /**
- * @brief master transfer mode interrupt procession function
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_master_tx_isr_int(i2c_handle_type* hi2c)
- {
-
- if(i2c_flag_get(hi2c->i2cx, I2C_TDBE_FLAG) != RESET)
- {
- if(hi2c->pcount == 0)
- {
-
- hi2c->status = I2C_END;
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, FALSE);
-
-
- i2c_stop_generate(hi2c->i2cx);
- }
- else
- {
-
- i2c_data_send(hi2c->i2cx, *hi2c->pbuff++);
- hi2c->pcount--;
- }
- }
- }
-
- /**
- * @brief master receive mode interrupt procession function
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_master_rx_isr_int(i2c_handle_type* hi2c)
- {
- if(i2c_flag_get(hi2c->i2cx, I2C_TDC_FLAG) != RESET)
- {
- if(hi2c->pcount == 3)
- {
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
- hi2c->pcount--;
- }
- else if(hi2c->pcount == 2)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
-
- (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
- hi2c->pcount--;
-
-
- (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
- hi2c->pcount--;
-
-
- hi2c->status = I2C_END;
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, FALSE);
- }
- else
- {
-
- (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
- hi2c->pcount--;
- }
- }
-
- else if(i2c_flag_get(hi2c->i2cx, I2C_RDBF_FLAG) != RESET)
- {
- if(hi2c->pcount > 3)
- {
-
- (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
- hi2c->pcount--;
- }
- else if((hi2c->pcount == 3) || (hi2c->pcount == 2))
- {
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_DATA_INT, FALSE);
- }
- else
- {
-
- (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
- hi2c->pcount--;
-
-
- hi2c->status = I2C_END;
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, FALSE);
- }
- }
- }
-
- /**
- * @brief slave transfer mode interrupt procession function
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_slave_tx_isr_int(i2c_handle_type* hi2c)
- {
-
- if(i2c_flag_get(hi2c->i2cx, I2C_ADDR7F_FLAG) != RESET)
- {
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
-
- else if(i2c_flag_get(hi2c->i2cx, I2C_TDBE_FLAG) != RESET)
- {
- if(hi2c->pcount)
- {
-
- i2c_data_send(hi2c->i2cx, (*hi2c->pbuff++));
- hi2c->pcount--;
-
- if(hi2c->pcount == 0)
- {
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_DATA_INT | I2C_EVT_INT | I2C_ERR_INT, FALSE);
-
-
- hi2c->status = i2c_wait_flag(hi2c, I2C_ACKFAIL_FLAG, I2C_EVENT_CHECK_NONE, hi2c->timeout);
-
-
- i2c_flag_clear(hi2c->i2cx, I2C_ACKFAIL_FLAG);
-
-
- hi2c->status = I2C_END;
- }
- }
- }
- }
-
- /**
- * @brief slave receive mode interrupt procession function
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_slave_rx_isr_int(i2c_handle_type* hi2c)
- {
-
- if(i2c_flag_get(hi2c->i2cx, I2C_ADDR7F_FLAG) != RESET)
- {
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
-
- else if(i2c_flag_get(hi2c->i2cx, I2C_RDBF_FLAG) != RESET)
- {
- if(hi2c->pcount)
- {
-
- (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
-
- hi2c->pcount--;
- }
- }
-
-
- else if(i2c_flag_get(hi2c->i2cx, I2C_STOPF_FLAG) != RESET)
- {
-
- i2c_flag_clear(hi2c->i2cx, I2C_STOPF_FLAG);
-
-
- hi2c->status = I2C_END;
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, FALSE);
- }
- }
-
- /**
- * @brief master interrupt processing function in dma mode.
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_master_tx_isr_dma(i2c_handle_type* hi2c)
- {
-
- if(i2c_flag_get(hi2c->i2cx, I2C_TDC_FLAG) != RESET)
- {
-
- i2c_stop_generate(hi2c->i2cx);
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, FALSE);
-
-
- hi2c->status = I2C_END;
- }
- }
-
- /**
- * @brief slave interrupt processing function in dma mode.
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_slave_tx_rx_isr_dma(i2c_handle_type* hi2c)
- {
-
- if(i2c_flag_get(hi2c->i2cx, I2C_ADDR7F_FLAG) != RESET)
- {
-
- i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
- }
-
-
- if(i2c_flag_get(hi2c->i2cx, I2C_STOPF_FLAG) != RESET)
- {
-
- i2c_flag_clear(hi2c->i2cx, I2C_STOPF_FLAG);
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, FALSE);
-
-
- i2c_ack_enable(hi2c->i2cx, TRUE);
-
-
- hi2c->status = I2C_END;
- }
- }
-
- /**
- * @brief interrupt procession function.
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_evt_irq_handler(i2c_handle_type* hi2c)
- {
- switch(hi2c->mode)
- {
- case I2C_INT_MA_TX:
- i2c_master_tx_isr_int(hi2c);
- break;
- case I2C_INT_MA_RX:
- i2c_master_rx_isr_int(hi2c);
- break;
- case I2C_INT_SLA_TX:
- i2c_slave_tx_isr_int(hi2c);
- break;
- case I2C_INT_SLA_RX:
- i2c_slave_rx_isr_int(hi2c);
- break;
- case I2C_DMA_MA_TX:
- i2c_master_tx_isr_dma(hi2c);
- break;
- case I2C_DMA_SLA_TX:
- case I2C_DMA_SLA_RX:
- i2c_slave_tx_rx_isr_dma(hi2c);
- break;
- default:
- break;
- }
- }
-
- /**
- * @brief dma transmission complete interrupt function.
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_dma_tx_irq_handler(i2c_handle_type* hi2c)
- {
-
- if(dma_flag_get(DMA_GET_TC_FLAG(hi2c->dma_tx_channel)) != RESET)
- {
-
- dma_interrupt_enable(hi2c->dma_tx_channel, DMA_FDT_INT, FALSE);
-
-
- dma_flag_clear(DMA_GET_TC_FLAG(hi2c->dma_tx_channel));
-
-
- i2c_dma_enable(hi2c->i2cx, FALSE);
-
- hi2c->pcount = 0;
-
- switch(hi2c->mode)
- {
- case I2C_DMA_MA_TX:
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, TRUE);
- break;
- case I2C_DMA_SLA_TX:
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_ERR_INT, TRUE);
- break;
- default:
- break;
- }
- }
- }
-
- /**
- * @brief dma reveive complete interrupt function.
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_dma_rx_irq_handler(i2c_handle_type* hi2c)
- {
-
- if(dma_flag_get(DMA_GET_TC_FLAG(hi2c->dma_rx_channel)) != RESET)
- {
-
- dma_interrupt_enable(hi2c->dma_rx_channel, DMA_FDT_INT, FALSE);
-
-
- dma_flag_clear(DMA_GET_TC_FLAG(hi2c->dma_rx_channel));
-
-
- i2c_dma_enable(hi2c->i2cx, FALSE);
-
- hi2c->pcount = 0;
-
- switch(hi2c->mode)
- {
- case I2C_DMA_MA_RX:
-
- i2c_flag_clear(hi2c->i2cx, I2C_ACKFAIL_FLAG);
-
- if(hi2c->pcount != 1)
- {
-
- i2c_stop_generate(hi2c->i2cx);
- }
-
-
- hi2c->status = I2C_END;
- break;
- case I2C_DMA_SLA_RX:
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, TRUE);
- break;
- default:
- break;
- }
- }
- }
-
- /**
- * @brief i2c error interrupt function.
- * @param hi2c: the handle points to the operation information.
- * @retval none.
- */
- void i2c_err_irq_handler(i2c_handle_type* hi2c)
- {
-
- if(i2c_flag_get(hi2c->i2cx, I2C_BUSERR_FLAG) != RESET)
- {
- i2c_flag_clear(hi2c->i2cx, I2C_BUSERR_FLAG);
-
- hi2c->error_code = I2C_ERR_INTERRUPT;
- }
-
-
- if(i2c_flag_get(hi2c->i2cx, I2C_ARLOST_FLAG) != RESET)
- {
- i2c_flag_clear(hi2c->i2cx, I2C_ARLOST_FLAG);
-
- hi2c->error_code = I2C_ERR_INTERRUPT;
- }
-
-
- if(i2c_flag_get(hi2c->i2cx, I2C_ACKFAIL_FLAG) != RESET)
- {
- i2c_flag_clear(hi2c->i2cx, I2C_ACKFAIL_FLAG);
-
- switch(hi2c->mode)
- {
- case I2C_DMA_SLA_TX:
-
- i2c_ack_enable(hi2c->i2cx, FALSE);
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, FALSE);
-
-
- hi2c->status = I2C_END;
- break;
- default:
- hi2c->error_code = I2C_ERR_INTERRUPT;
- break;
- }
- }
-
-
- if(i2c_flag_get(hi2c->i2cx, I2C_OUF_FLAG) != RESET)
- {
- i2c_flag_clear(hi2c->i2cx, I2C_OUF_FLAG);
-
- hi2c->error_code = I2C_ERR_INTERRUPT;
- }
-
-
- if(i2c_flag_get(hi2c->i2cx, I2C_PECERR_FLAG) != RESET)
- {
- i2c_flag_clear(hi2c->i2cx, I2C_PECERR_FLAG);
-
- hi2c->error_code = I2C_ERR_INTERRUPT;
- }
-
-
- if(i2c_flag_get(hi2c->i2cx, I2C_TMOUT_FLAG) != RESET)
- {
- i2c_flag_clear(hi2c->i2cx, I2C_TMOUT_FLAG);
-
- hi2c->error_code = I2C_ERR_INTERRUPT;
- }
-
-
- if(i2c_flag_get(hi2c->i2cx, I2C_ALERTF_FLAG) != RESET)
- {
- i2c_flag_clear(hi2c->i2cx, I2C_ALERTF_FLAG);
-
- hi2c->error_code = I2C_ERR_INTERRUPT;
- }
-
-
- i2c_interrupt_enable(hi2c->i2cx, I2C_ERR_INT, FALSE);
- }
-
- /**
- * @}
- */
-
3.2、i2c.c
初始化I2C端口
- #include "main.h"
-
- i2c_handle_type hi2cx;
-
- void i2c_lowlevel_init(i2c_handle_type* hi2c);
-
- /**
- * @brief initializes peripherals used by the i2c.
- * @param none
- * @retval none
- */
- void i2c_lowlevel_init(i2c_handle_type* hi2c)
- {
- gpio_init_type gpio_initstructure;
-
- if(hi2c->i2cx == I2Cx_PORT)
- {
-
- crm_periph_clock_enable(I2Cx_CLK, TRUE);
- crm_periph_clock_enable(I2Cx_SCL_GPIO_CLK, TRUE);
- crm_periph_clock_enable(I2Cx_SDA_GPIO_CLK, TRUE);
-
-
- gpio_initstructure.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
- gpio_initstructure.gpio_pull = GPIO_PULL_UP;
- gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
- gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
-
-
- gpio_initstructure.gpio_pins = I2Cx_SCL_PIN;
- gpio_init(I2Cx_SCL_GPIO_PORT, &gpio_initstructure);
-
-
- gpio_initstructure.gpio_pins = I2Cx_SDA_PIN;
- gpio_init(I2Cx_SDA_GPIO_PORT, &gpio_initstructure);
-
- i2c_init(hi2c->i2cx, I2C_FSMODE_DUTY_2_1, I2Cx_SPEED);
-
- i2c_own_address1_set(hi2c->i2cx, I2C_ADDRESS_MODE_7BIT, I2Cx_ADDRESS);
- }
- }
-
- void init_i2c(void){
- hi2cx.i2cx = I2Cx_PORT;
- i2c_config(&hi2cx);
- }
-
- void i2c_senddat(uint16_t address, uint8_t* pdata, uint16_t size)
- {
- i2c_master_transmit(&hi2cx, address, pdata, size, I2C_TIMEOUT);
-
- }
-
-
3.3、oled.c
- #include "main.h"
- #include "oled/oled.h"
- #include "oled/codetab.h"
-
- void I2C_WriteByte(uint8_t addr, uint8_t dat)
- {
- uint8_t temp[2];
- temp[0] = addr;
- temp[1] = dat;
- i2c_senddat(OLED_ADDRESS, temp, 2);
- }
-
-
- void WriteCmd(unsigned char I2C_Command)
- {
- I2C_WriteByte(0x00, I2C_Command);
- }
-
- void WriteDat(unsigned char I2C_Data)
- {
- I2C_WriteByte(0x40, I2C_Data);
- }
-
- void OLED_WrCmd(unsigned char IIC_Command)
- {
- WriteCmd(IIC_Command);
- }
-
- void OLED_Init(void)
- {
- delay_ms(100);
- WriteCmd(0xAE);
- WriteCmd(0x20);
- WriteCmd(0x10);
- WriteCmd(0xb0);
- WriteCmd(0xc8);
- WriteCmd(0x00);
- WriteCmd(0x10);
- WriteCmd(0x40);
- WriteCmd(0x81);
- WriteCmd(0xff);
- WriteCmd(0xa1);
- WriteCmd(0xa6);
- WriteCmd(0xa8);
- WriteCmd(0x3F);
- WriteCmd(0xa4);
- WriteCmd(0xd3);
- WriteCmd(0x00);
- WriteCmd(0xd5);
- WriteCmd(0xf0);
- WriteCmd(0xd9);
- WriteCmd(0x22);
- WriteCmd(0xda);
- WriteCmd(0x12);
- WriteCmd(0xdb);
- WriteCmd(0x40);
- WriteCmd(0x8d);
- WriteCmd(0x14);
- WriteCmd(0xaf);
- }
-
- void OLED_SetPos(unsigned char x, unsigned char y)
- {
- WriteCmd(0xb0+y);
- WriteCmd(((x&0xf0)>>4)|0x10);
- WriteCmd((x&0x0f)|0x01);
- }
-
- void OLED_Fill(unsigned char fill_Data)
- {
- unsigned char m,n;
- for(m=0;m<8;m++)
- {
- WriteCmd(0xb0+m);
- WriteCmd(0x00);
- WriteCmd(0x10);
- for(n=0;n<128;n++)
- {
- WriteDat(fill_Data);
- }
- }
- }
-
- void OLED_CLS(void)
- {
- OLED_Fill(0xff);
- }
-
- void OLED_ON(void)
- {
- WriteCmd(0X8D);
- WriteCmd(0X14);
- WriteCmd(0XAF);
- }
-
- void OLED_OFF(void)
- {
- WriteCmd(0X8D);
- WriteCmd(0X10);
- WriteCmd(0XAE);
- }
-
- void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
- {
- unsigned char c = 0,i = 0,j = 0;
- switch(TextSize)
- {
- case 1:
- {
- while(ch[j] != '\0')
- {
- c = ch[j] - 32;
- if(x > 126)
- {
- x = 0;
- y++;
- }
- OLED_SetPos(x,y);
- for(i=0;i<6;i++)
- WriteDat(F6x8[c][i]);
- x += 6;
- j++;
- }
- }break;
- case 2:
- {
- while(ch[j] != '\0')
- {
- c = ch[j] - 32;
- if(x > 120)
- {
- x = 0;
- y++;
- }
- OLED_SetPos(x,y);
- for(i=0;i<8;i++)
- WriteDat(F8X16[c*16+i]);
- OLED_SetPos(x,y+1);
- for(i=0;i<8;i++)
- WriteDat(F8X16[c*16+i+8]);
- x += 8;
- j++;
- }
- }break;
- }
- }
-
- void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
- {
- unsigned char wm=0;
- unsigned int adder=32*N;
- OLED_SetPos(x , y);
- for(wm = 0;wm < 16;wm++)
- {
- WriteDat(F16x16[adder]);
- adder += 1;
- }
- OLED_SetPos(x,y + 1);
- for(wm = 0;wm < 16;wm++)
- {
- WriteDat(F16x16[adder]);
- adder += 1;
- }
- }
-
- void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
- {
- unsigned int j=0;
- unsigned char x,y;
-
- if(y1%8==0)
- y = y1/8;
- else
- y = y1/8 + 1;
- for(y=y0;y<y1;y++)
- {
- OLED_SetPos(x0,y);
- for(x=x0;x<x1;x++)
- {
- WriteDat(BMP[j++]);
- }
- }
- }
-
- void oled_test(void)
- {
- OLED_Init();
- OLED_Fill(0x00);
- OLED_ShowStr(0,0,"AT32A403A BOARD",2);
- OLED_ShowStr(0,2,"I2C OLED TEST",2);
- }
-
3.4、main.c
-
-
- int main(void)
- {
- system_clock_config();
-
- delay_init();
- init_usart(115200);
- init_led();
- //init_eint();
- init_key();
- init_i2c();
- OLED_Init();
- oled_test();
-
- while(1)
- {
- delay_ms(5);
- button_ticks();
- }
- }
四、运行结果
下载程序后,复位开发板,OLED显示
|