Arianna station communication peripherals.
Dependents: AutonomousDAQ AutonomousDAQ
SnCommUsb.h@4:8328c2972290, 2013-10-05 (annotated)
- Committer:
- uci1
- Date:
- Sat Oct 05 04:39:19 2013 +0000
- Revision:
- 4:8328c2972290
- Parent:
- 3:a7f72492f19e
- Child:
- 5:2ee6cbb948c0
set callback in NewSocket, no wait after fEth setup, fEthSetup false in CloseConn, opt use of rtos wait, CPU baud rate to 115200, remove input of 0 returning false from IsTimedOut, set sys time tracks prev and curr times, no type punting in sbd stime
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 0:26c9189e5924 | 1 | #ifndef SN_SnCommUsb |
uci1 | 0:26c9189e5924 | 2 | #define SN_SnCommUsb |
uci1 | 0:26c9189e5924 | 3 | |
uci1 | 0:26c9189e5924 | 4 | #include "SnCommPeripheral.h" |
uci1 | 0:26c9189e5924 | 5 | |
uci1 | 0:26c9189e5924 | 6 | #ifdef ENABLE_USB |
uci1 | 0:26c9189e5924 | 7 | |
uci1 | 0:26c9189e5924 | 8 | |
uci1 | 0:26c9189e5924 | 9 | #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember)) |
uci1 | 0:26c9189e5924 | 10 | |
uci1 | 0:26c9189e5924 | 11 | #ifdef USE_MODSERIAL |
uci1 | 0:26c9189e5924 | 12 | namespace AjK { |
uci1 | 0:26c9189e5924 | 13 | class MODSERIAL; |
uci1 | 0:26c9189e5924 | 14 | }; |
uci1 | 0:26c9189e5924 | 15 | #define SN_USB_SERIAL AjK::MODSERIAL |
uci1 | 0:26c9189e5924 | 16 | #else |
uci1 | 0:26c9189e5924 | 17 | #define SN_USB_SERIAL Serial |
uci1 | 0:26c9189e5924 | 18 | #endif |
uci1 | 0:26c9189e5924 | 19 | |
uci1 | 0:26c9189e5924 | 20 | class SnCommUsb : public SnCommPeripheral { |
uci1 | 0:26c9189e5924 | 21 | public: |
uci1 | 0:26c9189e5924 | 22 | typedef int (SN_USB_SERIAL::*USBCheckDataInBuf)(); |
uci1 | 0:26c9189e5924 | 23 | //typedef int (SnCommUsb::*USBPutGetC)(char* const); |
uci1 | 0:26c9189e5924 | 24 | |
uci1 | 0:26c9189e5924 | 25 | private: |
uci1 | 0:26c9189e5924 | 26 | static SN_USB_SERIAL* fgCpu; |
uci1 | 0:26c9189e5924 | 27 | |
uci1 | 0:26c9189e5924 | 28 | int GetC(char* const d); |
uci1 | 0:26c9189e5924 | 29 | int PutC(const char* const d); |
uci1 | 0:26c9189e5924 | 30 | |
uci1 | 0:26c9189e5924 | 31 | protected: |
uci1 | 0:26c9189e5924 | 32 | template<class DATA, class SENDRECV> |
uci1 | 0:26c9189e5924 | 33 | int DoIO(DATA data, |
uci1 | 0:26c9189e5924 | 34 | const uint32_t length, |
uci1 | 0:26c9189e5924 | 35 | const uint32_t timeout_clock, |
uci1 | 0:26c9189e5924 | 36 | USBCheckDataInBuf able, |
uci1 | 0:26c9189e5924 | 37 | SENDRECV fcn) { |
uci1 | 0:26c9189e5924 | 38 | // TODO: if B64, must return number of bytes of raw (non encoded) message |
uci1 | 0:26c9189e5924 | 39 | int res=0; |
uci1 | 0:26c9189e5924 | 40 | uint32_t b=0; |
uci1 | 0:26c9189e5924 | 41 | while ( (length>b) ) { |
uci1 | 0:26c9189e5924 | 42 | if (IsTimedOut(timeout_clock)) { |
uci1 | 0:26c9189e5924 | 43 | break; |
uci1 | 0:26c9189e5924 | 44 | } |
uci1 | 0:26c9189e5924 | 45 | if ( CALL_MEMBER_FN(*fgCpu, able)() ) { // readable/writable ? |
uci1 | 0:26c9189e5924 | 46 | res = CALL_MEMBER_FN(*this, fcn)(data+b); |
uci1 | 0:26c9189e5924 | 47 | if (res<sizeof(char)) { |
uci1 | 0:26c9189e5924 | 48 | return b; // error |
uci1 | 0:26c9189e5924 | 49 | } else { |
uci1 | 0:26c9189e5924 | 50 | b += res; |
uci1 | 0:26c9189e5924 | 51 | } |
uci1 | 0:26c9189e5924 | 52 | } else { |
uci1 | 4:8328c2972290 | 53 | #ifdef USE_RTOS |
uci1 | 4:8328c2972290 | 54 | Thread::wait(10); |
uci1 | 4:8328c2972290 | 55 | #else |
uci1 | 0:26c9189e5924 | 56 | wait_ms(10); |
uci1 | 4:8328c2972290 | 57 | #endif |
uci1 | 0:26c9189e5924 | 58 | } |
uci1 | 0:26c9189e5924 | 59 | } |
uci1 | 0:26c9189e5924 | 60 | |
uci1 | 0:26c9189e5924 | 61 | return b; |
uci1 | 0:26c9189e5924 | 62 | } |
uci1 | 0:26c9189e5924 | 63 | /* |
uci1 | 0:26c9189e5924 | 64 | int DoIO(char* const data, |
uci1 | 0:26c9189e5924 | 65 | const uint32_t length, |
uci1 | 0:26c9189e5924 | 66 | const uint32_t timeout_clock, |
uci1 | 0:26c9189e5924 | 67 | USBCheckDataInBuf able, |
uci1 | 0:26c9189e5924 | 68 | USBPutGetC fcn); |
uci1 | 0:26c9189e5924 | 69 | */ |
uci1 | 0:26c9189e5924 | 70 | virtual int32_t ReceiveAll(char* const buf, const uint32_t mlen, |
uci1 | 0:26c9189e5924 | 71 | const uint32_t timeout_clock); |
uci1 | 0:26c9189e5924 | 72 | virtual int32_t SendAll(const char* const data, const uint32_t length, |
uci1 | 0:26c9189e5924 | 73 | const uint32_t timeout_clock); |
uci1 | 3:a7f72492f19e | 74 | virtual int32_t FinishSending(const uint32_t) { |
uci1 | 3:a7f72492f19e | 75 | // all data sent in SendAll; 0 bytes sent here |
uci1 | 3:a7f72492f19e | 76 | return 0; |
uci1 | 3:a7f72492f19e | 77 | } |
uci1 | 0:26c9189e5924 | 78 | |
uci1 | 0:26c9189e5924 | 79 | |
uci1 | 0:26c9189e5924 | 80 | public: |
uci1 | 0:26c9189e5924 | 81 | SnCommUsb(SN_USB_SERIAL* c=0) { if (c!=0) { SetupPort(c); } } |
uci1 | 0:26c9189e5924 | 82 | |
uci1 | 0:26c9189e5924 | 83 | virtual ~SnCommUsb() {} |
uci1 | 0:26c9189e5924 | 84 | |
uci1 | 0:26c9189e5924 | 85 | static |
uci1 | 0:26c9189e5924 | 86 | bool IsPortSetup() { return fgCpu!=0; } |
uci1 | 0:26c9189e5924 | 87 | |
uci1 | 0:26c9189e5924 | 88 | static |
uci1 | 0:26c9189e5924 | 89 | bool SetupPort(SN_USB_SERIAL* cpu); |
uci1 | 0:26c9189e5924 | 90 | |
uci1 | 0:26c9189e5924 | 91 | virtual bool Connect(const uint32_t timeout) |
uci1 | 0:26c9189e5924 | 92 | { return IsPortSetup(); } |
uci1 | 0:26c9189e5924 | 93 | |
uci1 | 0:26c9189e5924 | 94 | virtual bool CloseConn(const uint32_t) { return true; } |
uci1 | 3:a7f72492f19e | 95 | virtual bool PowerDown(const uint32_t) { return true; } |
uci1 | 0:26c9189e5924 | 96 | |
uci1 | 0:26c9189e5924 | 97 | |
uci1 | 0:26c9189e5924 | 98 | |
uci1 | 0:26c9189e5924 | 99 | }; |
uci1 | 0:26c9189e5924 | 100 | |
uci1 | 0:26c9189e5924 | 101 | #endif // ENABLE_USB |
uci1 | 0:26c9189e5924 | 102 | |
uci1 | 0:26c9189e5924 | 103 | |
uci1 | 0:26c9189e5924 | 104 | #endif // SN_SnCommUsb |