OrangePi3 LTS 驱动开发-at24c02适配
kernel版本:orange-pi-5.10-media
5.1x版本,i2c0默认是关闭的,需要在orangepiEnv.txt中添加overlays=i2c0字样开启i2c0。如果不添加,默认设置为开,需要更改dts中的配置。
1、修改sun50i-h6.dtsi文件,屏蔽以下内容
// uart3_rts_cts_pins: uart3-rts-cts-pins {// pins = "PD25", "PD26";// function = "uart3";// };
防止pd25 pd25 用做uart3
2、修改sun50i-h6-orangepi-3-lts.dts文件
添加一下内容
model = "OrangePi 3 LTS";
compatible = "xunlong,orangepi-3-lts", "allwinner,sun50i-h6";aliases {serial0 = &uart0;serial1 = &uart1;serial3 = &uart3;serial9 = &r_uart;i2c0 = &i2c0;spi1 = &spi1;ethernet0 = &emac;};&i2c0 {
clock-frequency = <100000>;
status = "okay";
at24c02@50{compatible = "atmel,24c02";reg = <0x50>;status = "okay";
};
};
compatible = “atmel,24c02”; 是适配驱动中at24.c
0x50 是at24c02 的七位器件地址。
重新编译,替换kernel跟dtb后,查看at24c02相关的节点是否生成。
应用层测试app编写。往节点写字符串,然后读出来,打印。
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>#define EEPROM_FILE_PATH "/sys/bus/nvmem/devices/0-00501/nvmem"
#define EEPROM_SIZE 256 // atc02 只有256个字节大小int earse_eeprom()
{char easre_data[EEPROM_SIZE] = {0};int fd;fd = open(EEPROM_FILE_PATH, O_RDWR);if (fd < 0) {printf("Can't open file %s\r\n", EEPROM_FILE_PATH);return -1;}lseek(fd,0,SEEK_SET);write(fd,easre_data,sizeof(easre_data)); //erase eepromclose(fd);return 0;
}int read_eeprom(char *r_data,int len)
{char easre_data[EEPROM_SIZE] = {0};int fd;size_t size;fd = open(EEPROM_FILE_PATH, O_RDWR);if (fd < 0) {printf("Can't open file %s\r\n", EEPROM_FILE_PATH);return -1;}lseek(fd,0,SEEK_SET);size = read(fd,r_data,len);close(fd);if (size == len)return 0;return -1;
}int write_eeprom(char *r_data,int len)
{char easre_data[EEPROM_SIZE] = {0};int fd;size_t size;fd = open(EEPROM_FILE_PATH, O_RDWR);if (fd < 0) {printf("Can't open file %s\r\n", EEPROM_FILE_PATH);return -1;}lseek(fd,0,SEEK_SET);size = write(fd,r_data,len);close(fd);if (size == len)return 0;return -1;
}int main(int argc,char *argv)
{char *test = "hello world,this is from eeprom";char r_data[256] = {0};earse_eeprom();
write_eeprom(test,strlen(test));
read_eeprom(r_data,strlen(test));printf("%s\n",r_data);
return 0;
}