欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Android 9 读写eeprom at24c256

程序员文章站 2022-07-02 11:50:33
Android 9 读写eeprom at24c256linux 读取eeprom相对来说比较简单,因为内核中集成了大部分eeprom的驱动,文件目录在kernel/drivers/misc/eeprom/at24.c 。而且我们不用区分at24cxx的不同容量导致的读写方式不同,只要将at24.c编译到内核,然后dts中注册相应的i2c通道即可。手中的模块a0 a1 a2 都是接地,所以器件地址为0xa0,而dts中注册为七位地址,即0x50.&i2c1 {status = "okay"...

Android 9 读写eeprom at24c256

linux 读取eeprom相对来说比较简单,因为内核中集成了大部分eeprom的驱动,文件目录在kernel/drivers/misc/eeprom/at24.c 。
而且我们不用区分at24cxx的不同容量导致的读写方式不同,只要将at24.c编译到内核,然后dts中注册相应的i2c通道即可。手中的模块a0 a1 a2 都是接地,所以器件地址为0xa0,而dts中注册为七位地址,即0x50.

&i2c1 {
	status = "okay";
	clock-frequency = <300000>;
	pinctrl-names="default";
	pinctrl-0=<&i2c1_h_pins>;
	at24c256@50{
		compatible = "at24,24c256";
		reg = <0x50>;
		status = "okay";
	};
};

按理说升级编译好的kernel后,就应该在对应的i2c节点下找到eeprom的节点,可是找来找去都没有找到这个节点,查看代码发现有这么个东西。

	at24->nvmem_config.name = dev_name(&client->dev);
	at24->nvmem_config.dev = &client->dev;
	at24->nvmem_config.read_only = !writable;
	at24->nvmem_config.root_only = true;
	at24->nvmem_config.owner = THIS_MODULE;
	at24->nvmem_config.compat = true;
	at24->nvmem_config.base_dev = &client->dev;
	at24->nvmem_config.reg_read = at24_read;
	at24->nvmem_config.reg_write = at24_write;
	at24->nvmem_config.priv = at24;
	at24->nvmem_config.stride = 1;
	at24->nvmem_config.word_size = 1;
	at24->nvmem_config.size = chip.byte_len;
	at24->nvmem = nvmem_register(&at24->nvmem_config);
	if (IS_ERR(at24->nvmem)) {
		err = PTR_ERR(at24->nvmem);
		goto err_clients;
	}

原来新的内核中引入了nvmem的架构对eeprom操作。重新配置内核,打开nvmem的编译选项,重新升级后,找到了nvmem的节点。
Android 9 读写eeprom at24c256
应用层对eeprom的操作,都是通过这个节点。

写个应用层代码:

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "android/log.h"
#include <cutils/properties.h>
#include <stdbool.h>
#include <linux/input.h>

#define FILE_PATH  "/sys/bus/nvmem/devices/1-00500/nvmem"

int main()
{
	int fd;
	int size;
	
	char data[512] = {0};
	char r_data[64] = {0};
	char *w_data = "Hello World!Good Day!\n";
	fd = open(FILE_PATH, O_RDWR);
	if (fd < 0) {
	
		printf("Can't open file %s\r\n", FILE_PATH);
		return -1;
	}

	lseek(fd,0,SEEK_SET);
	write(fd,data,sizeof(data)); //erase eeprom
	lseek(fd,0,SEEK_SET);
	write(fd,w_data,strlen(w_data));
	lseek(fd,0,SEEK_SET);
	size = read(fd,r_data,sizeof(r_data));
	printf("read data==%s\n",r_data);
	close(fd);
	return 0;
}

Android.mk

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= max.c
LOCAL_MODULE := max
LOCAL_MODULE_TAGS := optional
LOCAL_SHARED_LIBRARIES += \
	libcutils \
	liblog 
LOCAL_MODULE_PATH :=$(PRODUCT_OUT)/vendor/bin
include $(BUILD_EXECUTABLE)

Android 9 读写eeprom at24c256
运行结果如图

本文地址:https://blog.csdn.net/weixin_43932857/article/details/110622051