An I/O controller for virtual pinball machines: accelerometer nudge sensing, analog plunger input, button input encoding, LedWiz compatible output controls, and more.

Dependencies:   mbed FastIO FastPWM USBDevice

Fork of Pinscape_Controller by Mike R

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nvm.h Source File

nvm.h

00001 // NVM - Non-Volatile Memory
00002 //
00003 // This module handles the storage of our configuration settings
00004 // and calibration data in flash memory, which allows us to
00005 // retrieve these settings after each power cycle.
00006 
00007 
00008 #ifndef NVM_H
00009 #define NVM_H
00010 
00011 #include "config.h"
00012 #include "FreescaleIAP.h"
00013 
00014 
00015 // Non-volatile memory (NVM) structure
00016 //
00017 // This structure defines the layout of our saved configuration
00018 // and calibration data in flash memory.
00019 //
00020 // The space in flash for the structure is reserved in the main
00021 // program, by exploiting the linker's placement of const data
00022 // in flash memory.  This gives us a region of the appropriate
00023 // size.
00024 struct NVM
00025 {
00026 public:
00027     // checksum - we use this to determine if the flash record
00028     // has been properly initialized
00029     uint32_t checksum;
00030 
00031     // signature and version reference values
00032     static const uint32_t SIGNATURE = 0x4D4A522A;
00033     static const uint16_t VERSION = 0x0003;
00034     
00035     // Is the data structure valid?  We test the signature and 
00036     // checksum to determine if we've been properly stored.
00037     bool valid() const
00038     {
00039         return (d.sig == SIGNATURE 
00040                 && d.vsn == VERSION
00041                 && d.sz == sizeof(NVM)
00042                 && checksum == CRC32(&d, sizeof(d)));
00043     }
00044     
00045     // Save to non-volatile memory.  Returns true on success, false
00046     // if an error code is returned from the flash programmer.
00047     bool save(FreescaleIAP &iap, int addr)
00048     {
00049         // update the checksum and structure size
00050         d.sig = SIGNATURE;
00051         d.vsn = VERSION;
00052         d.sz = sizeof(NVM);
00053         checksum = CRC32(&d, sizeof(d));
00054         
00055         // save the data to flash
00056         return iap.programFlash(addr, this, sizeof(*this)) == FreescaleIAP::Success;
00057     }
00058     
00059     // stored data (excluding the checksum)
00060     struct
00061     {
00062         // Signature, structure version, and structure size, as further
00063         // verification that we have valid data.
00064         uint32_t sig;
00065         uint16_t vsn;
00066         int sz;
00067         
00068         // configuration and calibration data
00069         Config c;
00070     } d;
00071 };
00072 
00073 #endif /* NVM_M */