#include #define AT24C02_TEST_DATA 0xCC /* 测试数据 */
static const char *AT24C02_I2C_BUS_NAME ="i2c1"; /* I2c总线设备名称*/
/*****在ARM中,读写方向是用寄存器的单独一位来确定的,有一个7位的域用来保存设备地址。所以该I2C设备在ARM I2C总线上的地址为0xA0 右移一位 0x50.**************/
static const rt_uint8_t AT24C02_ADDR = 0x50; /* 从机地址 */
static struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C总线设备句柄 */
static rt_bool_t initialized = RT_FALSE; /* 初始化状态*/
/****************************************************************************
* 名 称: u8 AT24C02_ReadReg
* 功 能:在AT24C02读寄存器
* 入口参数:ReadAddr:要读取数据所在的地址
* 返回参数:读到的8位数据
* 说 明:
****************************************************************************/
static rt_err_t AT24C02_ReadReg(struct rt_i2c_bus_device *bus,rt_uint8_t reg, rt_uint8_t len, rt_uint8_t *buf)
{
struct rt_i2c_msg msgs[2];
rt_uint8_t buff[1];
msgs[0].addr = AT24C02_ADDR;/*写地址*/
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = ® /*寄存器*/
msgs[0].len = 1;
msgs[1].addr = AT24C02_ADDR;/*地址*/
msgs[1].flags = RT_I2C_RD; /* 读取*/
msgs[1].buf = buff; /* 缓冲区 */
msgs[1].len = len; /* 长度 */
/* I2C传输数据 */
if( rt_i2c_transfer(bus, msgs, 2) == 2 )
{
*buf = buff[0];
rt_kprintf("传输值:%02X\n",buff[0]);
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
/****************************************************************************
* 名 称: u8 AT24C02_WriteByte(u8 WriteAddr,u8 WriteData)
* 功 能:在AT24C02指定地址写入一个数据
* 入口参数
* 返回参数:
* 说 明:
****************************************************************************/
static rt_err_t AT24C02_WriteReg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
{
struct rt_i2c_msg msgs[2];
rt_uint8_t *buf;
buf=data;
msgs[0].addr = AT24C02_ADDR;/*写地址*/
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = ® //寄存器
msgs[0].len = 1;
msgs[1].addr = AT24C02_ADDR;/*写地址*/
msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START;
msgs[1].buf = buf;
msgs[1].len = 1;
if( rt_i2c_transfer(bus, msgs, 2) == 2 )
{
rt_kprintf("传输值:%02X\n",buf[0]);
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
uint8_t read_temp_AT24C02(uint8_t *Rdata)
{
rt_uint8_t Testdata,temp[10];
rt_uint8_t *AT24C02_data;
rt_uint8_t status=RT_EOK;
*AT24C02_data = AT24C02_TEST_DATA;
status = AT24C02_ReadReg(i2c_bus,0, 1, temp); /* ??????? */
rt_kprintf("读取的值:%02X\n",temp[0]);
rt_thread_delay(rt_tick_from_millisecond(50)); // FRAM no need.
status = AT24C02_WriteReg(i2c_bus,1,AT24C02_data); /* ???? */
rt_thread_delay(rt_tick_from_millisecond(50)); // FRAM no need.
status = AT24C02_ReadReg(i2c_bus,0, 1, temp); /* ??????? */
*Rdata = temp[0];
if(status) rt_kprintf("I2C读写错误\n");
if(*Rdata==AT24C02_TEST_DATA)
rt_kprintf("读写成功:%02X\n",*Rdata);
return 0;
}
static void AT24C02_init(const char *name)
{
rt_uint8_t temp[2] = {0, 0};
/* 查找I2C总线设备,获取I2C总线句柄 */
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
if (i2c_bus == RT_NULL)
{
rt_kprintf("can't find %s device!\n", name);
}
else
{
rt_kprintf("find %s device!\n", name);
initialized = RT_TRUE;
}
}
static void i2c_AT24C02_sample(int argc, char *argv[])
{
uint8_t RSdata;
char name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(name, AT24C02_I2C_BUS_NAME, RT_NAME_MAX);
}
if (!initialized)
{
/* 初始化 */
AT24C02_init(name);
}
if (initialized)
{
/* 读取 */
read_temp_AT24C02(&RSdata);
rt_kprintf("read 24c02 sensor : %X\n", RSdata);
}
else
{
rt_kprintf("initialize sensor failed!\n");
}
}
/* 导出 msh 命令列表 */
MSH_CMD_EXPORT(i2c_AT24C02_sample, i2c AT24C02 sample);