2021-05-31 00:43:23 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-11-18 22:46:21 -05:00
|
|
|
|
|
|
|
#include "esp_efuse.h"
|
|
|
|
#include "esp_efuse_utility.h"
|
2019-05-13 06:02:45 -04:00
|
|
|
#include "soc/efuse_periph.h"
|
2018-11-18 22:46:21 -05:00
|
|
|
#include "assert.h"
|
|
|
|
#include "sdkconfig.h"
|
2018-12-07 04:57:25 -05:00
|
|
|
#include "esp_efuse_table.h"
|
2018-11-18 22:46:21 -05:00
|
|
|
|
|
|
|
const static char *TAG = "efuse";
|
|
|
|
|
|
|
|
#if defined(BOOTLOADER_BUILD)
|
2020-02-16 07:46:30 -05:00
|
|
|
#define EFUSE_LOCK_ACQUIRE_RECURSIVE()
|
|
|
|
#define EFUSE_LOCK_RELEASE_RECURSIVE()
|
2018-11-18 22:46:21 -05:00
|
|
|
#else
|
2021-06-17 23:52:47 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2018-11-18 22:46:21 -05:00
|
|
|
#include <sys/lock.h>
|
|
|
|
static _lock_t s_efuse_lock;
|
2020-02-16 07:46:30 -05:00
|
|
|
#define EFUSE_LOCK_ACQUIRE_RECURSIVE() _lock_acquire_recursive(&s_efuse_lock)
|
|
|
|
#define EFUSE_LOCK_RELEASE_RECURSIVE() _lock_release_recursive(&s_efuse_lock)
|
2018-11-18 22:46:21 -05:00
|
|
|
#endif
|
|
|
|
|
2020-12-03 21:42:57 -05:00
|
|
|
static int s_batch_writing_mode = 0;
|
2019-09-18 09:19:48 -04:00
|
|
|
|
2018-11-18 22:46:21 -05:00
|
|
|
// Public API functions
|
|
|
|
|
|
|
|
// read value from EFUSE, writing it into an array
|
|
|
|
esp_err_t esp_efuse_read_field_blob(const esp_efuse_desc_t* field[], void* dst, size_t dst_size_bits)
|
|
|
|
{
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
if (field == NULL || dst == NULL || dst_size_bits == 0) {
|
|
|
|
err = ESP_ERR_INVALID_ARG;
|
|
|
|
} else {
|
2021-06-17 23:52:47 -04:00
|
|
|
do {
|
|
|
|
memset((uint8_t *)dst, 0, esp_efuse_utility_get_number_of_items(dst_size_bits, 8));
|
|
|
|
err = esp_efuse_utility_process(field, dst, dst_size_bits, esp_efuse_utility_fill_buff);
|
|
|
|
#ifndef BOOTLOADER_BUILD
|
|
|
|
if (err == ESP_ERR_DAMAGED_READING) {
|
|
|
|
vTaskDelay(1);
|
|
|
|
}
|
|
|
|
#endif // BOOTLOADER_BUILD
|
|
|
|
} while (err == ESP_ERR_DAMAGED_READING);
|
2018-11-18 22:46:21 -05:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-04-25 01:13:18 -04:00
|
|
|
bool esp_efuse_read_field_bit(const esp_efuse_desc_t *field[])
|
|
|
|
{
|
|
|
|
uint8_t value = 0;
|
|
|
|
esp_err_t err = esp_efuse_read_field_blob(field, &value, 1);
|
|
|
|
assert(err == ESP_OK);
|
|
|
|
return (err == ESP_OK) && value;
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:46:21 -05:00
|
|
|
// read number of bits programmed as "1" in the particular field
|
|
|
|
esp_err_t esp_efuse_read_field_cnt(const esp_efuse_desc_t* field[], size_t* out_cnt)
|
|
|
|
{
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
if (field == NULL || out_cnt == NULL) {
|
|
|
|
err = ESP_ERR_INVALID_ARG;
|
|
|
|
} else {
|
2021-06-17 23:52:47 -04:00
|
|
|
do {
|
|
|
|
*out_cnt = 0;
|
|
|
|
err = esp_efuse_utility_process(field, out_cnt, 0, esp_efuse_utility_count_once);
|
|
|
|
#ifndef BOOTLOADER_BUILD
|
|
|
|
if (err == ESP_ERR_DAMAGED_READING) {
|
|
|
|
vTaskDelay(1);
|
|
|
|
}
|
|
|
|
#endif // BOOTLOADER_BUILD
|
|
|
|
} while (err == ESP_ERR_DAMAGED_READING);
|
2018-11-18 22:46:21 -05:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write array to EFUSE
|
|
|
|
esp_err_t esp_efuse_write_field_blob(const esp_efuse_desc_t* field[], const void* src, size_t src_size_bits)
|
|
|
|
{
|
2020-02-16 07:46:30 -05:00
|
|
|
EFUSE_LOCK_ACQUIRE_RECURSIVE();
|
2018-11-18 22:46:21 -05:00
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
if (field == NULL || src == NULL || src_size_bits == 0) {
|
|
|
|
err = ESP_ERR_INVALID_ARG;
|
|
|
|
} else {
|
2020-12-03 21:42:57 -05:00
|
|
|
if (s_batch_writing_mode == 0) {
|
2019-09-18 09:19:48 -04:00
|
|
|
esp_efuse_utility_reset();
|
|
|
|
}
|
2018-11-18 22:46:21 -05:00
|
|
|
err = esp_efuse_utility_process(field, (void*)src, src_size_bits, esp_efuse_utility_write_blob);
|
|
|
|
|
2020-12-03 21:42:57 -05:00
|
|
|
if (s_batch_writing_mode == 0) {
|
2018-11-18 22:46:21 -05:00
|
|
|
if (err == ESP_OK) {
|
2019-09-18 09:19:48 -04:00
|
|
|
err = esp_efuse_utility_apply_new_coding_scheme();
|
|
|
|
if (err == ESP_OK) {
|
2022-04-26 13:10:41 -04:00
|
|
|
err = esp_efuse_utility_burn_efuses();
|
2019-09-18 09:19:48 -04:00
|
|
|
}
|
2018-11-18 22:46:21 -05:00
|
|
|
}
|
2019-09-18 09:19:48 -04:00
|
|
|
esp_efuse_utility_reset();
|
2018-11-18 22:46:21 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 07:46:30 -05:00
|
|
|
EFUSE_LOCK_RELEASE_RECURSIVE();
|
2018-11-18 22:46:21 -05:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// program cnt bits to "1"
|
|
|
|
esp_err_t esp_efuse_write_field_cnt(const esp_efuse_desc_t* field[], size_t cnt)
|
|
|
|
{
|
2020-02-16 07:46:30 -05:00
|
|
|
EFUSE_LOCK_ACQUIRE_RECURSIVE();
|
2018-11-18 22:46:21 -05:00
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
if (field == NULL || cnt == 0) {
|
|
|
|
err = ESP_ERR_INVALID_ARG;
|
|
|
|
} else {
|
2020-12-03 21:42:57 -05:00
|
|
|
if (s_batch_writing_mode == 0) {
|
2019-09-18 09:19:48 -04:00
|
|
|
esp_efuse_utility_reset();
|
|
|
|
}
|
2018-11-18 22:46:21 -05:00
|
|
|
err = esp_efuse_utility_process(field, &cnt, 0, esp_efuse_utility_write_cnt);
|
|
|
|
|
|
|
|
if (cnt != 0) {
|
|
|
|
ESP_LOGE(TAG, "The required number of bits can not be set. [Not set %d]", cnt);
|
|
|
|
err = ESP_ERR_EFUSE_CNT_IS_FULL;
|
|
|
|
}
|
2019-09-18 09:19:48 -04:00
|
|
|
if (err == ESP_OK_EFUSE_CNT) {
|
|
|
|
err = ESP_OK;
|
|
|
|
}
|
2018-11-18 22:46:21 -05:00
|
|
|
|
2020-12-03 21:42:57 -05:00
|
|
|
if (s_batch_writing_mode == 0) {
|
2018-11-18 22:46:21 -05:00
|
|
|
if (err == ESP_OK) {
|
2019-09-18 09:19:48 -04:00
|
|
|
err = esp_efuse_utility_apply_new_coding_scheme();
|
|
|
|
if (err == ESP_OK) {
|
2022-04-26 13:10:41 -04:00
|
|
|
err = esp_efuse_utility_burn_efuses();
|
2019-09-18 09:19:48 -04:00
|
|
|
}
|
2018-11-18 22:46:21 -05:00
|
|
|
}
|
2019-09-18 09:19:48 -04:00
|
|
|
esp_efuse_utility_reset();
|
2018-11-18 22:46:21 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 07:46:30 -05:00
|
|
|
EFUSE_LOCK_RELEASE_RECURSIVE();
|
2018-11-18 22:46:21 -05:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-04-25 00:58:30 -04:00
|
|
|
esp_err_t esp_efuse_write_field_bit(const esp_efuse_desc_t* field[])
|
|
|
|
{
|
|
|
|
esp_err_t err;
|
|
|
|
uint8_t existing = 0;
|
|
|
|
const uint8_t one = 1;
|
|
|
|
|
|
|
|
if (field == NULL || field[0]->bit_count != 1) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check existing value. esp_efuse_write_field_blob() also checks this, but will log an error */
|
|
|
|
err = esp_efuse_read_field_blob(field, &existing, 1);
|
|
|
|
if (err != ESP_OK || existing) {
|
|
|
|
return err; // Error reading, or the bit is already written and we can no-op this
|
|
|
|
}
|
|
|
|
|
|
|
|
return esp_efuse_write_field_blob(field, &one, 1);
|
|
|
|
}
|
|
|
|
|
2018-11-18 22:46:21 -05:00
|
|
|
// get the length of the field in bits
|
|
|
|
int esp_efuse_get_field_size(const esp_efuse_desc_t* field[])
|
|
|
|
{
|
|
|
|
int bits_counter = 0;
|
2018-12-12 02:50:31 -05:00
|
|
|
if (field != NULL) {
|
|
|
|
int i = 0;
|
|
|
|
while (field[i] != NULL) {
|
|
|
|
bits_counter += field[i]->bit_count;
|
|
|
|
++i;
|
|
|
|
}
|
2018-11-18 22:46:21 -05:00
|
|
|
}
|
|
|
|
return bits_counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reading efuse register.
|
|
|
|
uint32_t esp_efuse_read_reg(esp_efuse_block_t blk, unsigned int num_reg)
|
|
|
|
{
|
2021-06-17 23:52:47 -04:00
|
|
|
uint32_t ret_val = 0;
|
|
|
|
esp_err_t err = esp_efuse_read_block(blk, &ret_val, num_reg * 32, 32);
|
|
|
|
assert(err == ESP_OK);
|
|
|
|
(void)err;
|
2018-11-18 22:46:21 -05:00
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// writing efuse register.
|
|
|
|
esp_err_t esp_efuse_write_reg(esp_efuse_block_t blk, unsigned int num_reg, uint32_t val)
|
|
|
|
{
|
2020-02-16 07:46:30 -05:00
|
|
|
EFUSE_LOCK_ACQUIRE_RECURSIVE();
|
2020-12-03 21:42:57 -05:00
|
|
|
if (s_batch_writing_mode == 0) {
|
2019-09-18 09:19:48 -04:00
|
|
|
esp_efuse_utility_reset();
|
|
|
|
}
|
2018-11-18 22:46:21 -05:00
|
|
|
esp_err_t err = esp_efuse_utility_write_reg(blk, num_reg, val);
|
2020-12-03 21:42:57 -05:00
|
|
|
if (s_batch_writing_mode == 0) {
|
2018-11-18 22:46:21 -05:00
|
|
|
if (err == ESP_OK) {
|
2019-09-18 09:19:48 -04:00
|
|
|
err = esp_efuse_utility_apply_new_coding_scheme();
|
|
|
|
if (err == ESP_OK) {
|
2022-04-26 13:10:41 -04:00
|
|
|
err = esp_efuse_utility_burn_efuses();
|
2019-09-18 09:19:48 -04:00
|
|
|
}
|
2018-11-18 22:46:21 -05:00
|
|
|
}
|
2019-09-18 09:19:48 -04:00
|
|
|
esp_efuse_utility_reset();
|
2018-11-18 22:46:21 -05:00
|
|
|
}
|
2020-02-16 07:46:30 -05:00
|
|
|
EFUSE_LOCK_RELEASE_RECURSIVE();
|
2018-11-18 22:46:21 -05:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function reads the key from the efuse block, starting at the offset and the required size.
|
|
|
|
esp_err_t esp_efuse_read_block(esp_efuse_block_t blk, void* dst_key, size_t offset_in_bits, size_t size_bits)
|
|
|
|
{
|
|
|
|
esp_err_t err = ESP_OK;
|
2019-06-18 07:34:05 -04:00
|
|
|
if (blk == EFUSE_BLK0 || blk >= EFUSE_BLK_MAX || dst_key == NULL || size_bits == 0) {
|
2018-11-18 22:46:21 -05:00
|
|
|
err = ESP_ERR_INVALID_ARG;
|
|
|
|
} else {
|
|
|
|
const esp_efuse_desc_t field_desc[] = {
|
|
|
|
{blk, offset_in_bits, size_bits},
|
|
|
|
};
|
|
|
|
|
|
|
|
const esp_efuse_desc_t* field[] = {
|
|
|
|
&field_desc[0],
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
err = esp_efuse_read_field_blob(field, dst_key, size_bits);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function writes the key from the efuse block, starting at the offset and the required size.
|
|
|
|
esp_err_t esp_efuse_write_block(esp_efuse_block_t blk, const void* src_key, size_t offset_in_bits, size_t size_bits)
|
|
|
|
{
|
|
|
|
esp_err_t err = ESP_OK;
|
2019-06-18 07:34:05 -04:00
|
|
|
if (blk == EFUSE_BLK0 || blk >= EFUSE_BLK_MAX || src_key == NULL || size_bits == 0) {
|
2018-11-18 22:46:21 -05:00
|
|
|
err = ESP_ERR_INVALID_ARG;
|
|
|
|
} else {
|
|
|
|
const esp_efuse_desc_t field_desc[] = {
|
|
|
|
{blk, offset_in_bits, size_bits},
|
|
|
|
};
|
|
|
|
|
|
|
|
const esp_efuse_desc_t* field[] = {
|
|
|
|
&field_desc[0],
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
err = esp_efuse_write_field_blob(field, src_key, size_bits);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
2019-09-18 09:19:48 -04:00
|
|
|
|
|
|
|
esp_err_t esp_efuse_batch_write_begin(void)
|
|
|
|
{
|
2020-12-03 21:42:57 -05:00
|
|
|
EFUSE_LOCK_ACQUIRE_RECURSIVE();
|
|
|
|
assert(s_batch_writing_mode >= 0);
|
|
|
|
if (++s_batch_writing_mode == 1) {
|
|
|
|
esp_efuse_utility_reset();
|
|
|
|
ESP_LOGI(TAG, "Batch mode of writing fields is enabled");
|
|
|
|
};
|
2019-09-18 09:19:48 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_efuse_batch_write_cancel(void)
|
|
|
|
{
|
2020-12-03 21:42:57 -05:00
|
|
|
if (s_batch_writing_mode == 0) {
|
|
|
|
ESP_LOGE(TAG, "Batch mode was not enabled");
|
2019-09-18 09:19:48 -04:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2020-12-03 21:42:57 -05:00
|
|
|
if (--s_batch_writing_mode == 0) {
|
|
|
|
esp_efuse_utility_reset();
|
|
|
|
ESP_LOGI(TAG, "Batch mode of writing fields is cancelled");
|
|
|
|
EFUSE_LOCK_RELEASE_RECURSIVE();
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
2019-09-18 09:19:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_efuse_batch_write_commit(void)
|
|
|
|
{
|
2020-12-03 21:42:57 -05:00
|
|
|
if (s_batch_writing_mode == 0) {
|
|
|
|
ESP_LOGE(TAG, "Batch mode was not enabled");
|
2019-09-18 09:19:48 -04:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
2020-12-03 21:42:57 -05:00
|
|
|
}
|
|
|
|
if (--s_batch_writing_mode == 0) {
|
2019-09-18 09:19:48 -04:00
|
|
|
esp_err_t err = esp_efuse_utility_apply_new_coding_scheme();
|
|
|
|
if (err == ESP_OK) {
|
2022-04-26 13:10:41 -04:00
|
|
|
err = esp_efuse_utility_burn_efuses();
|
2020-12-03 21:42:57 -05:00
|
|
|
ESP_LOGI(TAG, "Batch mode. Prepared fields are committed");
|
|
|
|
} else {
|
|
|
|
esp_efuse_utility_reset();
|
2019-09-18 09:19:48 -04:00
|
|
|
}
|
2020-12-03 21:42:57 -05:00
|
|
|
EFUSE_LOCK_RELEASE_RECURSIVE();
|
2019-09-18 09:19:48 -04:00
|
|
|
return err;
|
|
|
|
}
|
2020-12-03 21:42:57 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
2021-08-11 10:06:48 -04:00
|
|
|
|
|
|
|
esp_err_t esp_efuse_check_errors(void)
|
|
|
|
{
|
|
|
|
return esp_efuse_utility_check_errors();
|
|
|
|
}
|