From 1a2a285a00d5cb7476f33ddaa83e8a91112f7329 Mon Sep 17 00:00:00 2001 From: Evan Lojewski Date: Sat, 3 Oct 2020 11:21:45 -0600 Subject: NVM: Add an API to determine the NVM size. (#124) --- libs/NVRam/include/NVRam.h | 2 ++ libs/NVRam/nvm.c | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) (limited to 'libs') diff --git a/libs/NVRam/include/NVRam.h b/libs/NVRam/include/NVRam.h index 919aeb7..5ec5b5f 100644 --- a/libs/NVRam/include/NVRam.h +++ b/libs/NVRam/include/NVRam.h @@ -61,6 +61,8 @@ void NVRam_enableWrites(void); void NVRam_disable(void); void NVRam_disableWrites(void); +uint32_t NVRam_size(void); + uint32_t NVRam_crc(const uint8_t *pcDatabuf, // Pointer to data buffer uint32_t ulDatalen, // Length of data buffer in bytes uint32_t crc); // Initial value diff --git a/libs/NVRam/nvm.c b/libs/NVRam/nvm.c index 1e4d0cb..e3c33ba 100644 --- a/libs/NVRam/nvm.c +++ b/libs/NVRam/nvm.c @@ -45,6 +45,8 @@ #include +#define BCM_NVRAM_MAGIC (0x669955AAu) + #define ATMEL_AT45DB0X1B_PAGE_POS (9u) #define ATMEL_AT45DB0X1B_PAGE_SIZE (264u) #define ATMEL_AT45DB0X1B_ERASE (false) @@ -90,7 +92,7 @@ static inline uint32_t NVRam_translate(uint32_t address) } else { - return address; + return address; } #else return address; @@ -323,3 +325,26 @@ void NVRam_write(uint32_t address, uint32_t *buffer, uint32_t words) } #endif } + +uint32_t NVRam_size(void) +{ + size_t size; + uint32_t magic = NVRam_readWord(0); + + if (magic != htonl(BCM_NVRAM_MAGIC)) + { + // Unable to determine the size. + return 0; + } + + // Scan for the magic wrapping around, starting at 2KB flash size. + size = 2u * 1024u; + do + { + size *= 2; + + magic = NVRam_readWord(size); + } while (magic != htonl(BCM_NVRAM_MAGIC)); + + return size; +} -- cgit v1.2.1