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
SnCommUsb.cpp@16:744ce85aede2, 2012-09-12 (annotated)
- Committer:
- uci1
- Date:
- Wed Sep 12 04:47:22 2012 +0000
- Revision:
- 16:744ce85aede2
- Parent:
- 13:7a1fb885a8e4
- Child:
- 18:55f1581f2ee4
SBD comm seems to be working. USB comm seems to be working (at 115200 baud). AFAR comm seems to be working. This version is set for USB communication and has zero text output.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 13:7a1fb885a8e4 | 78 | return res; // 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 | 16:744ce85aede2 | 87 | return b; // timeout |
| uci1 | 13:7a1fb885a8e4 | 88 | } |
| uci1 | 13:7a1fb885a8e4 | 89 | |
| uci1 | 13:7a1fb885a8e4 | 90 | int32_t SnCommUsb::ReceiveAll(char* const buf, const uint32_t mlen, |
| uci1 | 13:7a1fb885a8e4 | 91 | const uint32_t timeout_clock) { |
| uci1 | 16:744ce85aede2 | 92 | return DoIO(buf, mlen, timeout_clock, &SN_USB_SERIAL::readable, &SnCommUsb::GetC); |
| uci1 | 13:7a1fb885a8e4 | 93 | } |
| uci1 | 13:7a1fb885a8e4 | 94 | |
| uci1 | 13:7a1fb885a8e4 | 95 | int32_t SnCommUsb::SendAll(char* const data, const uint32_t length, |
| uci1 | 13:7a1fb885a8e4 | 96 | const uint32_t timeout_clock) { |
| uci1 | 16:744ce85aede2 | 97 | return DoIO(data, length, timeout_clock, &SN_USB_SERIAL::writeable, &SnCommUsb::PutC); |
| uci1 | 13:7a1fb885a8e4 | 98 | } |
| uci1 | 13:7a1fb885a8e4 | 99 | |
| uci1 | 1:e392595b4b76 | 100 | |
| uci1 | 1:e392595b4b76 | 101 | SnCommWin::ECommWinResult SnCommUsb::OpenWindow(const uint32_t timeout, |
| uci1 | 1:e392595b4b76 | 102 | const bool sendStatus, |
| uci1 | 1:e392595b4b76 | 103 | const SnConfigFrame& conf, |
| uci1 | 1:e392595b4b76 | 104 | const SnEventFrame& evt, |
| uci1 | 13:7a1fb885a8e4 | 105 | const SnPowerFrame& pow, |
| uci1 | 13:7a1fb885a8e4 | 106 | const uint16_t seq, |
| uci1 | 13:7a1fb885a8e4 | 107 | const float thmrate, |
| uci1 | 13:7a1fb885a8e4 | 108 | const float evtrate, |
| uci1 | 2:e67f7c158087 | 109 | char* const genBuf) { |
| uci1 | 13:7a1fb885a8e4 | 110 | #ifdef DEBUG |
| uci1 | 13:7a1fb885a8e4 | 111 | printf("SnCommUsb::OpenWindow\r\n"); |
| uci1 | 13:7a1fb885a8e4 | 112 | #endif |
| uci1 | 13:7a1fb885a8e4 | 113 | |
| uci1 | 13:7a1fb885a8e4 | 114 | SnCommWin::ECommWinResult ret = |
| uci1 | 13:7a1fb885a8e4 | 115 | Connect(timeout) ? SnCommWin::kConnected : SnCommWin::kCanNotConnect; |
| uci1 | 1:e392595b4b76 | 116 | |
| uci1 | 1:e392595b4b76 | 117 | if (sendStatus) { |
| uci1 | 13:7a1fb885a8e4 | 118 | ret = SendStatus(conf, evt, pow, seq, thmrate, evtrate, genBuf, timeout); |
| uci1 | 1:e392595b4b76 | 119 | } |
| uci1 | 1:e392595b4b76 | 120 | |
| uci1 | 1:e392595b4b76 | 121 | return ret; |
| uci1 | 1:e392595b4b76 | 122 | } |