S K UCI / Mbed 2 deprecated AutonomousDAQ

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