S K UCI / Mbed 2 deprecated AutonomousDAQ

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Oct 30 05:23:57 2012 +0000
Revision:
25:57b2627fe756
Parent:
18:55f1581f2ee4
AFAR comms. Upped baud to 921600. Store data in run-seq subdirs, max of 100 files per dir, to prevent station grinding to a halt due when 800 files are in one directory. Cache file size count. Stagger tickers. Fixed some gcc warnings.

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