S K UCI / Mbed 2 deprecated AutonomousDAQ

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 "SnCommUsb.h"
uci1 0:664899e0b988 2
uci1 0:664899e0b988 3 #include "Timer.h"
uci1 0:664899e0b988 4
uci1 0:664899e0b988 5 #include "SnStatusFrame.h"
uci1 0:664899e0b988 6 #include "SnConfigFrame.h"
uci1 0:664899e0b988 7 #include "SnEventFrame.h"
uci1 0:664899e0b988 8
uci1 0:664899e0b988 9 MODSERIAL* SnCommUsb::fgCpu = 0;
uci1 0:664899e0b988 10
uci1 0:664899e0b988 11 SnCommWin::ECommWinResult SnCommUsb::SetupPort(MODSERIAL& cpu) {
uci1 0:664899e0b988 12 fgCpu = &cpu;
uci1 0:664899e0b988 13
uci1 0:664899e0b988 14 // set up serial-usb port
uci1 0:664899e0b988 15 fgCpu->baud( 230400 );
uci1 0:664899e0b988 16 fgCpu->format( 8, Serial::None, 1 );
uci1 0:664899e0b988 17 fgCpu->txBufferFlush();
uci1 0:664899e0b988 18 fgCpu->rxBufferFlush();
uci1 0:664899e0b988 19 return kOkNoMsg;
uci1 0:664899e0b988 20 }
uci1 0:664899e0b988 21
uci1 0:664899e0b988 22 SnCommWin::ECommWinResult SnCommUsb::OpenWindow(Timer& timer,
uci1 0:664899e0b988 23 const uint32_t timeout,
uci1 0:664899e0b988 24 const bool sendStatus,
uci1 0:664899e0b988 25 const SnConfigFrame& conf,
uci1 0:664899e0b988 26 const SnEventFrame& evt,
uci1 0:664899e0b988 27 char* const evtBuf,
uci1 0:664899e0b988 28 char* const statBuf) {
uci1 0:664899e0b988 29 // usb port always connected (or never)
uci1 0:664899e0b988 30 SnCommWin::ECommWinResult ret = SnCommWin::kConnected;
uci1 0:664899e0b988 31
uci1 0:664899e0b988 32 if (sendStatus) {
uci1 0:664899e0b988 33 ret = SendStatus(conf, evt, evtBuf, statBuf);
uci1 0:664899e0b988 34 }
uci1 0:664899e0b988 35
uci1 0:664899e0b988 36 return ret;
uci1 0:664899e0b988 37 }
uci1 0:664899e0b988 38
uci1 0:664899e0b988 39 SnCommWin::ECommWinResult SnCommUsb::GetConfig(SnConfigFrame& conf,
uci1 0:664899e0b988 40 Timer& timer,
uci1 0:664899e0b988 41 const uint32_t timeOut,
uci1 0:664899e0b988 42 char* const confBuf) {
uci1 0:664899e0b988 43 // confBuf not used by usb. bytes read directly from serial port
uci1 0:664899e0b988 44
uci1 0:664899e0b988 45 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 0:664899e0b988 46
uci1 0:664899e0b988 47 bool hasData = fgCpu->readable();
uci1 0:664899e0b988 48
uci1 0:664899e0b988 49 while ( (hasData==false) && (timer.read() < timeOut) ) {
uci1 0:664899e0b988 50 wait_ms(250);
uci1 0:664899e0b988 51
uci1 0:664899e0b988 52 hasData = fgCpu->readable();
uci1 0:664899e0b988 53 }
uci1 0:664899e0b988 54
uci1 0:664899e0b988 55 if( hasData ) {
uci1 0:664899e0b988 56 conf.ReadFrom(*fgCpu);
uci1 0:664899e0b988 57 res = SnCommWin::kOkWithMsg;
uci1 0:664899e0b988 58 } else {
uci1 0:664899e0b988 59 res = SnCommWin::kOkNoMsg;
uci1 0:664899e0b988 60 }
uci1 0:664899e0b988 61
uci1 0:664899e0b988 62 return res;
uci1 0:664899e0b988 63 }
uci1 0:664899e0b988 64
uci1 0:664899e0b988 65 SnCommWin::ECommWinResult SnCommUsb::SendStatus(const SnConfigFrame& conf,
uci1 0:664899e0b988 66 const SnEventFrame& evt,
uci1 0:664899e0b988 67 char* const evtBuf,
uci1 0:664899e0b988 68 char* const statBuf) {
uci1 0:664899e0b988 69 // statBuf not used. status written directly to serial port
uci1 0:664899e0b988 70 return SnStatusFrame::WriteTo(*fgCpu, SnConfigFrame::kUSB, conf, evt, evtBuf);
uci1 0:664899e0b988 71 }
uci1 0:664899e0b988 72
uci1 0:664899e0b988 73 SnCommWin::ECommWinResult SnCommUsb::SendData(FILE* inf) {
uci1 0:664899e0b988 74 int c;
uci1 0:664899e0b988 75 do {
uci1 0:664899e0b988 76 c = getc(inf);
uci1 0:664899e0b988 77 fgCpu->putc(c);
uci1 0:664899e0b988 78 } while (c!=EOF);
uci1 0:664899e0b988 79 return SnCommWin::kOkMsgSent;
uci1 0:664899e0b988 80 }
uci1 0:664899e0b988 81
uci1 0:664899e0b988 82 SnCommWin::ECommWinResult SnCommUsb::SendConfAndEvents(FILE* inf,
uci1 0:664899e0b988 83 const SnConfigFrame& curConf,
uci1 0:664899e0b988 84 SnEventFrame& evt,
uci1 0:664899e0b988 85 char* const evtBuf,
uci1 0:664899e0b988 86 char* const confBuf,
uci1 0:664899e0b988 87 const uint32_t nevts,
uci1 0:664899e0b988 88 const uint32_t firstEvt) {
uci1 0:664899e0b988 89 // firstEvt==0 ==> start at beginning
uci1 0:664899e0b988 90 // nevts==0 ==> NO events! (see SnCommWin::SendData
uci1 0:664899e0b988 91 // for the fcn to send the full file)
uci1 0:664899e0b988 92
uci1 0:664899e0b988 93 // TODO: check memory for temporary config/event frames?
uci1 0:664899e0b988 94 SnConfigFrame conf;
uci1 0:664899e0b988 95 conf.ReadFrom(inf);
uci1 0:664899e0b988 96 conf.WriteTo(*fgCpu);
uci1 0:664899e0b988 97
uci1 0:664899e0b988 98 uint8_t sLoseLSB=0, sLoseMSB=0;
uci1 0:664899e0b988 99 uint16_t sWvBase=0;
uci1 0:664899e0b988 100 conf.GetPackParsFor(SnConfigFrame::kUSB, sLoseLSB, sLoseMSB, sWvBase);
uci1 0:664899e0b988 101 // do we have to unpack & repack events?
uci1 0:664899e0b988 102 const bool repack = (sLoseLSB != conf.GetWvLoseLSB())
uci1 0:664899e0b988 103 && (sLoseMSB != conf.GetWvLoseMSB())
uci1 0:664899e0b988 104 && (sWvBase != conf.GetWvBaseline());
uci1 0:664899e0b988 105
uci1 0:664899e0b988 106 // size of event in file
uci1 0:664899e0b988 107 const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(),
uci1 0:664899e0b988 108 conf.GetWvLoseMSB());
uci1 0:664899e0b988 109 // move up to first event
uci1 0:664899e0b988 110 fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET);
uci1 0:664899e0b988 111 if (repack) {
uci1 0:664899e0b988 112 // repack ==> send event by event
uci1 0:664899e0b988 113 // size of event sent over afar
uci1 0:664899e0b988 114 const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB);
uci1 0:664899e0b988 115 char* b;
uci1 0:664899e0b988 116 register uint32_t i;
uci1 0:664899e0b988 117 for (i=0; (i<nevts) && (ferror(inf)==0) && (feof(inf)==0); i++) {
uci1 0:664899e0b988 118 evt.ReadFrom(inf, evtBuf,
uci1 0:664899e0b988 119 conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
uci1 0:664899e0b988 120 conf.GetWvBaseline());
uci1 0:664899e0b988 121 evt.WriteTo(evtBuf, sLoseLSB, sLoseMSB, sWvBase);
uci1 0:664899e0b988 122 b = evtBuf;
uci1 0:664899e0b988 123 for (uint32_t j=0; j<ssize; j++, b++) {
uci1 0:664899e0b988 124 fgCpu->putc( *b );
uci1 0:664899e0b988 125 }
uci1 0:664899e0b988 126 }
uci1 0:664899e0b988 127 if (i!=nevts) {
uci1 0:664899e0b988 128 return SnCommWin::kFailPartSent;
uci1 0:664899e0b988 129 }
uci1 0:664899e0b988 130 } else {
uci1 0:664899e0b988 131 // no repacking ==> just send the bytes from the file
uci1 0:664899e0b988 132 const uint32_t nbytes = nevts*esize;
uci1 0:664899e0b988 133 register uint32_t i;
uci1 0:664899e0b988 134 for (i=0; i<nbytes && (ferror(inf)==0) && (feof(inf)==0); i++) {
uci1 0:664899e0b988 135 fgCpu->putc( fgetc(inf) );
uci1 0:664899e0b988 136 }
uci1 0:664899e0b988 137 if (i!=nbytes) {
uci1 0:664899e0b988 138 return SnCommWin::kFailPartSent;
uci1 0:664899e0b988 139 }
uci1 0:664899e0b988 140 }
uci1 0:664899e0b988 141
uci1 0:664899e0b988 142 return SnCommWin::kOkMsgSent;
uci1 0:664899e0b988 143 }