我无法使用 ESP32 将多个字节写入 24LC256 EEPROM。
以下函数负责读取和写入 EEPROM。(我知道使用此 EEPROM 的页面写入仅限于 64 字节的增量,此代码仅用于测试)
EEPROM写功能
esp_err_t eeprom_write(uint8_t deviceaddress, uint16_t eeaddress, uint8_t* data, size_t size) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (deviceaddress << 1) | EEPROM_WRITE_ADDR, 1);
i2c_master_write_byte(cmd, eeaddress>>8, 1);
i2c_master_write_byte(cmd, eeaddress&0xFF, 1);
i2c_master_write(cmd, data, size, 1); // Start page write
i2c_master_stop(cmd); // Call stop command
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_1, cmd, 1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
EEPROM读取功能
esp_err_t eeprom_read(uint8_t deviceaddress, uint16_t eeaddress, uint8_t* data, size_t size) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (deviceaddress<<1)|EEPROM_WRITE_ADDR, 1);
i2c_master_write_byte(cmd, eeaddress<<8, 1);
i2c_master_write_byte(cmd, eeaddress&0xFF, 1);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (deviceaddress<<1)|EEPROM_READ_ADDR, 1);
// Sequential read support
if (size > 1) {
i2c_master_read(cmd, data, size-1, 0); // Send ack for these bytes
// as part of a sequential read
}
i2c_master_read_byte(cmd, data+size-1, 1); // Do not ack the last byte
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_1, cmd, 1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
奇怪的是,我能够将 13 个字节写入 EEPROM,一切似乎都很好。
eeprom_write(0x50, 0x0000, data_wr, 13); // Returns ESP_OK
eeprom_read(0x50, 0x0000, data_rd, 64); // Returns ESP_OK
但是,当向 EEPROM 写入超过 13 个字节时,顺序读取功能就会出错。
eeprom_write(0x50, 0x0000, data_wr, 14); // Returns ESP_OK
eeprom_read(0x50, 0x0000, data_rd, 64); // Return ESP_FAIL
我确信我所做的一切都遵循 24LC256 数据表中的读写规则。有什么我想念的吗?