Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Sat Jun 30 02:03:51 2012 +0000
Revision:
0:664899e0b988
Child:
1:e392595b4b76
first version. SD card writing and data readout works. communications not tested.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #include "SnSDUtils.h"
uci1 0:664899e0b988 2
uci1 0:664899e0b988 3 #include "mbed.h"
uci1 0:664899e0b988 4 #include "SDFileSystem.h"
uci1 0:664899e0b988 5 #include <string.h>
uci1 0:664899e0b988 6
uci1 0:664899e0b988 7 #include "SnConfigFrame.h"
uci1 0:664899e0b988 8 #include "SnEventFrame.h"
uci1 0:664899e0b988 9
uci1 0:664899e0b988 10
uci1 0:664899e0b988 11 const char* SnSDUtils::kSDsubDir = "/sd";
uci1 0:664899e0b988 12 char SnSDUtils::fgCurFileName[kFNBufSize];
uci1 0:664899e0b988 13 const uint8_t SnSDUtils::kIOvers = 1;
uci1 0:664899e0b988 14
uci1 0:664899e0b988 15 const char* SnSDUtils::GetOutFileName(const uint64_t macadr,
uci1 0:664899e0b988 16 const uint32_t run,
uci1 0:664899e0b988 17 const uint16_t seq) {
uci1 0:664899e0b988 18 // returns the formatted file name, or NULL if the directory is too long
uci1 0:664899e0b988 19 // and the full name cannot fit in the buffer
uci1 0:664899e0b988 20 // NOTE: this fcn uses a static buffer, and so should not be called
uci1 0:664899e0b988 21 // multiple times in the same line (that includes the calling of functions
uci1 0:664899e0b988 22 // that call this function!)
uci1 0:664899e0b988 23 //
uci1 0:664899e0b988 24 // filename = SnEvtsM[6-byte hex mac adr]r[6-digit run num]s[5-digit seq num].dat
uci1 0:664899e0b988 25 // 35 chars 7 + 12 +1+ 5 +1+ 5 + 4
uci1 0:664899e0b988 26
uci1 0:664899e0b988 27 if (strlen(kSDsubDir)<(kFNBufSize-37)) {
uci1 0:664899e0b988 28 static char tbuf[kFNBufSize];
uci1 0:664899e0b988 29 memset(tbuf, 0, sizeof(char)*kFNBufSize);
uci1 0:664899e0b988 30 // if file name format changes, GetSeqNum must be changed too
uci1 0:664899e0b988 31 sprintf(tbuf, "%s/SnEvtsM%012llXr%05ds%05d.dat",
uci1 0:664899e0b988 32 kSDsubDir,
uci1 0:664899e0b988 33 macadr>>16, // 64 -> 48 bits
uci1 0:664899e0b988 34 run, seq);
uci1 0:664899e0b988 35 return tbuf;
uci1 0:664899e0b988 36 } else {
uci1 0:664899e0b988 37 return NULL;
uci1 0:664899e0b988 38 }
uci1 0:664899e0b988 39 }
uci1 0:664899e0b988 40
uci1 0:664899e0b988 41 uint16_t SnSDUtils::GetSeqNum(const uint64_t macadr,
uci1 0:664899e0b988 42 const uint32_t run) {
uci1 0:664899e0b988 43 // count the files having expected filename format
uci1 0:664899e0b988 44
uci1 0:664899e0b988 45 const char* fn = GetOutFileName(macadr, run, 0)
uci1 0:664899e0b988 46 + strlen(kSDsubDir) + 1; // take out dir and '/'s
uci1 0:664899e0b988 47
uci1 0:664899e0b988 48 DIR* d;
uci1 0:664899e0b988 49 struct dirent* dent;
uci1 0:664899e0b988 50
uci1 0:664899e0b988 51 uint16_t seq=0;
uci1 0:664899e0b988 52 if ( (d = opendir( kSDsubDir ))!=NULL ) {
uci1 0:664899e0b988 53 // don't compare seq#. don't use num of chars in case seq is >999
uci1 0:664899e0b988 54 const uint32_t ncomp = strrchr(fn, 's') - fn;
uci1 0:664899e0b988 55 while ( (dent = readdir(d))!=NULL ) {
uci1 0:664899e0b988 56 if (strncmp(dent->d_name, fn, ncomp)==0) {
uci1 0:664899e0b988 57 seq++;
uci1 0:664899e0b988 58 }
uci1 0:664899e0b988 59 }
uci1 0:664899e0b988 60 closedir(d);
uci1 0:664899e0b988 61 }
uci1 0:664899e0b988 62
uci1 0:664899e0b988 63 return seq;
uci1 0:664899e0b988 64 }
uci1 0:664899e0b988 65
uci1 0:664899e0b988 66 FILE* SnSDUtils::OpenExistingFile(const char* name) {
uci1 0:664899e0b988 67 FILE* f = 0;
uci1 0:664899e0b988 68 if (name!=NULL) {
uci1 0:664899e0b988 69 f = fopen(name, "wb");
uci1 0:664899e0b988 70 }
uci1 0:664899e0b988 71 return f;
uci1 0:664899e0b988 72 }
uci1 0:664899e0b988 73
uci1 0:664899e0b988 74 FILE* SnSDUtils::OpenNewOutputFile(const uint64_t macadr,
uci1 0:664899e0b988 75 const uint32_t run) {
uci1 0:664899e0b988 76 // opens a new file in the specified directory and writes this
uci1 0:664899e0b988 77 // this mbed's mac address as the first sizeof(uint64_t) bytes (i.e. 4 bytes)
uci1 0:664899e0b988 78 //
uci1 0:664899e0b988 79 const uint16_t seq = GetSeqNum(macadr, run);
uci1 0:664899e0b988 80 memset(fgCurFileName, 0, sizeof(char)*kFNBufSize);
uci1 0:664899e0b988 81 strcpy(fgCurFileName,GetOutFileName(macadr, run, seq));
uci1 0:664899e0b988 82 //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,seq);
uci1 0:664899e0b988 83 FILE* f = 0;
uci1 0:664899e0b988 84 if (fgCurFileName!=NULL) {
uci1 0:664899e0b988 85 f = fopen(fgCurFileName, "wb");
uci1 0:664899e0b988 86 }
uci1 0:664899e0b988 87 return f;
uci1 0:664899e0b988 88 }
uci1 0:664899e0b988 89
uci1 0:664899e0b988 90 bool SnSDUtils::WriteFileHeader(FILE* f, const uint64_t macadr) {
uci1 0:664899e0b988 91 // MUST INCREMENT kIOvers if these writes are altered
uci1 0:664899e0b988 92 fwrite(&kIOvers, sizeof(uint8_t), 1, f);
uci1 0:664899e0b988 93 fwrite(&macadr, sizeof(uint64_t), 1, f);
uci1 0:664899e0b988 94 return ferror(f);
uci1 0:664899e0b988 95 }
uci1 0:664899e0b988 96
uci1 0:664899e0b988 97 bool SnSDUtils::WriteEventTo(FILE* efile, char* const evtBuf,
uci1 0:664899e0b988 98 const SnEventFrame& evt,
uci1 0:664899e0b988 99 const SnConfigFrame& conf) {
uci1 0:664899e0b988 100 // write event to SD card
uci1 0:664899e0b988 101
uci1 0:664899e0b988 102 uint8_t sLoseLSB=0, sLoseMSB=0;
uci1 0:664899e0b988 103 uint16_t sWvBase=0;
uci1 0:664899e0b988 104 conf.GetPackParsFor(SnConfigFrame::kSDcard, sLoseLSB, sLoseMSB, sWvBase);
uci1 0:664899e0b988 105 const bool ret = evt.WriteTo(efile, evtBuf, sLoseLSB, sLoseMSB, sWvBase);
uci1 0:664899e0b988 106 fflush(efile);
uci1 0:664899e0b988 107 return ret;
uci1 0:664899e0b988 108 }
uci1 0:664899e0b988 109
uci1 0:664899e0b988 110 bool SnSDUtils::WriteConfig(FILE* efile,
uci1 0:664899e0b988 111 const SnConfigFrame& conf) {
uci1 0:664899e0b988 112 conf.WriteTo(efile);
uci1 0:664899e0b988 113 return true;
uci1 0:664899e0b988 114 }
uci1 0:664899e0b988 115
uci1 0:664899e0b988 116 void SnSDUtils::DeleteFile(FILE*& f, const char* fname) {
uci1 0:664899e0b988 117 fclose(f);
uci1 0:664899e0b988 118 f=0;
uci1 0:664899e0b988 119 remove(fname);
uci1 0:664899e0b988 120 }
uci1 0:664899e0b988 121
uci1 0:664899e0b988 122 SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm,
uci1 0:664899e0b988 123 const bool doDelete) {
uci1 0:664899e0b988 124
uci1 0:664899e0b988 125 DIR* d;
uci1 0:664899e0b988 126 struct dirent* dent;
uci1 0:664899e0b988 127
uci1 0:664899e0b988 128 SnCommWin::ECommWinResult rs = SnCommWin::kUndefFail;
uci1 0:664899e0b988 129
uci1 0:664899e0b988 130 if ( (d = opendir( kSDsubDir ))!=NULL ) {
uci1 0:664899e0b988 131 FILE* f;
uci1 0:664899e0b988 132 while ( (dent = readdir(d))!=NULL ) {
uci1 0:664899e0b988 133 if (strncmp(dent->d_name, "SnEvts", 6)==0) {
uci1 0:664899e0b988 134 f = SnSDUtils::OpenExistingFile(dent->d_name);
uci1 0:664899e0b988 135 rs = comm->SendData(f);
uci1 0:664899e0b988 136 if (rs<SnCommWin::kAllFails) {
uci1 0:664899e0b988 137 break;
uci1 0:664899e0b988 138 } else if (doDelete) {
uci1 0:664899e0b988 139 DeleteFile(f, dent->d_name);
uci1 0:664899e0b988 140 }
uci1 0:664899e0b988 141 }
uci1 0:664899e0b988 142 }
uci1 0:664899e0b988 143 closedir(d);
uci1 0:664899e0b988 144 }
uci1 0:664899e0b988 145
uci1 0:664899e0b988 146 return rs;
uci1 0:664899e0b988 147 }
uci1 0:664899e0b988 148