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.

Committer:
joeata2wh
Date:
Thu Mar 31 16:18:02 2016 +0000
Revision:
5:286459acee56
Parent:
4:fa5bbe31a039
Child:
6:5368ef5fced0
both read and write log seems to be working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeata2wh 1:b2e12bf6b4aa 1 /* DataLog.h - Data logger for logging enviornmental
joeata2wh 1:b2e12bf6b4aa 2 data to EPROM, SRAM or FRAM chip without a file system
joeata2wh 3:5550814cc21c 3 inteface. Supports multiple and evolving records types
joeata2wh 3:5550814cc21c 4 and trnsport back to serial
joeata2wh 3:5550814cc21c 5
joeata2wh 3:5550814cc21c 6 See data-log-text.txt for detailed design and layout notes
joeata2wh 3:5550814cc21c 7 See: xj-data-log-test-and-example.c for example use.
joeata2wh 4:fa5bbe31a039 8 https://developer.mbed.org/users/joeata2wh/code/xj-data-log-test-and-example/wiki/Homepage
joeata2wh 4:fa5bbe31a039 9
joeata2wh 1:b2e12bf6b4aa 10 By Joseph Ellsworth CTO of A2WH
joeata2wh 1:b2e12bf6b4aa 11 Take a look at A2WH.com Producing Water from Air using Solar Energy
joeata2wh 1:b2e12bf6b4aa 12 March-2016 License: https://developer.mbed.org/handbook/MIT-Licence
joeata2wh 1:b2e12bf6b4aa 13 Please contact us http://a2wh.com for help with custom design projects.
joeata2wh 3:5550814cc21c 14
joeata2wh 1:b2e12bf6b4aa 15 Before you complain about not using proper C++ classes, I intend to
joeata2wh 1:b2e12bf6b4aa 16 port the entire A2WH project to PSoc where I may or may not be able to
joeata2wh 1:b2e12bf6b4aa 17 use the full set of C++ features. I am using techniques that should be
joeata2wh 1:b2e12bf6b4aa 18 easier to port to a ANSI C enviornment. You may think this is a wierd
joeata2wh 1:b2e12bf6b4aa 19 decision but the PSoC enviornments gives me transparant support for
joeata2wh 1:b2e12bf6b4aa 20 differential ADC and very low power analog comparators that remain active
joeata2wh 1:b2e12bf6b4aa 21 when CPU is in deep sleep and which can wake the CPU up from deep sleep
joeata2wh 1:b2e12bf6b4aa 22 which makes very low power designs easier. mBed is still weak for this
joeata2wh 1:b2e12bf6b4aa 23 kind of advanced peripherial support.
joeata2wh 1:b2e12bf6b4aa 24
joeata2wh 1:b2e12bf6b4aa 25 */
joeata2wh 1:b2e12bf6b4aa 26 #ifndef DataLog_H
joeata2wh 1:b2e12bf6b4aa 27 #define DataLog_H
joeata2wh 1:b2e12bf6b4aa 28 #include "mbed.h"
joeata2wh 3:5550814cc21c 29
joeata2wh 1:b2e12bf6b4aa 30 #include "W25Q80BV.h" // Note: We are using this library because we started with it but
joeata2wh 1:b2e12bf6b4aa 31 // the actual chip we are using is a 2 MBit FRAM MB85RS2MTPH-G-JNE
joeata2wh 1:b2e12bf6b4aa 32 // also tested with SRAM 23LCV1024-I/P Prefer SRAM or FRAM because
joeata2wh 1:b2e12bf6b4aa 33 // we made a simplifying assumption that we could write next log
joeata2wh 1:b2e12bf6b4aa 34 // address to the same position over and over. Without wear leveling
joeata2wh 1:b2e12bf6b4aa 35 // this could rapidly wear out a e-prom chip. Could have written
joeata2wh 1:b2e12bf6b4aa 36 // this to the clock chip which uses SRAM but FRAM has such high write
joeata2wh 1:b2e12bf6b4aa 37 // durability that we don't have to.
joeata2wh 1:b2e12bf6b4aa 38
joeata2wh 1:b2e12bf6b4aa 39 #define DataLogChipType W25Q80BV
joeata2wh 1:b2e12bf6b4aa 40
joeata2wh 1:b2e12bf6b4aa 41 // TODO: Add the SDIO link here to copy data from dlog chip to
joeata2wh 1:b2e12bf6b4aa 42 // SD card when available.
joeata2wh 1:b2e12bf6b4aa 43
joeata2wh 2:8d06af2f1fcc 44
joeata2wh 1:b2e12bf6b4aa 45 const long dlChipMaxAddr = 250000; // 2 mBit 2000000 / 8
joeata2wh 1:b2e12bf6b4aa 46 #define dlChipFullErr -2
joeata2wh 1:b2e12bf6b4aa 47 #define dlAddressTooLarge -3
joeata2wh 1:b2e12bf6b4aa 48 #define dlMaxOperationSize -4;
joeata2wh 1:b2e12bf6b4aa 49 // Chip DLog Chip Memory Layout
joeata2wh 1:b2e12bf6b4aa 50 const long dlAddrInitByte = 1500; // data before this is assumed to be used for system config variables
joeata2wh 4:fa5bbe31a039 51 const char dlInitByteValue = 214;
joeata2wh 1:b2e12bf6b4aa 52 const int dlMaxReadWriteSize = 32000; // limit imposed by streaming interface for the chip
joeata2wh 5:286459acee56 53 const long dlAddrNextWritePos = dlAddrInitByte + 1;
joeata2wh 4:fa5bbe31a039 54 const long dlNextWritePosSize = 4;
joeata2wh 4:fa5bbe31a039 55 const long dlAddrCurrYDay = dlAddrNextWritePos + dlNextWritePosSize;
joeata2wh 4:fa5bbe31a039 56 const long dlCurrYDaySize = 2;
joeata2wh 4:fa5bbe31a039 57 const long dlAddrHeaders = dlAddrCurrYDay + dlCurrYDaySize ;
joeata2wh 1:b2e12bf6b4aa 58 const long dlHeadersLen = 256;
joeata2wh 5:286459acee56 59 const long dlAddrDateIndex = dlAddrHeaders + dlHeadersLen + 1;
joeata2wh 5:286459acee56 60 const long dlDateIndexLen = 1000;
joeata2wh 5:286459acee56 61 const long dlAddrFirstLogEntry= dlAddrDateIndex + dlDateIndexLen + 1;
joeata2wh 1:b2e12bf6b4aa 62 const char dlEmpty[] = {0,0,0,0,0,0,0};
joeata2wh 5:286459acee56 63 const long dlMaxLogSize = dlChipMaxAddr - dlAddrFirstLogEntry;
joeata2wh 1:b2e12bf6b4aa 64 #define MIN(X,Y) X <? Y
joeata2wh 1:b2e12bf6b4aa 65 #define MAX(X,Y) X >? Y
joeata2wh 1:b2e12bf6b4aa 66
joeata2wh 1:b2e12bf6b4aa 67 struct DLOG {
joeata2wh 1:b2e12bf6b4aa 68 DataLogChipType *chip;
joeata2wh 1:b2e12bf6b4aa 69 long nextWritePos;
joeata2wh 1:b2e12bf6b4aa 70 int currYDay; // tm_yday from gmtime only log date date when date changes
joeata2wh 1:b2e12bf6b4aa 71 char *buff;
joeata2wh 1:b2e12bf6b4aa 72 int buffLen;
joeata2wh 1:b2e12bf6b4aa 73 };
joeata2wh 1:b2e12bf6b4aa 74
joeata2wh 5:286459acee56 75 long dlLen(struct DLOG *wrk) {
joeata2wh 5:286459acee56 76 return wrk->nextWritePos - dlAddrFirstLogEntry;
joeata2wh 5:286459acee56 77 }
joeata2wh 1:b2e12bf6b4aa 78 // save the current nextWritePos to the chip so we hav it
joeata2wh 1:b2e12bf6b4aa 79 // just in case of a reboot
joeata2wh 1:b2e12bf6b4aa 80 void dlSaveNextWritePos(struct DLOG *wrk) {
joeata2wh 4:fa5bbe31a039 81 wrk->chip->writeStream(dlAddrNextWritePos,(char *) &wrk->nextWritePos,dlNextWritePosSize); // write next write postion
joeata2wh 1:b2e12bf6b4aa 82 }
joeata2wh 1:b2e12bf6b4aa 83
joeata2wh 1:b2e12bf6b4aa 84 long dlReadNextWritePos(struct DLOG *wrk) {
joeata2wh 4:fa5bbe31a039 85 wrk->chip->readStream(dlAddrNextWritePos, (char *) &wrk->nextWritePos, dlNextWritePosSize);
joeata2wh 4:fa5bbe31a039 86 //printf("dlReadNextWritePos wrk->nextWritePos=%ld\n\r",wrk->nextWritePos);
joeata2wh 1:b2e12bf6b4aa 87 return wrk->nextWritePos;
joeata2wh 1:b2e12bf6b4aa 88 }
joeata2wh 1:b2e12bf6b4aa 89
joeata2wh 1:b2e12bf6b4aa 90 int dlReadCurrYDay(struct DLOG *wrk) {
joeata2wh 5:286459acee56 91 wrk->chip->readStream(dlAddrCurrYDay, (char *) &wrk->currYDay, dlCurrYDaySize);
joeata2wh 4:fa5bbe31a039 92 //printf("dlReadCurrYDay wrk->currYDay=%d\r\n", wrk->currYDay);
joeata2wh 1:b2e12bf6b4aa 93 return wrk->currYDay;
joeata2wh 1:b2e12bf6b4aa 94 }
joeata2wh 1:b2e12bf6b4aa 95
joeata2wh 1:b2e12bf6b4aa 96 void dlUpdateCurrDate(struct DLOG *wrk, int newYDay) {
joeata2wh 1:b2e12bf6b4aa 97 wrk->currYDay = newYDay;
joeata2wh 4:fa5bbe31a039 98 wrk->chip->writeStream(dlAddrCurrYDay,(char *) &wrk->currYDay, dlCurrYDaySize); // write next write postion
joeata2wh 4:fa5bbe31a039 99 }
joeata2wh 4:fa5bbe31a039 100
joeata2wh 4:fa5bbe31a039 101 // Erase Log from Chip but do not touch
joeata2wh 4:fa5bbe31a039 102 // data on chip outside of log space.
joeata2wh 4:fa5bbe31a039 103 void dlEraseLog(struct DLOG *wrk) {
joeata2wh 4:fa5bbe31a039 104 //printf("dlEraseLogStart\r\n");
joeata2wh 5:286459acee56 105 wrk->nextWritePos = dlAddrFirstLogEntry;
joeata2wh 4:fa5bbe31a039 106 wrk->currYDay = -99;
joeata2wh 5:286459acee56 107 memset(wrk->buff,0,wrk->buffLen);
joeata2wh 4:fa5bbe31a039 108 wrk->chip->writeStream(dlAddrInitByte, (char *) &dlInitByteValue,1); // write init byte
joeata2wh 5:286459acee56 109 wrk->chip->writeStream(dlAddrNextWritePos,(char *) &wrk->nextWritePos,dlNextWritePosSize); // reset the next write postion
joeata2wh 5:286459acee56 110 wrk->chip->writeStream(dlAddrCurrYDay, (char *) &wrk->currYDay, dlCurrYDaySize); // Reset the currYDay
joeata2wh 4:fa5bbe31a039 111 wrk->chip->writeStream(dlAddrHeaders,wrk->buff,MIN(wrk->buffLen,dlHeadersLen)); // nulls over the header region
joeata2wh 5:286459acee56 112 wrk->chip->writeStream(dlAddrDateIndex,wrk->buff,MIN(wrk->buffLen,dlDateIndexLen)); // nulls over the header region
joeata2wh 4:fa5bbe31a039 113 wrk->chip->writeStream(wrk->nextWritePos,wrk->buff,wrk->buffLen); // nulls first of the log
joeata2wh 4:fa5bbe31a039 114 //printf("dlEraseLogDone\r\n");
joeata2wh 1:b2e12bf6b4aa 115 }
joeata2wh 1:b2e12bf6b4aa 116
joeata2wh 1:b2e12bf6b4aa 117 // New data log chip detected write data to initialize it.
joeata2wh 1:b2e12bf6b4aa 118 long dlInitializeChip(struct DLOG *wrk) {
joeata2wh 4:fa5bbe31a039 119 dlEraseLog(wrk);
joeata2wh 1:b2e12bf6b4aa 120 return wrk->nextWritePos;
joeata2wh 1:b2e12bf6b4aa 121 }
joeata2wh 1:b2e12bf6b4aa 122
joeata2wh 1:b2e12bf6b4aa 123
joeata2wh 4:fa5bbe31a039 124
joeata2wh 1:b2e12bf6b4aa 125 /* read a initialization byte from chip. If the byte
joeata2wh 1:b2e12bf6b4aa 126 doesn't contain the expected value then write one
joeata2wh 1:b2e12bf6b4aa 127 and assume that we are starting our log ad the beginning */
joeata2wh 1:b2e12bf6b4aa 128 long dlCheckChipInit(struct DLOG *wrk){
joeata2wh 1:b2e12bf6b4aa 129 wrk->buff[0] = 0;
joeata2wh 4:fa5bbe31a039 130 wrk->chip->readStream(dlAddrInitByte, wrk->buff, 1);
joeata2wh 1:b2e12bf6b4aa 131 if (wrk->buff[0] != dlInitByteValue)
joeata2wh 4:fa5bbe31a039 132 {
joeata2wh 4:fa5bbe31a039 133 //printf("Found empty chip running init");
joeata2wh 1:b2e12bf6b4aa 134 return dlInitializeChip(wrk);
joeata2wh 4:fa5bbe31a039 135 }
joeata2wh 1:b2e12bf6b4aa 136 else {
joeata2wh 4:fa5bbe31a039 137 //printf("Found existing log\r\n");
joeata2wh 1:b2e12bf6b4aa 138 dlReadCurrYDay(wrk);
joeata2wh 1:b2e12bf6b4aa 139 return dlReadNextWritePos(wrk);
joeata2wh 1:b2e12bf6b4aa 140 }
joeata2wh 1:b2e12bf6b4aa 141 }
joeata2wh 1:b2e12bf6b4aa 142
joeata2wh 1:b2e12bf6b4aa 143
joeata2wh 1:b2e12bf6b4aa 144 // make and instance of our dlog structure fill it in
joeata2wh 1:b2e12bf6b4aa 145 // in and load any current data such as next write postion
joeata2wh 1:b2e12bf6b4aa 146 // already loaded in the chip.
joeata2wh 1:b2e12bf6b4aa 147 struct DLOG *dlMake(DataLogChipType *dataLogMem, char *buff, short buffLen) {
joeata2wh 1:b2e12bf6b4aa 148 struct DLOG *tout = (struct DLOG *) malloc(sizeof(struct DLOG));
joeata2wh 1:b2e12bf6b4aa 149 tout->chip = dataLogMem;
joeata2wh 5:286459acee56 150 tout->nextWritePos = dlAddrFirstLogEntry;
joeata2wh 1:b2e12bf6b4aa 151 tout->buff = buff;
joeata2wh 1:b2e12bf6b4aa 152 tout->buffLen = buffLen;
joeata2wh 1:b2e12bf6b4aa 153 dlCheckChipInit(tout);
joeata2wh 1:b2e12bf6b4aa 154 return tout;
joeata2wh 1:b2e12bf6b4aa 155 }
joeata2wh 1:b2e12bf6b4aa 156
joeata2wh 4:fa5bbe31a039 157
joeata2wh 1:b2e12bf6b4aa 158 // writes log stream entry to chip and updates the next write
joeata2wh 1:b2e12bf6b4aa 159 // position. Also adds a null terminator to data on chip
joeata2wh 1:b2e12bf6b4aa 160 // log entries should not contain null characters because we
joeata2wh 1:b2e12bf6b4aa 161 // eventually plant to delay flush of nextWritePos and use
joeata2wh 1:b2e12bf6b4aa 162 // scan forware to find the end when a crash occurs.
joeata2wh 1:b2e12bf6b4aa 163 // returns -2 if the write request would go beyond chip size.
joeata2wh 3:5550814cc21c 164 long dlWrite(struct DLOG *wrk, char *aStr) {
joeata2wh 1:b2e12bf6b4aa 165 int slen = strlen(aStr);
joeata2wh 1:b2e12bf6b4aa 166 if ((wrk->nextWritePos + slen) >= dlChipMaxAddr) {
joeata2wh 1:b2e12bf6b4aa 167 return dlChipFullErr;
joeata2wh 1:b2e12bf6b4aa 168 }
joeata2wh 4:fa5bbe31a039 169 wrk->chip->writeStream(wrk->nextWritePos, aStr,slen);
joeata2wh 1:b2e12bf6b4aa 170 wrk->nextWritePos += slen;
joeata2wh 1:b2e12bf6b4aa 171 dlSaveNextWritePos(wrk); // WARNING THIS IS THE LINE THAT WILL KILL EPROM CHIPS
joeata2wh 1:b2e12bf6b4aa 172 // with over-write fatigue.
joeata2wh 1:b2e12bf6b4aa 173 // TODO: add err check read first and last bytes.
joeata2wh 1:b2e12bf6b4aa 174 // compare to what was written.
joeata2wh 1:b2e12bf6b4aa 175 return wrk->nextWritePos;
joeata2wh 1:b2e12bf6b4aa 176 }
joeata2wh 4:fa5bbe31a039 177 char dlLFEmpty[] = "\n\000";
joeata2wh 4:fa5bbe31a039 178 //record log file entry with time stamp and header
joeata2wh 3:5550814cc21c 179 long dlLog(struct DLOG *wrk, char *recType, char *str) {
joeata2wh 4:fa5bbe31a039 180
joeata2wh 4:fa5bbe31a039 181 time_t seconds;
joeata2wh 4:fa5bbe31a039 182 time(&seconds);
joeata2wh 4:fa5bbe31a039 183 //printf("dlLog ctime(&seconds)=%s\r\n", ctime(&seconds));
joeata2wh 4:fa5bbe31a039 184 struct tm *ptm = localtime( &seconds );
joeata2wh 5:286459acee56 185 //printf("dlLog asctime=%s\r\n", asctime(ptm));
joeata2wh 5:286459acee56 186 //printf("dlLog ptm->tm_yday=%d\r\n", ptm->tm_yday );
joeata2wh 1:b2e12bf6b4aa 187 if (ptm->tm_yday != wrk->currYDay) {
joeata2wh 4:fa5bbe31a039 188 int year = 1900 + ptm->tm_year;
joeata2wh 4:fa5bbe31a039 189 int month= ptm->tm_mon + 1;
joeata2wh 4:fa5bbe31a039 190
joeata2wh 1:b2e12bf6b4aa 191 dlUpdateCurrDate(wrk, ptm->tm_yday);
joeata2wh 4:fa5bbe31a039 192 sprintf(wrk->buff,"\n00:00:00 DATE\t%04d-%02d-%02d", year, month, ptm->tm_mday);
joeata2wh 3:5550814cc21c 193 dlWrite(wrk, wrk->buff);
joeata2wh 5:286459acee56 194 //printf("update date dateRec=%s\r\n", wrk->buff);
joeata2wh 1:b2e12bf6b4aa 195 }
joeata2wh 4:fa5bbe31a039 196
joeata2wh 4:fa5bbe31a039 197 memset(wrk->buff,wrk->buffLen,0);
joeata2wh 4:fa5bbe31a039 198 sprintf(wrk->buff,"\n%02d:%02d:%02d %s\t", ptm->tm_hour, ptm->tm_min, ptm->tm_sec, recType);
joeata2wh 4:fa5bbe31a039 199 dlWrite(wrk, wrk->buff);
joeata2wh 4:fa5bbe31a039 200 //dlWrite(wrk, dlLFEmpty); // add terminating lineFeed
joeata2wh 5:286459acee56 201 //printf ("recHead=%s\r\n", wrk->buff);
joeata2wh 4:fa5bbe31a039 202 return dlWrite(wrk, str);
joeata2wh 1:b2e12bf6b4aa 203 }
joeata2wh 1:b2e12bf6b4aa 204
joeata2wh 1:b2e12bf6b4aa 205 // read a block of bytes from log starting at offset
joeata2wh 1:b2e12bf6b4aa 206 // for len bytes placed in buffer. If offset is >
joeata2wh 1:b2e12bf6b4aa 207 // log size then return dlAddressTooLarge if len would
joeata2wh 1:b2e12bf6b4aa 208 // be greate than log size only return that available.
joeata2wh 3:5550814cc21c 209 long dlRead(struct DLOG *wrk, char *buff, long offset, int len) {
joeata2wh 5:286459acee56 210 long addr = dlAddrFirstLogEntry + offset;
joeata2wh 1:b2e12bf6b4aa 211 if ((addr + len) >= dlChipMaxAddr)
joeata2wh 1:b2e12bf6b4aa 212 return dlAddressTooLarge;
joeata2wh 1:b2e12bf6b4aa 213 wrk->chip->readStream(offset, buff, len);
joeata2wh 1:b2e12bf6b4aa 214 return 1;
joeata2wh 1:b2e12bf6b4aa 215 }
joeata2wh 1:b2e12bf6b4aa 216
joeata2wh 3:5550814cc21c 217 long dlReadSend(struct DLOG *wrk, Serial *dest, long offset, long len) {
joeata2wh 5:286459acee56 218 long addr = dlAddrFirstLogEntry + offset;
joeata2wh 1:b2e12bf6b4aa 219 long maxAddr = MIN(addr + len, dlChipMaxAddr); // no overflow past end of chip
joeata2wh 1:b2e12bf6b4aa 220 maxAddr = MIN(maxAddr, wrk->nextWritePos); // no overlow pas end of log
joeata2wh 4:fa5bbe31a039 221 int chunkSize = wrk->buffLen -1;
joeata2wh 5:286459acee56 222 long endAdd = MIN(addr + len, maxAddr);
joeata2wh 5:286459acee56 223 //printf("\r\ndlReadSend addr=%ld offset=%ld len=%ld maxAddr=%ld chunkSize=%d endAdd=%ld nextWritePos=%ld\r\n",
joeata2wh 5:286459acee56 224 // addr, offset, len, maxAddr, chunkSize, endAdd, wrk->nextWritePos);
joeata2wh 1:b2e12bf6b4aa 225 if (addr >= maxAddr)
joeata2wh 1:b2e12bf6b4aa 226 return dlAddressTooLarge;
joeata2wh 5:286459acee56 227 int chunkCnt=0;
joeata2wh 1:b2e12bf6b4aa 228 do {
joeata2wh 5:286459acee56 229 memset(wrk->buff, wrk->buffLen, 0);
joeata2wh 1:b2e12bf6b4aa 230 if (addr + chunkSize > endAdd)
joeata2wh 1:b2e12bf6b4aa 231 chunkSize = endAdd - addr;
joeata2wh 5:286459acee56 232 //printf("\r\n\tdlReadSend addr=%ld endAdd=%ld chunkSize=%ld chunkCnt=%d\r\n", addr, endAdd, chunkSize, chunkCnt);
joeata2wh 1:b2e12bf6b4aa 233 wrk->chip->readStream(addr, wrk->buff, chunkSize);
joeata2wh 5:286459acee56 234 wrk->buff[chunkSize] = 0;
joeata2wh 1:b2e12bf6b4aa 235 dest->printf("%s", wrk->buff);
joeata2wh 5:286459acee56 236 int tlen = strlen(wrk->buff);
joeata2wh 5:286459acee56 237 //printf("\r\n\tdlReadSend tlen=%d\r\n", tlen);
joeata2wh 1:b2e12bf6b4aa 238 addr += chunkSize;
joeata2wh 5:286459acee56 239 wait(0.05);
joeata2wh 5:286459acee56 240 chunkCnt++;
joeata2wh 1:b2e12bf6b4aa 241 } while (addr < endAdd);
joeata2wh 5:286459acee56 242 return 1;
joeata2wh 1:b2e12bf6b4aa 243 }
joeata2wh 1:b2e12bf6b4aa 244
joeata2wh 5:286459acee56 245 long dlReadSendAll(struct DLOG *wrk, Serial *dest) {
joeata2wh 5:286459acee56 246 return dlReadSend(wrk, dest, 0, wrk->nextWritePos);
joeata2wh 5:286459acee56 247 }
joeata2wh 5:286459acee56 248
joeata2wh 5:286459acee56 249 long dlReadSendLast(struct DLOG *wrk, Serial *dest, long numSend) {
joeata2wh 5:286459acee56 250 long logLen = dlLen(wrk);
joeata2wh 5:286459acee56 251 long calcLen = MIN(numSend, logLen);
joeata2wh 5:286459acee56 252 long startPos = logLen - calcLen;
joeata2wh 5:286459acee56 253 startPos = MAX(0, startPos);
joeata2wh 5:286459acee56 254 //printf("dlReadSendLast logLen=%ld startPos=%ld calcLen=%ld nextWritePos=%ld\r\n ", logLen, startPos,calcLen, wrk->nextWritePos);
joeata2wh 5:286459acee56 255 return dlReadSend(wrk, dest, startPos, calcLen);
joeata2wh 5:286459acee56 256 }
joeata2wh 1:b2e12bf6b4aa 257
joeata2wh 1:b2e12bf6b4aa 258 #endif