S K UCI / Mbed 2 deprecated AutonomousDAQ

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Sat Sep 29 04:54:15 2012 +0000
Revision:
18:55f1581f2ee4
Parent:
16:744ce85aede2
Child:
25:57b2627fe756
This version uses USB communication only. Changed forced trigger period to be a float, so subsecond trigs are possible. Changed from EthernetInterface to NetServicesMin. This allows slow (2KBps) transfer over TCP, but at least it's robust.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 1:e392595b4b76 1 #include "SnCommUsb.h"
uci1 13:7a1fb885a8e4 2
uci1 13:7a1fb885a8e4 3 #include <stdint.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 13:7a1fb885a8e4 11 #include "Watchdog.h"
uci1 13:7a1fb885a8e4 12
uci1 13:7a1fb885a8e4 13 #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
uci1 1:e392595b4b76 14
uci1 16:744ce85aede2 15 SN_USB_SERIAL* SnCommUsb::fgCpu = 0;
uci1 13:7a1fb885a8e4 16
uci1 13:7a1fb885a8e4 17 static const uint8_t __kMaxUChar = ~0;
uci1 13:7a1fb885a8e4 18
uci1 16:744ce85aede2 19 SnCommWin::ECommWinResult SnCommUsb::SetupPort(SN_USB_SERIAL* cpu) {
uci1 16:744ce85aede2 20 fgCpu = cpu;
uci1 1:e392595b4b76 21
uci1 1:e392595b4b76 22 // set up serial-usb port
uci1 16:744ce85aede2 23 if (fgCpu!=0) {
uci1 16:744ce85aede2 24 //fgCpu->baud( 230400 );
uci1 16:744ce85aede2 25 fgCpu->baud( 115200 );
uci1 16:744ce85aede2 26 fgCpu->format( 8, Serial::None, 1 );
uci1 16:744ce85aede2 27 #ifdef USE_MODSERIAL
uci1 16:744ce85aede2 28 fgCpu->txBufferFlush();
uci1 16:744ce85aede2 29 fgCpu->rxBufferFlush();
uci1 13:7a1fb885a8e4 30 #endif
uci1 16:744ce85aede2 31 while ( fgCpu->readable() ) {
uci1 16:744ce85aede2 32 fgCpu->getc();
uci1 16:744ce85aede2 33 }
uci1 16:744ce85aede2 34 return kOkNoMsg;
uci1 16:744ce85aede2 35 } else {
uci1 16:744ce85aede2 36 return kCanNotConnect;
uci1 16:744ce85aede2 37 }
uci1 1:e392595b4b76 38 }
uci1 1:e392595b4b76 39
uci1 13:7a1fb885a8e4 40 int SnCommUsb::GetC(char* const d) {
uci1 13:7a1fb885a8e4 41 // return number of bytes gotten (1) or -1 on error
uci1 13:7a1fb885a8e4 42 // no check that d is not 0
uci1 13:7a1fb885a8e4 43 const int c = fgCpu->getc();
uci1 13:7a1fb885a8e4 44 if ( (c>-1) && (c<=__kMaxUChar)) {
uci1 13:7a1fb885a8e4 45 *d = static_cast<char>(c);
uci1 13:7a1fb885a8e4 46 return sizeof(char);
uci1 13:7a1fb885a8e4 47 } else {
uci1 13:7a1fb885a8e4 48 return -1;
uci1 13:7a1fb885a8e4 49 }
uci1 13:7a1fb885a8e4 50 }
uci1 13:7a1fb885a8e4 51
uci1 13:7a1fb885a8e4 52 int SnCommUsb::PutC(char* const d) {
uci1 13:7a1fb885a8e4 53 // return number of bytes put (1) or -1 on error
uci1 13:7a1fb885a8e4 54 // no check that d is not 0
uci1 13:7a1fb885a8e4 55 const int c = fgCpu->putc(*d);
uci1 13:7a1fb885a8e4 56 if (c<0) { // putc can return 0 on success
uci1 13:7a1fb885a8e4 57 return -1;
uci1 13:7a1fb885a8e4 58 } else {
uci1 13:7a1fb885a8e4 59 return sizeof(char);
uci1 13:7a1fb885a8e4 60 }
uci1 13:7a1fb885a8e4 61 }
uci1 13:7a1fb885a8e4 62
uci1 13:7a1fb885a8e4 63 int SnCommUsb::DoIO(char* const data,
uci1 13:7a1fb885a8e4 64 const uint32_t length,
uci1 13:7a1fb885a8e4 65 const uint32_t timeout_clock,
uci1 13:7a1fb885a8e4 66 USBCheckDataInBuf able,
uci1 13:7a1fb885a8e4 67 USBPutGetC fcn) {
uci1 13:7a1fb885a8e4 68 // TODO: if B64, must return number of bytes of raw (non encoded) message
uci1 13:7a1fb885a8e4 69 int res=0;
uci1 13:7a1fb885a8e4 70 uint32_t b=0;
uci1 13:7a1fb885a8e4 71 while ( (length>b) ) {
uci1 13:7a1fb885a8e4 72 if (IsTimedOut(timeout_clock)) {
uci1 13:7a1fb885a8e4 73 break;
uci1 13:7a1fb885a8e4 74 }
uci1 13:7a1fb885a8e4 75 if ( CALL_MEMBER_FN(*fgCpu, able)() ) { // readable/writable ?
uci1 13:7a1fb885a8e4 76 res = CALL_MEMBER_FN(*this, fcn)(data+b);
uci1 13:7a1fb885a8e4 77 if (res<sizeof(char)) {
uci1 18:55f1581f2ee4 78 return b; // error
uci1 13:7a1fb885a8e4 79 } else {
uci1 13:7a1fb885a8e4 80 b += res;
uci1 13:7a1fb885a8e4 81 }
uci1 13:7a1fb885a8e4 82 } else {
uci1 13:7a1fb885a8e4 83 wait_ms(10);
uci1 13:7a1fb885a8e4 84 }
uci1 13:7a1fb885a8e4 85 Watchdog::kick(); // don't reset; wait until timeout
uci1 13:7a1fb885a8e4 86 }
uci1 18:55f1581f2ee4 87
uci1 18:55f1581f2ee4 88 return b;
uci1 13:7a1fb885a8e4 89 }
uci1 13:7a1fb885a8e4 90
uci1 13:7a1fb885a8e4 91 int32_t SnCommUsb::ReceiveAll(char* const buf, const uint32_t mlen,
uci1 13:7a1fb885a8e4 92 const uint32_t timeout_clock) {
uci1 16:744ce85aede2 93 return DoIO(buf, mlen, timeout_clock, &SN_USB_SERIAL::readable, &SnCommUsb::GetC);
uci1 13:7a1fb885a8e4 94 }
uci1 13:7a1fb885a8e4 95
uci1 13:7a1fb885a8e4 96 int32_t SnCommUsb::SendAll(char* const data, const uint32_t length,
uci1 13:7a1fb885a8e4 97 const uint32_t timeout_clock) {
uci1 16:744ce85aede2 98 return DoIO(data, length, timeout_clock, &SN_USB_SERIAL::writeable, &SnCommUsb::PutC);
uci1 13:7a1fb885a8e4 99 }
uci1 13:7a1fb885a8e4 100
uci1 1:e392595b4b76 101
uci1 1:e392595b4b76 102 SnCommWin::ECommWinResult SnCommUsb::OpenWindow(const uint32_t timeout,
uci1 1:e392595b4b76 103 const bool sendStatus,
uci1 1:e392595b4b76 104 const SnConfigFrame& conf,
uci1 1:e392595b4b76 105 const SnEventFrame& evt,
uci1 13:7a1fb885a8e4 106 const SnPowerFrame& pow,
uci1 13:7a1fb885a8e4 107 const uint16_t seq,
uci1 13:7a1fb885a8e4 108 const float thmrate,
uci1 13:7a1fb885a8e4 109 const float evtrate,
uci1 2:e67f7c158087 110 char* const genBuf) {
uci1 13:7a1fb885a8e4 111 #ifdef DEBUG
uci1 13:7a1fb885a8e4 112 printf("SnCommUsb::OpenWindow\r\n");
uci1 13:7a1fb885a8e4 113 #endif
uci1 13:7a1fb885a8e4 114
uci1 13:7a1fb885a8e4 115 SnCommWin::ECommWinResult ret =
uci1 13:7a1fb885a8e4 116 Connect(timeout) ? SnCommWin::kConnected : SnCommWin::kCanNotConnect;
uci1 1:e392595b4b76 117
uci1 1:e392595b4b76 118 if (sendStatus) {
uci1 13:7a1fb885a8e4 119 ret = SendStatus(conf, evt, pow, seq, thmrate, evtrate, genBuf, timeout);
uci1 1:e392595b4b76 120 }
uci1 1:e392595b4b76 121
uci1 1:e392595b4b76 122 return ret;
uci1 1:e392595b4b76 123 }