首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从用户土地读取32k i2c eeprom

从用户土地读取32k i2c eeprom
EN

Stack Overflow用户
提问于 2021-08-22 04:25:31
回答 1查看 607关注 0票数 1

我需要在嵌入式设备中读取eeprom。

到目前为止,这种“几乎”有效:

代码语言:javascript
复制
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>

#include <linux/i2c-dev.h>


#define READ_SIZE   (256)
#define NB_PAGES    (128)



void dump_to_file(const char *output_file_path,
          const uint8_t *buffer, const int buffer_length)
{
    int output_file = open(output_file_path, O_RDWR|O_APPEND|O_CREAT);
    if (output_file < 0) {
        printf("Failed opening output file %s\n", output_file_path);
        return;
    }

    write(output_file, buffer, buffer_length);
}


int main(int argc, char *argv[])
{
    /* got these values from i2cdetect */
    const char *i2c_device = "/dev/i2c-4";
    const int device_address = 0x50;

    /* open the i2c device file */
    int file = open(i2c_device, O_RDWR);
    if (file < 0) {
        printf("Failed opening %s\n", i2c_device);
        return 1;
    }

    if (ioctl(file, I2C_SLAVE, device_address) < 0) {
        printf("Failed addressing device at %02X\n", device_address);
        close(file);
        return 1;
    }

    int i = 0;
    for (i = 0; i < NB_PAGES; i++) {
        char buf[READ_SIZE] = {0};

        if (read(file, buf, READ_SIZE) != READ_SIZE) {
            printf("Failed reading\n");
            close(file);
            return 1;
        }

        dump_to_file(argv[1], buf, READ_SIZE);
    }

    close(file);

    return 0;
}

“几乎”,我的意思是它转储整个eeprom,但开始取决于最后一个块读。

情况不总是一样的。

如果我看了10个街区。然后再运行程序,我读的是下一个,而不是前10个。

如何设置起始地址?

更新:如果我更新:

代码语言:javascript
复制
i2cset -y 4 0x50 0x00 0x00

运行上面的代码,它就能工作了。那么,如何在代码中放入与i2cset命令相当的内容呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-22 07:23:46

完成了!这并不容易,因为我在任何地方都找不到文件。但我认为,由于eeprom是32K,也许是“像”一个24c256。但即使在这种情况下,我在用户空间中也没有发现任何东西,直到我本能地决定离开。我研究了i2cset源代码,了解了它做了什么,并将其放入代码中。

这是结果,它从用户空间转储了一个完整的i2c 32k eprom。

注意,在这里可以找到一个完整的备份和还原实用程序:https://gist.github.com/Zibri/cf8ac0b311301aeeaa8910c7da824bff

代码语言:javascript
复制
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>


#define READ_SIZE       (256)
#define NB_PAGES        (128)

void dump_to_file(const char *output_file_path,
                  const uint8_t *buffer, const int buffer_length)
{
        int output_file = open(output_file_path, O_RDWR|O_APPEND|O_CREAT);
        if (output_file < 0) {
                printf("Failed opening output file %s\n", output_file_path);
                return;
        }

        write(output_file, buffer, buffer_length);
}


int main(int argc, char *argv[])
{

        const char *i2c_device = "/dev/i2c-4";
        const int device_address = 0x50;

        int file = open(i2c_device, O_RDWR);
        if (file < 0) {
                printf("Failed opening %s\n", i2c_device);
                return 1;
        }

        if (ioctl(file, I2C_SLAVE, device_address) < 0) {
                printf("Failed addressing device at %02X\n", device_address);
                close(file);
                return 1;
        }

        int i = 0;

        write(file,'\x00\x00',2); // ADDRESS

        for (i = 0; i < NB_PAGES; i++) {
                char buf[READ_SIZE] = {0};

                if (read(file, buf, READ_SIZE) != READ_SIZE) {
                        printf("Failed reading\n");
                        close(file);
                        return 1;
                }

                dump_to_file(argv[1], buf, READ_SIZE);
        }

        close(file);

        return 0;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68878486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档