Lora-net/LoRaMac-node

NVM context restore not workig because of wrong crc in 0-sized RegionGroup1

Closed this issue · 0 comments

LoRaMac-node 4.6.0.

Flash default state is 0xFFFFFFFF in most devices.
Nvm has default value 0, it is an uninitialized variable, so it is zeroed by compiler startup code before main().
RestoreNvmData() compares CRC in flash with CRC in Nvm, they are not equal (0xFFFFFFFF!=0x00000000), so flash is not copied to Nvm.
Nvm.RegionGroup1 size is 0 bytes for EU868 region, its crc is always 0.
LoRaMacHandleNvm() compares Nvm.RegionGroup1.Crc32 to Crc32(&Nvm.RegionGroup1,0), they are always equal, LORAMAC_NVM_NOTIFY_FLAG_REGION_GROUP1 is never set.
NvmDataMgmtStore() never stores Nvm.RegionGroup1 because LORAMAC_NVM_NOTIFY_FLAG_REGION_GROUP1 is never set, so crc is 0xFFFFFFFF (or any value other than 0) forever.
NvmDataMgmtRestore() checks crc of every Nvm member on flash. RegionGroup1 has wrong crc, true = (calculatedCrc32 != readCrc32), because it is never writen to flash.
NvmDataMgmtRestore() restores context if all crc in flash are right, but RegionGroup1.crc32 is always wrong. Therefore context stored to flash is never restored.

If flash default state is 0 then it works fine.
If flash or eeprom is set to 0 when main flash is written then it works fine.

Proposed solution:
In function:
bool NvmmCrc32Check( uint16_t size, uint16_t offset )
add:
if (size > sizeof(readCrc32))
before:

if( NvmmRead( ( uint8_t* ) &readCrc32, sizeof( readCrc32 ),
                  ( offset + ( size - sizeof( readCrc32 ) ) ) ) == sizeof( readCrc32 ) )

Another issue is EEPROM durability, if a device sends a message every minute, EEPROM from STM32L0 will be worn in 52 days.
Note that EEPROM granularity for STM32L is a 32 bit word, when 4 consecutive bytes are written it writes 4 times the complete word.

Another issue is execution is stopped for a long time (0.5s is a long time when MCU is executing any other task than lora, for example motor control, 7-segment refresh, touch measurement, etc.) on every message sent because of EEPROM write duration.
Note that writing a byte takes the same time than writing a word, and maybe the same time than writing a half-page (16 words, 64 bytes).

I don't open an issue for EEPROM because it has been discussed many times.
Regards