Mirror with some correction
Dependencies: mbed FastIO FastPWM USBDevice
nvm.h@116:7a67265d7c19, 2021-10-01 (annotated)
- Committer:
- arnoz
- Date:
- Fri Oct 01 08:19:46 2021 +0000
- Revision:
- 116:7a67265d7c19
- Parent:
- 79:682ae3171a08
- Correct information regarding your last merge
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 | 79:682ae3171a08 | 45 | // Save to non-volatile memory. Returns true on success, false |
mjr | 79:682ae3171a08 | 46 | // if an error code is returned from the flash programmer. |
mjr | 79:682ae3171a08 | 47 | bool save(FreescaleIAP &iap, int addr) |
mjr | 35:e959ffba78fd | 48 | { |
mjr | 35:e959ffba78fd | 49 | // update the checksum and structure size |
mjr | 35:e959ffba78fd | 50 | d.sig = SIGNATURE; |
mjr | 35:e959ffba78fd | 51 | d.vsn = VERSION; |
mjr | 35:e959ffba78fd | 52 | d.sz = sizeof(NVM); |
mjr | 35:e959ffba78fd | 53 | checksum = CRC32(&d, sizeof(d)); |
mjr | 35:e959ffba78fd | 54 | |
mjr | 76:7f5912b6340e | 55 | // save the data to flash |
mjr | 79:682ae3171a08 | 56 | return iap.programFlash(addr, this, sizeof(*this)) == FreescaleIAP::Success; |
mjr | 76:7f5912b6340e | 57 | } |
mjr | 76:7f5912b6340e | 58 | |
mjr | 35:e959ffba78fd | 59 | // stored data (excluding the checksum) |
mjr | 35:e959ffba78fd | 60 | struct |
mjr | 35:e959ffba78fd | 61 | { |
mjr | 35:e959ffba78fd | 62 | // Signature, structure version, and structure size, as further |
mjr | 35:e959ffba78fd | 63 | // verification that we have valid data. |
mjr | 35:e959ffba78fd | 64 | uint32_t sig; |
mjr | 35:e959ffba78fd | 65 | uint16_t vsn; |
mjr | 35:e959ffba78fd | 66 | int sz; |
mjr | 35:e959ffba78fd | 67 | |
mjr | 35:e959ffba78fd | 68 | // configuration and calibration data |
mjr | 35:e959ffba78fd | 69 | Config c; |
mjr | 35:e959ffba78fd | 70 | } d; |
mjr | 35:e959ffba78fd | 71 | }; |
mjr | 35:e959ffba78fd | 72 | |
mjr | 35:e959ffba78fd | 73 | #endif /* NVM_M */ |