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
- Committer:
- uci1
- Date:
- 2012-10-30
- Revision:
- 25:57b2627fe756
- Parent:
- 18:55f1581f2ee4
File content as of revision 25:57b2627fe756:
#include "SnCommUsb.h"
#include <stdint.h>
#include "SnStatusFrame.h"
#include "SnConfigFrame.h"
#include "SnEventFrame.h"
#include "SnHeaderFrame.h"
#include "SnSDUtils.h"
#include "Watchdog.h"
//#define DEBUG
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
SN_USB_SERIAL* SnCommUsb::fgCpu = 0;
static const uint8_t __kMaxUChar = ~0;
SnCommWin::ECommWinResult SnCommUsb::SetupPort(SN_USB_SERIAL* cpu) {
fgCpu = cpu;
#ifdef DEBUG
printf("fgCpu=%p\r\n",fgCpu);
#endif
// set up serial-usb port
if (fgCpu!=0) {
fgCpu->baud( CPUBAUD_SN );
fgCpu->format( 8, Serial::None, 1 );
#ifdef USE_MODSERIAL
fgCpu->txBufferFlush();
fgCpu->rxBufferFlush();
#endif
while ( fgCpu->readable() ) {
fgCpu->getc();
}
return kOkNoMsg;
} else {
return kCanNotConnect;
}
}
int SnCommUsb::GetC(char* const d) {
// return number of bytes gotten (1) or -1 on error
// no check that d is not 0
const int c = fgCpu->getc();
if ( (c>-1) && (c<=__kMaxUChar)) {
*d = static_cast<char>(c);
return sizeof(char);
} else {
return -1;
}
}
int SnCommUsb::PutC(char* const d) {
// return number of bytes put (1) or -1 on error
// no check that d is not 0
const int c = fgCpu->putc(*d);
if (c<0) { // putc can return 0 on success
return -1;
} else {
return sizeof(char);
}
}
int SnCommUsb::DoIO(char* const data,
const uint32_t length,
const uint32_t timeout_clock,
USBCheckDataInBuf able,
USBPutGetC fcn) {
// TODO: if B64, must return number of bytes of raw (non encoded) message
int res=0;
uint32_t b=0;
while ( (length>b) ) {
if (IsTimedOut(timeout_clock)) {
break;
}
if ( CALL_MEMBER_FN(*fgCpu, able)() ) { // readable/writable ?
res = CALL_MEMBER_FN(*this, fcn)(data+b);
if (res<sizeof(char)) {
return b; // error
} else {
b += res;
}
} else {
wait_ms(10);
}
Watchdog::kick(); // don't reset; wait until timeout
}
return b;
}
int32_t SnCommUsb::ReceiveAll(char* const buf, const uint32_t mlen,
const uint32_t timeout_clock) {
return DoIO(buf, mlen, timeout_clock, &SN_USB_SERIAL::readable, &SnCommUsb::GetC);
}
int32_t SnCommUsb::SendAll(char* const data, const uint32_t length,
const uint32_t timeout_clock) {
#ifdef DEBUG
printf("calling (PutC)DoIO len=%u, to=%u, time=%u\r\n",
length, timeout_clock, time(0));
#endif
return DoIO(data, length, timeout_clock, &SN_USB_SERIAL::writeable, &SnCommUsb::PutC);
}
SnCommWin::ECommWinResult SnCommUsb::OpenWindow(const uint32_t timeout,
const bool sendStatus,
const SnConfigFrame& conf,
const SnEventFrame& evt,
const SnPowerFrame& pow,
const uint16_t seq,
const float thmrate,
const float evtrate,
char* const genBuf) {
#ifdef DEBUG
printf("SnCommUsb::OpenWindow\r\n");
#endif
SnCommWin::ECommWinResult ret =
Connect(timeout) ? SnCommWin::kConnected : SnCommWin::kCanNotConnect;
if (sendStatus) {
#ifdef DEBUG
printf("SnCommUsb calling SendStatus\r\n");
#endif
ret = SendStatus(conf, evt, pow, seq, thmrate, evtrate, genBuf, timeout);
}
return ret;
}