Writing a char array to efuse
Posted: Thu Dec 29, 2022 6:47 pm
Hi everyone,
I'm trying to create a code that gets an input using serial(UART) and saves it to the ESP_EFUSE_USER_DATA.
I'm learning C as I code so the problem is most probably with the way I C (
) and not the ESP framework.
Relevant code starts at line 26
Relevant code starts at line 14
So what I'm trying to do is to convert the string "serial_num" to a char of hex chars so that the function esp_efuse_write_field_blob() will save is as hex.
Can you guys give me a hand here?
Also, I'm sure the way I've handled the string manipulation is not the right way, feel free to roast me!
Thank in advance!
I'm trying to create a code that gets an input using serial(UART) and saves it to the ESP_EFUSE_USER_DATA.
I'm learning C as I code so the problem is most probably with the way I C (
Relevant code starts at line 26
Code: UART.c Select all
static void uart_event_task()
{
uart_event_t event;
size_t buffered_size;
uint8_t *dtmp = (uint8_t *)malloc(RD_BUF_SIZE);
char cmd[30]; // the received command via UART
char serialNum[20]; // The serial number itself
for (;;)
{
// Waiting for UART event.
if (xQueueReceive(uart0_queue, (void *)&event, (TickType_t)portMAX_DELAY))
{
bzero(dtmp, RD_BUF_SIZE);
ESP_LOGI(TAG_CONFIG, "uart[%d] event:", EX_UART_NUM);
switch (event.type)
{
// Event of UART receving data
/*We'd better handler data event fast, there would be much more data events than
other types of events. If we take too much time on data event, the queue might
be full.*/
case UART_DATA:
ESP_LOGI(TAG_CONFIG, "[UART DATA]: %d", event.size);
uart_read_bytes(EX_UART_NUM, dtmp, event.size, portMAX_DELAY);
ESP_LOGI(TAG_CONFIG, "[DATA EVT]:");
int lastChar = 0; // Used to set the string terminator char after getting the command via uart
for (int i = 0; i <= event.size; i++)
{
const char letter = (const char)dtmp[i];
if (dtmp[i] == 10 || dtmp[i] == 0)
{
// If we detect a terminator char from the string stop adding new chars to cmd
break;
}
cmd[i] = letter;
lastChar = i;
}
cmd[++lastChar] = '\0'; // set the terminator string
ESP_LOGE(TAG_CONFIG, "Your command: %s ", cmd);
if (strstr(cmd, set_serial) != NULL) // check if cmd contains a specific string, in this case the string is "set_ser"
{
// contains
int lastChar2 = 0;
for (int i = strlen(set_serial) + 1; i < lastChar; i++)
{
int index = i - strlen(set_serial) - 1; // get the correct index to write in serialNum
serialNum[index] = cmd[i];
lastChar2 = index;
}
serialNum[++lastChar2] = '\0'; // Add termination to string
ESP_LOGW(TAG_CONFIG, "setting serial: %s ", serialNum);
for (int i = 0; i < strlen(serialNum); i++)
{
ESP_LOGW(TAG_CONFIG, "setting serial(AS UINT): %x ", serialAsInt[i]);
}
// Converting the serial number to hex, not sure if this is needed
int len = strlen(serialNum);
char hex_str[(len * 2) + 1];
// converting ascii string to hex string
string2hexString(serialNum, hex_str);
printf("serialNum: %s\n", serialNum);
printf("hex_str: %s\n", hex_str);
esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(EFUSE_BLK3);
write_efuse_fields(serialNum, coding_scheme);
}
Relevant code starts at line 14
Code: Untitled.c Select all
static void write_efuse_fields(char *serial_num, esp_efuse_coding_scheme_t coding_scheme)
{
#if CONFIG_IDF_TARGET_ESP32
const esp_efuse_coding_scheme_t coding_scheme_for_batch_mode = EFUSE_CODING_SCHEME_3_4;
#else
const esp_efuse_coding_scheme_t coding_scheme_for_batch_mode = EFUSE_CODING_SCHEME_RS;
#endif
ESP_LOGI(TAG_CONFIG, "write custom efuse fields");
ESP_LOGI(TAG_CONFIG, "Writing serial: ", &serial_num);
ESP_ERROR_CHECK(esp_efuse_batch_write_begin());
// uint8_t unique_id[16] = {0x48, 0x42, 0x30, 0x31, 0x39, 0x39, 0x39, 0x39, 0x30, 0x31, 0x32, 0x33};
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_efuse_write_field_blob(ESP_EFUSE_USER_DATA, &serial_num, 128));
if (coding_scheme == coding_scheme_for_batch_mode)
{
esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_efuse_batch_write_commit());
if (err != ESP_OK)
{
// Clean up
// free(serial_num);
ESP_LOGE(TAG_CONFIG, "Failed to write serial to efuse");
}
}
}
Can you guys give me a hand here?
Also, I'm sure the way I've handled the string manipulation is not the right way, feel free to roast me!
Thank in advance!