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.

dataLog.h

Committer:
joeata2wh
Date:
2016-03-30
Revision:
3:5550814cc21c
Parent:
2:8d06af2f1fcc
Child:
4:fa5bbe31a039

File content as of revision 3:5550814cc21c:

/* DataLog.h - Data logger for logging enviornmental
  data to EPROM, SRAM or FRAM chip without a file system
  inteface.  Supports multiple and evolving records types
  and trnsport back to serial 
  
  See data-log-text.txt for detailed design and layout notes
  See: xj-data-log-test-and-example.c for example use.
  
  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.
  
  Before you complain about not using proper C++ classes, I intend to 
  port the entire A2WH project to PSoc where I may or may not be able to
  use the full set of C++ features.  I am using techniques that should be
  easier to port to a ANSI C enviornment.  You may think this is a wierd
  decision but the PSoC enviornments gives me transparant support for 
  differential ADC and very low power analog comparators that remain active
  when CPU is in deep sleep and which can wake the CPU up from deep sleep
  which makes very low power designs easier.  mBed is still weak for this
  kind of advanced peripherial support. 

*/
#ifndef DataLog_H
#define DataLog_H
#include "mbed.h"

#include "W25Q80BV.h"  // Note:  We are using this library because we started with it but
                       //  the actual chip we are using is a 2 MBit FRAM MB85RS2MTPH-G-JNE
                       //  also tested with SRAM 23LCV1024-I/P  Prefer SRAM or FRAM because
                       //  we made a simplifying assumption that we could write next log 
                       //  address to the same position over and over.  Without wear leveling
                       //  this could rapidly wear out a e-prom chip.  Could have written 
                       //  this to the clock chip which uses SRAM but FRAM has such high write
                       //  durability that we don't have to. 
                       
#define DataLogChipType W25Q80BV

// TODO: Add the SDIO link here to copy data from dlog chip to 
//   SD card when available. 


const long dlChipMaxAddr = 250000; // 2 mBit 2000000 / 8
#define dlChipFullErr -2
#define dlAddressTooLarge -3
#define dlMaxOperationSize -4;
// Chip DLog Chip Memory Layout
const long dlAddrInitByte  = 1500; // data before this is assumed to be used for system config variables
const char dlInitByteValue = 213;
const int dlMaxReadWriteSize = 32000; // limit imposed by streaming interface for the chip
const long dlAddrNextWritePos = dlInitByteValue + 1;
const long dlAddrNextWritePosSize = 4;
const long dlAddrCurrYDay = dlAddrNextWritePos + dlAddrNextWritePosSize;
const long dlAddrCurrYDaySize = 2;
const long dlAddrHeaders =   dlAddrCurrYDay + dlAddrCurrYDaySize ;
const long dlHeadersLen  =   256;
const long dlDateIndex   =   dlAddrHeaders + dlHeadersLen + 1;
const long dlDateIndexLen=   1000;
const long dlFirstLogEntry=  dlDateIndexLen + dlDateIndexLen + 1;
const long dlBuffLen = 256;
const char dlEmpty[] = {0,0,0,0,0,0,0};
const long dlMaxLogSize = dlChipMaxAddr - dlFirstLogEntry;
#define MIN(X,Y) X <? Y
#define MAX(X,Y) X >? Y

struct DLOG {
   DataLogChipType *chip;
   long nextWritePos;
   int  currYDay; // tm_yday from gmtime only log date date when date changes
   char *buff;
   int  buffLen;
};


// save the current nextWritePos to the chip so we hav it
// just in case of a reboot
void dlSaveNextWritePos(struct DLOG *wrk) {
   wrk->chip->writeStream(dlAddrNextWritePos,(char *) &wrk->nextWritePos,dlAddrNextWritePosSize);   // write next write postion
}

long dlReadNextWritePos(struct DLOG *wrk) {
  wrk->chip->readStream(dlAddrNextWritePos, (char *) &wrk->nextWritePos, dlAddrNextWritePosSize);
  return wrk->nextWritePos;
}

int dlReadCurrYDay(struct DLOG *wrk) {
  wrk->chip->readStream(dlAddrNextWritePos, (char *) &wrk->currYDay, dlAddrCurrYDaySize);
  return wrk->currYDay;  
}

void dlUpdateCurrDate(struct DLOG *wrk, int newYDay) {
  wrk->currYDay = newYDay;
  wrk->chip->writeStream(dlAddrCurrYDay,(char *) &wrk->currYDay, dlAddrCurrYDaySize);   // write next write postion
}

// New data log chip detected write data to initialize it.
long dlInitializeChip(struct DLOG *wrk) {
   wrk->nextWritePos =  dlFirstLogEntry;
   wrk->chip->writeStream(dlAddrInitByte, (char *) &dlInitByteValue,1);  // write init byte
   wrk->chip->writeStream(dlAddrNextWritePos,(char *) &wrk->nextWritePos,dlAddrNextWritePosSize);   // write next write postion   
   
   memset(wrk->buff,0,wrk->buffLen);
   wrk->chip->writeStream(dlAddrHeaders,wrk->buff,MIN(wrk->buffLen,dlHeadersLen));   // nulls over the header region
   wrk->chip->writeStream(dlDateIndex,wrk->buff,MIN(wrk->buffLen,dlDateIndexLen));   // nulls over the header region
   
   wrk->chip->writeStream(dlDateIndex,wrk->buff,MIN(wrk->buffLen,dlDateIndexLen));   // nulls over the header region
   wrk->currYDay = -99;
   return wrk->nextWritePos;
}


/* read a initialization byte from chip.  If the byte
doesn't contain the expected value then write one
and assume that we are starting our log ad the beginning */
long dlCheckChipInit(struct DLOG *wrk){
  wrk->buff[0] = 0; 
  wrk->chip->readStream(dlInitByteValue, wrk->buff, 1);
  if (wrk->buff[0] != dlInitByteValue) 
    return dlInitializeChip(wrk);      
  else {
      dlReadCurrYDay(wrk);
      return dlReadNextWritePos(wrk);
    }
}


// make and instance of our dlog structure fill it in
// in and load any current data such as next write postion
// already loaded in the chip. 
struct DLOG *dlMake(DataLogChipType *dataLogMem, char *buff, short buffLen) {  
  struct DLOG *tout = (struct DLOG *) malloc(sizeof(struct DLOG));
  tout->chip  = dataLogMem;
  tout->nextWritePos = dlFirstLogEntry;
  tout->buff = buff;
  tout->buffLen = buffLen;
  dlCheckChipInit(tout);
  return tout;
}

// writes log stream entry to chip and updates the next write 
// position. Also adds a null terminator to data on chip
// log entries should not contain null characters because we
// eventually plant to delay flush of nextWritePos and use
// scan forware to find the end when a crash occurs. 
// returns -2 if the write request would go beyond chip size.
long dlWrite(struct DLOG *wrk, char *aStr) {
  int slen = strlen(aStr);
  if ((wrk->nextWritePos + slen) >= dlChipMaxAddr) {
      return dlChipFullErr;
      }
  wrk->chip->writeStream(wrk->nextWritePos, aStr,slen); 
  wrk->nextWritePos += slen;
  wrk->chip->writeStream(wrk->nextWritePos, (char *)dlEmpty, 1);  // add terminating null 
  
  dlSaveNextWritePos(wrk); // WARNING THIS IS THE LINE THAT WILL KILL EPROM CHIPS 
                           // with over-write fatigue.
  // TODO: add err check read first and last bytes.
  // compare to what was written.
  return wrk->nextWritePos;
}

long dlLog(struct DLOG *wrk, char *recType, char *str) {
  time_t seconds = time(NULL);
  tm *ptm = gmtime ( &seconds );
  if (ptm->tm_yday != wrk->currYDay) {
     dlUpdateCurrDate(wrk, ptm->tm_yday);
     sprintf(wrk->buff,"DATE\t000000\n\000");
     dlWrite(wrk, wrk->buff);
  }
  memset(wrk->buff,12,0);
  sprintf(wrk->buff,"%s\t%2d%2d%2d\t", recType, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  dlWrite(wrk, wrk->buff);   
  return write(wrk, str);
}

// read a block of bytes from log starting at offset
// for len bytes placed in buffer.  If offset is > 
// log size then return dlAddressTooLarge if len would
// be greate than log size only return that available.
long dlRead(struct DLOG *wrk, char *buff, long offset, int len) {
   long addr = dlFirstLogEntry + offset;
   if ((addr + len) >= dlChipMaxAddr)
      return dlAddressTooLarge;        
   wrk->chip->readStream(offset, buff, len);       
   return 1;
}

long dlReadSend(struct DLOG *wrk, Serial *dest, long offset, long len) {
   long addr = dlFirstLogEntry + offset;
   long maxAddr = MIN(addr + len, dlChipMaxAddr); // no overflow past end of chip
   maxAddr = MIN(maxAddr, wrk->nextWritePos); // no overlow pas end of log
   int chunkSize = dlBuffLen -1;
   if (addr >= maxAddr)  
     return dlAddressTooLarge;
   long endAdd = MIN(addr + len, maxAddr);
   
   do {
      memset(wrk->buff, chunkSize, 0);
      if (addr + chunkSize > endAdd)
        chunkSize = endAdd - addr;
      wrk->chip->readStream(addr, wrk->buff, chunkSize);   
      dest->printf("%s", wrk->buff); 
      addr += chunkSize;      
   } while (addr < endAdd);       
   return -1;
 }  
 
    
        //if (wroteValue == 0) {
        //  memset(buff,0,60);
        //  strcpy(buff, "This is a Test of writting    ");
        //  dataLogMem.writeStream(read_addr,buff,35);   
        //  wroteValue = 1;

#endif