Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Fri Jul 20 19:04:02 2012 +0000
Revision:
1:e392595b4b76
Parent:
0:664899e0b988
Child:
3:24c5f0f50bf1
many features checked and working. afar implemented. sending of data not yet tested. contains many debug prints

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #include "SnEventFrame.h"
uci1 0:664899e0b988 2
uci1 0:664899e0b988 3 #include "SnCRCUtils.h"
uci1 0:664899e0b988 4
uci1 1:e392595b4b76 5 const uint8_t SnEventFrame::kIOVers = 1; // MUST BE INCREASED if any member var changes (==> also if kNchans, etc. change!)
uci1 1:e392595b4b76 6
uci1 0:664899e0b988 7 void SnEventFrame::ReadFrom(const char* const buf,
uci1 0:664899e0b988 8 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 0:664899e0b988 9 const uint16_t wvBaseline) {
uci1 0:664899e0b988 10 // no check on the length of buf is done here
uci1 0:664899e0b988 11 // that should be been done already
uci1 0:664899e0b988 12 //
uci1 0:664899e0b988 13 // must match ::WriteToBuf
uci1 0:664899e0b988 14
uci1 0:664899e0b988 15 union Usub {
uci1 0:664899e0b988 16 const char* s;
uci1 0:664899e0b988 17 const uint8_t* u;
uci1 0:664899e0b988 18 } b;
uci1 0:664899e0b988 19 b.s = buf;
uci1 0:664899e0b988 20
uci1 1:e392595b4b76 21 uint8_t Rv=0;
uci1 0:664899e0b988 22 b.s = SnBitUtils::ReadFrom(b.s, Rv); // i/o version
uci1 0:664899e0b988 23 if (Rv>0) {
uci1 0:664899e0b988 24 b.s = SnBitUtils::ReadFrom(b.s, fMbedTime);
uci1 0:664899e0b988 25 b.s = SnBitUtils::ReadFrom(b.s, fEvtNum);
uci1 0:664899e0b988 26 b.s = SnBitUtils::ReadFrom(b.s, fDTms);
uci1 0:664899e0b988 27 b.s = SnBitUtils::ReadFrom(b.s, fTrgNum);
uci1 0:664899e0b988 28 b.s = SnBitUtils::ReadFrom(b.s, fTrgBits);
uci1 0:664899e0b988 29 b.u = UnpackWavef(b.u, fData, loseLSB, loseMSB, wvBaseline);
uci1 0:664899e0b988 30 b.s = SnBitUtils::ReadFrom(b.s, fCRC);
uci1 0:664899e0b988 31 }
uci1 0:664899e0b988 32
uci1 0:664899e0b988 33 }
uci1 0:664899e0b988 34
uci1 0:664899e0b988 35 void SnEventFrame::WriteTo(char* const buf,
uci1 0:664899e0b988 36 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 0:664899e0b988 37 const uint16_t wvBaseline) const {
uci1 0:664899e0b988 38 // no check on the length of the buf is done here
uci1 0:664899e0b988 39 // that should be done already
uci1 0:664899e0b988 40 //
uci1 0:664899e0b988 41 // must match ReadFromBuf
uci1 0:664899e0b988 42
uci1 0:664899e0b988 43 union {
uci1 0:664899e0b988 44 char* s;
uci1 0:664899e0b988 45 uint8_t* u;
uci1 0:664899e0b988 46 } b;
uci1 0:664899e0b988 47 b.s = buf;
uci1 0:664899e0b988 48
uci1 0:664899e0b988 49 b.s = SnBitUtils::WriteTo(b.s, kIOVers); // i/o version
uci1 0:664899e0b988 50
uci1 0:664899e0b988 51 b.s = SnBitUtils::WriteTo(b.s, fMbedTime);
uci1 0:664899e0b988 52 b.s = SnBitUtils::WriteTo(b.s, fEvtNum);
uci1 0:664899e0b988 53 b.s = SnBitUtils::WriteTo(b.s, fDTms);
uci1 0:664899e0b988 54 b.s = SnBitUtils::WriteTo(b.s, fTrgNum);
uci1 0:664899e0b988 55 b.s = SnBitUtils::WriteTo(b.s, fTrgBits);
uci1 0:664899e0b988 56 b.u = PackWavef(b.u, fData, loseLSB, loseMSB, wvBaseline);
uci1 0:664899e0b988 57 b.s = SnBitUtils::WriteTo(b.s, fCRC);
uci1 0:664899e0b988 58
uci1 0:664899e0b988 59 }
uci1 0:664899e0b988 60
uci1 0:664899e0b988 61 void SnEventFrame::CalcCRC() {
uci1 0:664899e0b988 62 // CRC made using union on a little endian (mbed) processor
uci1 0:664899e0b988 63 fCRC = 0u;
uci1 0:664899e0b988 64 const uint16_t* dev = fData;
uci1 1:e392595b4b76 65 for (uint16_t i=0; i<kTotSamps; i++, dev++) {
uci1 0:664899e0b988 66 fCRC = update_crc32_xfer_short(fCRC, *dev);
uci1 0:664899e0b988 67 }
uci1 0:664899e0b988 68 }
uci1 0:664899e0b988 69
uci1 0:664899e0b988 70 bool SnEventFrame::ReadFromFileToBuf(FILE* f,
uci1 0:664899e0b988 71 char* const evtBuf,
uci1 0:664899e0b988 72 const uint8_t loseLSB,
uci1 0:664899e0b988 73 const uint8_t loseMSB) {
uci1 0:664899e0b988 74 // file position expected to be at this event frame already
uci1 0:664899e0b988 75 bool ret = false;
uci1 0:664899e0b988 76 if (f!=0) {
uci1 0:664899e0b988 77 if (fread(evtBuf, SizeOf(loseLSB, loseMSB), 1u, f)!=NULL) {
uci1 0:664899e0b988 78 ret = true;
uci1 0:664899e0b988 79 }
uci1 0:664899e0b988 80 }
uci1 0:664899e0b988 81 return ret;
uci1 0:664899e0b988 82 }
uci1 0:664899e0b988 83
uci1 0:664899e0b988 84 bool SnEventFrame::ReadFrom(FILE* f,
uci1 0:664899e0b988 85 char* const evtBuf,
uci1 0:664899e0b988 86 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 0:664899e0b988 87 const uint16_t wvBaseline) {
uci1 0:664899e0b988 88 // file position expected to be at this event frame already
uci1 0:664899e0b988 89 const bool ret = ReadFromFileToBuf(f, evtBuf, loseLSB, loseMSB);
uci1 0:664899e0b988 90 if (ret) {
uci1 0:664899e0b988 91 ReadFrom(evtBuf, loseLSB, loseMSB, wvBaseline);
uci1 0:664899e0b988 92 }
uci1 0:664899e0b988 93 return ret;
uci1 0:664899e0b988 94 }
uci1 0:664899e0b988 95
uci1 0:664899e0b988 96 bool SnEventFrame::WriteTo(FILE* f, char* const evtBuf,
uci1 0:664899e0b988 97 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 0:664899e0b988 98 const uint16_t wvBaseline) const {
uci1 0:664899e0b988 99 // file pointer should be in correct location
uci1 0:664899e0b988 100
uci1 0:664899e0b988 101 WriteTo(evtBuf, loseLSB, loseMSB, wvBaseline);
uci1 0:664899e0b988 102 return WriteToFileFromBuf(f, evtBuf, loseLSB, loseMSB);
uci1 0:664899e0b988 103 }
uci1 0:664899e0b988 104
uci1 0:664899e0b988 105 bool SnEventFrame::WriteToFileFromBuf(FILE* f, char* const evtBuf,
uci1 0:664899e0b988 106 const uint8_t loseLSB, const uint8_t loseMSB) {
uci1 0:664899e0b988 107 // file pointer should be in correct location
uci1 0:664899e0b988 108
uci1 0:664899e0b988 109 bool ret = false;
uci1 0:664899e0b988 110 if (f!=0) {
uci1 0:664899e0b988 111 fwrite(evtBuf, SizeOf(loseLSB, loseMSB), 1u, f);
uci1 0:664899e0b988 112 ret = (ferror(f)==false);
uci1 0:664899e0b988 113 }
uci1 0:664899e0b988 114 return ret;
uci1 0:664899e0b988 115
uci1 0:664899e0b988 116 }
uci1 0:664899e0b988 117
uci1 0:664899e0b988 118
uci1 0:664899e0b988 119 uint8_t* SnEventFrame::PackWavef(uint8_t* const buf, const uint16_t* const data,
uci1 0:664899e0b988 120 const uint8_t loseLSB, const uint8_t loseMSB,
uci1 0:664899e0b988 121 const uint16_t wvBaseline) {
uci1 0:664899e0b988 122 // Compress the data. This is potentially LOSSY; it depends
uci1 0:664899e0b988 123 // on the dynamic range and on the options.
uci1 0:664899e0b988 124 // See SnConfigFrame::fWvLoseLSB and SnConfigFrame::fWvLoseMSB.
uci1 0:664899e0b988 125 // If the number of least signficant bits to lose is not 0, the
uci1 0:664899e0b988 126 // compression will be lossy (decreased resolution -- this is ok
uci1 0:664899e0b988 127 // if the noise of the signal is much greater than the resolution).
uci1 0:664899e0b988 128 // Losing the most significant bits will only be lossy if the
uci1 0:664899e0b988 129 // signal-SnConfigFrame::fWvBaseline cannot fit in the reduced
uci1 0:664899e0b988 130 // dynamic range (each MSB bit reducing the DR by a factor of 2).
uci1 0:664899e0b988 131 //
uci1 0:664899e0b988 132 // Note that the mbed uses little endian. Behavior should be the
uci1 0:664899e0b988 133 // same on big endian, but this has not been tested.
uci1 0:664899e0b988 134 //
uci1 0:664899e0b988 135 // Use an unsigned buffer to prevent bits being changed during
uci1 0:664899e0b988 136 // an implicit unsigned -> signed cast.
uci1 0:664899e0b988 137 //
uci1 0:664899e0b988 138 // buf = the byte array into which the data should be packed
uci1 0:664899e0b988 139 // data = the data to pack
uci1 0:664899e0b988 140 // loseLSB = number of least significant bits to throw away
uci1 0:664899e0b988 141 // loseMSB = number of most significant bits to throw away
uci1 0:664899e0b988 142 // wvBaseline = baseline to subtract to from ADC before packing
uci1 0:664899e0b988 143
uci1 0:664899e0b988 144 const uint32_t blen = SizeOfPackedWavef(loseLSB, loseMSB);
uci1 0:664899e0b988 145 const uint8_t packSmpBits = BITS_IN_SHORT-loseLSB-loseMSB;
uci1 0:664899e0b988 146
uci1 0:664899e0b988 147 // make sure this buffer space is all 0's to start
uci1 0:664899e0b988 148 memset(buf, 0, blen*sizeof(uint8_t));
uci1 0:664899e0b988 149
uci1 0:664899e0b988 150 const uint16_t clipHi = uint16_t(
uci1 0:664899e0b988 151 uint16_t(uint16_t(0xFFFFu >> loseLSB) << (loseLSB+loseMSB))
uci1 0:664899e0b988 152 >> loseMSB);
uci1 0:664899e0b988 153
uci1 0:664899e0b988 154 uint8_t* b = buf;
uci1 0:664899e0b988 155 const uint16_t* dev = data;
uci1 0:664899e0b988 156 uint16_t dum;
uci1 0:664899e0b988 157 int8_t sbit=0;
uci1 0:664899e0b988 158 for (uint16_t i=0; i<kTotSamps; i++, dev++) {
uci1 0:664899e0b988 159 // dump the bits we don't want
uci1 0:664899e0b988 160 dum = (*dev) - wvBaseline;
uci1 0:664899e0b988 161 if (dum<clipHi) {
uci1 0:664899e0b988 162 dum >>= loseLSB;
uci1 0:664899e0b988 163 dum <<= (loseLSB+loseMSB);
uci1 0:664899e0b988 164 } else {
uci1 0:664899e0b988 165 dum = clipHi << loseMSB;
uci1 0:664899e0b988 166 }
uci1 0:664899e0b988 167 if (sbit<=0) {
uci1 0:664899e0b988 168 // lose MSB's put in previous short (or none if sbit==0)
uci1 0:664899e0b988 169 dum <<= -sbit;
uci1 0:664899e0b988 170 *b |= dum >> 8u; // "first" byte of the short
uci1 0:664899e0b988 171 dum <<= 8u;
uci1 0:664899e0b988 172 *(b+1) |= dum >> 8u; // "second"
uci1 0:664899e0b988 173 // move to next number (dev++ in the for loop)
uci1 0:664899e0b988 174 // move starting bit up
uci1 0:664899e0b988 175 // but stay in this byte of the buf (do not increment b)
uci1 0:664899e0b988 176 // since kPackSmpBits <= kBitsInShort, sbit can't
uci1 0:664899e0b988 177 // move past the end of the current two bytes
uci1 0:664899e0b988 178 sbit += packSmpBits;
uci1 0:664899e0b988 179 } else {
uci1 0:664899e0b988 180 // first few bits towards the end of this short
uci1 0:664899e0b988 181 dum >>= sbit;
uci1 0:664899e0b988 182 *b |= dum >> 8u; // "first" byte of the short
uci1 0:664899e0b988 183 dum <<= 8u;
uci1 0:664899e0b988 184 *(b+1) |= dum >> 8u; // "second"
uci1 0:664899e0b988 185 if ( (sbit+packSmpBits) >= BITS_IN_SHORT ) {
uci1 0:664899e0b988 186 b+=2; // move to next short in the buf
uci1 0:664899e0b988 187 i--; dev--; // but stay on this number
uci1 0:664899e0b988 188 // move starting bit back into the short we just filled
uci1 0:664899e0b988 189 sbit -= BITS_IN_SHORT;
uci1 0:664899e0b988 190 } else {
uci1 0:664899e0b988 191 // stay in this buffer and move to the next number
uci1 0:664899e0b988 192 // move starting bit up
uci1 0:664899e0b988 193 sbit += packSmpBits;
uci1 0:664899e0b988 194 }
uci1 0:664899e0b988 195 }
uci1 0:664899e0b988 196 }
uci1 0:664899e0b988 197
uci1 0:664899e0b988 198 return buf+blen;
uci1 0:664899e0b988 199 }
uci1 0:664899e0b988 200
uci1 0:664899e0b988 201 const uint8_t* SnEventFrame::UnpackWavef(const uint8_t* const buf,
uci1 0:664899e0b988 202 uint16_t* const data,
uci1 0:664899e0b988 203 const uint8_t loseLSB,
uci1 0:664899e0b988 204 const uint8_t loseMSB,
uci1 0:664899e0b988 205 const uint16_t wvBaseline) {
uci1 0:664899e0b988 206 if (loseLSB==0 && loseMSB==0 && wvBaseline==0) {
uci1 0:664899e0b988 207 memcpy(data, buf, kTotSamps*sizeof(uint16_t));
uci1 0:664899e0b988 208 } else {
uci1 0:664899e0b988 209
uci1 0:664899e0b988 210 const uint8_t packSmpBits = BITS_IN_SHORT-loseLSB-loseMSB;
uci1 0:664899e0b988 211
uci1 0:664899e0b988 212 // make sure data is all 0's to start
uci1 0:664899e0b988 213 memset(data, 0, kTotSamps*sizeof(uint16_t));
uci1 0:664899e0b988 214
uci1 0:664899e0b988 215 const uint8_t* b = buf;
uci1 0:664899e0b988 216 uint16_t* dev = data;
uci1 0:664899e0b988 217 uint16_t dum;
uci1 0:664899e0b988 218 int8_t sbit=0;
uci1 0:664899e0b988 219 for (uint16_t i=0; i<kTotSamps; i++, dev++) {
uci1 0:664899e0b988 220 dum = (*b) << 8u;
uci1 0:664899e0b988 221 dum |= *(b+1);
uci1 0:664899e0b988 222 if (sbit<=0) {
uci1 0:664899e0b988 223 dum >>= (-sbit+loseLSB+loseMSB);
uci1 0:664899e0b988 224 dum <<= loseLSB;
uci1 0:664899e0b988 225 *dev |= dum;
uci1 0:664899e0b988 226 // add baseline and move to next number (dev++ in the for loop)
uci1 0:664899e0b988 227 *dev += wvBaseline;
uci1 0:664899e0b988 228 // but stay in this short of the buf (do not increment b)
uci1 0:664899e0b988 229 // move starting bit up
uci1 0:664899e0b988 230 sbit += packSmpBits;
uci1 0:664899e0b988 231 } else {
uci1 0:664899e0b988 232 dum <<= sbit;
uci1 0:664899e0b988 233 dum >>= loseMSB+loseLSB;
uci1 0:664899e0b988 234 dum <<= loseLSB;
uci1 0:664899e0b988 235 *dev = dum;
uci1 0:664899e0b988 236 if ( (sbit+packSmpBits) >= BITS_IN_SHORT ) {
uci1 0:664899e0b988 237 b+=2; // move to next short in the buf
uci1 0:664899e0b988 238 i--; dev--; // but stay on this number
uci1 0:664899e0b988 239 // move starting bit back into the short we just read from
uci1 0:664899e0b988 240 sbit -= BITS_IN_SHORT;
uci1 0:664899e0b988 241 } else {
uci1 0:664899e0b988 242 // add baseline and move to next number (dev++ in the for loop)
uci1 0:664899e0b988 243 *dev += wvBaseline;
uci1 0:664899e0b988 244 sbit += packSmpBits;
uci1 0:664899e0b988 245 }
uci1 0:664899e0b988 246 }
uci1 0:664899e0b988 247 }
uci1 0:664899e0b988 248 }
uci1 0:664899e0b988 249
uci1 0:664899e0b988 250 return buf+SizeOfPackedWavef(loseLSB, loseMSB);
uci1 0:664899e0b988 251 }
uci1 0:664899e0b988 252