Proivdes data log data structure for FRAM, EPROM chip with functions to read chip and send back on serial data string.

Dependencies:   W25Q80BV multi-serial-command-listener

Dependents:   xj-data-log-test-and-example

Data Logging Data structure

Both Read and write seem to be working fine but testing has been limited.

Motivation

I needed a flexible data log structure that could tolerate evolving data structures as I discovered more things that needed to be measured. I also wanted something that is mostly human readable while remaining sufficiently concise to make efficient use of expensive storage resources.

I found it challenging to track everything needed to perform after the fact analysis we need to improve our state machine. In addition what I wanted to measure changed with time and I needed a robust way to log this data so we could analyze it latter. without breaking or converting all the old data. A self describing data format like JSON or XML would work but FRAM is expensive so I wanted something flexible but still concise.

I am working on A2WH which is a electronic controller for a sophisticated product that balances many sensors, battery charging from photo voltaic panels, controlling speed of many different fans, humidity and environmental data. Our main challenge is we never have enough battery power to run everything so we have to make decisions about what to run in an effort to produce the maximum amount of water from the available solar power resource. Our 2nd challenge is that balancing system actions such as increasing or decreasing fan speeds is driven by a complex internal prediction model that attempts balance many competing thermodynamic requirements. To get all this right requires substantial after the fact analysis and that requires logging a large amount of evolving data.

Design Notes

See: data-log-read.me.txt in the same project

Sample Use and Basic Test

Serial Command Interface

COMMANDS
  readall= send entire contents of log
  readlast 999
     999 = number of bytes from tail of log to retrieve
  tread 333 444
     333 = starting offset to start reading log
     444 = number of bytes to retrieve from log
  erase = erase log and start a new one
  help  = display this help

Other Chips

For legacy reasons I am using the library for "W25Q80BV.h" simply because I started with it. The actual FRAM chip I am using is 2 MBit FRAM MB85RS2MTPH-G-JNE I also tested it with SRAM 23LCV1024-I/P

Simplifying Design Decision

I made a simplifying assumption that every-time we generate a log entry I record the offset of the next write at a specific location in the chip. This works and is fast but it causes lots of updates against a single location. I prefer FRAM because this would rapidly fatigue FLASH chips like the W25Q80BV. Storing this pointer data in the CPU has the same fatigue problem.

Another other option would be to store this offset and our other critical configuration data in the clock chip but it is susceptible to loosing power and loosing this critical data.

One reason I don't log directly to the micro-sd is for the same fatigue problem but it is mostly for power management.

The FRAM chip provides adequate durability and data retention through power outage. The power outage retention is critical because the A2WH systems can be buried under feet of snow in the winter and solar panels do not provide much recharge under that condition.

One design option I have considered but not yet implemented is using a much smaller FRAM chip critical configuration data and rapid update data and then log directly to a larger and less expensive FLASH chip .

Journaling to micro-SD

I latter decided to add features to allow after the fact copying of the data to micro-sd cards to obtain larger log storage without soldering in more chips. I found the micro-sd consume quite a lot of power so I still want to log direct to the FRAM then copy to the micro-sd when I have surplus power available. Still thinking about consolidation tactics to allow re-use of FRAM after the data has been copied ot micro-sd.

Future

  • Support fast indexing by date to only pull back log entries between two dates.
  • Record most recent record headers for each record types where they are fast to access so we can send them with the data when only sending back portions of the data.
  • Support wrap around use of data log to re-use storage on chip.
  • Copy Data to micro SD card and consolidate FRAM chip for re-use.

License

By Joseph Ellsworth CTO of A2WH Take a look at A2WH.com Producing Water from Air using Solar Energy March-2016 License: https://developer.mbed.org/handbook/MIT-Licence Please contact us http://a2wh.com for help with custom design projects.

data-log-read.me.txt

Committer:
joeata2wh
Date:
2016-04-13
Revision:
11:bf816d33be80
Parent:
4:fa5bbe31a039

File content as of revision 11:bf816d33be80:

To make the data log format somewhat flexible and 
some what easy to debug while remaining somewhat
consise while remaining human readable.  The format
will be a custom format but is designed to be relatively
easy to parse while accomodating the following caveates:
   * Fields logged over time will change as firmware is reved.
   * Old log entries for the same record type will have different fileds.
   * storage in fram is realtive expensive so self describing data 
     formats like JSON would not be cost effective. 
   * When things mess up we want to be able to parse it and 
     analyze it and have some chance of recovery 
   * We need fast access to pont in time for at least 1 day 
     of data. 
     
will use a custom format.  

Each record type will have record type label which will be
composed of 4 characters separated from the data by a The known
record types are:

Records will be appended to the data log in sequential order. Each
record will be terminated with a \n.  Some records will contain fixed
length data but will still be terminated with \n and will never contain
a \n in the stored data. unless the first number after the record type 
is 16 bit number storing the number 

The general assumption is that all data will be stored for the short
term in high speed FRAM or SRAM chips and copied to EPROM or Micro 
SD chips in the background once sufficnet data has accumulated to 
minimize over-write fatigue in the long term storage.  I used FRAM 
specifically to allow after power loss data retention without having 
to worry about overrite fatigue for specific segments that would need
rapid updates.    The other assumption is that data is stored and logged
then will be accessed generally starting at a given date reading forward 

Short term Simplification Notes:
  Each time the CPU reboots it will record current record header field 
  names for all record types active at that time.  This will create some
  duplicates but save some overhead in firmware.
  
  The firmware will only record next log position internally.  It will
  simply write next log item at end of prior one.
  
  Firmware will not record day start indexes.
  
  Firmware will provide simple dump feature to send all data to serial
  IO line. 
  
  TODO:  How to read serial input in non blocking
     fashion. 


Beginning Address.  Since the log is presumably stored in a FRAM chip
without support for a file system driver we must know the offset of where
the log begins.  Space before the log is generally used for system configuration
data. 

Pre-Log Values: 
  DATOFNDX:
    Date Offset Index.  Array of 100 string formated number pairs
    containing dateOffset records. Each pair contains
    one number which is string containing the date of 
    the first record in that date offset in the format
    ccyymmd,offset the assumption is we can quickly scan
    or binary search this array to find the first offset
    record that describes the date range we are seeking then
    jump and read that offset record.  Each time we add a new 
    date offset log item we update this value. 
    

Record Types:

date: 
  Date types will be composed of the format ccyy-mm-dd hh:mm:ss
  a new date record will be recorded whenever the system detects 
  a new date when preparing to generate a new log item.  At items
  that occur after this date are presumed to occur on that date. 
  
Record Type active defenition:
  A space at first of system that stores the iformation about the
  currently active record types. 

Record Type defenition: 
  Prefix RTDE,RecordType,List of Field Names This is written into
  the log whenever the system reads a record type defenition from the 
  current record types that is different than the once currently used
  in the firmware.  When ths occurs it will write a new record type 
  defenition  
  

next_write_offset:  NWOF 
  Next write offset record stores the byte postion of the next
  postion in the log byte offset where the next 

date_offset: 
  Fixed Length Binary Data - 
  Preix: DATOF 
         NumRec
         Offset Of Next DATOF 32 bit number
         Offsets (array of 32 bit numbers)
         RecordOffset records are store in a region of  storage
  after the before the actual data logs are written.  These are
  fixed length records where the starting byte offset for each
  days of logging are stored as 32 bit unsigned integer numbers
  as an array. The first number contains the number of spaces 
  pre-allocated. The last number written will be the offset to 
  the next date_offset record.  In general we will allocate 365
  365 days worht of space so the total row length will be 
  32 + (365 * 32) + 32 bits = 11,744 bits
  X number of days with a a
  
  
reading: 

System state: