[已解决] 请教我该如何读取XIP的spi-flash的唯一ID?
Posted: Wed Jun 16, 2021 1:14 am
以前在其它单片机系统,读取spi-flash的唯一ID是很轻松的事。但现在发现在idf+esp32上十分困难,idf没有提供这个功能函数;自己也很难不改动idf而实现。由于esp32在spi-flash里执行代码,所以操纵flash再也不是一件随心所欲的事了。
请教,我该怎么办?
请教,我该怎么办?
谢谢!既然是要修改idf源码,那我就贴上我的修改吧。供大家使用【for idf-4.2.1】ESP_Eavo wrote: ↑Wed Jun 16, 2021 8:14 am您好。
暂时没有可直接读取的接口,可参考 https://github.com/espressif/esp-idf/tr ... /spi_flash 改动代码实现。
Code: Select all
esp_err_t IRAM_ATTR esp_flash_read_uid(esp_flash_t* chip, uint32_t* out_id)
{
if (chip == NULL) {
chip = esp_flash_default_chip;
}
if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) {
return ESP_ERR_FLASH_NOT_INITIALISED;
}
if (out_id == NULL) {
return ESP_ERR_INVALID_ARG;
}
esp_err_t err = spiflash_start(chip);
if (err != ESP_OK) {
return err;
}
spi_flash_trans_t t = {
.command = 0x4B,
.miso_len = 12,
.miso_data = (uint8_t*)out_id,
};
chip->host->common_command(chip->host, &t);
return spiflash_end(chip, err);
}
Code: Select all
uint32_t uid_temp[3] = {0};
esp_flash_read_uid(partition->flash_chip, uid_temp);
Code: Select all
esp_err_t esp_flash_read_unique_chip_id(esp_flash_t *chip, uint64_t* out_uid);
我用的是稳定版本idf-4.2.1,它没有这个函数的。ESP_Gargamel wrote: ↑Wed Jun 16, 2021 1:19 pm你试一下这个接口:Code: Select all
esp_err_t esp_flash_read_unique_chip_id(esp_flash_t *chip, uint64_t* out_uid);
Code: Select all
esp_flash_t *init_chip;
esp_flash_spi_device_config_t dev_cfg = {
.host_id = -1,
};
esp_err_t err = spi_bus_add_flash_device(&init_chip, &dev_cfg);
if (err != ESP_OK)
{
ESP_LOGE("aaa", "Failed to Add Flash: %s (0x%x)", esp_err_to_name(err), err);
return NULL;
}
Code: Select all
extern bool spi_user_cmd(spi_cmd_dir_t mode, spi_cmd_t *p_cmd);
esp_err_t esp_flash_read_uid(uint32_t *out_uid)
{
spi_cmd_t cmd =
{
.cmd = 0x4B,
.cmd_len = 1,
.addr = NULL,
.addr_len = 0,
.data = out_uid,
.data_len = 8,
.dummy_bits = 32
};
return spi_user_cmd(SPI_RX | SPI_RAW, &cmd) ? ESP_OK : ESP_FAIL;
}