NVS read/write string data

mm_1993
Posts: 65
Joined: Sat Aug 28, 2021 5:16 am

NVS read/write string data

Postby mm_1993 » Sun Nov 21, 2021 12:04 pm

Hello everyone
new problem again and I need your help

I write two function for read and write string data (for save wifi pass and user name and change ble device name with an app).

the write function work good and without problem
but read function make panic
I change my data value type &... many try but without good result

my function are :
  1. void NVS_Write_String(const char* name, const char* key, const char* stringVal)
  2. {
  3.     nvs_handle_t nvsHandle;
  4.     esp_err_t retVal;
  5.  
  6.     retVal = nvs_open(name, NVS_READWRITE, &nvsHandle);
  7.     if(retVal != ESP_OK)
  8.     {
  9.         ESP_LOGE("NVS", "Error (%s) opening NVS handle for Write", esp_err_to_name(retVal));
  10.     }
  11.     else
  12.     {
  13.         printf("opening NVS Write handle Done \r\n");
  14.         retVal = nvs_set_str(nvsHandle, key, stringVal);
  15.         if(retVal != ESP_OK)
  16.         {
  17.             ESP_LOGE("NVS", "Error (%s) Can not write/set value: %s", esp_err_to_name(retVal), stringVal);
  18.         }
  19.  
  20.         retVal = nvs_commit(nvsHandle);
  21.         if(retVal != ESP_OK)
  22.         {
  23.             ESP_LOGE("NVS", "Error (%s) Can not commit - write", esp_err_to_name(retVal));
  24.         }
  25.         else
  26.         {
  27.             ESP_LOGI("NVS", "Write Commit Done!");
  28.         }
  29.        
  30.     }
  31.  
  32.     nvs_close(nvsHandle);
  33.    
  34. }

and
  1. char* NVS_Read_String(const char* name, const char* key)
  2. {
  3.     nvs_handle_t nvsHandle;
  4.     esp_err_t retVal;
  5.     size_t len;
  6.     char* savedData = NULL;
  7.  
  8.     len = sizeof(savedData);
  9.     ESP_LOGW("NVS", "Show Value-> name: %s, key: %s, len: %d", name, key, len);
  10.  
  11.     retVal = nvs_open(name, NVS_READWRITE, &nvsHandle);
  12.     if(retVal != ESP_OK)
  13.     {
  14.         ESP_LOGE("NVS", "Error (%s) opening NVS handle for Write", esp_err_to_name(retVal));
  15.     }
  16.     else
  17.     {
  18.         printf("opening NVS Read handle Done \r\n");
  19.         retVal = nvs_get_str(nvsHandle, key, savedData, &len);
  20.         if(retVal == ESP_OK)
  21.         {
  22.             ESP_LOGW("NVS", "*****(%s) Can read/get value: %s", esp_err_to_name(retVal), savedData);
  23.         }
  24.         else
  25.             ESP_LOGE("NVS", "Error (%s) Can not read/get value: %s", esp_err_to_name(retVal), savedData);
  26.  
  27.         retVal = nvs_commit(nvsHandle);
  28.         if(retVal != ESP_OK)
  29.         {
  30.             ESP_LOGE("NVS", "Error (%s) Can not commit - read", esp_err_to_name(retVal));
  31.         }
  32.         else
  33.         {
  34.             ESP_LOGI("NVS", "Read Commit Done!");
  35.         }
  36.     }
  37.  
  38.     nvs_close(nvsHandle);
  39.  
  40.     return savedData;
  41.  
  42. }
and main :
  1.  
  2. #define BLE_NVS_Data        "BLE_NVS"
  3. #define BLE_Dev_Name       "Test BLE"
  4. #define Wifi_NVS_Data        "wifi_nvs"
  5. #define wifi_ssid                  "ssid"
  6. #define wifi_pass                 "pass"
  7.  
  8. void main(void)
  9. {
  10.     char* temp = NULL;
  11.  
  12.     NVS_Write_String(BLE_NVS_Data, BLE_Dev_Name, "Helloooooo");
  13.     temp = NVS_Read_String(BLE_NVS_Data, BLE_Dev_Name);
  14.     printf("Read NVS String Value ........... %s \r\n", temp);
  15.  
  16. }
  17.  
and error is :
  1. opening NVS Write handle Done
  2. I (1171) NVS: Write Commit Done!
  3. W (1171) NVS: Show Value-> name: BLE_NVS, key: Test BLE, len: 4
  4. opening NVS Read handle Done
  5. Guru Meditation Error: Core  0 panic'ed (Load access fault). Exception was unhandled.
  6.  
  7. Stack dump detected
  8. Core  0 register dump:
  9. MEPC    : 0x40058eb6  RA      : 0x420fcbd4  SP      : 0x3fc9ca20  GP      : 0x3fc8e888
  10. 0x420fcbd4: _vfprintf_r at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/riscv32-esp-elf/src/newlib/newlib/libc/stdio/vfprintf.c:1528
  11.  
  12. TP      : 0x3fc66e44  T0      : 0x4005890e  T1      : 0x10000000  T2      : 0xffffffff
  13. S0/FP   : 0x00000000  S1      : 0x00000004  A0      : 0x00000000  A1      : 0xffffffff
  14. A2      : 0x00000004  A3      : 0x7f7f7f7f  A4      : 0x00000000  A5      : 0x00000004
  15. A6      : 0xa0000000  A7      : 0x0000000a  S2      : 0x00000000  S3      : 0x3fc9a8a4
  16. S4      : 0x3fc9d058  S5      : 0x00000009  S6      : 0x00000003  S7      : 0x00000003
  17. S8      : 0x3c114f6c  S9      : 0x00000000  S10     : 0x3fc9caf4  S11     : 0xffffffff
  18. T3      : 0x00000000  T4      : 0x00000000  T5      : 0x00000000  T6      : 0x00000000
  19. MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x00000005  MTVAL   : 0x00000000
  20. 0x40380001: _vector_table at ??:?
  21.  
  22. MHARTID : 0x00000000
  23.  
  24. Failed to run gdb_panic_server.py script: Command '['riscv32-esp-elf-gdb', '--batch', '-n', 'e:\\test-esp32\\c3-example\\my_first_ble5_test\\build\\my_First_BLE5_test.elf', '-ex', 'target remote | "C:\\ESP32\\.espressif\\python_env\\idf4.3_py3.8_env\\Scripts\\python.exe" "C:/ESP32/esp-idf/tools\\..\\tools\\gdb_panic_server.py" --target esp32c3 "C:\\Users\\Xarrin\\AppData\\Local\\Temp\\tmpw_10kq3y"', '-ex', 'bt']' returned non-zero exit status 3221225781.
  25. b''
  26.  
  27.  
  28. Core  0 register dump:
  29. MEPC    : 0x40058eb6  RA      : 0x420fcbd4  SP      : 0x3fc9ca20  GP      : 0x3fc8e888
  30. TP      : 0x3fc66e44  T0      : 0x4005890e  T1      : 0x10000000  T2      : 0xffffffff
  31. S0/FP   : 0x00000000  S1      : 0x00000004  A0      : 0x00000000  A1      : 0xffffffff
  32. A2      : 0x00000004  A3      : 0x7f7f7f7f  A4      : 0x00000000  A5      : 0x00000004
  33. A6      : 0xa0000000  A7      : 0x0000000a  S2      : 0x00000000  S3      : 0x3fc9a8a4
  34. S4      : 0x3fc9d058  S5      : 0x00000009  S6      : 0x00000003  S7      : 0x00000003
  35. S8      : 0x3c114f6c  S9      : 0x00000000  S10     : 0x3fc9caf4  S11     : 0xffffffff
  36. T3      : 0x00000000  T4      : 0x00000000  T5      : 0x00000000  T6      : 0x00000000
  37. MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x00000005  MTVAL   : 0x00000000
  38. MHARTID : 0x00000000
  39.  
  40. Stack memory:
  41. 3fc9ca20: 0x0000001e 0x00000073 0x00000004 0x3c114f6e 0x3c143c08 0x00000000 0x00000000 0x00000000
  42. 3fc9ca40: 0x00000000 0x00000037 0x00000001 0x00000006 0x00000000 0x3c14161c 0x00000000 0x00000000
  43. 3fc9ca60: 0x00000000 0x00000000 0xffffffff 0x7fefffff 0x00000000 0x3fe00000 0x00000003 0x3c114e3a
  44. 3fc9ca80: 0x00000000 0x3fc00000 0x00000000 0x40300000 0x000003ad 0x00000000 0x00000020 0x3fc9cc4c
  45. 3fc9caa0: 0x00000020 0x0000c080 0x0000c060 0x3fc9cebc 0x3fc9cd40 0x00000000 0x700000bb 0x00000004
  46. 3fc9cac0: 0x00000000 0x00676f6c 0x00000000 0x40388abe 0x000003c1 0x00000020 0x3fc8fefc 0x420687a4
  47. 3fc9cae0: 0x3fc9caec 0x00000001 0x00000016 0x3c114f56 0x00000016 0x3c1123b0 0x00000006 0x008c2008
  48. 3fc9cb00: 0x00000017 0x00000000 0x00000020 0x3fc9cc2c 0x00001a00 0x0000c0a0 0x0000c080 0x4038daa6
  49. 3fc9cb20: 0x00000000 0x00000020 0x00000000 0x00010003 0xffffffff 0xffffffff 0xffffffff 0x40388abe
  50. 3fc9cb40: 0x00000411 0x00000020 0x3fc8fefc 0x420687a4 0x3c130608 0xffffffff 0x3fc8fefc 0x40381374
  51. 3fc9cb60: 0x000005a0 0xffffffff 0xffffffff 0x403811c2 0xffffffff 0xffffffff 0x00000001 0x403837b0
  52. 3fc9cb80: 0x00000020 0x00000000 0x00000000 0x31383131 0xffffffff 0x00000020 0x00000000 0x4038352e
  53. 3fc9cba0: 0x00000020 0x3fc9dfc0 0x00000001 0x3fc8fedc 0x00000002 0x3fc9dfc0 0x00000001 0x3fc9cc6c
  54. 3fc9cbc0: 0x3fc99510 0x00000000 0x3fc9dfc0 0x3fc99510 0x3fc9cc2c 0x00000020 0x00000002 0x420686e4
  55. 3fc9cbe0: 0x00000001 0x0000007e 0x3c14171c 0x00000000 0x00000004 0x00000004 0x00000002 0x4206a6c4
  56. 3fc9cc00: 0x3fc99510 0x00000001 0x3fc9dfc0 0x4206b5e4 0x0000049d 0x00000004 0x00000042 0x4206bc00
  57. 3fc9cc20: 0x3c114e7c 0x3fc99510 0x700000bb 0x008c2008 0x3c1123b0 0x00000000 0x00000020 0x3fc9cdec
  58. 3fc9cc40: 0x00000000 0x00009300 0x700000bb 0x008c2008 0x00000004 0x00000000 0x00000020 0x3fc9ce30
  59. 3fc9cc60: 0x00000020 0x00009300 0x000092e0 0x4038daa6 0x00000000 0x00000020 0x3fc8fefc 0xff022105
  60. 3fc9cc80: 0xbf310ff3 0x74736554 0x454c4220 0x40388abe 0x00000000 0x00000020 0x3fc8fefc 0x420687a4
  61. 3fc9cca0: 0xffffffff 0xffffffff 0x3fc8fefc 0x40381374 0xffffffff 0xffffffff 0xffffffff 0x403811c2
  62. 3fc9ccc0: 0x00000020 0x00000020 0x3c114e6c 0x403837b0 0x00000020 0x00000000 0x00000000 0x403836fe
  63. 3fc9cce0: 0x00000020 0x00000020 0x00000000 0x4038352e 0xffffffff 0xffffffff 0x3fc8fefc 0x3fc8fedc
  64. 3fc9cd00: 0xffffffff 0xffffffff 0xffffffff 0x40389606 0x00000005 0x00000017 0x3c114e6c 0x3fc9ce30
  65. 3fc9cd20: 0x3fc9ce30 0x00000020 0x00000015 0x420686e4 0x00000020 0x00000020 0x00000000 0x00000000
  66. 3fc9cd40: 0x00000016 0x3fc9dec4 0x00000015 0x4206a6c4 0xffffffff 0x00000000 0x00000000 0x4206b5e4
  67. 3fc9cd60: 0x0000000a 0x3fc9dec4 0x00000015 0x4206ba6c 0x3fc9cdcc 0x00000004 0x3fc8e518 0x3fc9cdec
  68. 3fc9cd80: 0xff002105 0x3fc9ce94 0x74736554 0x454c4220 0x00000000 0x00000000 0xffffffff 0xffffffff
  69. 3fc9cda0: 0x00000004 0x3fc9d058 0x3fc9aa38 0x00000000 0x00000000 0x00000000 0x00000000 0x3fc9ce2c
  70. 3fc9cdc0: 0x00000005 0x00000021 0x3c114e6c 0x3fc9ce30 0x000000ff 0x000000ff 0x3fc9dec4 0x40389606
  71. 3fc9cde0: 0x0000001f 0x00000000 0x3fc9ab50 0x40387290 0x00000000 0x00000000 0x3fc9ab50 0x403878b6
  72. 3fc9ce00: 0x00000000 0x00000000 0x3fc9ab50 0x00000000 0x00000005 0x00000021 0x3fc9de54 0x00000000
  73.  
  74.  
  75.  
  76. ELF file SHA256: 00828b55ea266189
  77. ELF file SHA256: 00828b55ea266189
  78.  
  79. Rebooting...
  80.  �`ESP-ROM:esp32c3-api1-20210207
.
.
I want to use these functions
anyone know where that little bug ? I think somewhere of my code is wrong but I dont know :cry:

thank you all
regards

chegewara
Posts: 2258
Joined: Wed Jun 14, 2017 9:00 pm

Re: NVS read/write string data

Postby chegewara » Sun Nov 21, 2021 2:36 pm

Code: Select all

    char* savedData = NULL;
 
    len = sizeof(savedData);
    

User avatar
rudi ;-)
Posts: 1698
Joined: Fri Nov 13, 2015 3:25 pm

Re: NVS read/write string data

Postby rudi ;-) » Sun Nov 21, 2021 4:19 pm

-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

Who is online

Users browsing this forum: MicroController and 77 guests