S K UCI / Mbed 2 deprecated AutonomousDAQ

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Aug 21 02:18:27 2012 +0000
Revision:
13:7a1fb885a8e4
Parent:
6:6f002d202f59
Child:
16:744ce85aede2
Put resetting of event/power number counters to directly after making of file. Move startup config to inside OpenCommWin (and force it on startup). This should ensure that even numbers always correspond to sequence numbers.

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 13:7a1fb885a8e4 15 AjK::MODSERIAL* SnCommUsb::fgCpu = 0;
uci1 13:7a1fb885a8e4 16
uci1 13:7a1fb885a8e4 17 static const uint8_t __kMaxUChar = ~0;
uci1 13:7a1fb885a8e4 18
uci1 13:7a1fb885a8e4 19 #ifdef LED_ON_RECV
uci1 1:e392595b4b76 20 void __Recv(MODSERIAL_IRQ_INFO* s) {
uci1 1:e392595b4b76 21 static DigitalOut led4(LED4);
uci1 1:e392595b4b76 22 static DigitalOut led3(LED3);
uci1 13:7a1fb885a8e4 23 for (uint8_t i=0; i<10; i++) {
uci1 1:e392595b4b76 24 led4=!led4;
uci1 1:e392595b4b76 25 led3=!led3;
uci1 1:e392595b4b76 26 wait(0.1);
uci1 1:e392595b4b76 27 }
uci1 1:e392595b4b76 28 }
uci1 13:7a1fb885a8e4 29 #endif
uci1 1:e392595b4b76 30
uci1 13:7a1fb885a8e4 31 SnCommWin::ECommWinResult SnCommUsb::SetupPort(AjK::MODSERIAL& cpu) {
uci1 1:e392595b4b76 32 fgCpu = &cpu;
uci1 1:e392595b4b76 33
uci1 1:e392595b4b76 34 // set up serial-usb port
uci1 1:e392595b4b76 35 fgCpu->baud( 230400 );
uci1 1:e392595b4b76 36 fgCpu->format( 8, Serial::None, 1 );
uci1 1:e392595b4b76 37 fgCpu->txBufferFlush();
uci1 1:e392595b4b76 38 fgCpu->rxBufferFlush();
uci1 13:7a1fb885a8e4 39 #ifdef LED_ON_RECV
uci1 13:7a1fb885a8e4 40 fgCpu->attach(&__Recv, MODSERIAL::RxIrq);
uci1 13:7a1fb885a8e4 41 #endif
uci1 1:e392595b4b76 42 return kOkNoMsg;
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 13:7a1fb885a8e4 83 return res; // 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 13:7a1fb885a8e4 92 return res; // timeout
uci1 13:7a1fb885a8e4 93 }
uci1 13:7a1fb885a8e4 94
uci1 13:7a1fb885a8e4 95 int32_t SnCommUsb::ReceiveAll(char* const buf, const uint32_t mlen,
uci1 13:7a1fb885a8e4 96 const uint32_t timeout_clock) {
uci1 13:7a1fb885a8e4 97 return DoIO(buf, mlen, timeout_clock, &AjK::MODSERIAL::readable, &SnCommUsb::GetC);
uci1 13:7a1fb885a8e4 98 }
uci1 13:7a1fb885a8e4 99
uci1 13:7a1fb885a8e4 100 int32_t SnCommUsb::SendAll(char* const data, const uint32_t length,
uci1 13:7a1fb885a8e4 101 const uint32_t timeout_clock) {
uci1 13:7a1fb885a8e4 102 return DoIO(data, length, timeout_clock, &AjK::MODSERIAL::writeable, &SnCommUsb::PutC);
uci1 13:7a1fb885a8e4 103 }
uci1 13:7a1fb885a8e4 104
uci1 1:e392595b4b76 105
uci1 1:e392595b4b76 106 SnCommWin::ECommWinResult SnCommUsb::OpenWindow(const uint32_t timeout,
uci1 1:e392595b4b76 107 const bool sendStatus,
uci1 1:e392595b4b76 108 const SnConfigFrame& conf,
uci1 1:e392595b4b76 109 const SnEventFrame& evt,
uci1 13:7a1fb885a8e4 110 const SnPowerFrame& pow,
uci1 13:7a1fb885a8e4 111 const uint16_t seq,
uci1 13:7a1fb885a8e4 112 const float thmrate,
uci1 13:7a1fb885a8e4 113 const float evtrate,
uci1 2:e67f7c158087 114 char* const genBuf) {
uci1 13:7a1fb885a8e4 115 #ifdef DEBUG
uci1 13:7a1fb885a8e4 116 printf("SnCommUsb::OpenWindow\r\n");
uci1 13:7a1fb885a8e4 117 #endif
uci1 13:7a1fb885a8e4 118
uci1 13:7a1fb885a8e4 119 SnCommWin::ECommWinResult ret =
uci1 13:7a1fb885a8e4 120 Connect(timeout) ? SnCommWin::kConnected : SnCommWin::kCanNotConnect;
uci1 1:e392595b4b76 121
uci1 1:e392595b4b76 122 if (sendStatus) {
uci1 13:7a1fb885a8e4 123 ret = SendStatus(conf, evt, pow, seq, thmrate, evtrate, genBuf, timeout);
uci1 1:e392595b4b76 124 }
uci1 1:e392595b4b76 125
uci1 1:e392595b4b76 126 return ret;
uci1 1:e392595b4b76 127 }