本帖最后由 点赞 于 2025-7-30 21:46 编辑
主要代码:
读寄存器:
static rt_err_t read_regs(at24cxx_device_t dev, rt_uint8_t len, rt_uint8_t *buf)
{
struct rt_i2c_msg msgs;
msgs.addr = AT24CXX_ADDR | dev->AddrInput;
msgs.flags = RT_I2C_RD;
msgs.buf = buf;
msgs.len = len;
if (rt_i2c_transfer(dev->i2c, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
读一个字节:
static uint8_t at24cxx_read_one_byte(at24cxx_device_t dev, uint16_t readAddr)
{
rt_uint8_t buf[2];
rt_uint8_t temp;
#if (PKG_AT24CXX_EE_TYPE > AT24C16)
buf[0] = (uint8_t)(readAddr>>8);
buf[1] = (uint8_t)readAddr;
if (rt_i2c_master_send(dev->i2c, AT24CXX_ADDR | dev->AddrInput, RT_I2C_WR, buf, 2) == 0)
#else
buf[0] = readAddr;
if (rt_i2c_master_send(dev->i2c, AT24CXX_ADDR | dev->AddrInput, RT_I2C_WR, buf, 1) == 0)
#endif
{
return RT_ERROR;
}
read_regs(dev, 1, &temp);
return temp;
}
写一个字节:
static rt_err_t at24cxx_write_one_byte(at24cxx_device_t dev, uint16_t writeAddr, uint8_t dataToWrite)
{
rt_uint8_t buf[3];
#if (PKG_AT24CXX_EE_TYPE > AT24C16)
buf[0] = (uint8_t)(writeAddr>>8);
buf[1] = (uint8_t)writeAddr;
buf[2] = dataToWrite;
if (rt_i2c_master_send(dev->i2c, AT24CXX_ADDR | dev->AddrInput, RT_I2C_WR, buf, 3) == 3)
#else
buf[0] = writeAddr; //cmd
buf[1] = dataToWrite;
//buf[2] = data[1];
if (rt_i2c_master_send(dev->i2c, AT24CXX_ADDR | dev->AddrInput, RT_I2C_WR, buf, 2) == 2)
#endif
return RT_EOK;
else
return -RT_ERROR;
}
读一页:
static rt_err_t at24cxx_read_page(at24cxx_device_t dev, uint32_t readAddr, uint8_t *pBuffer, uint16_t numToRead)
{
struct rt_i2c_msg msgs[2];
uint8_t AddrBuf[2];
msgs[0].addr = AT24CXX_ADDR | dev->AddrInput;
msgs[0].flags = RT_I2C_WR;
#if (PKG_AT24CXX_EE_TYPE > AT24C16)
AddrBuf[0] = readAddr >> 8;
AddrBuf[1] = readAddr;
msgs[0].buf = AddrBuf;
msgs[0].len = 2;
#else
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;
msgs[1].len = numToRead;
if(rt_i2c_transfer(dev->i2c, msgs, 2) <= 0)
{
return RT_ERROR;
}
return RT_EOK;
}
写一页:
static rt_err_t at24cxx_write_page(at24cxx_device_t dev, uint32_t wirteAddr, uint8_t *pBuffer, uint16_t numToWrite)
{
struct rt_i2c_msg msgs[2];
uint8_t AddrBuf[2];
msgs[0].addr = AT24CXX_ADDR | dev->AddrInput;
msgs[0].flags = RT_I2C_WR;
#if (PKG_AT24CXX_EE_TYPE > AT24C16)
AddrBuf[0] = (uint8_t)(wirteAddr>>8);
AddrBuf[1] = (uint8_t)wirteAddr;
msgs[0].buf = AddrBuf;
msgs[0].len = 2;
#else
AddrBuf[0] = wirteAddr;
msgs[0].buf = AddrBuf;
msgs[0].len = 1;
#endif
msgs[1].addr = AT24CXX_ADDR | dev->AddrInput;
msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START;
msgs[1].buf = pBuffer;
msgs[1].len = numToWrite;
if(rt_i2c_transfer(dev->i2c, msgs, 2) <= 0)
{
return RT_ERROR;
}
return RT_EOK;
}
通过在eeprom的最后一个字节,先写后读检查eeprom:
rt_err_t at24cxx_check(at24cxx_device_t dev)
{
uint8_t temp;
RT_ASSERT(dev);
temp = at24cxx_read_one_byte(dev, AT24CXX_MAX_MEM_ADDRESS - 1);
if (temp == 0x55) return RT_EOK;
else
{
at24cxx_write_one_byte(dev, AT24CXX_MAX_MEM_ADDRESS - 1, 0x55);
rt_thread_mdelay(EE_TWR); // wait 5ms befor next operation
temp = at24cxx_read_one_byte(dev, AT24CXX_MAX_MEM_ADDRESS - 1);
if (temp == 0x55) return RT_EOK;
}
return RT_ERROR;
}
读取任意位置任意长度的数据:
rt_err_t at24cxx_read(at24cxx_device_t dev, uint32_t ReadAddr, uint8_t *pBuffer, uint16_t NumToRead)
{
rt_err_t result;
RT_ASSERT(dev);
if(ReadAddr + NumToRead > AT24CXX_MAX_MEM_ADDRESS || NumToRead == 0)
{
return RT_ERROR;
}
result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
while (NumToRead)
{
*pBuffer++ = at24cxx_read_one_byte(dev, ReadAddr++);
NumToRead--;
}
}
else
{
LOG_E("The at24cxx could not respond at this time. Please try again");
}
rt_mutex_release(dev->lock);
return RT_EOK;
}
在任意地址写任意长度的数据:
rt_err_t at24cxx_write(at24cxx_device_t dev, uint32_t WriteAddr, uint8_t *pBuffer, uint16_t NumToWrite)
{
uint16_t i = 0;
rt_err_t result;
RT_ASSERT(dev);
if(WriteAddr + NumToWrite > AT24CXX_MAX_MEM_ADDRESS || NumToWrite == 0)
{
return RT_ERROR;
}
result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
while (1) //NumToWrite--
{
if (at24cxx_write_one_byte(dev, WriteAddr, pBuffer[i]) == RT_EOK)
{
rt_thread_mdelay(2);
WriteAddr++;
}
if (++i == NumToWrite)
{
break;
}
rt_thread_mdelay(EE_TWR);
}
}
else
{
LOG_E("The at24cxx could not respond at this time. Please try again");
}
rt_mutex_release(dev->lock);
return RT_EOK;
}
硬件初始化:
at24cxx_device_t at24cxx_init(const char *i2c_bus_name, uint8_t AddrInput)
{
at24cxx_device_t dev;
RT_ASSERT(i2c_bus_name);
dev = rt_calloc(1, sizeof(struct at24cxx_device));
if (dev == RT_NULL)
{
LOG_E("Can't allocate memory for at24cxx device on '%s' ", i2c_bus_name);
return RT_NULL;
}
dev->i2c = rt_i2c_bus_device_find(i2c_bus_name);
if (dev->i2c == RT_NULL)
{
LOG_E("Can't find at24cxx device on '%s' ", i2c_bus_name);
rt_free(dev);
return RT_NULL;
}
dev->lock = rt_mutex_create("mutex_at24cxx", RT_IPC_FLAG_FIFO);
if (dev->lock == RT_NULL)
{
LOG_E("Can't create mutex for at24cxx device on '%s' ", i2c_bus_name);
rt_free(dev);
return RT_NULL;
}
if(AddrInput > 7)
{
LOG_E("The AddrInput is invalid");
rt_free(dev);
return RT_NULL;
}
else
{
#if (PKG_AT24CXX_EE_TYPE == AT24C04)
if(AddrInput > 3)
{
LOG_E("The AddrInput is invalid");
rt_free(dev);
return RT_NULL;
}
#elif (PKG_AT24CXX_EE_TYPE == AT24C08)
if(AddrInput > 1)
{
LOG_E("The AddrInput is invalid");
rt_free(dev);
return RT_NULL;
}
#elif (PKG_AT24CXX_EE_TYPE == AT24C16)
if(AddrInput != 0)
{
LOG_E("The AddrInput is invalid");
rt_free(dev);
return RT_NULL;
}
#endif //PKG_AT24CXX_EE_TYPE
}
dev->AddrInput = AddrInput;
return dev;
}
|