Mirror with some correction
Dependencies: mbed FastIO FastPWM USBDevice
nvm.h@76:7f5912b6340e, 2017-02-03 (annotated)
- Committer:
- mjr
- Date:
- Fri Feb 03 20:50:02 2017 +0000
- Revision:
- 76:7f5912b6340e
- Parent:
- 59:94eb9265b6d7
- Child:
- 79:682ae3171a08
Rework flash driver to make it truly stable (hopefully to 100% reliability); host-loaded configuration; performance improvements; more performance diagnostics.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjr | 35:e959ffba78fd | 1 | // NVM - Non-Volatile Memory |
mjr | 35:e959ffba78fd | 2 | // |
mjr | 35:e959ffba78fd | 3 | // This module handles the storage of our configuration settings |
mjr | 35:e959ffba78fd | 4 | // and calibration data in flash memory, which allows us to |
mjr | 35:e959ffba78fd | 5 | // retrieve these settings after each power cycle. |
mjr | 35:e959ffba78fd | 6 | |
mjr | 35:e959ffba78fd | 7 | |
mjr | 35:e959ffba78fd | 8 | #ifndef NVM_H |
mjr | 35:e959ffba78fd | 9 | #define NVM_H |
mjr | 35:e959ffba78fd | 10 | |
mjr | 35:e959ffba78fd | 11 | #include "config.h" |
mjr | 35:e959ffba78fd | 12 | #include "FreescaleIAP.h" |
mjr | 35:e959ffba78fd | 13 | |
mjr | 35:e959ffba78fd | 14 | |
mjr | 35:e959ffba78fd | 15 | // Non-volatile memory (NVM) structure |
mjr | 35:e959ffba78fd | 16 | // |
mjr | 35:e959ffba78fd | 17 | // This structure defines the layout of our saved configuration |
mjr | 35:e959ffba78fd | 18 | // and calibration data in flash memory. |
mjr | 35:e959ffba78fd | 19 | // |
mjr | 76:7f5912b6340e | 20 | // The space in flash for the structure is reserved in the main |
mjr | 76:7f5912b6340e | 21 | // program, by exploiting the linker's placement of const data |
mjr | 76:7f5912b6340e | 22 | // in flash memory. This gives us a region of the appropriate |
mjr | 76:7f5912b6340e | 23 | // size. |
mjr | 35:e959ffba78fd | 24 | struct NVM |
mjr | 35:e959ffba78fd | 25 | { |
mjr | 35:e959ffba78fd | 26 | public: |
mjr | 35:e959ffba78fd | 27 | // checksum - we use this to determine if the flash record |
mjr | 35:e959ffba78fd | 28 | // has been properly initialized |
mjr | 35:e959ffba78fd | 29 | uint32_t checksum; |
mjr | 35:e959ffba78fd | 30 | |
mjr | 35:e959ffba78fd | 31 | // signature and version reference values |
mjr | 35:e959ffba78fd | 32 | static const uint32_t SIGNATURE = 0x4D4A522A; |
mjr | 35:e959ffba78fd | 33 | static const uint16_t VERSION = 0x0003; |
mjr | 35:e959ffba78fd | 34 | |
mjr | 35:e959ffba78fd | 35 | // Is the data structure valid? We test the signature and |
mjr | 35:e959ffba78fd | 36 | // checksum to determine if we've been properly stored. |
mjr | 76:7f5912b6340e | 37 | bool valid() const |
mjr | 35:e959ffba78fd | 38 | { |
mjr | 35:e959ffba78fd | 39 | return (d.sig == SIGNATURE |
mjr | 35:e959ffba78fd | 40 | && d.vsn == VERSION |
mjr | 35:e959ffba78fd | 41 | && d.sz == sizeof(NVM) |
mjr | 35:e959ffba78fd | 42 | && checksum == CRC32(&d, sizeof(d))); |
mjr | 35:e959ffba78fd | 43 | } |
mjr | 35:e959ffba78fd | 44 | |
mjr | 35:e959ffba78fd | 45 | // save to non-volatile memory |
mjr | 35:e959ffba78fd | 46 | void save(FreescaleIAP &iap, int addr) |
mjr | 35:e959ffba78fd | 47 | { |
mjr | 35:e959ffba78fd | 48 | // update the checksum and structure size |
mjr | 35:e959ffba78fd | 49 | d.sig = SIGNATURE; |
mjr | 35:e959ffba78fd | 50 | d.vsn = VERSION; |
mjr | 35:e959ffba78fd | 51 | d.sz = sizeof(NVM); |
mjr | 35:e959ffba78fd | 52 | checksum = CRC32(&d, sizeof(d)); |
mjr | 35:e959ffba78fd | 53 | |
mjr | 76:7f5912b6340e | 54 | // save the data to flash |
mjr | 35:e959ffba78fd | 55 | iap.program_flash(addr, this, sizeof(*this)); |
mjr | 35:e959ffba78fd | 56 | } |
mjr | 35:e959ffba78fd | 57 | |
mjr | 76:7f5912b6340e | 58 | // verify that the NVM matches the in-memory configuration |
mjr | 76:7f5912b6340e | 59 | bool verify(int addr) |
mjr | 76:7f5912b6340e | 60 | { |
mjr | 76:7f5912b6340e | 61 | return memcmp((NVM *)addr, this, sizeof(*this)) == 0; |
mjr | 76:7f5912b6340e | 62 | } |
mjr | 76:7f5912b6340e | 63 | |
mjr | 35:e959ffba78fd | 64 | // stored data (excluding the checksum) |
mjr | 35:e959ffba78fd | 65 | struct |
mjr | 35:e959ffba78fd | 66 | { |
mjr | 35:e959ffba78fd | 67 | // Signature, structure version, and structure size, as further |
mjr | 35:e959ffba78fd | 68 | // verification that we have valid data. |
mjr | 35:e959ffba78fd | 69 | uint32_t sig; |
mjr | 35:e959ffba78fd | 70 | uint16_t vsn; |
mjr | 35:e959ffba78fd | 71 | int sz; |
mjr | 35:e959ffba78fd | 72 | |
mjr | 35:e959ffba78fd | 73 | // configuration and calibration data |
mjr | 35:e959ffba78fd | 74 | Config c; |
mjr | 35:e959ffba78fd | 75 | } d; |
mjr | 35:e959ffba78fd | 76 | }; |
mjr | 35:e959ffba78fd | 77 | |
mjr | 35:e959ffba78fd | 78 | #endif /* NVM_M */ |