Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommUsb.cpp@2:e67f7c158087, 2012-07-24 (annotated)
- Committer:
- uci1
- Date:
- Tue Jul 24 02:07:23 2012 +0000
- Revision:
- 2:e67f7c158087
- Parent:
- 1:e392595b4b76
- Child:
- 3:24c5f0f50bf1
added header to i/o. still lots of debugging prints.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 2:e67f7c158087 | 8 | #include "SnHeaderFrame.h" |
| uci1 | 2:e67f7c158087 | 9 | #include "SnSDUtils.h" |
| uci1 | 1:e392595b4b76 | 10 | |
| uci1 | 1:e392595b4b76 | 11 | MODSERIAL* SnCommUsb::fgCpu = 0; |
| uci1 | 1:e392595b4b76 | 12 | |
| uci1 | 1:e392595b4b76 | 13 | void __Recv(MODSERIAL_IRQ_INFO* s) { |
| uci1 | 1:e392595b4b76 | 14 | static DigitalOut led4(LED4); |
| uci1 | 1:e392595b4b76 | 15 | static DigitalOut led3(LED3); |
| uci1 | 1:e392595b4b76 | 16 | for (uint8_t i=0; i<20; i++) { |
| uci1 | 1:e392595b4b76 | 17 | led4=!led4; |
| uci1 | 1:e392595b4b76 | 18 | led3=!led3; |
| uci1 | 1:e392595b4b76 | 19 | wait(0.1); |
| uci1 | 1:e392595b4b76 | 20 | } |
| uci1 | 1:e392595b4b76 | 21 | } |
| uci1 | 1:e392595b4b76 | 22 | |
| uci1 | 1:e392595b4b76 | 23 | SnCommWin::ECommWinResult SnCommUsb::SetupPort(MODSERIAL& cpu) { |
| uci1 | 1:e392595b4b76 | 24 | fgCpu = &cpu; |
| uci1 | 1:e392595b4b76 | 25 | |
| uci1 | 1:e392595b4b76 | 26 | // set up serial-usb port |
| uci1 | 1:e392595b4b76 | 27 | fgCpu->baud( 230400 ); |
| uci1 | 1:e392595b4b76 | 28 | fgCpu->format( 8, Serial::None, 1 ); |
| uci1 | 1:e392595b4b76 | 29 | fgCpu->txBufferFlush(); |
| uci1 | 1:e392595b4b76 | 30 | fgCpu->rxBufferFlush(); |
| uci1 | 1:e392595b4b76 | 31 | //fgCpu->attach(&__Recv, MODSERIAL::RxIrq); |
| uci1 | 1:e392595b4b76 | 32 | return kOkNoMsg; |
| uci1 | 1:e392595b4b76 | 33 | } |
| uci1 | 1:e392595b4b76 | 34 | |
| uci1 | 1:e392595b4b76 | 35 | |
| uci1 | 1:e392595b4b76 | 36 | SnCommWin::ECommWinResult SnCommUsb::OpenWindow(const uint32_t timeout, |
| uci1 | 1:e392595b4b76 | 37 | const bool sendStatus, |
| uci1 | 1:e392595b4b76 | 38 | const SnConfigFrame& conf, |
| uci1 | 1:e392595b4b76 | 39 | const SnEventFrame& evt, |
| uci1 | 2:e67f7c158087 | 40 | char* const genBuf) { |
| uci1 | 1:e392595b4b76 | 41 | // usb port always connected (or never) |
| uci1 | 1:e392595b4b76 | 42 | SnCommWin::ECommWinResult ret = SnCommWin::kConnected; |
| uci1 | 1:e392595b4b76 | 43 | |
| uci1 | 1:e392595b4b76 | 44 | if (sendStatus) { |
| uci1 | 2:e67f7c158087 | 45 | ret = SendStatus(conf, evt, genBuf); |
| uci1 | 1:e392595b4b76 | 46 | } |
| uci1 | 1:e392595b4b76 | 47 | |
| uci1 | 1:e392595b4b76 | 48 | return ret; |
| uci1 | 1:e392595b4b76 | 49 | } |
| uci1 | 1:e392595b4b76 | 50 | |
| uci1 | 1:e392595b4b76 | 51 | SnCommWin::ECommWinResult SnCommUsb::GetConfig(SnConfigFrame& conf, |
| uci1 | 1:e392595b4b76 | 52 | const uint32_t timeOut, |
| uci1 | 1:e392595b4b76 | 53 | char* const confBuf) { |
| uci1 | 1:e392595b4b76 | 54 | // confBuf not used by usb. bytes read directly from serial port |
| uci1 | 1:e392595b4b76 | 55 | |
| uci1 | 1:e392595b4b76 | 56 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
| uci1 | 1:e392595b4b76 | 57 | |
| uci1 | 1:e392595b4b76 | 58 | bool hasData = fgCpu->readable()==1; |
| uci1 | 2:e67f7c158087 | 59 | |
| uci1 | 1:e392595b4b76 | 60 | while ( (hasData==false) && (time(0) < timeOut) ) { |
| uci1 | 1:e392595b4b76 | 61 | wait_ms(10); |
| uci1 | 1:e392595b4b76 | 62 | |
| uci1 | 1:e392595b4b76 | 63 | hasData = fgCpu->readable()==1; |
| uci1 | 1:e392595b4b76 | 64 | } |
| uci1 | 1:e392595b4b76 | 65 | |
| uci1 | 1:e392595b4b76 | 66 | if( hasData ) { |
| uci1 | 2:e67f7c158087 | 67 | uint8_t mcode; |
| uci1 | 2:e67f7c158087 | 68 | uint32_t mlen; |
| uci1 | 2:e67f7c158087 | 69 | SnHeaderFrame::ReadFrom(*fgCpu, mcode, mlen); |
| uci1 | 2:e67f7c158087 | 70 | // TODO: do something with the header info? |
| uci1 | 1:e392595b4b76 | 71 | conf.ReadFrom(*fgCpu); |
| uci1 | 1:e392595b4b76 | 72 | res = SnCommWin::kOkWithMsg; |
| uci1 | 1:e392595b4b76 | 73 | } else { |
| uci1 | 1:e392595b4b76 | 74 | res = SnCommWin::kOkNoMsg; |
| uci1 | 1:e392595b4b76 | 75 | } |
| uci1 | 1:e392595b4b76 | 76 | |
| uci1 | 1:e392595b4b76 | 77 | return res; |
| uci1 | 1:e392595b4b76 | 78 | } |
| uci1 | 1:e392595b4b76 | 79 | |
| uci1 | 1:e392595b4b76 | 80 | SnCommWin::ECommWinResult SnCommUsb::SendStatus(const SnConfigFrame& conf, |
| uci1 | 1:e392595b4b76 | 81 | const SnEventFrame& evt, |
| uci1 | 2:e67f7c158087 | 82 | char* const genBuf) { |
| uci1 | 2:e67f7c158087 | 83 | const bool ok = SnHeaderFrame::WriteTo(*fgCpu, |
| uci1 | 2:e67f7c158087 | 84 | SnHeaderFrame::kStatusCode, SnStatusFrame::SizeOf(conf)); |
| uci1 | 2:e67f7c158087 | 85 | if (ok) { |
| uci1 | 2:e67f7c158087 | 86 | return SnStatusFrame::WriteTo(*fgCpu, SnConfigFrame::kUSB, conf, evt, genBuf); |
| uci1 | 2:e67f7c158087 | 87 | } else { |
| uci1 | 2:e67f7c158087 | 88 | return SnCommWin::kFailNoneSent; |
| uci1 | 2:e67f7c158087 | 89 | } |
| uci1 | 1:e392595b4b76 | 90 | } |
| uci1 | 1:e392595b4b76 | 91 | |
| uci1 | 1:e392595b4b76 | 92 | SnCommWin::ECommWinResult SnCommUsb::SendData(FILE* inf) { |
| uci1 | 2:e67f7c158087 | 93 | const int foff = ftell(inf); |
| uci1 | 2:e67f7c158087 | 94 | fseek(inf, 0, SEEK_END); |
| uci1 | 2:e67f7c158087 | 95 | const int fsize = ftell(inf) - foff; |
| uci1 | 2:e67f7c158087 | 96 | fseek(inf, foff, SEEK_SET); |
| uci1 | 2:e67f7c158087 | 97 | if (fsize>0) { |
| uci1 | 2:e67f7c158087 | 98 | SnHeaderFrame::WriteTo(*fgCpu, |
| uci1 | 2:e67f7c158087 | 99 | SnHeaderFrame::kFileCode, fsize); |
| uci1 | 2:e67f7c158087 | 100 | SendBytes(inf, fsize); |
| uci1 | 2:e67f7c158087 | 101 | return SnCommWin::kOkMsgSent; |
| uci1 | 2:e67f7c158087 | 102 | } else { |
| uci1 | 2:e67f7c158087 | 103 | return SnCommWin::kFailNoneSent; |
| uci1 | 2:e67f7c158087 | 104 | } |
| uci1 | 2:e67f7c158087 | 105 | } |
| uci1 | 2:e67f7c158087 | 106 | |
| uci1 | 2:e67f7c158087 | 107 | bool SnCommUsb::SendBytes(FILE* f, const uint32_t nbytes) { |
| uci1 | 2:e67f7c158087 | 108 | register uint32_t i=0; |
| uci1 | 2:e67f7c158087 | 109 | for (i=0; (i<nbytes) |
| uci1 | 2:e67f7c158087 | 110 | && (feof(f)==0) |
| uci1 | 2:e67f7c158087 | 111 | && (ferror(f)==0); i++) { |
| uci1 | 2:e67f7c158087 | 112 | fgCpu->putc( getc(f) ); |
| uci1 | 2:e67f7c158087 | 113 | } |
| uci1 | 2:e67f7c158087 | 114 | return (i==nbytes); |
| uci1 | 2:e67f7c158087 | 115 | } |
| uci1 | 2:e67f7c158087 | 116 | |
| uci1 | 2:e67f7c158087 | 117 | bool SnCommUsb::SendBytes(const char* const buf, |
| uci1 | 2:e67f7c158087 | 118 | const uint32_t nbytes) { |
| uci1 | 2:e67f7c158087 | 119 | const char* b = buf; |
| uci1 | 2:e67f7c158087 | 120 | for (uint32_t j=0; j<nbytes; j++, b++) { |
| uci1 | 2:e67f7c158087 | 121 | fgCpu->putc( *b ); |
| uci1 | 2:e67f7c158087 | 122 | } |
| uci1 | 2:e67f7c158087 | 123 | return true; |
| uci1 | 1:e392595b4b76 | 124 | } |
| uci1 | 1:e392595b4b76 | 125 | |
| uci1 | 1:e392595b4b76 | 126 | SnCommWin::ECommWinResult SnCommUsb::SendConfAndEvents(FILE* inf, |
| uci1 | 1:e392595b4b76 | 127 | const SnConfigFrame& curConf, |
| uci1 | 1:e392595b4b76 | 128 | SnEventFrame& evt, |
| uci1 | 2:e67f7c158087 | 129 | char* const genBuf, |
| uci1 | 1:e392595b4b76 | 130 | const uint32_t nevts, |
| uci1 | 1:e392595b4b76 | 131 | const uint32_t firstEvt) { |
| uci1 | 1:e392595b4b76 | 132 | // firstEvt==0 ==> start at beginning |
| uci1 | 1:e392595b4b76 | 133 | // nevts==0 ==> NO events! (see SnCommWin::SendData |
| uci1 | 1:e392595b4b76 | 134 | // for the fcn to send the full file) |
| uci1 | 1:e392595b4b76 | 135 | |
| uci1 | 1:e392595b4b76 | 136 | // TODO: check memory for temporary config/event frames? |
| uci1 | 2:e67f7c158087 | 137 | uint64_t macadr; |
| uci1 | 2:e67f7c158087 | 138 | uint32_t run; |
| uci1 | 2:e67f7c158087 | 139 | uint16_t seq; |
| uci1 | 2:e67f7c158087 | 140 | SnSDUtils::ReadFileHeader(inf, macadr, run, seq); |
| uci1 | 1:e392595b4b76 | 141 | SnConfigFrame conf; |
| uci1 | 1:e392595b4b76 | 142 | conf.ReadFrom(inf); |
| uci1 | 1:e392595b4b76 | 143 | |
| uci1 | 1:e392595b4b76 | 144 | uint8_t sLoseLSB=0, sLoseMSB=0; |
| uci1 | 1:e392595b4b76 | 145 | uint16_t sWvBase=0; |
| uci1 | 2:e67f7c158087 | 146 | curConf.GetPackParsFor(SnConfigFrame::kUSB, sLoseLSB, sLoseMSB, sWvBase); |
| uci1 | 1:e392595b4b76 | 147 | // do we have to unpack & repack events? |
| uci1 | 1:e392595b4b76 | 148 | const bool repack = (sLoseLSB != conf.GetWvLoseLSB()) |
| uci1 | 1:e392595b4b76 | 149 | && (sLoseMSB != conf.GetWvLoseMSB()) |
| uci1 | 1:e392595b4b76 | 150 | && (sWvBase != conf.GetWvBaseline()); |
| uci1 | 1:e392595b4b76 | 151 | |
| uci1 | 1:e392595b4b76 | 152 | // size of event in file |
| uci1 | 1:e392595b4b76 | 153 | const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(), |
| uci1 | 1:e392595b4b76 | 154 | conf.GetWvLoseMSB()); |
| uci1 | 1:e392595b4b76 | 155 | // move up to first event |
| uci1 | 2:e67f7c158087 | 156 | const uint32_t csize = conf.SizeOf(); |
| uci1 | 2:e67f7c158087 | 157 | fseek(inf, csize + (firstEvt*esize), SEEK_SET); |
| uci1 | 2:e67f7c158087 | 158 | |
| uci1 | 2:e67f7c158087 | 159 | // send the header and conf |
| uci1 | 2:e67f7c158087 | 160 | char* b = genBuf; |
| uci1 | 2:e67f7c158087 | 161 | SnHeaderFrame::WriteTo(b, |
| uci1 | 2:e67f7c158087 | 162 | SnHeaderFrame::kConfAndEvtsCode, csize); |
| uci1 | 2:e67f7c158087 | 163 | conf.WriteTo(b); |
| uci1 | 2:e67f7c158087 | 164 | bool ok = SendBytes(genBuf, csize); |
| uci1 | 2:e67f7c158087 | 165 | |
| uci1 | 2:e67f7c158087 | 166 | // size of event sent over afar |
| uci1 | 2:e67f7c158087 | 167 | const uint32_t ssize = (repack) ? |
| uci1 | 2:e67f7c158087 | 168 | SnEventFrame::SizeOf(sLoseLSB, sLoseMSB) : |
| uci1 | 2:e67f7c158087 | 169 | esize; |
| uci1 | 2:e67f7c158087 | 170 | |
| uci1 | 2:e67f7c158087 | 171 | for (uint32_t i=0; i<nevts; i++) { |
| uci1 | 2:e67f7c158087 | 172 | if (repack) { |
| uci1 | 2:e67f7c158087 | 173 | evt.ReadFrom(inf, genBuf, |
| uci1 | 1:e392595b4b76 | 174 | conf.GetWvLoseLSB(), conf.GetWvLoseMSB(), |
| uci1 | 1:e392595b4b76 | 175 | conf.GetWvBaseline()); |
| uci1 | 2:e67f7c158087 | 176 | evt.WriteTo(genBuf, sLoseLSB, sLoseMSB, sWvBase); |
| uci1 | 2:e67f7c158087 | 177 | |
| uci1 | 2:e67f7c158087 | 178 | ok &= SendBytes(SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode, |
| uci1 | 2:e67f7c158087 | 179 | ssize), |
| uci1 | 2:e67f7c158087 | 180 | SnHeaderFrame::SizeOf()); |
| uci1 | 2:e67f7c158087 | 181 | ok &= SendBytes(genBuf, |
| uci1 | 2:e67f7c158087 | 182 | evt.SizeOf(conf.GetWvLoseLSB(), conf.GetWvLoseMSB())); |
| uci1 | 2:e67f7c158087 | 183 | } else { |
| uci1 | 2:e67f7c158087 | 184 | ok &= SendBytes(SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode, |
| uci1 | 2:e67f7c158087 | 185 | ssize), |
| uci1 | 2:e67f7c158087 | 186 | SnHeaderFrame::SizeOf()); |
| uci1 | 2:e67f7c158087 | 187 | ok &= SendBytes(inf, ssize); |
| uci1 | 1:e392595b4b76 | 188 | } |
| uci1 | 1:e392595b4b76 | 189 | } |
| uci1 | 2:e67f7c158087 | 190 | return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
| uci1 | 1:e392595b4b76 | 191 | } |