GD32F的IIC模块读取是,读的字节数为2,将I2C_CTL0的POAP置为1?

[复制链接]
 楼主| 威风ets 发表于 2021-1-8 11:50 | 显示全部楼层 |阅读模式
GD32F103的IIC模块在进行eeprom读取的时候,当读取的字节数为2时,需要将I2C_CTL0的POAP置为1;POAP的定义如下:
       0:ACKEN位决定对当前正在接收的字节发送ACK;
       1: ACKEN位决定是否对下一个字节发送ACK.
我的理解时:当读取的字节数为2时,该位置1,表示接收第一个字节后,发送ACK;接收第二个字节后,不发送ACK。这样就能保证时序的正确性。
不知道各位大佬是如何理解的,请赐教。谢谢!

代码如下:
void eeprom_buffer_read(uint8_t* p_buffer, uint8_t read_address, uint16_t number_of_byte)
{  
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));

    if(2 == number_of_byte)
   {
        i2c_ackpos_config(I2C0,I2C_ACKPOS_NEXT);   //如果接收的的字节数等于2,则将将I2C_CTL0的POAP置为1。
    }

    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);

    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));

    /* send slave address to I2C bus */
    i2c_master_addressing(I2C0, eeprom_address, I2C_TRANSMITTER);

    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));

    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);

    /* wait until the transmit data buffer is empty */
    while(SET != i2c_flag_get( I2C0 , I2C_FLAG_TBE));

    /* enable I2C0*/
    i2c_enable(I2C0);

    /* send the EEPROM's internal address to write to */
    i2c_data_transmit(I2C0, read_address);  

    /* wait until BTC bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));

    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);

    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));

    /* send slave address to I2C bus */
    i2c_master_addressing(I2C0, eeprom_address, I2C_RECEIVER);

    if(number_of_byte < 3){
        /* disable acknowledge */
        i2c_ack_config(I2C0,I2C_ACK_DISABLE);
    }

    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));

    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);

    if(1 == number_of_byte){
        /* send a stop condition to I2C bus */
        i2c_stop_on_bus(I2C0);
    }

    /* while there is data to be read */
    while(number_of_byte){
        if(3 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));

            /* disable acknowledge */
            i2c_ack_config(I2C0,I2C_ACK_DISABLE);
        }
        if(2 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));

            /* send a stop condition to I2C bus */
            i2c_stop_on_bus(I2C0);
        }

        /* wait until the RBNE bit is set and clear it */
        if(i2c_flag_get(I2C0, I2C_FLAG_RBNE)){
            /* read a byte from the EEPROM */
            *p_buffer = i2c_data_receive(I2C0);

            /* point to the next location where the byte read will be saved */
            p_buffer++;

            /* decrement the read bytes counter */
            number_of_byte--;
        }
    }

    /* wait until the stop condition is finished */
    while(I2C_CTL0(I2C0)&0x0200);

    /* enable acknowledge */
    i2c_ack_config(I2C0,I2C_ACK_ENABLE);

    i2c_ackpos_config(I2C0,I2C_ACKPOS_CURRENT);
}

评分

参与人数 1威望 +15 收起 理由
tyw + 15 加分下载

查看全部评分

tyw 发表于 2021-1-8 11:58 | 显示全部楼层
JinxinWang 发表于 2022-5-15 11:32 | 显示全部楼层

GD32F的IIC模块读取是,读的字节数为2,将I2C_CTL0的POAP置为1?

谢邀。今天遇到类似问题,思考了下。GD32F103的IIC模块在进行eeprom读取的时候,当读取的字节数为2时,需要将I2C_CTL0的POAP置为1;POAP的定义如下:
       0:ACKEN位决定对当前正在接收的字节发送ACK;
       1: ACKEN位决定是否对下一个字节发送ACK.
查看源码,在配置POPA位后
if(number_of_byte < 3){
        /* disable acknowledge */
        i2c_ack_config(I2C0,I2C_ACK_DISABLE);
    }
其中i2c_ack_config()的定义如下:
void i2c_ack_config(uint32_t i2c_periph, uint32_t ack)
{
    if(I2C_ACK_ENABLE == ack){
        I2C_CTL0(i2c_periph) |= I2C_CTL0_ACKEN;
    }else{
        I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_ACKEN);
    }
}
是用来配置ACKEN位的。
结合两段代码,此时ACKEN位配置的结果是下一个字节不发送ACK。
总结:
单单配置POPA为是没办法影响到ACK的发送的。ACKEN位才能影响ACK发送与否。POPA决定ACKEN是作用在当前字节还是下一字节。

评论

[url=home.php?mod=space&uid=3320462]@JinxinWang[/url] :https://racede.me/talk_about_stm32_i2c_peripheral.html 这个网页的资料。可能是DR和DSR存了两位的数据。例程考虑的还是很详细的。虽然还是不太懂这个破自行车是怎么开起来的。  发表于 2022-5-15 11:51
[url=home.php?mod=space&uid=3320462]@JinxinWang[/url] :https://racede.me/talk_about_stm32_i2c_peripheral.html#:~:text=%E6%88%91%E4%BB%AC%E5%8F%AF%E4%BB%A5%E7%9C%8B%E8%A7%81STM32%E7%9A%84%E7%A1%AC%E4%BB%B6I2C%E6%9C%89%E4%B8%A4%E4%B8%AA%E5%92%8C%E6%95%B0%E6  发表于 2022-5-15 11:49
比较困扰我的是: if(3 == number_of_byte){ /* wait until BTC bit is set */ while(!i2c_flag_get(I2C0, I2C_FLAG_BTC)); /* disable acknowledge */ i2c_ack_config(I2C0,I2C_ACK_DISABLE); } 这段代码实在看不懂,注释掉后会  发表于 2022-5-15 11:36
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

1

主题

1

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部