Writing a char array to efuse

schafon
Posts: 5
Joined: Mon Dec 12, 2022 7:48 pm

Writing a char array to efuse

Postby schafon » 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 ( :lol: ) and not the ESP framework.

Relevant code starts at line 26
  1. static void uart_event_task()
  2. {
  3.     uart_event_t event;
  4.     size_t buffered_size;
  5.     uint8_t *dtmp = (uint8_t *)malloc(RD_BUF_SIZE);
  6.     char cmd[30]; // the received command via UART
  7.     char serialNum[20]; // The serial number itself
  8.     for (;;)
  9.     {
  10.         // Waiting for UART event.
  11.         if (xQueueReceive(uart0_queue, (void *)&event, (TickType_t)portMAX_DELAY))
  12.         {
  13.             bzero(dtmp, RD_BUF_SIZE);
  14.             ESP_LOGI(TAG_CONFIG, "uart[%d] event:", EX_UART_NUM);
  15.             switch (event.type)
  16.             {
  17.             // Event of UART receving data
  18.             /*We'd better handler data event fast, there would be much more data events than
  19.             other types of events. If we take too much time on data event, the queue might
  20.             be full.*/
  21.             case UART_DATA:
  22.                 ESP_LOGI(TAG_CONFIG, "[UART DATA]: %d", event.size);
  23.                 uart_read_bytes(EX_UART_NUM, dtmp, event.size, portMAX_DELAY);
  24.                 ESP_LOGI(TAG_CONFIG, "[DATA EVT]:");
  25.                 int lastChar = 0; // Used to set the string terminator char after getting the command via uart
  26.                 for (int i = 0; i <= event.size; i++)
  27.                 {
  28.                     const char letter = (const char)dtmp[i];
  29.                     if (dtmp[i] == 10 || dtmp[i] == 0)
  30.                     {
  31.                         // If we detect a terminator char from the string stop adding new chars to cmd
  32.                         break;
  33.                     }
  34.                     cmd[i] = letter;
  35.                     lastChar = i;
  36.                 }
  37.                 cmd[++lastChar] = '\0'; // set the terminator string
  38.  
  39.                 ESP_LOGE(TAG_CONFIG, "Your command: %s ", cmd);
  40.  
  41.                 if (strstr(cmd, set_serial) != NULL) // check if cmd contains a specific string, in this case the string is "set_ser"
  42.                 {
  43.                     // contains
  44.                     int lastChar2 = 0;
  45.                     for (int i = strlen(set_serial) + 1; i < lastChar; i++)
  46.                     {
  47.                         int index = i - strlen(set_serial) - 1; // get the correct index to write in serialNum
  48.                         serialNum[index] = cmd[i];
  49.                         lastChar2 = index;
  50.                     }
  51.                     serialNum[++lastChar2] = '\0'; // Add termination to string
  52.                     ESP_LOGW(TAG_CONFIG, "setting serial: %s ", serialNum);
  53.                     for (int i = 0; i < strlen(serialNum); i++)
  54.                     {
  55.                         ESP_LOGW(TAG_CONFIG, "setting serial(AS UINT): %x ", serialAsInt[i]);
  56.                     }
  57.  
  58.                     // Converting the serial number to hex, not sure if this is needed
  59.                     int len = strlen(serialNum);
  60.                     char hex_str[(len * 2) + 1];
  61.                     // converting ascii string to hex string
  62.                     string2hexString(serialNum, hex_str);
  63.                     printf("serialNum: %s\n", serialNum);
  64.                     printf("hex_str: %s\n", hex_str);
  65.  
  66.                     esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(EFUSE_BLK3);
  67.                     write_efuse_fields(serialNum, coding_scheme);
  68.                 }
  69.  

Relevant code starts at line 14
  1. static void write_efuse_fields(char *serial_num, esp_efuse_coding_scheme_t coding_scheme)
  2. {
  3. #if CONFIG_IDF_TARGET_ESP32
  4.     const esp_efuse_coding_scheme_t coding_scheme_for_batch_mode = EFUSE_CODING_SCHEME_3_4;
  5. #else
  6.     const esp_efuse_coding_scheme_t coding_scheme_for_batch_mode = EFUSE_CODING_SCHEME_RS;
  7. #endif
  8.  
  9.     ESP_LOGI(TAG_CONFIG, "write custom efuse fields");
  10.     ESP_LOGI(TAG_CONFIG, "Writing serial: ", &serial_num);
  11.     ESP_ERROR_CHECK(esp_efuse_batch_write_begin());
  12.  
  13.     // uint8_t unique_id[16] = {0x48, 0x42, 0x30, 0x31, 0x39, 0x39, 0x39, 0x39, 0x30, 0x31, 0x32, 0x33};
  14.     ESP_ERROR_CHECK_WITHOUT_ABORT(esp_efuse_write_field_blob(ESP_EFUSE_USER_DATA, &serial_num, 128));
  15.  
  16.     if (coding_scheme == coding_scheme_for_batch_mode)
  17.     {
  18.  
  19.         esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_efuse_batch_write_commit());
  20.         if (err != ESP_OK)
  21.         {
  22.             // Clean up
  23.             // free(serial_num);
  24.             ESP_LOGE(TAG_CONFIG, "Failed to write serial to efuse");
  25.         }
  26.     }
  27. }
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!

Who is online

Users browsing this forum: No registered users and 437 guests