一、简介
在实际的应用中,保存在RAM中的数据掉电后就丢失了,保存在FLASH中的数据又不能随意改变,也就是不能用它来记录变化的数值。但是在某些特定场合,我们又确实需要记录下某些数据,而它们还时常需要改变或更新,掉电之后数据还不能丢失。比如,我们的家用电表度数,电视机里边的频道记忆,一般都是使用EEPROM来保存数据,特点就是掉电后存储的数据不丢失。
EEPROM (Electrically Erasable Programmable read only memory)是指带电可擦可编程只读存储器。是一种掉电后数据不丢失的存储芯片。EEPROM可以在电脑上或专用设备上擦除已有信息,重新编程。一般用在即插即用。一般情况下,EEPROM拥有30万到100万次的寿命,也就是它可以反复写入30~100万次,而读取次数是无限的。
本文基于瑞芯微RK2206芯片 + Harmony LiteOS操作系统,通过i2c总线控制EEPROM读写。
我使用的EEPROM型号是K24C02,它是一个常用的基于IIC通信协议的EEPROM元件,例如ATMEL公司的AT24C02、CATALYST公司的CAT24C02和ST公司的ST24C02等芯片。IIC是一个通信协议,它拥有严密的通信时序逻辑要求,而EEPROM是一个元件,只是这个元件采样了IIC协议的接口与单片机相连而已,二者并没有必然的联系,EEPROM可以用其它接口,I2C也可以用在其它很多器件上。根据K24C02芯片手册,可获知如下:
(1)K24C02芯片的从设备地址。因本章节采用的2Kbit的EEPROM,所以该芯片I2C从设备地址为0x51。如下图所示:
图K24C02的从设备地址图
(2)K24C02芯片的读操作
K24C02芯片的读操作共分为3种,分别为当前地址读(Current Address Read)、随机读(Random Read)和连续读(Sequential Read)。
其中,当前地址读(Current Address Read)操作是控制i2c与K24C02通信,通信内容为:从设备地址(1个字节,bit0为1,表示读) + 数据(1个字节,K24C02发送给CPU的存储内容)。该读操作没有附带EEPROM的存储地址,存储地址是由上一次存储地址累加而来。如下图所示:
图 K24C02的当前地址读操作
而随机读操作则控制i2c与K24C02进行2次通信:
第1次i2c通信:从设备地址(1个字节,bit0为0,表示写) + 存储地址(1个字节,CPU发送给K24C02的存储地址)
第2次i2c通信:从设备地址(1个字节,bit0为1,表示读) + 数据(1个字节,K24C02发送给CPU的存储内容)。
具体数据传输如下图所示:
图 K24C02的随机地址读操作
连续读操作(Sequential Read)则控制控制i2c往K24C02发送N个字节,通信内容为:从设备地址(bit0为1,表示读) + N个数据(K24C02发送给CPU)。具体数据传输如下图所示:
图 K24C02的连续读操作
(3)K24C02芯片的写操作
K24C02芯片写数据操作可分为2种,分别为字节写操作(Byte Write)和页写操作(Page Write)。
其中,字节写操作(Byte Write)控制i2c与K24C02通信,通信内容为:从设备地址(1个字节,bit0为0,表示写) + 存储地址(1个字节) + 数据(1个字节,CPU发送给K24C0的存储内容)。具体数据传输如下图所示:
图 K24C02的字节写操作
页写操作(Page Write)则控制i2c与K24C02通信,通信内容为:从设备地址(1个字节,bit0为0,表示写) + 存储地址(1个字节) + 数据(N个字节,CPU发送给K24C0的存储内容)。其中,存储数据的N个字节,N不能超过Page大小(K24C02的页大小为8个字节)。
三、程序设计
程序控制RK2206芯片的I2C与K24C02芯片通信,每5秒往某一块存储空间(该存储空间地址依次累加)写入不同数据,然后再读取出来。
1、主程序设计
如图所示为EEPROM存储主程序流程图,开机LiteOS系统初始化后,进入主程序后。主程序首先初始化i2c总线。其次,程序进入主循环,每5秒将不同的数据写入到一块存储空间,然后再读取回去。其中,存储空间地址每次循环都累加32,数据也随着循环而累加1。
while (1)
{
printf("************ Eeprom Process ************\n");
printf("BlockSize = 0x%x\n", eeprom_get_blocksize());
/* 写EEPROM */
memset(buffer, 0, sizeof(buffer));
for (unsigned int i = 0; i < FOR_CHAR; i++)
{
buffer[i] = data_offset + i;
printf("Write Byte: %d = %c\n", addr_offset + i, buffer[i]);
}
ret = eeprom_write(addr_offset, buffer, FOR_CHAR);
if (ret != FOR_CHAR)
{
printf("EepromWrite failed(%d)\n", ret);
}
/* 读EEPROM */
memset(buffer, 0, sizeof(buffer));
ret = eeprom_read(addr_offset, buffer, FOR_CHAR);
if (ret != FOR_CHAR)
{
printf("Read Bytes: failed!\n");
}
else
{
for (unsigned int i = 0; i < FOR_CHAR; i++)
{
printf("Read Byte: %d = %c\n", addr_offset + i, buffer[i]);
}
}
data_offset++;
if (data_offset >= CHAR_END)
{
data_offset = CHAR_START;
}
addr_offset += FOR_ADDRESS;
if (addr_offset >= 200)
{
addr_offset = 0;
}
printf("\n");
LOS_Msleep(5000);
}
2、EEPROM初始化程序设计
主程序通过控制RK2206芯片的接口对i2c总线进行初始化。
#define EEPROM_I2C_BUS 0
#define EEPROM_I2C_ADDRESS 0x51
static I2cBusIo m_i2cBus = {
.scl = {.gpio = GPIO0_PA1, .func = MUX_FUNC3, .type = PULL_NONE, .drv = DRIVE_KEEP, .dir = LZGPIO_DIR_KEEP, .val = LZGPIO_LEVEL_KEEP},
.sda = {.gpio = GPIO0_PA0, .func = MUX_FUNC3, .type = PULL_NONE, .drv = DRIVE_KEEP, .dir = LZGPIO_DIR_KEEP, .val = LZGPIO_LEVEL_KEEP},
.id = FUNC_ID_I2C0,
.mode = FUNC_MODE_M2,
};
static unsigned int m_i2c_freq = 100000;
unsigned int eeprom_init()
{
if (I2cIoInit(m_i2cBus) != LZ_HARDWARE_SUCCESS) {
printf("%s, %d: I2cIoInit failed!\n", __FILE__, __LINE__);
return __LINE__;
}
if (LzI2cInit(EEPROM_I2C_BUS, m_i2c_freq) != LZ_HARDWARE_SUCCESS) {
printf("%s, %d: I2cInit failed!\n", __FILE__, __LINE__);
return __LINE__;
}
/* GPIO0_A0 => I2C1_SDA_M1 */
PinctrlSet(GPIO0_PA0, MUX_FUNC3, PULL_NONE, DRIVE_KEEP);
/* GPIO0_A1 => I2C1_SCL_M1 */
PinctrlSet(GPIO0_PA1, MUX_FUNC3, PULL_NONE, DRIVE_KEEP);
return 0;
}
3、EEPROM读操作程序设计
蜂鸣器控制程序当开启蜂鸣器时,打开蜂鸣器,并且设置PWM波的周期为100毫秒,其中占空比50%;当关闭蜂鸣器时,则停止蜂鸣器。
#define EEPROM_I2C_BUS 0
#define EEPROM_I2C_ADDRESS 0x51
/* EEPROM型号:K24C02,2Kbit(256Byte),32页,每页8个字节(Byte) */
#define EEPROM_ADDRESS_MAX 256
#define EEPROM_PAGE 8
unsigned int eeprom_readbyte(unsigned int addr, unsigned char *data)
{
unsigned int ret = 0;
unsigned char buffer[1];
LzI2cMsg msgs[2];
/* K24C02的存储地址是0~255 */
if (addr >= EEPROM_ADDRESS_MAX) {
printf("%s, %s, %d: addr(0x%x) >= EEPROM_ADDRESS_MAX(0x%x)\n", __FILE__, __func__, __LINE__, addr, EEPROM_ADDRESS_MAX);
return 0;
}
buffer[0] = (unsigned char)addr;
msgs[0].addr = EEPROM_I2C_ADDRESS;
msgs[0].flags = 0;
msgs[0].buf = &buffer[0];
msgs[0].len = 1;
msgs[1].addr = EEPROM_I2C_ADDRESS;
msgs[1].flags = I2C_M_RD;
msgs[1].buf = data;
msgs[1].len = 1;
ret = LzI2cTransfer(EEPROM_I2C_BUS, msgs, 2);
if (ret != LZ_HARDWARE_SUCCESS) {
printf("%s, %s, %d: LzI2cTransfer failed(%d)!\n", __FILE__, __func__, __LINE__, ret);
return 0;
}
return 1;
}
unsigned int eeprom_read(unsigned int addr, unsigned char *data, unsigned int data_len)
{
unsigned int ret = 0;
if (addr >= EEPROM_ADDRESS_MAX) {
printf("%s, %s, %d: addr(0x%x) >= EEPROM_ADDRESS_MAX(0x%x)\n", __FILE__, __func__, __LINE__, addr, EEPROM_ADDRESS_MAX);
return 0;
}
if ((addr + data_len) > EEPROM_ADDRESS_MAX) {
printf("%s, %s, %d: addr + len(0x%x) > EEPROM_ADDRESS_MAX(0x%x)\n", __FILE__, __func__, __LINE__, addr + data_len, EEPROM_ADDRESS_MAX);
return 0;
}
ret = eeprom_readbyte(addr, data);
if (ret != 1) {
printf("%s, %s, %d: EepromReadByte failed(%d)\n", __FILE__, __func__, __LINE__, ret);
return 0;
}
if (data_len > 1) {
ret = LzI2cRead(EEPROM_I2C_BUS, EEPROM_I2C_ADDRESS, &data[1], data_len - 1);
if (ret < 0) {
printf("%s, %s, %d: LzI2cRead failed(%d)!\n", __FILE__, __func__, __LINE__, ret);
return 0;
}
}
return data_len;
}
4、EEPROM写操作程序设计
主程序根据存储地址、存储数据和数据长度的不同,选用字节写操作或页写操作。具体源代码如下所示:
#define EEPROM_I2C_BUS 0
#define EEPROM_I2C_ADDRESS 0x51
/* EEPROM型号:K24C02,2Kbit(256Byte),32页,每页8个字节(Byte) */
#define EEPROM_ADDRESS_MAX 256
#define EEPROM_PAGE 8
unsigned int eeprom_writebyte(unsigned int addr, unsigned char data)
{
unsigned int ret = 0;
LzI2cMsg msgs[1];
unsigned char buffer[2];
/* K24C02的存储地址是0~255 */
if (addr >= EEPROM_ADDRESS_MAX) {
printf("%s, %s, %d: addr(0x%x) >= EEPROM_ADDRESS_MAX(0x%x)\n", __FILE__, __func__, __LINE__, addr, EEPROM_ADDRESS_MAX);
return 0;
}
buffer[0] = (unsigned char)(addr & 0xFF);
buffer[1] = data;
msgs[0].addr = EEPROM_I2C_ADDRESS;
msgs[0].flags = 0;
msgs[0].buf = &buffer[0];
msgs[0].len = 2;
ret = LzI2cTransfer(EEPROM_I2C_BUS, msgs, 1);
if (ret != LZ_HARDWARE_SUCCESS) {
printf("%s, %s, %d: LzI2cTransfer failed(%d)!\n", __FILE__, __func__, __LINE__, ret);
return 0;
}
/* K24C02芯片需要时间完成写操作,在此之前不响应其他操作*/
eeprog_delay_usec(1000);
return 1;
}
unsigned int eeprom_writepage(unsigned int addr, unsigned char *data, unsigned int data_len)
{
unsigned int ret = 0;
LzI2cMsg msgs[1];
unsigned char buffer[EEPROM_PAGE + 1];
/* K24C02的存储地址是0~255 */
if (addr >= EEPROM_ADDRESS_MAX) {
printf("%s, %s, %d: addr(0x%x) >= EEPROM_ADDRESS_MAX(0x%x)\n", __FILE__, __func__, __LINE__, addr, EEPROM_ADDRESS_MAX);
return 0;
}
if ((addr % EEPROM_PAGE) != 0) {
printf("%s, %s, %d: addr(0x%x) is not page addr(0x%x)\n", __FILE__, __func__, __LINE__, addr, EEPROM_PAGE);
return 0;
}
if ((addr + data_len) > EEPROM_ADDRESS_MAX) {
printf("%s, %s, %d: addr + data_len(0x%x) > EEPROM_ADDRESS_MAX(0x%x)\n", __FILE__, __func__, __LINE__, addr + data_len, EEPROM_ADDRESS_MAX);
return 0;
}
if (data_len > EEPROM_PAGE) {
printf("%s, %s, %d: data_len(%d) > EEPROM_PAGE(%d)\n", __FILE__, __func__, __LINE__, data_len, EEPROM_PAGE);
return 0;
}
buffer[0] = addr;
memcpy(&buffer[1], data, data_len);
msgs[0].addr = EEPROM_I2C_ADDRESS;
msgs[0].flags = 0;
msgs[0].buf = &buffer[0];
msgs[0].len = 1 + data_len;
ret = LzI2cTransfer(EEPROM_I2C_BUS, msgs, 1);
if (ret != LZ_HARDWARE_SUCCESS) {
printf("%s, %s, %d: LzI2cTransfer failed(%d)!\n", __FILE__, __func__, __LINE__, ret);
return 0;
}
/* K24C02芯片需要时间完成写操作,在此之前不响应其他操作*/
eeprog_delay_usec(1000);
return data_len;
}
unsigned int eeprom_write(unsigned int addr, unsigned char *data, unsigned int data_len)
{
unsigned int ret = 0;
unsigned int offset_current = 0;
unsigned int page_start, page_end;
unsigned char is_data_front = 0;
unsigned char is_data_back = 0;
unsigned int len;
if (addr >= EEPROM_ADDRESS_MAX) {
printf("%s, %s, %d: addr(0x%x) >= EEPROM_ADDRESS_MAX(0x%x)\n", __FILE__, __func__, __LINE__, addr, EEPROM_ADDRESS_MAX);
return 0;
}
if ((addr + data_len) > EEPROM_ADDRESS_MAX) {
printf("%s, %s, %d: addr + len(0x%x) > EEPROM_ADDRESS_MAX(0x%x)\n", __FILE__, __func__, __LINE__, addr + data_len, EEPROM_ADDRESS_MAX);
return 0;
}
/* 判断addr是否是页地址 */
page_start = addr / EEPROM_PAGE;
if ((addr % EEPROM_PAGE) != 0) {
page_start += 1;
is_data_front = 1;
}
/* 判断addr + data_len是否是页地址 */
page_end = (addr + data_len) / EEPROM_PAGE;
if ((addr + data_len) % EEPROM_PAGE != 0) {
page_end += 1;
is_data_back = 1;
}
offset_current = 0;
/* 处理前面非页地址的数据,如果是页地址则不执行 */
for (unsigned int i = addr; i < (page_start * EEPROM_PAGE); i++) {
ret = eeprom_writebyte(i, data[offset_current]);
if (ret != 1) {
printf("%s, %s, %d: EepromWriteByte failed(%d)\n", __FILE__, __func__, __LINE__, ret);
return offset_current;
}
offset_current++;
}
/* 处理后续的数据,如果数据长度不足一个Page,则不执行 */
for (unsigned int page = page_start; page < page_end; page++) {
len = EEPROM_PAGE;
if ((page == (page_end - 1)) && (is_data_back)) {
len = (addr + data_len) % EEPROM_PAGE;
}
ret = eeprom_writepage(page * EEPROM_PAGE, &data[offset_current], len);
if (ret != len) {
printf("%s, %s, %d: EepromWritePage failed(%d)\n", __FILE__, __func__, __LINE__, ret);
return offset_current;
}
offset_current += EEPROM_PAGE;
}
return data_len;
四、编译过程
1、搭建和下载源代码
我已将OpenHarmony源代码上传到Gitee社区中,大家可以根据以下网址下载。
https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk2206-openharmony3.0lts
注意:编译环境可根据以下网址来操作:https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk2206-openharmony3.0lts/blob/master/vendor/lockzhiner/rk2206/README_zh.md
2、打开sdk下面路径的文件
/vendor/lockzhiner/rk2206/samples/b3_eeprom/eeprom_example.c
注意:Gitee上的EEPROM案例为通用案例,请大家根据上述的需求修改相关源代码。
3、修改编译脚本
修改 vendor/lockzhiner/rk2206/sample 路径下 BUILD.gn 文件,指定 eeprom_example 参与编译。
"./b3_eeprom:eeprom_example",
修改 device/lockzhiner/rk2206/sdk_liteos 路径下 Makefile 文件,添加 -leeprom_example 参与编译。
hardware_LIBS = -lhal_iothardware -lhardware -leeprom_example
4、编译固件
hb set -root .
hb set
hb build -f
5、烧写固件
请参考Gitee网址的说明手册(“烧录打印”章节):https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk2206-openharmony3.0lts/blob/master/device/rockchip/README_zh.md
五、实验结果
程序编译烧写到开发板后,按下开发板的RESET按键,通过串口软件查看日志如下:
************ Eeprom Process ************
BlockSize = 0x8
Write Byte: 3 = !
Write Byte: 4 = "
Write Byte: 5 = #
Write Byte: 6 = $
Write Byte: 7 = %
Write Byte: 8 = &
Write Byte: 9 = '
Write Byte: 10 = (
Write Byte: 11 = )
Write Byte: 12 = *
Write Byte: 13 = +
Write Byte: 14 = ,
Write Byte: 15 = -
Write Byte: 16 = .
Write Byte: 17 = /
Write Byte: 18 = 0
Write Byte: 19 = 1
Write Byte: 20 = 2
Write Byte: 21 = 3
Write Byte: 22 = 4
Write Byte: 23 = 5
Write Byte: 24 = 6
Write Byte: 25 = 7
Write Byte: 26 = 8
Write Byte: 27 = 9
Write Byte: 28 = :
Write Byte: 29 = ;
Write Byte: 30 = <
Write Byte: 31 = =
Write Byte: 32 = >
Read Byte: 3 = !
Read Byte: 4 = "
Read Byte: 5 = #
Read Byte: 6 = $
Read Byte: 7 = %
Read Byte: 8 = &
Read Byte: 9 = '
Read Byte: 10 = (
Read Byte: 11 = )
Read Byte: 12 = *
Read Byte: 13 = +
Read Byte: 14 = ,
Read Byte: 15 = -
Read Byte: 16 = .
Read Byte: 17 = /
Read Byte: 18 = 0
Read Byte: 19 = 1
Read Byte: 20 = 2
Read Byte: 21 = 3
Read Byte: 22 = 4
Read Byte: 23 = 5
Read Byte: 24 = 6
Read Byte: 25 = 7
Read Byte: 26 = 8
Read Byte: 27 = 9
Read Byte: 28 = :
Read Byte: 29 = ;
Read Byte: 30 = <
Read Byte: 31 = =
Read Byte: 32 = >
......
审核编辑:汤梓红
-
EEPROM
+关注
关注
9文章
1019浏览量
81564 -
开发板
+关注
关注
25文章
5033浏览量
97382 -
RK2206
+关注
关注
0文章
34浏览量
1781 -
小凌派
+关注
关注
0文章
27浏览量
1199
发布评论请先 登录
相关推荐
评论