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:
- 1:e392595b4b76
- Parent:
- 0:664899e0b988
- Child:
- 2:e67f7c158087
--- a/SnCommUsb.cpp Sat Jun 30 02:03:51 2012 +0000
+++ b/SnCommUsb.cpp Fri Jul 20 19:04:02 2012 +0000
@@ -1,143 +1,166 @@
-#include "SnCommUsb.h"
-
-#include "Timer.h"
-
-#include "SnStatusFrame.h"
-#include "SnConfigFrame.h"
-#include "SnEventFrame.h"
-
-MODSERIAL* SnCommUsb::fgCpu = 0;
-
-SnCommWin::ECommWinResult SnCommUsb::SetupPort(MODSERIAL& cpu) {
- fgCpu = &cpu;
-
- // set up serial-usb port
- fgCpu->baud( 230400 );
- fgCpu->format( 8, Serial::None, 1 );
- fgCpu->txBufferFlush();
- fgCpu->rxBufferFlush();
- return kOkNoMsg;
-}
-
-SnCommWin::ECommWinResult SnCommUsb::OpenWindow(Timer& timer,
- const uint32_t timeout,
- const bool sendStatus,
- const SnConfigFrame& conf,
- const SnEventFrame& evt,
- char* const evtBuf,
- char* const statBuf) {
- // usb port always connected (or never)
- SnCommWin::ECommWinResult ret = SnCommWin::kConnected;
-
- if (sendStatus) {
- ret = SendStatus(conf, evt, evtBuf, statBuf);
- }
-
- return ret;
-}
-
-SnCommWin::ECommWinResult SnCommUsb::GetConfig(SnConfigFrame& conf,
- Timer& timer,
- const uint32_t timeOut,
- char* const confBuf) {
- // confBuf not used by usb. bytes read directly from serial port
-
- SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
-
- bool hasData = fgCpu->readable();
-
- while ( (hasData==false) && (timer.read() < timeOut) ) {
- wait_ms(250);
-
- hasData = fgCpu->readable();
- }
-
- if( hasData ) {
- conf.ReadFrom(*fgCpu);
- res = SnCommWin::kOkWithMsg;
- } else {
- res = SnCommWin::kOkNoMsg;
- }
-
- return res;
-}
-
-SnCommWin::ECommWinResult SnCommUsb::SendStatus(const SnConfigFrame& conf,
- const SnEventFrame& evt,
- char* const evtBuf,
- char* const statBuf) {
- // statBuf not used. status written directly to serial port
- return SnStatusFrame::WriteTo(*fgCpu, SnConfigFrame::kUSB, conf, evt, evtBuf);
-}
-
-SnCommWin::ECommWinResult SnCommUsb::SendData(FILE* inf) {
- int c;
- do {
- c = getc(inf);
- fgCpu->putc(c);
- } while (c!=EOF);
- return SnCommWin::kOkMsgSent;
-}
-
-SnCommWin::ECommWinResult SnCommUsb::SendConfAndEvents(FILE* inf,
- const SnConfigFrame& curConf,
- SnEventFrame& evt,
- char* const evtBuf,
- char* const confBuf,
- 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?
- SnConfigFrame conf;
- conf.ReadFrom(inf);
- conf.WriteTo(*fgCpu);
-
- uint8_t sLoseLSB=0, sLoseMSB=0;
- uint16_t sWvBase=0;
- conf.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
- fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET);
- if (repack) {
- // repack ==> send event by event
- // size of event sent over afar
- const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB);
- char* b;
- register uint32_t i;
- for (i=0; (i<nevts) && (ferror(inf)==0) && (feof(inf)==0); i++) {
- evt.ReadFrom(inf, evtBuf,
- conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
- conf.GetWvBaseline());
- evt.WriteTo(evtBuf, sLoseLSB, sLoseMSB, sWvBase);
- b = evtBuf;
- for (uint32_t j=0; j<ssize; j++, b++) {
- fgCpu->putc( *b );
- }
- }
- if (i!=nevts) {
- return SnCommWin::kFailPartSent;
- }
- } else {
- // no repacking ==> just send the bytes from the file
- const uint32_t nbytes = nevts*esize;
- register uint32_t i;
- for (i=0; i<nbytes && (ferror(inf)==0) && (feof(inf)==0); i++) {
- fgCpu->putc( fgetc(inf) );
- }
- if (i!=nbytes) {
- return SnCommWin::kFailPartSent;
- }
- }
-
- return SnCommWin::kOkMsgSent;
-}
+#include "SnCommUsb.h"
+
+#include "Timer.h"
+
+#include "SnStatusFrame.h"
+#include "SnConfigFrame.h"
+#include "SnEventFrame.h"
+
+MODSERIAL* SnCommUsb::fgCpu = 0;
+
+void __Recv(MODSERIAL_IRQ_INFO* s) {
+ static DigitalOut led4(LED4);
+ static DigitalOut led3(LED3);
+ for (uint8_t i=0; i<20; i++) {
+ led4=!led4;
+ led3=!led3;
+ wait(0.1);
+ }
+}
+
+SnCommWin::ECommWinResult SnCommUsb::SetupPort(MODSERIAL& cpu) {
+ fgCpu = &cpu;
+
+ // set up serial-usb port
+ fgCpu->baud( 230400 );
+ fgCpu->format( 8, Serial::None, 1 );
+ fgCpu->txBufferFlush();
+ fgCpu->rxBufferFlush();
+ //fgCpu->attach(&__Recv, MODSERIAL::RxIrq);
+ return kOkNoMsg;
+}
+
+
+SnCommWin::ECommWinResult SnCommUsb::OpenWindow(const uint32_t timeout,
+ const bool sendStatus,
+ const SnConfigFrame& conf,
+ const SnEventFrame& evt,
+ char* const evtBuf,
+ char* const statBuf) {
+ // usb port always connected (or never)
+ SnCommWin::ECommWinResult ret = SnCommWin::kConnected;
+
+ if (sendStatus) {
+ ret = SendStatus(conf, evt, evtBuf, statBuf);
+ }
+
+ return ret;
+}
+
+SnCommWin::ECommWinResult SnCommUsb::GetConfig(SnConfigFrame& conf,
+ const uint32_t timeOut,
+ char* const confBuf) {
+ // confBuf not used by usb. bytes read directly from serial port
+
+ SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
+
+ bool hasData = fgCpu->readable()==1;
+
+ DigitalOut led1(LED1);
+ DigitalOut led3(LED3);
+ DigitalOut led4(LED4);
+
+ while ( (hasData==false) && (time(0) < timeOut) ) {
+ wait_ms(10);
+
+ hasData = fgCpu->readable()==1;
+
+ led1=1; wait(0.5);
+ led1=0; wait(0.5);
+ }
+
+ if( hasData ) {
+ led3=1; wait(1);
+ conf.ReadFrom(*fgCpu);
+ res = SnCommWin::kOkWithMsg;
+ } else {
+ led4=1; wait(1);
+ res = SnCommWin::kOkNoMsg;
+ }
+
+ led1=0;
+ led3=0;
+ led4=0;
+
+ return res;
+}
+
+SnCommWin::ECommWinResult SnCommUsb::SendStatus(const SnConfigFrame& conf,
+ const SnEventFrame& evt,
+ char* const evtBuf,
+ char* const statBuf) {
+ // statBuf not used. status written directly to serial port
+ return SnStatusFrame::WriteTo(*fgCpu, SnConfigFrame::kUSB, conf, evt, evtBuf);
+}
+
+SnCommWin::ECommWinResult SnCommUsb::SendData(FILE* inf) {
+ int c;
+ do {
+ c = getc(inf);
+ fgCpu->putc(c);
+ } while (c!=EOF);
+ return SnCommWin::kOkMsgSent;
+}
+
+SnCommWin::ECommWinResult SnCommUsb::SendConfAndEvents(FILE* inf,
+ const SnConfigFrame& curConf,
+ SnEventFrame& evt,
+ char* const evtBuf,
+ char* const confBuf,
+ 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?
+ SnConfigFrame conf;
+ conf.ReadFrom(inf);
+ conf.WriteTo(*fgCpu);
+
+ uint8_t sLoseLSB=0, sLoseMSB=0;
+ uint16_t sWvBase=0;
+ conf.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
+ fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET);
+ if (repack) {
+ // repack ==> send event by event
+ // size of event sent over afar
+ const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB);
+ char* b;
+ register uint32_t i;
+ for (i=0; (i<nevts) && (ferror(inf)==0) && (feof(inf)==0); i++) {
+ evt.ReadFrom(inf, evtBuf,
+ conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
+ conf.GetWvBaseline());
+ evt.WriteTo(evtBuf, sLoseLSB, sLoseMSB, sWvBase);
+ b = evtBuf;
+ for (uint32_t j=0; j<ssize; j++, b++) {
+ fgCpu->putc( *b );
+ }
+ }
+ if (i!=nevts) {
+ return SnCommWin::kFailPartSent;
+ }
+ } else {
+ // no repacking ==> just send the bytes from the file
+ const uint32_t nbytes = nevts*esize;
+ register uint32_t i;
+ for (i=0; i<nbytes && (ferror(inf)==0) && (feof(inf)==0); i++) {
+ fgCpu->putc( fgetc(inf) );
+ }
+ if (i!=nbytes) {
+ return SnCommWin::kFailPartSent;
+ }
+ }
+
+ return SnCommWin::kOkMsgSent;
+}