Linux读写eeprom

本文介绍了一种通过IIC接口操作EEPROM的方法,包括读取、写入、清空等基本操作,并提供了详细的C语言代码实现。适用于嵌入式系统开发人员了解如何通过IIC总线与EEPROM交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在设备树中挂载对应驱动
并在kernel 编译菜单中添加对应驱动

例如:
对iic1上的0x50地址的eeprom进行挂载
即可对
/sys/bus/i2c/devices/1-0050/eeprom
进行open write read操作
可对该文件进行软链接到/dev/下

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>

#define TMP_EEPROM_DEV "/tmp/tftp/eeprom.conf"
#define EEPROM_DEV "/dev/eeprom_backboard"
#define EEPROM_SIZE 512


static void print_tip(void)
{
   printf("Tips:\n");
   printf("p   ---print eeprom (/dev/eeprom_backboard)\n");
   printf("c   ---clean eeprom (/dev/eeprom_backboard)\n");
   printf("t   ---print eeprom (/tmp/tftp/eeprom.conf)\n");
   printf("w [start_Byte(D)] [value1(H)] [value2(H)] ...   ---write eeprom from start_Byte(0-512)\n");
   printf("                                                ---like (./eeprom w 132 ff ee dd cc bb 33)\n");
   printf("mac_eth1    ---buff[138 - 143] like (./eeprom mac_eth1 00 11 22 33 44 55)\n");
   printf("#mac_inband  ---buff[132 - 137] like (./eeprom mac_inband 00 11 22 33 44 55)\n");
   printf("#mac_ethoam  ---buff[144 - 149] like (./eeprom mac_ethoam 00 11 22 33 44 55)\n");
}

static int eeprom_print(void)
{
   int fd = open(EEPROM_DEV, O_RDONLY);
   if (fd < 0) {
      printf("%s error.\n", EEPROM_DEV);
      return -1;
   }

   int cnt, i;
   unsigned char buff_r[EEPROM_SIZE];
   cnt = read(fd, buff_r, EEPROM_SIZE);
   printf("read OK:%d Byte\n", cnt);
   printf("Byte:   0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15\n");
   printf("  0: ");
   for (i = 0; i < EEPROM_SIZE; i++) {
      printf("0x%2x ", buff_r[i]);
      if ((i % 16) == 15) {
         printf("\n");
         printf("%3d: ", (i / 16 + 1));
      }
   }
   printf("\n");
   return 0;
}

static int tmp_eeprom_print(void)
{
   int fd = open(TMP_EEPROM_DEV, O_RDONLY);
   if (fd < 0) {
      printf("%s error.\n", TMP_EEPROM_DEV);
      return -1;
   }

   int cnt, i;
   unsigned char buff_r[EEPROM_SIZE];
   cnt = read(fd, buff_r, EEPROM_SIZE);
   printf("Byte:   0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15\n");
   printf("read OK:%d Byte\n", cnt);
   printf("  0: ");
   for (i = 0; i < EEPROM_SIZE; i++) {
      printf("0x%2x ", buff_r[i]);
      if ((i % 16) == 15) {
         printf("\n");
         printf("%3d: ", (i / 16 + 1));
      }
   }
   printf("\n");
   return 0;
}

static int eeprom_clean(unsigned char buff)
{
   int fd = open(EEPROM_DEV, O_RDWR);
   if (fd < 0) {
      printf("%s error.\n", EEPROM_DEV);
      return -1;
   }

   int cnt, i;
   unsigned char buff_clean[EEPROM_SIZE];
   memset(buff_clean, buff, EEPROM_SIZE);
   cnt = write(fd, buff_clean, EEPROM_SIZE);
   if (cnt != EEPROM_SIZE) {
      printf("clean error\n");
   } else printf("clean OK!\n");
   return 0;
}

static int eeprom_write(int start_Byte, char size, char *buff_write)
{
   if (start_Byte < 0) {
      printf("ERROR: start_Byte %d < 0, please write [start_Byte(D)] again\n", start_Byte);
      return -1;
   }
   if (!size) {
      printf("ERROR: please write [value(H)] ---like (./eeprom w 132 ff ee dd cc bb 33)\n");
      return -1;
   }
   if ((size + start_Byte) > EEPROM_SIZE) {
      printf("ERROR: too large to eeprom's size\n");
      return -1;
   }

   int fd = open(EEPROM_DEV, O_RDWR);
   if (fd < 0) {
      printf("%s error.\n", EEPROM_DEV);
      return -1;
   }

   int cnt, i;
   unsigned char buff[size];

   for (i = 0; i < size; i++) {
      buff[i] = *buff_write;
      printf("buff[%d] = 0x%x ", start_Byte + i, *buff_write);
      *buff_write++;
   }
   printf("\n");
   printf("write size = %d\n", size);
   lseek(fd, start_Byte, SEEK_SET);
   cnt = write(fd, buff, size);
   if (cnt != size) {
      printf("ERROR: write error!\n");
   }


   return 0;
}

int main(int argc, char *argv[])
{
   const char *str_cmd = NULL;
   char size;
   char buff_write[512] = {0};
   int start_Byte;
   int i = 0;
   unsigned char clean_buff = 0;

   if (argc > 1) {
      str_cmd = argv[1];
      switch (*str_cmd) {
         case 'p':
            eeprom_print();
            break;
         case 'c':
            clean_buff = strtoul(argv[2], NULL, 16);
            eeprom_clean(clean_buff);
            break;
         case 'w':
            if (argc >= 3) {
               start_Byte = strtoul(argv[2], NULL, 10);
               size = argc - 3;
               for (i = 0; i < size; i++){
                  buff_write[i] = strtoul(argv[i + 3], NULL, 16);
               }
               eeprom_write(start_Byte, size, buff_write);
            } else {
               printf("ERROR: please write [start_Byte(D)] ---like (./eeprom w 132 ff ee dd cc bb 33)\n");
            }
            break;
         case 'm':
            if (!strcmp(str_cmd, "mac_inband")) {
               if (argc != 8) {
                  printf("ERROR: need 6 value(H)\n");
               } else {
                  for (i = 0; i < 6; i++){
                     buff_write[i] = strtoul(argv[i + 2], NULL, 16);
                  }
                  eeprom_write(132, 6, buff_write);
               }
            } else if (!strcmp(str_cmd, "mac_eth1")) {
               if (argc != 8) {
                  printf("ERROR: need 6 value(H)\n");
               } else {
                  for (i = 0; i < 6; i++){
                     buff_write[i] = strtoul(argv[i + 2], NULL, 16);
                  }
                  eeprom_write(138, 6, buff_write);
               }
            } else if (!strcmp(str_cmd, "mac_ethoam")) {
               if (argc != 8) {
                  printf("ERROR: need 6 value(H)\n");
               } else {
                  for (i = 0; i < 6; i++){
                     buff_write[i] = strtoul(argv[i + 2], NULL, 16);
                  }
                  eeprom_write(144, 6, buff_write);
               }
            } else print_tip();
            break;
         case 't':
            tmp_eeprom_print();
            break;
         default:
            print_tip();
            break;
      }
   } else print_tip();

   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值