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
Diff: SnCommUsb.cpp
- Revision:
- 13:7a1fb885a8e4
- Parent:
- 6:6f002d202f59
- Child:
- 16:744ce85aede2
diff -r d472f9811262 -r 7a1fb885a8e4 SnCommUsb.cpp
--- a/SnCommUsb.cpp Sat Aug 18 05:00:32 2012 +0000
+++ b/SnCommUsb.cpp Tue Aug 21 02:18:27 2012 +0000
@@ -1,6 +1,6 @@
#include "SnCommUsb.h"
-/*
-#include "Timer.h"
+
+#include <stdint.h>
#include "SnStatusFrame.h"
#include "SnConfigFrame.h"
@@ -8,19 +8,27 @@
#include "SnHeaderFrame.h"
#include "SnSDUtils.h"
-MODSERIAL* SnCommUsb::fgCpu = 0;
+#include "Watchdog.h"
+
+#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
+AjK::MODSERIAL* SnCommUsb::fgCpu = 0;
+
+static const uint8_t __kMaxUChar = ~0;
+
+#ifdef LED_ON_RECV
void __Recv(MODSERIAL_IRQ_INFO* s) {
static DigitalOut led4(LED4);
static DigitalOut led3(LED3);
- for (uint8_t i=0; i<20; i++) {
+ for (uint8_t i=0; i<10; i++) {
led4=!led4;
led3=!led3;
wait(0.1);
}
}
+#endif
-SnCommWin::ECommWinResult SnCommUsb::SetupPort(MODSERIAL& cpu) {
+SnCommWin::ECommWinResult SnCommUsb::SetupPort(AjK::MODSERIAL& cpu) {
fgCpu = &cpu;
// set up serial-usb port
@@ -28,205 +36,92 @@
fgCpu->format( 8, Serial::None, 1 );
fgCpu->txBufferFlush();
fgCpu->rxBufferFlush();
- //fgCpu->attach(&__Recv, MODSERIAL::RxIrq);
+#ifdef LED_ON_RECV
+ fgCpu->attach(&__Recv, MODSERIAL::RxIrq);
+#endif
return kOkNoMsg;
}
+int SnCommUsb::GetC(char* const d) {
+ // return number of bytes gotten (1) or -1 on error
+ // no check that d is not 0
+ const int c = fgCpu->getc();
+ if ( (c>-1) && (c<=__kMaxUChar)) {
+ *d = static_cast<char>(c);
+ return sizeof(char);
+ } else {
+ return -1;
+ }
+}
+
+int SnCommUsb::PutC(char* const d) {
+ // return number of bytes put (1) or -1 on error
+ // no check that d is not 0
+ const int c = fgCpu->putc(*d);
+ if (c<0) { // putc can return 0 on success
+ return -1;
+ } else {
+ return sizeof(char);
+ }
+}
+
+int SnCommUsb::DoIO(char* const data,
+ const uint32_t length,
+ const uint32_t timeout_clock,
+ USBCheckDataInBuf able,
+ USBPutGetC fcn) {
+ // TODO: if B64, must return number of bytes of raw (non encoded) message
+ int res=0;
+ uint32_t b=0;
+ while ( (length>b) ) {
+ if (IsTimedOut(timeout_clock)) {
+ break;
+ }
+ if ( CALL_MEMBER_FN(*fgCpu, able)() ) { // readable/writable ?
+ res = CALL_MEMBER_FN(*this, fcn)(data+b);
+ if (res<sizeof(char)) {
+ return res; // error
+ } else {
+ b += res;
+ }
+ } else {
+ wait_ms(10);
+ }
+ Watchdog::kick(); // don't reset; wait until timeout
+ }
+ return res; // timeout
+}
+
+int32_t SnCommUsb::ReceiveAll(char* const buf, const uint32_t mlen,
+ const uint32_t timeout_clock) {
+ return DoIO(buf, mlen, timeout_clock, &AjK::MODSERIAL::readable, &SnCommUsb::GetC);
+}
+
+int32_t SnCommUsb::SendAll(char* const data, const uint32_t length,
+ const uint32_t timeout_clock) {
+ return DoIO(data, length, timeout_clock, &AjK::MODSERIAL::writeable, &SnCommUsb::PutC);
+}
+
SnCommWin::ECommWinResult SnCommUsb::OpenWindow(const uint32_t timeout,
const bool sendStatus,
const SnConfigFrame& conf,
const SnEventFrame& evt,
+ const SnPowerFrame& pow,
+ const uint16_t seq,
+ const float thmrate,
+ const float evtrate,
char* const genBuf) {
- // usb port always connected (or never)
- SnCommWin::ECommWinResult ret = SnCommWin::kConnected;
+#ifdef DEBUG
+ printf("SnCommUsb::OpenWindow\r\n");
+#endif
+
+ SnCommWin::ECommWinResult ret =
+ Connect(timeout) ? SnCommWin::kConnected : SnCommWin::kCanNotConnect;
if (sendStatus) {
- ret = SendStatus(conf, evt, genBuf);
+ ret = SendStatus(conf, evt, pow, seq, thmrate, evtrate, genBuf, timeout);
}
return ret;
}
-
-SnCommWin::ECommWinResult SnCommUsb::WaitHandshake(const uint32_t timeout,
- char* const buf,
- const uint32_t bsize) {
- printf("WaitHandshake, to=%u\r\n",timeout);
-
- bool hasData = fgCpu->readable()==1;
-
- while ( (hasData==false) && (time(0) < timeout) ) {
- wait_ms(10);
-
- hasData = fgCpu->readable()==1;
- }
-
- if( hasData ) {
- uint8_t mcode;
- uint32_t mlen;
- SnHeaderFrame::ReadFrom(*fgCpu, mcode, mlen);
- if (mcode==SnHeaderFrame::kHandshakeCode) {
- return SnCommWin::kOkWithMsg;
- } else {
- // TODO: somehow handle unexpected message?
- return SnCommWin::kUnexpectedRec;
- }
- } else {
- return SnCommWin::kOkNoMsg;
- }
-}
-
-SnCommWin::ECommWinResult SnCommUsb::GetConfig(SnConfigFrame& conf,
- const uint32_t timeOut,
- char* const confBuf,
- const uint32_t bsize) {
- // confBuf not used by usb. bytes read directly from serial port
-
- SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
-
- bool hasData = fgCpu->readable()==1;
-
- while ( (hasData==false) && (time(0) < timeOut) ) {
- wait_ms(10);
-
- hasData = fgCpu->readable()==1;
- }
-
- if( hasData ) {
- uint8_t mcode;
- uint32_t mlen;
- SnHeaderFrame::ReadFrom(*fgCpu, mcode, mlen);
- // TODO: do something with the header info?
- conf.ReadFrom(*fgCpu);
- res = SnCommWin::kOkWithMsg;
- } else {
- res = SnCommWin::kOkNoMsg;
- }
-
- return res;
-}
-
-SnCommWin::ECommWinResult SnCommUsb::SendFilename(const char* fn, char* const genBuf) {
- const size_t flen = strlen(fn);
- bool ok = SnHeaderFrame::WriteTo(*fgCpu,
- SnHeaderFrame::kFilenameCode,
- SnHeaderFrame::SizeOf());
- if (ok) {
- ok = SendBytes(fn, flen);
- }
- return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
-}
-
-SnCommWin::ECommWinResult SnCommUsb::SendStatus(const SnConfigFrame& conf,
- const SnEventFrame& evt,
- char* const genBuf) {
- const bool ok = SnHeaderFrame::WriteTo(*fgCpu,
- SnHeaderFrame::kStatusCode, SnStatusFrame::SizeOf(conf));
- if (ok) {
- return SnStatusFrame::WriteTo(*fgCpu, SnConfigFrame::kUSB, conf, evt, genBuf);
- } else {
- return SnCommWin::kFailNoneSent;
- }
-}
-
-SnCommWin::ECommWinResult SnCommUsb::SendData(FILE* inf) {
- const int foff = ftell(inf);
- fseek(inf, 0, SEEK_END);
- const int fsize = ftell(inf) - foff;
- fseek(inf, foff, SEEK_SET);
- if (fsize>0) {
- SnHeaderFrame::WriteTo(*fgCpu,
- SnHeaderFrame::kFileCode, fsize);
- SendBytes(inf, fsize);
- return SnCommWin::kOkMsgSent;
- } else {
- return SnCommWin::kFailNoneSent;
- }
-}
-
-bool SnCommUsb::SendBytes(FILE* f, const uint32_t nbytes) {
- register uint32_t i=0;
- for (i=0; (i<nbytes)
- && (feof(f)==0)
- && (ferror(f)==0); i++) {
- fgCpu->putc( getc(f) );
- }
- return (i==nbytes);
-}
-
-bool SnCommUsb::SendBytes(const char* const buf,
- const uint32_t nbytes) {
- const char* b = buf;
- for (uint32_t j=0; j<nbytes; j++, b++) {
- fgCpu->putc( *b );
- }
- return true;
-}
-
-SnCommWin::ECommWinResult SnCommUsb::SendConfAndEvents(FILE* inf,
- const SnConfigFrame& curConf,
- SnEventFrame& evt,
- char* const genBuf,
- const uint32_t nevts,
- const uint32_t firstEvt) {
- // firstEvt==0 ==> start at beginning
- // nevts==0 ==> NO events! (see SnCommWin::SendData
- // for the fcn to send the full file)
-
- // TODO: check memory for temporary config/event frames?
- uint64_t macadr;
- uint32_t run;
- uint16_t seq, v1, v2;
- SnSDUtils::ReadFileHeader(inf, macadr, run, seq, v1, v2);
- SnConfigFrame conf;
- conf.ReadFrom(inf);
-
- uint8_t sLoseLSB=0, sLoseMSB=0;
- uint16_t sWvBase=0;
- curConf.GetPackParsFor(SnConfigFrame::kUSB, sLoseLSB, sLoseMSB, sWvBase);
- // do we have to unpack & repack events?
- const bool repack = (sLoseLSB != conf.GetWvLoseLSB())
- && (sLoseMSB != conf.GetWvLoseMSB())
- && (sWvBase != conf.GetWvBaseline());
-
- // size of event in file
- const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(),
- conf.GetWvLoseMSB());
- // move up to first event
- const uint32_t csize = conf.SizeOf();
- fseek(inf, csize + (firstEvt*esize), SEEK_SET);
-
- // send the header and conf
- char* b = genBuf;
- SnHeaderFrame::WriteTo(b,
- SnHeaderFrame::kConfAndEvtsCode, csize);
- conf.WriteTo(b);
- bool ok = SendBytes(genBuf, csize);
-
- // size of event sent over afar
- const uint32_t ssize = (repack) ?
- SnEventFrame::SizeOf(sLoseLSB, sLoseMSB) :
- esize;
-
- for (uint32_t i=0; i<nevts; i++) {
- if (repack) {
- evt.ReadFrom(inf, genBuf,
- conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
- conf.GetWvBaseline());
- evt.WriteTo(genBuf, sLoseLSB, sLoseMSB, sWvBase);
-
- ok &= SendBytes(SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode,
- ssize),
- SnHeaderFrame::SizeOf());
- ok &= SendBytes(genBuf,
- evt.SizeOf(conf.GetWvLoseLSB(), conf.GetWvLoseMSB()));
- } else {
- ok &= SendBytes(SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode,
- ssize),
- SnHeaderFrame::SizeOf());
- ok &= SendBytes(inf, ssize);
- }
- }
- return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
-}
-*/
\ No newline at end of file