打印

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

[复制链接]
2509|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
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 | 只看该作者
STM32的函数说明 368页 5.3M 超清书签版.pdf (5.23 MB, 下载次数: 0) STM32F101xx和STM32F103xx固件函数库 368页 2.8M 超清书签版.pdf (2.79 MB, 下载次数: 0)
stm32F103 完整版 463页 4.2M 超清书签版.pdf (4.19 MB, 下载次数: 0)
STM32不完全手册 V2.0 491页 22.3M 超清书签版.pdf (21.89 MB, 下载次数: 0)
STM32菜鸟学习手册一罗嗦版 307页 15.8M 超清书签版.pdf (15.44 MB, 下载次数: 0)
STM32从入门到精通 291页 16.5M 超清书签版.pdf (16.14 MB, 下载次数: 0)
STM32单片机快速入门 V1.0 56页 2.4M 超清版.pdf (2.44 MB, 下载次数: 0)
零死角玩转 STM32F103—指南者 692页 115.4M.rar (85.49 MB, 下载次数: 0)
稀里煳涂学习STM32 63页 4.0M 超清版.pdf (3.93 MB, 下载次数: 0)
零死角玩转STM32 初级篇 131页 4.3M 超清书签版.pdf (4.28 MB, 下载次数: 0)
零死角玩转STM32 高级篇 152页 6.9M 超清书签版.pdf (6.83 MB, 下载次数: 0)
零死角玩转STM32 系统篇 76页 2.0M 超清书签版.pdf (1.99 MB, 下载次数: 0)
零死角玩转STM32 中级篇 127页 5.1M 超清书签版.pdf (5.04 MB, 下载次数: 0)



使用特权

评论回复
板凳
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是作用在当前字节还是下一字节。

使用特权

评论回复
评论
JinxinWang 2022-5-15 11:51 回复TA
@JinxinWang :https://racede.me/talk_about_stm32_i2c_peripheral.html 这个网页的资料。可能是DR和DSR存了两位的数据。例程考虑的还是很详细的。虽然还是不太懂这个破自行车是怎么开起来的。 
JinxinWang 2022-5-15 11:49 回复TA
@JinxinWang :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%8D%AE%E6%9C%89%E5%85%B3%E7%9A%84%E5%AF%84%E5%AD%98%E5%99%A8%E2%80%9C%E6%95%B0%E6%8D%AE%E5%AF%84%E5%AD%98%E5%99%A8%20%28Data%20register%29%E2%80%9D%20%28DR%29%E5%92%8C%E2%80%9C%E6%95%B0%E6%8D%AE%E7%A7%BB%E4%BD%8D%E5%AF%84%E5%AD%98%E5%99%A8,%28Data%20shift%20register%29%E2%80%9D%20%... 
JinxinWang 2022-5-15 11:36 回复TA
比较困扰我的是: 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); } 这段代码实在看不懂,注释掉后会出现卡死在 /* wait until the stop condition is finished */ while(I2C_CTL0(I2C0)&0x0200); 希望有缘人能解答。 
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝