10 I2C读取铁电fm24cl16数据出错

发布于 2020-07-07 14:38:04

cpu:rt1052

fm24cl16的读写时序如下:
image.png
image.png

读写数据的函数如下

rt_err_t fm24clxx_write_page(   struct rt_i2c_bus_device *dev,
                                uint8_t page_addr_7bit,
                                uint8_t wirteAddr,          //fm24cl16内部存储地址
                                uint8_t *pBuffer, 
                                uint16_t NumByteToWrite)
{
    struct rt_i2c_msg msgs[2];

    msgs[0].addr = page_addr_7bit ;
    msgs[0].flags = RT_I2C_WR ;
    msgs[0].buf = &wirteAddr;
    msgs[0].len = 1; 

    msgs[1].addr = page_addr_7bit;
    msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START;
    msgs[1].buf = pBuffer;
    msgs[1].len = NumByteToWrite;
      
    if(rt_i2c_transfer(dev, msgs, 2) != 2) 
    {
        LOG_E("i2c write data failed!");
        return RT_ERROR;
    }            
    
    return RT_EOK;    
}
rt_err_t fm24clxx_read_page(struct rt_i2c_bus_device *dev,
                            uint8_t page_addr_7bit, 
                            uint8_t readAddr,  //fm24cl16内部存储地址
                            uint8_t *pBuffer,  
                            uint16_t NumByteToRead)
{
    struct rt_i2c_msg msgs[2];

    msgs[0].addr = page_addr_7bit;
    msgs[0].flags = RT_I2C_WR ;
    msgs[0].buf  = &readAddr;
    msgs[0].len = 1;
    
    msgs[1].addr  = page_addr_7bit;
    msgs[1].flags = RT_I2C_RD;
    msgs[1].buf   = pBuffer;
    msgs[1].len   = NumByteToRead;
    
    if(rt_i2c_transfer(dev, msgs, 2) != 2) 
    {
        LOG_E("i2c read data failed!");
        return RT_ERROR;
    }            
    
    return RT_EOK;
}

实验测试:

#define FM24CLXX_ADDR_7BIT (0xA0 >> 1)  

int fm24cl16_test(void)
{
    uint8_t writebuf[]="welcome to rtt!";
    struct rt_i2c_msg msgs[2];
    struct rt_i2c_bus_device *i2c_bus = RT_NULL;     /* I2C总线设备句柄 */
    uint8_t addr =0;
    uint8_t readbuf[20] ={0};
    
    struct rt_i2c_bus_device *dev = RT_NULL;
    dev = rt_i2c_bus_device_find("i2c1");
    if(dev == RT_NULL)
    {
        LOG_E("cannot find i2c1 device!");
        return RT_ERROR;
    }
    
    fm24clxx_write_page(dev,FM24CLXX_ADDR_7BIT,0,writebuf,sizeof(writebuf));
    
    rt_thread_mdelay(100);
    fm24clxx_read_page(dev,FM24CLXX_ADDR_7BIT,0,readbuf,sizeof(readbuf));
   
    LOG_D("readbuf : %s",readbuf);

}
MSH_CMD_EXPORT(fm24cl16_test, fm24cl16 test);

最终测试结果:
image.png
裸机上用1052的官方库读写是没问题的,主要还是没理解rtt rt_i2c_transfer()函数的用法,所以请教大家问题可能在哪?

查看更多

关注者
0
被浏览
163
2 个回答
JQRR_7669
JQRR_7669 认证专家 2020-07-07

什么错误,说明白点

ccxzjz
ccxzjz 2020-09-01

fm24cl16 写数据时不需要发送两个消息,一个即可

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友