Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Fri Aug 31 02:09:09 2012 +0000
Revision:
15:f2569d8e4176
Parent:
12:d472f9811262
Child:
19:74155d652c37
Removed debug output between trigger and and dFPGA->MB request that corrupted data. Lots of work on SBD, but not quite working yet. Debug output off, but start/stop running notifications are on.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #ifndef SN_SnSDUtils
uci1 0:664899e0b988 2 #define SN_SnSDUtils
uci1 0:664899e0b988 3
uci1 0:664899e0b988 4 #include <stdio.h>
uci1 0:664899e0b988 5 #include <stdint.h>
uci1 0:664899e0b988 6
uci1 0:664899e0b988 7 #include "SnCommWin.h"
uci1 5:9cea89700c66 8 #include "SnBitUtils.h"
uci1 5:9cea89700c66 9 #include "SnPowerFrame.h"
uci1 8:95a325df1f6b 10 #include "SnHeaderFrame.h"
uci1 0:664899e0b988 11
uci1 0:664899e0b988 12 class SnEventFrame;
uci1 0:664899e0b988 13 class SnConfigFrame;
uci1 0:664899e0b988 14
uci1 0:664899e0b988 15 // a namespace-like class to handle the i/o with the SD card
uci1 0:664899e0b988 16
uci1 0:664899e0b988 17 class SnSDUtils {
uci1 0:664899e0b988 18 public:
uci1 1:e392595b4b76 19 static const char* const kSDsubDir;
uci1 1:e392595b4b76 20 static const uint8_t kFNBufSize=128;
uci1 1:e392595b4b76 21 static const uint8_t kIOvers; // file I/O version
uci1 5:9cea89700c66 22 static const uint32_t kMaxSizeOfFileHdr;
uci1 0:664899e0b988 23
uci1 0:664899e0b988 24 private:
uci1 0:664899e0b988 25
uci1 0:664899e0b988 26 static
uci1 3:24c5f0f50bf1 27 FILE* OpenSDFile(const char* name, const char* mode);
uci1 1:e392595b4b76 28
uci1 1:e392595b4b76 29 static
uci1 0:664899e0b988 30 uint16_t GetSeqNum(const uint64_t macadr,
uci1 0:664899e0b988 31 const uint32_t run);
uci1 0:664899e0b988 32
uci1 0:664899e0b988 33 static
uci1 0:664899e0b988 34 const char* GetOutFileName(const uint64_t macadr,
uci1 0:664899e0b988 35 const uint32_t run,
uci1 0:664899e0b988 36 const uint16_t seq);
uci1 0:664899e0b988 37
uci1 0:664899e0b988 38 static
uci1 0:664899e0b988 39 void DeleteFile(FILE*& f, const char* fname);
uci1 0:664899e0b988 40
uci1 12:d472f9811262 41 static char fgCurFileName[kFNBufSize];
uci1 12:d472f9811262 42 static FILE* fgCurFile;
uci1 12:d472f9811262 43 static uint16_t fgCurSeq;
uci1 0:664899e0b988 44
uci1 0:664899e0b988 45 public:
uci1 0:664899e0b988 46 SnSDUtils() {}
uci1 0:664899e0b988 47 virtual ~SnSDUtils() {}
uci1 0:664899e0b988 48
uci1 0:664899e0b988 49 static
uci1 10:3c93db1cfb12 50 int CloseOutputFile(FILE* f) {
uci1 12:d472f9811262 51 // TODO: set current file pointer to 0, IF f is current file
uci1 10:3c93db1cfb12 52 const int rt = (f!=0) ? fclose(f) : 0;
uci1 10:3c93db1cfb12 53 return rt;
uci1 10:3c93db1cfb12 54 }
uci1 0:664899e0b988 55
uci1 0:664899e0b988 56 static
uci1 0:664899e0b988 57 FILE* OpenNewOutputFile(const uint64_t macadr,
uci1 8:95a325df1f6b 58 const uint32_t run);
uci1 0:664899e0b988 59
uci1 0:664899e0b988 60 static
uci1 3:24c5f0f50bf1 61 FILE* OpenExistingFile(const char* name, const bool setcurrent);
uci1 0:664899e0b988 62
uci1 0:664899e0b988 63 static
uci1 0:664899e0b988 64 SnCommWin::ECommWinResult SendAllFiles(SnCommWin* comm,
uci1 3:24c5f0f50bf1 65 const uint32_t timeout,
uci1 3:24c5f0f50bf1 66 char* const buf,
uci1 6:6f002d202f59 67 const uint32_t bsize,
uci1 6:6f002d202f59 68 const SnConfigFrame& curConf,
uci1 8:95a325df1f6b 69 SnEventFrame& evt,
uci1 12:d472f9811262 70 SnPowerFrame& pow,
uci1 12:d472f9811262 71 const uint32_t handshakeTimeout);
uci1 0:664899e0b988 72
uci1 0:664899e0b988 73 static
uci1 0:664899e0b988 74 const char* GetCurFileName() { return fgCurFileName; }
uci1 0:664899e0b988 75
uci1 0:664899e0b988 76 static
uci1 1:e392595b4b76 77 FILE* GetCurFile() { return fgCurFile; }
uci1 1:e392595b4b76 78
uci1 1:e392595b4b76 79 static
uci1 12:d472f9811262 80 uint16_t GetCurSeqNum() { return fgCurSeq; }
uci1 10:3c93db1cfb12 81
uci1 10:3c93db1cfb12 82 static
uci1 10:3c93db1cfb12 83 uint16_t GetSeqNumFromFileName(const char* fn);
uci1 10:3c93db1cfb12 84
uci1 10:3c93db1cfb12 85 static
uci1 0:664899e0b988 86 bool WriteEventTo(FILE* efile, char* const evtBuf,
uci1 0:664899e0b988 87 const SnEventFrame& evt,
uci1 0:664899e0b988 88 const SnConfigFrame& conf);
uci1 0:664899e0b988 89
uci1 0:664899e0b988 90 static
uci1 0:664899e0b988 91 bool WriteConfig(FILE* efile,
uci1 0:664899e0b988 92 const SnConfigFrame& conf);
uci1 2:e67f7c158087 93
uci1 8:95a325df1f6b 94 template<class T>
uci1 2:e67f7c158087 95 static
uci1 8:95a325df1f6b 96 SnCommWin::ECommWinResult WritePowerTo(T& f,
uci1 8:95a325df1f6b 97 const SnPowerFrame& pow,
uci1 15:f2569d8e4176 98 uint32_t& pnum) {
uci1 8:95a325df1f6b 99 const SnCommWin::ECommWinResult rs =
uci1 8:95a325df1f6b 100 SnHeaderFrame::WriteTo(f, SnHeaderFrame::kPowerCode,
uci1 8:95a325df1f6b 101 SnPowerFrame::SizeOf(SnPowerFrame::kIOvers));
uci1 8:95a325df1f6b 102 const SnCommWin::ECommWinResult re = pow.WriteTo(f);
uci1 8:95a325df1f6b 103 ++pnum;
uci1 8:95a325df1f6b 104 return (rs<re) ? rs : re;
uci1 8:95a325df1f6b 105 }
uci1 8:95a325df1f6b 106
uci1 8:95a325df1f6b 107 template<class T>
uci1 8:95a325df1f6b 108 static
uci1 8:95a325df1f6b 109 SnCommWin::ECommWinResult ReadBlockHeader(T& f,
uci1 8:95a325df1f6b 110 uint8_t& mcode,
uci1 8:95a325df1f6b 111 uint32_t& mlen) {
uci1 8:95a325df1f6b 112 return SnHeaderFrame::ReadFrom(f, mcode, mlen);
uci1 8:95a325df1f6b 113 }
uci1 8:95a325df1f6b 114
uci1 5:9cea89700c66 115 template<class T>
uci1 5:9cea89700c66 116 static
uci1 5:9cea89700c66 117 SnCommWin::ECommWinResult WriteFileHeader(T& f, const uint64_t macadr,
uci1 8:95a325df1f6b 118 const uint32_t run, const uint16_t seq) {
uci1 5:9cea89700c66 119 // MUST INCREMENT kIOvers if these writes are altered
uci1 5:9cea89700c66 120 f = SnBitUtils::WriteTo(f, kIOvers);
uci1 5:9cea89700c66 121 f = SnBitUtils::WriteTo(f, macadr);
uci1 5:9cea89700c66 122 f = SnBitUtils::WriteTo(f, run);
uci1 5:9cea89700c66 123 f = SnBitUtils::WriteTo(f, seq);
uci1 5:9cea89700c66 124 return SnCommWin::kOkMsgSent;
uci1 5:9cea89700c66 125 }
uci1 5:9cea89700c66 126
uci1 5:9cea89700c66 127 template<class T>
uci1 5:9cea89700c66 128 static
uci1 5:9cea89700c66 129 SnCommWin::ECommWinResult ReadFileHeader(T& f, uint64_t& macadr,
uci1 5:9cea89700c66 130 uint32_t& run, uint16_t& seq,
uci1 8:95a325df1f6b 131 SnPowerFrame* pow=0) {
uci1 8:95a325df1f6b 132 SnCommWin::ECommWinResult res = SnCommWin::kOkWithMsg;
uci1 5:9cea89700c66 133 uint8_t Rv=0;
uci1 5:9cea89700c66 134 f = SnBitUtils::ReadFrom(f, Rv);
uci1 5:9cea89700c66 135 f = SnBitUtils::ReadFrom(f, macadr);
uci1 5:9cea89700c66 136 f = SnBitUtils::ReadFrom(f, run);
uci1 5:9cea89700c66 137 f = SnBitUtils::ReadFrom(f, seq);
uci1 8:95a325df1f6b 138 if (Rv==2) {
uci1 8:95a325df1f6b 139 uint16_t v1, v2;
uci1 8:95a325df1f6b 140 f = SnBitUtils::ReadFrom(f, v1);
uci1 8:95a325df1f6b 141 f = SnBitUtils::ReadFrom(f, v2);
uci1 8:95a325df1f6b 142 if (pow!=0) {
uci1 8:95a325df1f6b 143 pow->Set(v1, v2, 0, 0, 0);
uci1 8:95a325df1f6b 144 }
uci1 5:9cea89700c66 145 }
uci1 8:95a325df1f6b 146 return res;
uci1 5:9cea89700c66 147 }
uci1 5:9cea89700c66 148
uci1 5:9cea89700c66 149 static
uci1 8:95a325df1f6b 150 uint32_t SizeOfFileHeader(const uint8_t rv) {
uci1 8:95a325df1f6b 151 if (rv==2) {
uci1 8:95a325df1f6b 152 return kMaxSizeOfFileHdr;
uci1 8:95a325df1f6b 153 } else {
uci1 8:95a325df1f6b 154 return sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t);
uci1 8:95a325df1f6b 155 }
uci1 8:95a325df1f6b 156 }
uci1 0:664899e0b988 157
uci1 0:664899e0b988 158 friend class SnSDUtilsWhisperer; // to restrict access to specific functions
uci1 0:664899e0b988 159 };
uci1 0:664899e0b988 160
uci1 0:664899e0b988 161 class SnSDUtilsWhisperer {
uci1 0:664899e0b988 162 static
uci1 0:664899e0b988 163 void DeleteFile(FILE*& f, const char* fname) {
uci1 0:664899e0b988 164 return SnSDUtils::DeleteFile(f, fname);
uci1 0:664899e0b988 165 }
uci1 0:664899e0b988 166
uci1 0:664899e0b988 167 friend class SnCommWin; // the one who's allowed to use me
uci1 0:664899e0b988 168 };
uci1 0:664899e0b988 169
uci1 0:664899e0b988 170 #endif // SN_SnSDUtils