From 3b124e5211fd92e81f79a8e44ae114f53093e8d8 Mon Sep 17 00:00:00 2001 From: tanzhuolin <1045021517@qq.com> Date: Mon, 9 May 2022 22:12:58 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 33 +++++++++++++++++++---- at24cxx.c | 81 ++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 94 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 9bff72c..1fe0e9b 100644 --- a/README.md +++ b/README.md @@ -119,11 +119,11 @@ at24cxx 软件包提供了丰富的测试命令,项目只要在 RT-Thread 上 ``` msh />at24cxx Usage: -at24cxx probe - probe sensor by given name -at24cxx check - check device -at24cxx write - write data -at24cxx read - read data -msh /> +at24cxx probe - probe eeprom by given name +at24cxx check - check eeprom at24cxx +at24cxx
- read eeprom at24cxx data +at24cxx
- write eeprom at24cxx data +msh > ``` #### 3.2.1 在指定的 i2c 总线上探测传感器 @@ -136,8 +136,31 @@ msh /> msh />at24cxx probe i2c88 #探测失败,提示对应的 I2C 设备找不到 [E/aht10] can't find at24cxx device on 'i2c88' msh /> +msh >at24cxx read 0 10 +read at24cxx ok + -------------------------------------------- +31 32 33 34 35 36 37 38 39 30 + -------------------------------------------- + 1 2 3 4 5 6 7 8 9 0 +msh >at24cxx write 0 abcdef +write ok +``` +#### 3.2.2 读写数据 + ``` +msh /> + +msh >at24cxx write 0 abcdef #从 0 地址开始写,写 abcdef 字符串 +write ok +msh >at24cxx read 0 10 #从 0 地址开始读,读 10 个字节 +read at24cxx ok + -------------------------------------------- +61 62 63 64 65 66 37 38 39 30 + -------------------------------------------- + a b c d e f 7 8 9 0 +msh > +``` ## 4 注意事项 - 请在at24cxx.h中修改EE_TYPE为自己使用的型号(默认为AT25C512) 。 diff --git a/at24cxx.c b/at24cxx.c index d40ba89..c48d4f8 100644 --- a/at24cxx.c +++ b/at24cxx.c @@ -456,13 +456,14 @@ void at24cxx_deinit(at24cxx_device_t dev) rt_free(dev); } -uint8_t TEST_BUFFER[] = "WELCOM TO RTT"; -#define SIZE sizeof(TEST_BUFFER) + +static uint8_t opt_buffer[2048]; void at24cxx(int argc, char *argv[]) { static at24cxx_device_t dev = RT_NULL; - + uint16_t opt_addr, opt_len; + if (argc > 1) { if (!strcmp(argv[1], "probe")) @@ -489,13 +490,42 @@ void at24cxx(int argc, char *argv[]) { if (dev) { - uint8_t testbuffer[50]; + if( argc != 4 ) + { + rt_kprintf("Usage: at24cxx
.\n"); + return ; + } + + opt_addr = atoi(argv[2]); + opt_len = atoi(argv[3]); + if( opt_len > sizeof(opt_buffer) ) + { + rt_kprintf("read max len : %d.\n", sizeof(opt_buffer)); + return ; + } /* read the eeprom data */ - at24cxx_read(dev, 0, testbuffer, SIZE); - - rt_kprintf("read at24cxx : %s\n", testbuffer); - + if( at24cxx_read(dev, opt_addr, opt_buffer, opt_len) == RT_EOK ) + { + rt_kprintf("read at24cxx ok"); + + rt_kprintf("\n -------------------------------------------- \n"); + for(uint16_t i=0; i
.\n"); + return ; + } + + opt_addr = atoi(argv[2]); + + if( at24cxx_write(dev, opt_addr, (uint8_t *)argv[3], rt_strlen(argv[3])) == RT_EOK ) + { + rt_kprintf("write ok\n"); + } + else + { + rt_kprintf("write fail\n"); + } + } + else + { + rt_kprintf("Please using 'at24cxx probe ' first\n"); + } } else if (!strcmp(argv[1], "check")) { @@ -516,16 +567,16 @@ void at24cxx(int argc, char *argv[]) } else { - rt_kprintf("Unknown command. Please enter 'at24cxx0' for help\n"); + rt_kprintf("Unknown command. Please enter 'at24cxx' for help\n"); } } else { rt_kprintf("Usage:\n"); - rt_kprintf("at24cxx probe - probe eeprom by given name\n"); - rt_kprintf("at24cxx check - check eeprom at24cxx \n"); - rt_kprintf("at24cxx read - read eeprom at24cxx data\n"); - rt_kprintf("at24cxx write - write eeprom at24cxx data\n"); + rt_kprintf("at24cxx probe - probe eeprom by given name\n"); + rt_kprintf("at24cxx check - check eeprom at24cxx \n"); + rt_kprintf("at24cxx
- read eeprom at24cxx data\n"); + rt_kprintf("at24cxx
- write eeprom at24cxx data\n"); } } -- Gitee From 66b0d6dd736a9e339ac8c310df16aa92bbf777ac Mon Sep 17 00:00:00 2001 From: tanzhuolin <1045021517@qq.com> Date: Mon, 9 May 2022 14:16:31 +0000 Subject: [PATCH 2/3] update README.md. --- README.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/README.md b/README.md index 1fe0e9b..e406591 100644 --- a/README.md +++ b/README.md @@ -136,20 +136,11 @@ msh /> msh />at24cxx probe i2c88 #探测失败,提示对应的 I2C 设备找不到 [E/aht10] can't find at24cxx device on 'i2c88' msh /> -msh >at24cxx read 0 10 -read at24cxx ok - -------------------------------------------- -31 32 33 34 35 36 37 38 39 30 - -------------------------------------------- - 1 2 3 4 5 6 7 8 9 0 -msh >at24cxx write 0 abcdef -write ok ``` #### 3.2.2 读写数据 ``` msh /> - msh >at24cxx write 0 abcdef #从 0 地址开始写,写 abcdef 字符串 write ok msh >at24cxx read 0 10 #从 0 地址开始读,读 10 个字节 -- Gitee From a339d10ceb04e91291d6c269257cb899d76af253 Mon Sep 17 00:00:00 2001 From: tanzhuolin <1045021517@qq.com> Date: Mon, 9 May 2022 22:56:42 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=AF=BB=E5=86=99=E8=B6=85=E5=87=BA256?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- at24cxx.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/at24cxx.c b/at24cxx.c index c48d4f8..eb6ed65 100644 --- a/at24cxx.c +++ b/at24cxx.c @@ -76,10 +76,30 @@ static rt_err_t read_regs(at24cxx_device_t dev, rt_uint8_t len, rt_uint8_t *buf) return -RT_ERROR; } } + +void set_dev_AddrInput(at24cxx_device_t dev, uint16_t opt_addr) +{ +#if (EE_TYPE == AT24C16) + dev->AddrInput &= ~0x07; + dev->AddrInput |= opt_addr >> 8; +#elif (EE_TYPE == AT24C08) + dev->AddrInput &= ~0x03; + dev->AddrInput |= opt_addr >> 8; +#elif (EE_TYPE == AT24C04) + dev->AddrInput &= ~0x01; + dev->AddrInput |= opt_addr >> 8; +#elif (EE_TYPE == AT24C02) + +#endif +} + uint8_t at24cxx_read_one_byte(at24cxx_device_t dev, uint16_t readAddr) { rt_uint8_t buf[2]; rt_uint8_t temp; + + set_dev_AddrInput(dev, readAddr); + #if (EE_TYPE > AT24C16) buf[0] = (uint8_t)(readAddr>>8); buf[1] = (uint8_t)readAddr; @@ -98,6 +118,9 @@ uint8_t at24cxx_read_one_byte(at24cxx_device_t dev, uint16_t readAddr) rt_err_t at24cxx_write_one_byte(at24cxx_device_t dev, uint16_t writeAddr, uint8_t dataToWrite) { rt_uint8_t buf[3]; + + set_dev_AddrInput(dev, writeAddr); + #if (EE_TYPE > AT24C16) buf[0] = (uint8_t)(writeAddr>>8); buf[1] = (uint8_t)writeAddr; @@ -107,8 +130,6 @@ rt_err_t at24cxx_write_one_byte(at24cxx_device_t dev, uint16_t writeAddr, uint8_ buf[0] = writeAddr; //cmd buf[1] = dataToWrite; //buf[2] = data[1]; - - if (rt_i2c_master_send(dev->i2c, AT24CXX_ADDR | dev->AddrInput, 0, buf, 2) == 2) #endif return RT_EOK; @@ -122,6 +143,8 @@ rt_err_t at24cxx_read_page(at24cxx_device_t dev, uint32_t readAddr, uint8_t *pBu struct rt_i2c_msg msgs[2]; uint8_t AddrBuf[2]; + set_dev_AddrInput(dev, readAddr); + msgs[0].addr = AT24CXX_ADDR | dev->AddrInput; msgs[0].flags = RT_I2C_WR ; @@ -134,8 +157,8 @@ rt_err_t at24cxx_read_page(at24cxx_device_t dev, uint32_t readAddr, uint8_t *pBu AddrBuf[0] = readAddr; msgs[0].buf = AddrBuf; msgs[0].len = 1; + #endif - msgs[1].addr = AT24CXX_ADDR | dev->AddrInput; msgs[1].flags = RT_I2C_RD; msgs[1].buf = pBuffer; @@ -154,6 +177,8 @@ rt_err_t at24cxx_write_page(at24cxx_device_t dev, uint32_t wirteAddr, uint8_t *p struct rt_i2c_msg msgs[2]; uint8_t AddrBuf[2]; + set_dev_AddrInput(dev, wirteAddr); + msgs[0].addr = AT24CXX_ADDR | dev->AddrInput; msgs[0].flags = RT_I2C_WR ; -- Gitee