1229|4

469

帖子

3

TA的资源

五彩晶圆(初级)

楼主
 

【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)
  • {
  • /* reset i2c peripheral */
  • i2c_reset(hi2c->i2cx);
  • /* i2c peripheral initialization */
  • i2c_lowlevel_init(hi2c);
  • /* i2c peripheral enable */
  • 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)
  • {
  • /* check timeout */
  • 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)
  • {
  • /* check timeout */
  • if((timeout--) == 0)
  • {
  • hi2c->error_code = I2C_ERR_TIMEOUT;
  • return I2C_ERR_TIMEOUT;
  • }
  • }
  • }
  • else
  • {
  • while(i2c_flag_get(hi2c->i2cx, flag) == RESET)
  • {
  • /* check the ack fail flag */
  • if(event_check & I2C_EVENT_CHECK_ACKFAIL)
  • {
  • if(i2c_flag_get(hi2c->i2cx, I2C_ACKFAIL_FLAG) != RESET)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • /* clear ack fail flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ACKFAIL_FLAG);
  • hi2c->error_code = I2C_ERR_ACKFAIL;
  • return I2C_ERR_ACKFAIL;
  • }
  • }
  • /* check the stop flag */
  • if(event_check & I2C_EVENT_CHECK_STOP)
  • {
  • if(i2c_flag_get(hi2c->i2cx, I2C_STOPF_FLAG) != RESET)
  • {
  • /* clear stop flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_STOPF_FLAG);
  • hi2c->error_code = I2C_ERR_STOP;
  • return I2C_ERR_STOP;
  • }
  • }
  • /* check timeout */
  • 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)
  • {
  • /* disable the dma channel */
  • dma_channel_enable(dma_channelx, FALSE);
  • /* disable the transfer complete interrupt */
  • dma_interrupt_enable(dma_channelx, DMA_FDT_INT , FALSE);
  • /* configure buffer address and buffer size */
  • 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);
  • /* enable the transfer complete interrupt */
  • dma_interrupt_enable(dma_channelx, DMA_FDT_INT , TRUE);
  • /* enable the dma channel */
  • 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)
  • {
  • /* generate start condtion */
  • i2c_start_generate(hi2c->i2cx);
  • /* wait for the start flag to be set */
  • 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)
  • {
  • /* send slave address */
  • i2c_7bit_address_send(hi2c->i2cx, address, I2C_DIRECTION_TRANSMIT);
  • }
  • else
  • {
  • /* send slave 10-bit address header */
  • i2c_data_send(hi2c->i2cx, (uint8_t)((address & 0x0300) >> 7) | 0xF0);
  • /* wait for the addrh flag to be set */
  • 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;
  • }
  • /* send slave address */
  • i2c_data_send(hi2c->i2cx, (uint8_t)(address & 0x00FF));
  • }
  • /* wait for the addr7 flag to be set */
  • 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)
  • {
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* generate start condtion */
  • i2c_start_generate(hi2c->i2cx);
  • /* wait for the start flag to be set */
  • 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)
  • {
  • /* send slave address */
  • i2c_7bit_address_send(hi2c->i2cx, address, I2C_DIRECTION_RECEIVE);
  • }
  • else
  • {
  • /* send slave 10-bit address header */
  • i2c_data_send(hi2c->i2cx, (uint8_t)((address & 0x0300) >> 7) | 0xF0);
  • /* wait for the addrh flag to be set */
  • 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;
  • }
  • /* send slave address */
  • i2c_data_send(hi2c->i2cx, (uint8_t)(address & 0x00FF));
  • /* wait for the addr7 flag to be set */
  • 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;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* generate restart condtion */
  • i2c_start_generate(hi2c->i2cx);
  • /* wait for the start flag to be set */
  • 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;
  • }
  • /* send slave 10-bit address header */
  • i2c_data_send(hi2c->i2cx, (uint8_t)((address & 0x0300) >> 7) | 0xF1);
  • }
  • /* wait for the addr7 flag to be set */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* send slave address */
  • if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • while(size > 0)
  • {
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_3;
  • }
  • /* write data */
  • i2c_data_send(hi2c->i2cx, (*pdata++));
  • size--;
  • }
  • /* wait for the tdc flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_4;
  • }
  • /* generate stop condtion */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* wait for the addr7 flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • while(size > 0)
  • {
  • /* wait for the rdbf flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_STOP, timeout) != I2C_OK)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • return I2C_ERR_STEP_3;
  • }
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • }
  • /* wait for the stop flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_STOPF_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • return I2C_ERR_STEP_4;
  • }
  • /* clear stop flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_STOPF_FLAG);
  • /* disable ack */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* send slave address */
  • if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • if(size == 1)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • }
  • else if(size == 2)
  • {
  • /* ack acts on the next byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_NEXT);
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • }
  • else
  • {
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • }
  • while(size > 0)
  • {
  • if(size <= 3)
  • {
  • /* 1 byte */
  • if(size == 1)
  • {
  • /* wait for the rdbf flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_3;
  • }
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • }
  • /* 2 bytes */
  • else if(size == 2)
  • {
  • /* wait for the tdc flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_4;
  • }
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • }
  • /* 3 last bytes */
  • else
  • {
  • /* wait for the tdc flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_5;
  • }
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • /* wait for the tdc flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_6;
  • }
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • }
  • }
  • else
  • {
  • /* wait for the rdbf flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_7;
  • }
  • /* read data */
  • (*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)
  • {
  • /* initialization parameters */
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* wait for the addr7 flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • if(hi2c->i2cx->oaddr1_bit.addr1mode == I2C_ADDRESS_MODE_10BIT)
  • {
  • /* wait for the addr7 flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • return I2C_ERR_STEP_3;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • }
  • while(size > 0)
  • {
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • return I2C_ERR_STEP_4;
  • }
  • /* write data */
  • i2c_data_send(hi2c->i2cx, *pdata++);
  • size--;
  • }
  • /* wait for the ackfail flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_ACKFAIL_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_5;
  • }
  • /* clear ackfail flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ACKFAIL_FLAG);
  • /* disable ack */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_INT_MA_TX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* send slave address */
  • if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* enable interrupt */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_INT_SLA_RX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* enable interrupt */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_INT_MA_RX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* send slave address */
  • if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • if(hi2c->pcount == 1)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • }
  • else if(hi2c->pcount == 2)
  • {
  • /* ack acts on the next byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_NEXT);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • }
  • else
  • {
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • }
  • /* enable interrupt */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_INT_SLA_TX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* enable interrupt */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_DMA_MA_TX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* disable dma request */
  • i2c_dma_enable(hi2c->i2cx, FALSE);
  • /* configure the dma channel */
  • i2c_dma_config(hi2c, hi2c->dma_tx_channel, pdata, size);
  • /* send slave address */
  • if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* enable dma request */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_DMA_SLA_RX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* disable dma request */
  • i2c_dma_enable(hi2c->i2cx, FALSE);
  • /* configure the dma channel */
  • i2c_dma_config(hi2c, hi2c->dma_rx_channel, pdata, size);
  • /* enable dma request */
  • i2c_dma_enable(hi2c->i2cx, TRUE);
  • /* enable address interrupt */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_DMA_MA_RX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* disable dma request */
  • i2c_dma_enable(hi2c->i2cx, FALSE);
  • /* configure the dma channel */
  • i2c_dma_config(hi2c, hi2c->dma_rx_channel, pdata, size);
  • /* send slave address */
  • if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • if(size == 1)
  • {
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • /* enable dma request */
  • i2c_dma_enable(hi2c->i2cx, TRUE);
  • }
  • else
  • {
  • /* enable dma end transfer */
  • i2c_dma_end_transfer_set(hi2c->i2cx, TRUE);
  • /* enable dma request */
  • i2c_dma_enable(hi2c->i2cx, TRUE);
  • /* clear addr flag */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_DMA_SLA_TX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* disable dma request */
  • i2c_dma_enable(hi2c->i2cx, FALSE);
  • /* configure the dma channel */
  • i2c_dma_config(hi2c, hi2c->dma_tx_channel, pdata, size);
  • /* enable dma request */
  • i2c_dma_enable(hi2c->i2cx, TRUE);
  • /* enable address interrupt */
  • 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)
  • {
  • /* send memory address */
  • i2c_data_send(hi2c->i2cx, mem_address & 0xFF);
  • }
  • else
  • {
  • /* send memory address */
  • i2c_data_send(hi2c->i2cx, (mem_address >> 8) & 0xFF);
  • /* wait for the tdbe flag to be set */
  • err_code = i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout);
  • if(err_code != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return err_code;
  • }
  • /* send memory address */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* send slave address */
  • if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_3;
  • }
  • /* send memory address */
  • if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_4;
  • }
  • while(size > 0)
  • {
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_5;
  • }
  • /* write data */
  • i2c_data_send(hi2c->i2cx, (*pdata++));
  • size--;
  • }
  • /* wait for the tdc flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_6;
  • }
  • /* generate stop condtion */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* send slave address */
  • if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_3;
  • }
  • /* send memory address */
  • if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_4;
  • }
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_5;
  • }
  • /* send slave address */
  • if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_6;
  • }
  • if(size == 1)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • }
  • else if(size == 2)
  • {
  • /* ack acts on the next byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_NEXT);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • }
  • else
  • {
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • }
  • while(size > 0)
  • {
  • if(size <= 3)
  • {
  • /* 1 byte */
  • if(size == 1)
  • {
  • /* wait for the rdbf flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_7;
  • }
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • }
  • /* 2 bytes */
  • else if(size == 2)
  • {
  • /* wait for the tdc flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_8;
  • }
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • }
  • /* 3 last bytes */
  • else
  • {
  • /* wait for the tdc flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_9;
  • }
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • /* wait for the tdc flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDC_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_10;
  • }
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • /* read data */
  • (*pdata++) = i2c_data_receive(hi2c->i2cx);
  • size--;
  • }
  • }
  • else
  • {
  • /* wait for the rdbf flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_RDBF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_11;
  • }
  • /* read data */
  • (*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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_INT_MA_TX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* send slave address */
  • if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_3;
  • }
  • /* send memory address */
  • if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_4;
  • }
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_5;
  • }
  • /* enable interrupt */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_INT_MA_RX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* send slave address */
  • if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_3;
  • }
  • /* send memory address */
  • if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_4;
  • }
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_5;
  • }
  • /* send slave address */
  • if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_6;
  • }
  • if(hi2c->pcount == 1)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • }
  • else if(hi2c->pcount == 2)
  • {
  • /* ack acts on the next byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_NEXT);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • }
  • else
  • {
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • }
  • /* enable interrupt */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_DMA_MA_TX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* disable dma request */
  • i2c_dma_enable(hi2c->i2cx, FALSE);
  • /* configure the dma channel */
  • i2c_dma_config(hi2c, hi2c->dma_tx_channel, pdata, size);
  • /* send slave address */
  • if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_3;
  • }
  • /* send memory address */
  • if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_4;
  • }
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_5;
  • }
  • /* enable dma request */
  • 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)
  • {
  • /* initialization parameters */
  • hi2c->mode = I2C_DMA_MA_RX;
  • hi2c->status = I2C_START;
  • hi2c->pbuff = pdata;
  • hi2c->pcount = size;
  • hi2c->timeout = timeout;
  • hi2c->error_code = I2C_OK;
  • /* wait for the busy flag to be reset */
  • if(i2c_wait_flag(hi2c, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_1;
  • }
  • /* ack acts on the current byte */
  • i2c_master_receive_ack_set(hi2c->i2cx, I2C_MASTER_ACK_CURRENT);
  • /* enable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* disable dma request */
  • i2c_dma_enable(hi2c->i2cx, FALSE);
  • /* configure the dma channel */
  • i2c_dma_config(hi2c, hi2c->dma_rx_channel, pdata, size);
  • /* send slave address */
  • if(i2c_master_write_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_2;
  • }
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_3;
  • }
  • /* send memory address */
  • if(i2c_memory_address_send(hi2c, mem_address_width, mem_address, timeout) != I2C_OK)
  • {
  • return I2C_ERR_STEP_4;
  • }
  • /* wait for the tdbe flag to be set */
  • if(i2c_wait_flag(hi2c, I2C_TDBE_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_5;
  • }
  • /* send slave address */
  • if(i2c_master_read_addr(hi2c, address, timeout) != I2C_OK)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • return I2C_ERR_STEP_6;
  • }
  • if(size == 1)
  • {
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • /* enable dma request */
  • i2c_dma_enable(hi2c->i2cx, TRUE);
  • }
  • else
  • {
  • /* enable dma end transfer */
  • i2c_dma_end_transfer_set(hi2c->i2cx, TRUE);
  • /* enable dma request */
  • i2c_dma_enable(hi2c->i2cx, TRUE);
  • /* clear addr flag */
  • 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)
  • {
  • /* step 1: transfer data */
  • if(i2c_flag_get(hi2c->i2cx, I2C_TDBE_FLAG) != RESET)
  • {
  • if(hi2c->pcount == 0)
  • {
  • /* transfer complete */
  • hi2c->status = I2C_END;
  • /* disable interrupt */
  • i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, FALSE);
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • }
  • else
  • {
  • /* write data */
  • 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)
  • {
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* read data */
  • (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
  • hi2c->pcount--;
  • }
  • else if(hi2c->pcount == 2)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • /* read data */
  • (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
  • hi2c->pcount--;
  • /* read data */
  • (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
  • hi2c->pcount--;
  • /* transfer complete */
  • hi2c->status = I2C_END;
  • /* disable interrupt */
  • i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, FALSE);
  • }
  • else
  • {
  • /* read data */
  • (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
  • hi2c->pcount--;
  • }
  • }
  • else if(i2c_flag_get(hi2c->i2cx, I2C_RDBF_FLAG) != RESET)
  • {
  • if(hi2c->pcount > 3)
  • {
  • /* read data */
  • (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
  • hi2c->pcount--;
  • }
  • else if((hi2c->pcount == 3) || (hi2c->pcount == 2))
  • {
  • /* disable rdbf interrupt */
  • i2c_interrupt_enable(hi2c->i2cx, I2C_DATA_INT, FALSE);
  • }
  • else
  • {
  • /* read data */
  • (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
  • hi2c->pcount--;
  • /* transfer complete */
  • hi2c->status = I2C_END;
  • /* disable interrupt */
  • 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)
  • {
  • /* step 1: receive slave address */
  • if(i2c_flag_get(hi2c->i2cx, I2C_ADDR7F_FLAG) != RESET)
  • {
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • }
  • /* step 2: transfer data */
  • else if(i2c_flag_get(hi2c->i2cx, I2C_TDBE_FLAG) != RESET)
  • {
  • if(hi2c->pcount)
  • {
  • /* send data */
  • i2c_data_send(hi2c->i2cx, (*hi2c->pbuff++));
  • hi2c->pcount--;
  • if(hi2c->pcount == 0)
  • {
  • /* disable interrupt */
  • i2c_interrupt_enable(hi2c->i2cx, I2C_DATA_INT | I2C_EVT_INT | I2C_ERR_INT, FALSE);
  • /* wait for the ackfail flag to be set */
  • hi2c->status = i2c_wait_flag(hi2c, I2C_ACKFAIL_FLAG, I2C_EVENT_CHECK_NONE, hi2c->timeout);
  • /* clear ackfail flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ACKFAIL_FLAG);
  • /* transfer complete */
  • 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)
  • {
  • /* step 1: receive slave address */
  • if(i2c_flag_get(hi2c->i2cx, I2C_ADDR7F_FLAG) != RESET)
  • {
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • }
  • /* step 2: receive data */
  • else if(i2c_flag_get(hi2c->i2cx, I2C_RDBF_FLAG) != RESET)
  • {
  • if(hi2c->pcount)
  • {
  • /* read data */
  • (*hi2c->pbuff++) = i2c_data_receive(hi2c->i2cx);
  • hi2c->pcount--;
  • }
  • }
  • /* step 3: stop conditon is received, transfer ends */
  • else if(i2c_flag_get(hi2c->i2cx, I2C_STOPF_FLAG) != RESET)
  • {
  • /* clear stop flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_STOPF_FLAG);
  • /* transfer complete */
  • hi2c->status = I2C_END;
  • /* disable interrupt */
  • 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)
  • {
  • /* tdc interrupt */
  • if(i2c_flag_get(hi2c->i2cx, I2C_TDC_FLAG) != RESET)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • /* disable evt interrupt */
  • i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, FALSE);
  • /* transfer complete */
  • 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)
  • {
  • /* receive slave address */
  • if(i2c_flag_get(hi2c->i2cx, I2C_ADDR7F_FLAG) != RESET)
  • {
  • /* clear addr flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ADDR7F_FLAG);
  • }
  • /* wait for the stop flag to be set, trasnfer end */
  • if(i2c_flag_get(hi2c->i2cx, I2C_STOPF_FLAG) != RESET)
  • {
  • /* clear stop flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_STOPF_FLAG);
  • /* disable evt interrupt */
  • i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, FALSE);
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, TRUE);
  • /* transfer complete */
  • 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)
  • {
  • /* transfer complete */
  • if(dma_flag_get(DMA_GET_TC_FLAG(hi2c->dma_tx_channel)) != RESET)
  • {
  • /* disable the transfer complete interrupt */
  • dma_interrupt_enable(hi2c->dma_tx_channel, DMA_FDT_INT, FALSE);
  • /* clear the transfer complete flag */
  • dma_flag_clear(DMA_GET_TC_FLAG(hi2c->dma_tx_channel));
  • /* disable dma request */
  • i2c_dma_enable(hi2c->i2cx, FALSE);
  • hi2c->pcount = 0;
  • switch(hi2c->mode)
  • {
  • case I2C_DMA_MA_TX:
  • /* enable tdc interrupt, generate stop condition in tdc interrupt */
  • i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, TRUE);
  • break;
  • case I2C_DMA_SLA_TX:
  • /* enable ackfail interrupt, generate stop condition in ackfail interrupt */
  • 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)
  • {
  • /* transfer complete */
  • if(dma_flag_get(DMA_GET_TC_FLAG(hi2c->dma_rx_channel)) != RESET)
  • {
  • /* disable the transfer complete interrupt */
  • dma_interrupt_enable(hi2c->dma_rx_channel, DMA_FDT_INT, FALSE);
  • /* clear the transfer complete flag */
  • dma_flag_clear(DMA_GET_TC_FLAG(hi2c->dma_rx_channel));
  • /* disable dma request */
  • i2c_dma_enable(hi2c->i2cx, FALSE);
  • hi2c->pcount = 0;
  • switch(hi2c->mode)
  • {
  • case I2C_DMA_MA_RX:
  • /* clear ackfail flag */
  • i2c_flag_clear(hi2c->i2cx, I2C_ACKFAIL_FLAG);
  • if(hi2c->pcount != 1)
  • {
  • /* generate stop condtion */
  • i2c_stop_generate(hi2c->i2cx);
  • }
  • /* transfer complete */
  • hi2c->status = I2C_END;
  • break;
  • case I2C_DMA_SLA_RX:
  • /* enable stop interrupt, wait for the stop flag to be set */
  • 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)
  • {
  • /* buserr */
  • if(i2c_flag_get(hi2c->i2cx, I2C_BUSERR_FLAG) != RESET)
  • {
  • i2c_flag_clear(hi2c->i2cx, I2C_BUSERR_FLAG);
  • hi2c->error_code = I2C_ERR_INTERRUPT;
  • }
  • /* arlost */
  • if(i2c_flag_get(hi2c->i2cx, I2C_ARLOST_FLAG) != RESET)
  • {
  • i2c_flag_clear(hi2c->i2cx, I2C_ARLOST_FLAG);
  • hi2c->error_code = I2C_ERR_INTERRUPT;
  • }
  • /* ackfail */
  • 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:
  • /* disable ack */
  • i2c_ack_enable(hi2c->i2cx, FALSE);
  • /* disable evt interrupt */
  • i2c_interrupt_enable(hi2c->i2cx, I2C_EVT_INT, FALSE);
  • /* transfer complete */
  • hi2c->status = I2C_END;
  • break;
  • default:
  • hi2c->error_code = I2C_ERR_INTERRUPT;
  • break;
  • }
  • }
  • /* ouf */
  • if(i2c_flag_get(hi2c->i2cx, I2C_OUF_FLAG) != RESET)
  • {
  • i2c_flag_clear(hi2c->i2cx, I2C_OUF_FLAG);
  • hi2c->error_code = I2C_ERR_INTERRUPT;
  • }
  • /* pecerr */
  • if(i2c_flag_get(hi2c->i2cx, I2C_PECERR_FLAG) != RESET)
  • {
  • i2c_flag_clear(hi2c->i2cx, I2C_PECERR_FLAG);
  • hi2c->error_code = I2C_ERR_INTERRUPT;
  • }
  • /* tmout */
  • if(i2c_flag_get(hi2c->i2cx, I2C_TMOUT_FLAG) != RESET)
  • {
  • i2c_flag_clear(hi2c->i2cx, I2C_TMOUT_FLAG);
  • hi2c->error_code = I2C_ERR_INTERRUPT;
  • }
  • /* alertf */
  • if(i2c_flag_get(hi2c->i2cx, I2C_ALERTF_FLAG) != RESET)
  • {
  • i2c_flag_clear(hi2c->i2cx, I2C_ALERTF_FLAG);
  • hi2c->error_code = I2C_ERR_INTERRUPT;
  • }
  • /* disable all interrupts */
  • 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)
  • {
  • /* i2c periph clock enable */
  • 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 configuration */
  • 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;
  • /* configure i2c pins: scl */
  • gpio_initstructure.gpio_pins = I2Cx_SCL_PIN;
  • gpio_init(I2Cx_SCL_GPIO_PORT, &gpio_initstructure);
  • /* configure i2c pins: sda */
  • 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); //display off
  • WriteCmd(0x20); //Set Memory Addressing Mode
  • WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
  • WriteCmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
  • WriteCmd(0xc8); //Set COM Output Scan Direction
  • WriteCmd(0x00); //---set low column address
  • WriteCmd(0x10); //---set high column address
  • WriteCmd(0x40); //--set start line address
  • WriteCmd(0x81); //--set contrast control register
  • WriteCmd(0xff); //亮度调节 0x00~0xff
  • WriteCmd(0xa1); //--set segment re-map 0 to 127
  • WriteCmd(0xa6); //--set normal display
  • WriteCmd(0xa8); //--set multiplex ratio(1 to 64)
  • WriteCmd(0x3F); //
  • WriteCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
  • WriteCmd(0xd3); //-set display offset
  • WriteCmd(0x00); //-not offset
  • WriteCmd(0xd5); //--set display clock divide ratio/oscillator frequency
  • WriteCmd(0xf0); //--set divide ratio
  • WriteCmd(0xd9); //--set pre-charge period
  • WriteCmd(0x22); //
  • WriteCmd(0xda); //--set com pins hardware configuration
  • WriteCmd(0x12);
  • WriteCmd(0xdb); //--set vcomh
  • WriteCmd(0x40); //0x20,0.77xVcc
  • WriteCmd(0x8d); //--set DC-DC enable
  • WriteCmd(0x14); //
  • WriteCmd(0xaf); //--turn on oled panel
  • }
  • 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); //page0-page1
  • WriteCmd(0x00); //low column start address
  • WriteCmd(0x10); //high column start address
  • 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); //OLED唤醒
  • }
  • void OLED_OFF(void)
  • {
  • WriteCmd(0X8D); //设置电荷泵
  • WriteCmd(0X10); //关闭电荷泵
  • WriteCmd(0XAE); //OLED休眠
  • }
  • 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

  • #include "main.h"
  • 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显示

 

此帖出自汽车电子论坛

最新回复

硬件IIC就怕各种报错,和例程一样,就是报错     详情 回复 发表于 2024-1-31 09:00
点赞 关注

回复
举报

78

帖子

0

TA的资源

一粒金砂(中级)

沙发
 

学代码的道路真是好长啊,感谢分享,学习

此帖出自汽车电子论坛

点评

共同学习  详情 回复 发表于 2024-1-31 09:51
 
 

回复

161

帖子

0

TA的资源

一粒金砂(高级)

板凳
 

硬件IIC就怕各种报错,和例程一样,就是报错  

此帖出自汽车电子论坛

点评

硬件I2C有报错调试会费些时间,不同的芯片厂家,软件还有些区别,尽量参考官网的例程,调试会容易些。  详情 回复 发表于 2024-1-31 09:54
 
 
 

回复

469

帖子

3

TA的资源

五彩晶圆(初级)

4
 
Delon 发表于 2024-1-29 09:19 学代码的道路真是好长啊,感谢分享,学习

共同学习

此帖出自汽车电子论坛
 
 
 

回复

469

帖子

3

TA的资源

五彩晶圆(初级)

5
 
qzc飘曳 发表于 2024-1-31 09:00 硬件IIC就怕各种报错,和例程一样,就是报错  

硬件I2C有报错调试会费些时间,不同的芯片厂家,软件还有些区别,尽量参考官网的例程,调试会容易些。

此帖出自汽车电子论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
有奖直播报名| TI 面向楼宇和工厂自动化行业的毫米波雷达解决方案
【内容简介】TI 60GHz IWRL6432和 IWRL1432毫米波雷达传感器如何帮助解决楼宇和工厂自动化应用中的感应难题
【直播时间】5月28日(周三)上午10:00
【直播礼品】小米双肩包、contigo水杯、胶囊伞、安克充电器

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 7

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表