NVProperty generic key value store using the MCU flash area.

Dependents:   Turtle_RadioShuttle

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NVProperty_MBEDFlash.h Source File

NVProperty_MBEDFlash.h

00001 /*
00002  * This is an unpublished work copyright
00003  * (c) 2019 Helmut Tschemernjak
00004  * 30826 Garbsen (Hannover) Germany
00005  *
00006  *
00007  * Use is granted to registered RadioShuttle licensees only.
00008  * Licensees must own a valid serial number and product code.
00009  * Details see: www.radioshuttle.de
00010  */
00011 
00012 #ifndef __NVPROPERTY_MBEDFLASH__
00013 #define __NVPROPERTY_MBEDFLASH__
00014 
00015 class NVProperty_MBEDFlash : public NVPropertyProviderInterface {
00016 public:
00017     NVProperty_MBEDFlash(int propSizekB, bool erase);
00018     ~NVProperty_MBEDFlash();
00019     
00020     virtual int GetProperty(int key);
00021     virtual int64_t GetProperty64(int key);
00022     virtual int GetPropertyBlob(int key, const void *blob, int *size);
00023     virtual const char *GetPropertyStr(int key);
00024     virtual int SetProperty(int key, int64_t value, int type);
00025     virtual int SetPropertyStr(int key, const char *value, int type);
00026     virtual int SetPropertyBlob(int key, const void *blob, int size, int type);
00027     virtual int EraseProperty(int key);
00028     virtual int ReorgProperties(void);
00029     virtual int OpenPropertyStore(bool forWrite = false);
00030     virtual int ClosePropertyStore(bool flush = false);
00031     
00032 private:
00033     void _FlashInititalize(bool force = false);
00034     void _FlashEraseRow(int startRow, int count = 1);
00035     void _FlashWrite(uint8_t *address, const void *data, size_t length);
00036     bool _FlashIsCleared(uint8_t *address, int len);
00037     void _FlashWritePage(int page, int offset, uint8_t *data, int length);
00038 
00039     struct _flash_header {
00040         uint32_t magic;
00041         uint16_t version;
00042         uint16_t sizeKB;
00043     };
00044     
00045     static const int FLASH_ENTRY_HEADER         = 4;
00046     static const int FLASH_ENTRY_HEADER_SHORT   = 2;
00047     static const int MAX_DATA_ENTRY             = 256;
00048     
00049     struct _flashEntry {
00050         uint8_t key;    // Property key
00051         struct {
00052             uint8_t deleted : 1; // this key has been deleted
00053             uint8_t t_bit   : 1; // contains the bool value
00054             uint8_t reserv1 : 1; //
00055             uint8_t reserv2 : 1; //
00056             uint8_t type    : 4; // NVPType
00057         } t;
00058         union {
00059             int16_t v_16bit;
00060             int8_t  v_8bit;
00061             struct {
00062                 uint8_t d_len;               // data length
00063                 uint8_t f_reserv1       : 8;
00064             } option;
00065         } u;
00066         union {
00067             int32_t v_32bit;
00068             int32_t v_64bit[2]; // use use 2 x 32-bit to avoid 64-bit struct padding
00069             char v_str[MAX_DATA_ENTRY];
00070             uint8_t v_blob[MAX_DATA_ENTRY];
00071         } data;
00072     };
00073     
00074     _flashEntry * _GetFlashEntry(int key, uint8_t *start = NULL);
00075     int _GetFlashEntryLen(_flashEntry *k);
00076     int _GetFlashPaddingSize(int len);
00077     _flashEntry *_lastEntry;
00078     void _DumpAllEntires(void);
00079     int _FlashReorgEntries(int minRequiredSpace);
00080     bool _debug;
00081     int _propSizekB;
00082     int _pageSize;
00083     int _numPages;
00084     int _rowSize;
00085     uint8_t _flashErasedValue;
00086     FlashIAP *_flashIAP;
00087     uint8_t *_startAddress;
00088     uint8_t *_endAddress;
00089 
00090     static const int FLASH_PROP_MAGIC = 0x4e564d42; // "NVMB"
00091     static const int FLASH_PROP_VERSION = 1;
00092 };
00093 
00094 #endif // __NVPROPERTY_MBEDLASH__