B4R Question problem with rArduinoNvs lib under ESP32-C3

peacemaker

Expert
Licensed User
Longtime User
Hi, All and @candide

I have tried the rArduinoNvs lib (that is in file v.1.1, but actually rArduinoNvs.xml is with v.1.0) with ESP32-C3 MCU.
SDK is v.3.0.4.
And some variable are read from EEPROM wrongly:

Writing at 0x00038a74... (54 %)
Writing at 0x0003f1e3... (63 %)
Writing at 0x00045430... (72 %)
Writing at 0x0004b805... (81 %)
Writing at 0x00051744... (90 %)
Writing at 0x000588fe... (100 %)
Wrote 318576 bytes (175378 compressed) at 0x00010000 in 2.6 seconds (effective 992.6 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
AppStart
Begin OK
read byte Nvs 0xAB : AB
read Int Nvs 0x45AB : 45AB
read UInt Nvs 0x95AB : 95AB
read Long Nvs 0x1045AB : 000045AB
read ULong Nvs 0x8095AB : FFFF95AB
read Double Nvs 1045,123 : 1045.1230
read String Nvs by Return 'test du save des string' : test de save des string
read String in ArrayByte OK
read String Nvs by parameter Array 'test du save des string' : test de save des string
BlobLength to read :14
Read BlobArray OK
BlobArray read : [0123456789:;<=]

Configuration of test :
erase = true : memory erased at boot and data are written before read
erase = false : no memory erase, no data written before read, we read data stored in flash

Reading Long and ULong are wrong.
How to check and fix ?
 
Last edited:

candide

Active Member
Licensed User
it seems a problem with convertion of 0x12345678 in Long or ULong format

to check, you can run code below:
B4X:
    Log("  Long"): Log("0x001045AB","-",(0x001045AB+1))    '=> same error
    
    If erase Then
        If Nvs.setLong("myLong",0x001045AB,False) Then Log("write Long 0x001045AB OK") Else Log("write Long NOK")
    End If    
    Log("read Long Nvs 0x001045AB : ",longtoHexa(Nvs.getLong("myLong",0)),"---",Nvs.getLong("myLong",0) )
    If erase Then
        If Nvs.setLong("myLong",1066411,False) Then Log("write Long 1066411 OK") Else Log("write Long NOK")
    End If
    Log("read Long Nvs 0x001045AB : ",longtoHexa(Nvs.getLong("myLong",0)),"---",Nvs.getLong("myLong",0) )

    Log("ULong "): Log("0x001045AB","-",(0x001045AB+1))    '=> same error

    If erase Then
        If Nvs.setULong("myUlong",0x008095AB,False) Then Log("write ULong 0x008095AB OK") Else Log("write ULong NOK")
    End If    
    Log("read ULong Nvs 0x008095AB : ",UlongtoHexa(Nvs.getULong("myUlong",0)),"---",Nvs.getULong("myUlong",0))
    If erase Then
    If Nvs.setULong("myUlong",8426923,False) Then Log("write ULong 8426923 OK") Else Log("write ULong NOK")
    End If
    Log("read ULong Nvs 8426923 : ",UlongtoHexa(Nvs.getULong("myUlong",0)),"---",Nvs.getULong("myUlong",0))
    Log(" ")
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Indeed !
B4X:
write Long 0x001045AB OK
read Long Nvs 0x001045AB : 000045AB---17835
write Long 1066411 OK
read Long Nvs 1066411 : 001045AB---1066411
write ULong 0x008095AB OK
read ULong Nvs 0x008095AB : FFFF95AB---4294940075
write ULong 8426923 OK
read ULong Nvs 8426923 : 008095AB---8426923

@Erel, could you have a look ?
Is 0x001045AB 32bit HEX how converted in the IDE ?

B4X:
If Nvs.setLong("myLong",0x001045AB,False) Then Log("write Long 0x001045AB OK") Else Log("write Long NOK")
Log("read Long Nvs 0x001045AB : ",longtoHexa(Nvs.getLong("myLong",0)),"---",Nvs.getLong("myLong",0) )
If Nvs.setLong("myLong",1066411,False) Then Log("write Long 1066411 OK") Else Log("write Long NOK")
Log("read Long Nvs 1066411 : ",longtoHexa(Nvs.getLong("myLong",0)),"---",Nvs.getLong("myLong",0) )

If Nvs.setULong("myUlong",0x008095AB,False) Then Log("write ULong 0x008095AB OK") Else Log("write ULong NOK")
Log("read ULong Nvs 0x008095AB : ",UlongtoHexa(Nvs.getULong("myUlong",0)),"---",Nvs.getULong("myUlong",0))
If Nvs.setULong("myUlong",8426923,False) Then Log("write ULong 8426923 OK") Else Log("write ULong NOK")
Log("read ULong Nvs 8426923 : ",UlongtoHexa(Nvs.getULong("myUlong",0)),"---",Nvs.getULong("myUlong",0))

If to use dec numbers - it's OK, but 32bit HEX looks like wrongly used by the B4R IDE.
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User

Attachments

  • test_UInt_Nvs.zip
    1.3 KB · Views: 53
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two things here:
1. The hex notation literal is parsed as an Int number.
2. It uses the default little endianess.

You will need to use RandomAccessFile library for this
B4X:
Private Sub HexToULongBigEndianess (hex As String) As ULong
    Dim raf As RandomAccessFile
    Dim bc As ByteConverter
    raf.Initialize(bc.HexToBytes(hex), False)
    Return raf.ReadULong32(raf.CurrentPosition)
End Sub

B4X:
Dim l As ULong = HexToULongBigEndianess("11BC614F") 'don't start with 0x
 
Upvote 0
Top