From 02e6359b3a0a106bbbccf99dac282ae9247b0f90 Mon Sep 17 00:00:00 2001 From: Yaochenger <1516081466@qq.com> Date: Thu, 18 Apr 2024 17:58:03 +0800 Subject: [PATCH] =?UTF-8?q?[add]=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=93?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 152 +++++++++++++ include/at24cxxx.h | 55 +++++ package.yaml | 60 ++++++ src/at24cxx.c | 526 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 793 insertions(+) create mode 100644 README.md create mode 100644 include/at24cxxx.h create mode 100644 package.yaml create mode 100644 src/at24cxx.c diff --git a/README.md b/README.md new file mode 100644 index 0000000..054b3da --- /dev/null +++ b/README.md @@ -0,0 +1,152 @@ +# AT24CXX 软件包 + +## 1 介绍 + +AT24CXX 软件包提供了at24cxx 系列 EEPROM 基本功能。本文介绍该软件包的基本读写功能,以及 `Finsh/MSH` 测试命令等。 +目前已在 at24c02, at24c512验证通过。 + +### 1.1 目录结构 + +| 名称 | 说明 | +| ---- | ---- | +| at24cxx.h | EEPROM 使用头文件 | +| at24cxx.c | EEPROM 使用源代码 | +| SConscript | RT-Thread 默认的构建脚本 | +| README.md | 软件包使用说明 | +| at24cxx_datasheet.pdf | 官方数据手册 | + +### 1.2 许可证 + +AT24CXX 软件包遵循 Apache-2.0 许可,详见 LICENSE 文件。 + +### 1.3 依赖 + +依赖 `RT-Thread I2C` 设备驱动框架。 + +## 2 获取软件包 + +使用 `at24cxx` 软件包需要在 RT-Thread 的包管理器中选择它,具体路径如下: + +``` +RT-Thread online packages + peripheral libraries and drivers ---> + [*] at24cxx: eeprom at24cxx driver library ---> + Version (latest) ---> +``` + + +每个功能的配置说明如下: + +- `at24cxx: ee2prom at24cxx driver library`:选择使用 `at24cxx` 软件包; +- `Version`:配置软件包版本,默认最新版本。 + +然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。 + +## 3 使用 at24cxx 软件包 + +按照前文介绍,获取 `at24cxx` 软件包后,就可以按照 下文提供的 API 使用 ee2prom `at24cxx 与 `Finsh/MSH` 命令进行测试,详细内容如下。 + +### 3.1 API + +#### 3.1.1 初始化 + +`at24cxx_device_t at24cxx_init(const char *i2c_bus_name, uint8_t addr) + +根据总线名称,自动初始化对应的 AT24CXX 设备,具体参数与返回说明如下表 + +| 参数 | 描述 | +| :----- | :----------------------- | +| name | i2c 设备名称 | +| addr | 地址 | +| **返回** | **描述** | +| != NULL | 将返回 at24cxx 设备对象 | +| = NULL | 查找失败 | + +#### 3.1.2 反初始化 + +void at24cxx_deinit(aht10_device_t dev) + +如果设备不再使用,反初始化将回收 at24cxx 设备的相关资源,具体参数说明如下表 + +| 参数 | 描述 | +| :--- | :------------- | +| dev | at24cxx 设备对象 | + +#### 3.1.3 读取 + +rt_err_t at24cxx_read(struct rt_i2c_bus_device *bus,uint16_t ReadAddr,uint8_t *pBuffer,uint16_t NumToRead) + +通过 `at24cxx` 读取 eeprom ,在AT24CXX里面的指定地址开始读出指定个数的数据,具体参数与返回说明如下表 + +| 参数 | 描述 | +| :------- | :------------- | +| bus | at24cxx 设备对象 | +| **返回** | **描述** | +| != RT_EOK | 读取成功 | +| = RT_EOK| 读取失败 | + +#### 3.1.4 写入 + +rt_err_t at24cxx_write(struct rt_i2c_bus_device *bus,uint16_t WriteAddr,uint8_t *pBuffer,uint16_t NumToWrite) + + +通过 `at24cxx` 写入,在AT24CXX里面的指定地址开始写入指定个数的数据,具体参数与返回说明如下表 + +| 参数 | 描述 | +| :------- | :------------ | +| bus | at24cxx 设备对象 | +| **返回** | **描述** | +| != RT_EOK | 写入失败 | +| =RT_EOK | 写入成功 | + +#### 3.1.5 查看设备 + +rt_err_t at24cxx_check(struct rt_i2c_bus_device *bus) + +查看是否存在eeprom 设备,通过在最后一个字节写入标志位,进行判断,具体参数与返回说明如下表 + +| 参数 | 描述 | +| :------- | :------------ | +| bus | at24cxx 设备对象 | +| **返回** | **描述** | +| != RT_EOK | 设备不存在 | +| =RT_EOK | 设备存在 | + + +### 3.2 Finsh/MSH 测试命令 + +at24cxx 软件包提供了丰富的测试命令,项目只要在 RT-Thread 上开启 Finsh/MSH 功能即可。在做一些基于 `at24cxx` 的应用开发、调试时,这些命令会非常实用,它可以准确的读取指传感器测量的温度与湿度。具体功能可以输入 `at24cxx` ,可以查看完整的命令列表 + +``` +msh />at24cxx +Usage: +at24cxx probe - probe sensor by given name +at24cxx check - check device +at24cxx write - write data +at24cxx read - read data +msh /> +``` + +#### 3.2.1 在指定的 i2c 总线上探测传感器 + +当第一次使用 `at24cxx` 命令时,直接输入 `at24cxx probe ` ,其中 `` 为指定的 i2c 总线,例如:i2c0。如果有这个设备,就不会提示错误;如果总线上没有这个设备,将会显示提示找不到相关设备,日志如下: + +``` +msh />at24cxx probe i2c1 #探测成功,没有错误日志 +msh /> +msh />at24cxx probe i2c88 #探测失败,提示对应的 I2C 设备找不到 +[E/aht10] can't find at24cxx device on 'i2c88' +msh /> +``` + +## 4 注意事项 + +- 请在at24cxx.h中修改EE_TYPE为自己使用的型号(默认为AT25C512) 。 +- 请在at24cxx.h中修改EE_TWR为自己使用EEPROM的Write Cycle Time,具体值请查看芯片datasheet(默认为5ms) 。 +- 从设备地址为7位地址 0x50, 而不是 0xA0 。 + +## 5 联系方式 + +* 维护:[XiaojieFan](https://github.com/XiaojieFan) +* 主页:https://github.com/XiaojieFan/at24cxx + diff --git a/include/at24cxxx.h b/include/at24cxxx.h new file mode 100644 index 0000000..631e25b --- /dev/null +++ b/include/at24cxxx.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-13 XiaojieFan the first version + * 2019-12-04 RenMing Use PAGE WRITE instead of BYTE WRITE and input address can be selected + */ + +#ifndef __AT24CXX_H__ +#define __AT24CXX_H__ + +#include +#include +#include +#include +#include "aos/cli.h" + +#define AT24C01 0 +#define AT24C02 1 +#define AT24C04 2 +#define AT24C08 3 +#define AT24C16 4 +#define AT24C32 5 +#define AT24C64 6 +#define AT24C128 7 +#define AT24C256 8 +#define AT24C512 9 +#define AT24CTYPE 10 // Number of supported types + +#define EE_TWR 10 + +#ifndef EE_TYPE +#define EE_TYPE AT24C02 +#endif + +#define PKG_USING_AT24CXX + +struct at24cxx_device +{ + rvm_dev_t *i2c; + aos_mutex_t lock; + uint8_t AddrInput; +}; +typedef struct at24cxx_device *at24cxx_device_t; + +extern at24cxx_device_t at24cxx_init(const char *i2c_bus_name, unsigned int iic_idx, uint8_t AddrInput); +extern signed long at24cxx_read(at24cxx_device_t dev, uint32_t ReadAddr, uint8_t *pBuffer, uint16_t NumToRead); +extern signed long at24cxx_write(at24cxx_device_t dev, uint32_t WriteAddr, uint8_t *pBuffer, uint16_t NumToWrite); +extern signed long at24cxx_page_read(at24cxx_device_t dev, uint32_t ReadAddr, uint8_t *pBuffer, uint16_t NumToRead); +extern signed long at24cxx_page_write(at24cxx_device_t dev, uint32_t WriteAddr, uint8_t *pBuffer, uint16_t NumToWrite); + +#endif diff --git a/package.yaml b/package.yaml new file mode 100644 index 0000000..ff7cf16 --- /dev/null +++ b/package.yaml @@ -0,0 +1,60 @@ + +## 第一部分: 基础信息 +name: at24cxx # <必选项> 包名称 (符合C语言变量命名规则),长度少于等于64字节 +version: develop # <必选项> 组件版本号 +description: at24cxx rtthread 适配 # <必选项> 建议至少20字以上 +type: common # <必选项> 组件类型,为:solution, chip, board, common, sdk +tag: 核心模块 # <可选项> 组件分类,缺省值: '' +keywords: # <可选项> 标签,会影响到组件被搜索的效果,合理的标签很重要 + - base +license: Apache license v2.0 # <可选项> 源代码的许可证,要确保所有代码、文件的许可证不冲突。如:MIT,Apache license v2.0,BSD + +## 第二部分:依赖信息 +# 指定该组件依赖的组件及版本 +# sdk_chip: # <可选项> 该组件依赖sdk组件,合理的依赖才能保证组件能编译、使用 +# - sdk_chip_csky_dummy: v7.4.0 +# - sdk_chip_riscv_dummy: v7.4.0 +depends: # <可选项> 该组件依赖其他的组件,合理的依赖才能保证组件能编译、使用 + - devices: develop + +## 第四部分:编译连接信息 +# build_config: # <可选项> 编译配置项 +# include: # <可选项> 编译时,影响编译器的-I 参数 ,全局有效 +# - src # include 只能是该软件包下的目录,不能使用外部目录 +# internal_include: # <可选项> 编译时,影响编译器的-I 参数 ,组件内有效 +# - include +# cflag: '' # <可选项> C 编译器所需要要的编译参数 +# cxxflag: '' # <可选项> CXX 编译器所需要要的编译参数 +# asmflag: '' # <可选项> 汇编器所需要要参数 +# define: # <可选项> 宏定义, 增加编译器的-D 选项,如: +# XXX: 1 # -DXXX=1 +# AAA: 1 # -DAAA +# STR: "abc" # -DSTR="abc" +# libs: # 该组件中支持的二进制静态库,如:libxxx.a, libyyy.a +# - xxx # -lxxx +# - yyy # -lyyy +# libpath: # 指定静态库所在的路径(相对于该组件路径) +# - libs # -Llibs +build_config: + cflag: -O1 + include: + - include + +# source_file: # <可选项> 指定参与编译的源代码文件,支持通配符,采用相对路径 + # - src/*.c # 例:组件 src 目录下所有的扩展名为 c 的源代码文件 +source_file: + - src/*.c + +## 第五部分:配置信息 +# def_config: # 组件的可配置项 +# CONFIG_DEBUG: y +# CONFIG_PARAM_NOT_CHECK: y +# CONFIG_CLI: y +def_config: + +## 第六部分:安装信息 +# install: +# - dest: include/ # 安装的目的路径 dest是相对路径,通常是相对于YoC SDK 安装目录 +# source: # 安装源列表 +# - src/*.h # 支持通配符,相对路径 + diff --git a/src/at24cxx.c b/src/at24cxx.c new file mode 100644 index 0000000..0ffe070 --- /dev/null +++ b/src/at24cxx.c @@ -0,0 +1,526 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-13 XiaojieFan the first version + * 2019-12-04 RenMing ADD PAGE WRITE and input address can be selected + * 2022-10-11 GuangweiRen Delay 2ms after writing one byte + */ + +#include +#include +#include + +#include "at24cxxx.h" +extern int32_t aos_debug_printf(const char *fmt, ...); +static aos_mutex_t mutex = NULL; + +#ifdef PKG_USING_AT24CXX + +#define AT24CXX_ADDR (0xA0 >> 1) //A0 A1 A2 connect GND + +#if (EE_TYPE == AT24C01) + #define AT24CXX_PAGE_BYTE 8 + #define AT24CXX_MAX_MEM_ADDRESS 128 +#elif (EE_TYPE == AT24C02) + #define AT24CXX_PAGE_BYTE 8 + #define AT24CXX_MAX_MEM_ADDRESS 256 +#elif (EE_TYPE == AT24C04) + #define AT24CXX_PAGE_BYTE 16 + #define AT24CXX_MAX_MEM_ADDRESS 512 +#elif (EE_TYPE == AT24C08) + #define AT24CXX_PAGE_BYTE 16 + #define AT24CXX_MAX_MEM_ADDRESS 1024 +#elif (EE_TYPE == AT24C16) + #define AT24CXX_PAGE_BYTE 16 + #define AT24CXX_MAX_MEM_ADDRESS 2048 +#elif (EE_TYPE == AT24C32) + #define AT24CXX_PAGE_BYTE 32 + #define AT24CXX_MAX_MEM_ADDRESS 4096 +#elif (EE_TYPE == AT24C64) + #define AT24CXX_PAGE_BYTE 32 + #define AT24CXX_MAX_MEM_ADDRESS 8192 +#elif (EE_TYPE == AT24C128) + #define AT24CXX_PAGE_BYTE 64 + #define AT24CXX_MAX_MEM_ADDRESS 16384 +#elif (EE_TYPE == AT24C256) + #define AT24CXX_PAGE_BYTE 64 + #define AT24CXX_MAX_MEM_ADDRESS 32768 +#elif (EE_TYPE == AT24C512) + #define AT24CXX_PAGE_BYTE 128 + #define AT24CXX_MAX_MEM_ADDRESS 65536 +#endif + +static signed long read_regs(at24cxx_device_t dev, uint16_t memaddr, unsigned char len, unsigned char *buf) +{ + if (rvm_hal_iic_mem_read(dev->i2c, AT24CXX_ADDR | dev->AddrInput, memaddr, (uint16_t)0, buf, len, 0u) == 0) + { + return 0; + } + else + { + return -1; + } +} + +uint8_t at24cxx_read_one_byte(at24cxx_device_t dev, uint16_t readAddr) +{ + unsigned char buf[3]; + unsigned char temp = 0; +#if (EE_TYPE > AT24C16) + buf[0] = (uint8_t)(readAddr>>8); + buf[1] = (uint8_t)readAddr; +#else + buf[0] = readAddr; +#endif + read_regs(dev, readAddr, 1, (unsigned char *)&temp); + return temp; +} + +signed long at24cxx_write_one_byte(at24cxx_device_t dev, uint16_t writeAddr, uint8_t dataToWrite) +{ + unsigned char buf[3]; +#if (EE_TYPE > AT24C16) + buf[0] = (uint8_t)(writeAddr>>8); + buf[1] = (uint8_t)writeAddr; + buf[2] = dataToWrite; + if (rvm_hal_iic_master_send(dev->i2c, AT24CXX_ADDR | dev->AddrInput, (uint8_t *)buf, 3, 100u) == 0) +#else + buf[0] = writeAddr; //cmd + buf[1] = dataToWrite; + + if (rvm_hal_iic_master_send(dev->i2c, AT24CXX_ADDR | dev->AddrInput, (uint8_t *)buf, 2, 100u) == 0) +#endif + { + return 0; + } + else + { + return -1; + } + +} + +signed long at24cxx_read_page(at24cxx_device_t dev, uint32_t readAddr, uint8_t *pBuffer, uint16_t numToRead) +{ + uint8_t AddrBuf[2]; + int len; + +#if (EE_TYPE > AT24C16) + AddrBuf[0] = readAddr >> 8; + AddrBuf[1] = readAddr; + len = 2; +#else + AddrBuf[0] = readAddr; + len = 1; +#endif + rvm_hal_iic_master_send(dev->i2c, AT24CXX_ADDR | dev->AddrInput, (uint8_t *)AddrBuf, len, 100u); + rvm_hal_iic_master_recv(dev->i2c, AT24CXX_ADDR | dev->AddrInput, (uint8_t *)pBuffer, numToRead, 100u); + return 0; +} + +signed long at24cxx_write_page(at24cxx_device_t dev, uint32_t wirteAddr, uint8_t *pBuffer, uint16_t numToWrite) +{ + uint8_t AddrBuf[2]; + int len; + +#if (EE_TYPE > AT24C16) + AddrBuf[0] = wirteAddr >> 8; + AddrBuf[1] = wirteAddr; + len = 2; +#else + AddrBuf[0] = wirteAddr; + len = 1; +#endif + + rvm_hal_iic_master_send(dev->i2c, AT24CXX_ADDR | dev->AddrInput, (uint8_t *)AddrBuf, len, 100u); + rvm_hal_iic_master_recv(dev->i2c, AT24CXX_ADDR | dev->AddrInput, (uint8_t *)pBuffer, numToWrite, 100u); + + return 0; +} + +signed long at24cxx_check(at24cxx_device_t dev) +{ + uint8_t temp; + CHECK_RET_WITH_RET(dev, -1); + + temp = at24cxx_read_one_byte(dev, AT24CXX_MAX_MEM_ADDRESS - 1); + if (temp == 0x55) return 0; + else + { + at24cxx_write_one_byte(dev, AT24CXX_MAX_MEM_ADDRESS - 1, 0x55); + aos_msleep(EE_TWR); // wait 5ms befor next operation + temp = at24cxx_read_one_byte(dev, AT24CXX_MAX_MEM_ADDRESS - 1); + if (temp == 0x55) return 0; + } + return 1; +} + +/** + * This function read the specific numbers of data to the specific position + * + * @param bus the name of at24cxx device + * @param ReadAddr the start position to read + * @param pBuffer the read data store position + * @param NumToRead + * @return 0 write ok. + */ +signed long at24cxx_read(at24cxx_device_t dev, uint32_t ReadAddr, uint8_t *pBuffer, uint16_t NumToRead) +{ + signed long result; + CHECK_RET_WITH_RET(dev, -1); + + if(ReadAddr + NumToRead > AT24CXX_MAX_MEM_ADDRESS) + { + return 1; + } + + result = aos_mutex_lock(dev->lock, AOS_NO_WAIT); + + if (result == 0) + { + while (NumToRead) + { + *pBuffer++ = at24cxx_read_one_byte(dev, ReadAddr++); + NumToRead--; + } + } + else + { + aos_debug_printf("The at24cxx could not respond at this time. Please try again"); + } + aos_mutex_unlock(dev->lock); + + return 0; +} + +/** + * This function read the specific numbers of data to the specific position + * + * @param bus the name of at24cxx device + * @param ReadAddr the start position to read + * @param pBuffer the read data store position + * @param NumToRead + * @return 0 write ok. + */ +signed long at24cxx_page_read(at24cxx_device_t dev, uint32_t ReadAddr, uint8_t *pBuffer, uint16_t NumToRead) +{ + signed long result = 0; + uint16_t pageReadSize = AT24CXX_PAGE_BYTE - ReadAddr % AT24CXX_PAGE_BYTE; + + CHECK_RET_WITH_RET(dev, -1); + + if(ReadAddr + NumToRead > AT24CXX_MAX_MEM_ADDRESS) + { + return 1; + } + + result = aos_mutex_lock(dev->lock, AOS_WAIT_FOREVER); + if(result == 0) + { + while (NumToRead) + { + if(NumToRead > pageReadSize) + { + if(at24cxx_read_page(dev, ReadAddr, pBuffer, pageReadSize)) + { + result = 1; + } + + ReadAddr += pageReadSize; + pBuffer += pageReadSize; + NumToRead -= pageReadSize; + pageReadSize = AT24CXX_PAGE_BYTE; + } + else + { + if(at24cxx_read_page(dev, ReadAddr, pBuffer, NumToRead)) + { + result = 1; + } + NumToRead = 0; + } + } + } + else + { + aos_debug_printf("The at24cxx could not respond at this time. Please try again"); + } + + aos_mutex_unlock(dev->lock); + return result; +} + +/** + * This function write the specific numbers of data to the specific position + * + * @param bus the name of at24cxx device + * @param WriteAddr the start position to write + * @param pBuffer the data need to write + * @param NumToWrite + * @return 0 write ok.at24cxx_device_t dev + */ +signed long at24cxx_write(at24cxx_device_t dev, uint32_t WriteAddr, uint8_t *pBuffer, uint16_t NumToWrite) +{ + uint16_t i = 0; + signed long result; + CHECK_RET_WITH_RET(dev, -1); + + if(WriteAddr + NumToWrite > AT24CXX_MAX_MEM_ADDRESS) + { + return 1; + } + + result = aos_mutex_lock(dev->lock, AOS_WAIT_FOREVER); + if (result == 0) + { + while (1) //NumToWrite-- + { + if (at24cxx_write_one_byte(dev, WriteAddr, pBuffer[i]) == 0) + { + aos_msleep(2); + WriteAddr++; + } + if (++i == NumToWrite) + { + break; + } + aos_msleep(EE_TWR); + } + } + else + { + aos_debug_printf("The at24cxx could not respond at this time. Please try again"); + } + aos_mutex_unlock(dev->lock); + + return 0; +} + +/** + * This function write the specific numbers of data to the specific position + * + * @param bus the name of at24cxx device + * @param WriteAddr the start position to write + * @param pBuffer the data need to write + * @param NumToWrite + * @return 0 write ok.at24cxx_device_t dev + */ +signed long at24cxx_page_write(at24cxx_device_t dev, uint32_t WriteAddr, uint8_t *pBuffer, uint16_t NumToWrite) +{ + signed long result = 0; + uint16_t pageWriteSize = AT24CXX_PAGE_BYTE - WriteAddr % AT24CXX_PAGE_BYTE; + + CHECK_RET_WITH_RET(dev, -1); + + if(WriteAddr + NumToWrite > AT24CXX_MAX_MEM_ADDRESS) + { + return 1; + } + + result = aos_mutex_lock(dev->lock, AOS_WAIT_FOREVER); + if(result == 0) + { + while (NumToWrite) + { + if(NumToWrite > pageWriteSize) + { + if(at24cxx_write_page(dev, WriteAddr, pBuffer, pageWriteSize)) + { + result = 1; + } + aos_msleep(EE_TWR); // wait 5ms befor next operation + + WriteAddr += pageWriteSize; + pBuffer += pageWriteSize; + NumToWrite -= pageWriteSize; + pageWriteSize = AT24CXX_PAGE_BYTE; + } + else + { + if(at24cxx_write_page(dev, WriteAddr, pBuffer, NumToWrite)) + { + result = 1; + } + aos_msleep(EE_TWR); // wait 5ms befor next operation + + NumToWrite = 0; + } + } + } + else + { + aos_debug_printf("The at24cxx could not respond at this time. Please try again"); + } + + aos_mutex_unlock(dev->lock); + return result; +} + +/** + * This function initializes at24cxx registered device driver + * + * @param dev the name of at24cxx device + * + * @return the at24cxx device. + */ +at24cxx_device_t at24cxx_init(const char *i2c_bus_name, unsigned int iic_idx, uint8_t AddrInput) +{ + int result; + at24cxx_device_t dev; + char filename[8] = {0}; + rvm_hal_iic_config_t config = {0}; + + /* AT24Cxx_device allocation space */ + dev = calloc(1, sizeof(struct at24cxx_device)); + if (dev == NULL) + { + aos_debug_printf("Can't allocate memory for at24cxx device on '%s' ", i2c_bus_name); + return NULL; + } + + sprintf(filename, "iic%d", iic_idx); + + /* Register Device */ + rvm_iic_drv_register(iic_idx); + + /* Get device handle */ + dev->i2c = rvm_hal_iic_open(filename); + + /* Initialize as default parameter */ + rvm_hal_iic_config_default(&config); + + /* Configure device parameters */ + rvm_hal_iic_config(dev->i2c, &config); + if (dev->i2c == NULL) + { + aos_debug_printf("Can't find at24cxx device on '%s' ", i2c_bus_name); + free(dev); + return NULL; + } + + result = aos_mutex_new(&mutex); + + if(result != 0) + { + aos_debug_printf("create dynamic mutex failed.\n"); + return NULL; + } + + dev->lock = &mutex; + if (dev->lock == NULL) + { + aos_debug_printf("Can't create mutex for at24cxx device on '%s' ", i2c_bus_name); + free(dev); + return NULL; + } + + dev->AddrInput = AddrInput; + return dev; +} + +/** + * This function releases memory and deletes mutex lock + * + * @param dev the pointer of device driver structure + */ +int at24cxx_deinit(at24cxx_device_t dev) +{ + CHECK_RET_WITH_RET(dev, -1); + + aos_mutex_free(dev->lock); + + free(dev); + + return 0; +} + +uint8_t TEST_BUFFER[] = "WELCOM TO RTT"; +#define SIZE sizeof(TEST_BUFFER) + +void at24cxx(int argc, char *argv[]) +{ + int id = 0; + char* usr_str; + static at24cxx_device_t dev = NULL; + + if (argc > 1) + { + if (!strcmp(argv[1], "probe")) + { + if (argc > 2) + { + /* initialize the sensor when first probe */ + + /* deinit the old device */ + if (dev) + { + at24cxx_deinit(dev); + } + + int len = strlen(argv[2]); + + if( len > 3) + { + strtok(argv[2],"c"); + usr_str = strtok(NULL,"c"); + id = atoi(usr_str); + } + dev = at24cxx_init(argv[2], id, atoi(argv[3])); + + } + else + { + aos_debug_printf("at24cxx probe - probe sensor by given name\n"); + } + } + else if (!strcmp(argv[1], "read")) + { + if (dev) + { + uint8_t testbuffer[50]; + + /* read the eeprom data */ + at24cxx_read(dev, 0, testbuffer, SIZE); + + aos_debug_printf("read at24cxx : %s\n", testbuffer); + + } + else + { + aos_debug_printf("Please using 'at24cxx probe ' first\n"); + } + } + else if (!strcmp(argv[1], "write")) + { + at24cxx_write(dev, 0, TEST_BUFFER, SIZE); + aos_debug_printf("write ok\n"); + } + else if (!strcmp(argv[1], "check")) + { + if (at24cxx_check(dev) == 1) + { + aos_debug_printf("check faild \n"); + } + } + else + { + aos_debug_printf("Unknown command. Please enter 'at24cxx0' for help\n"); + } + } + else + { + aos_debug_printf("Usage:\n"); + aos_debug_printf("at24cxx probe - probe eeprom by given name\n"); + aos_debug_printf("at24cxx check - check eeprom at24cxx \n"); + aos_debug_printf("at24cxx read - read eeprom at24cxx data\n"); + aos_debug_printf("at24cxx write - write eeprom at24cxx data\n"); + + } +} +ALIOS_CLI_CMD_REGISTER(at24cxx, at24cxx, at24cxx eeprom function); + +#endif -- Gitee