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-08-21
- Revision:
- 13:7a1fb885a8e4
- Parent:
- 6:6f002d202f59
- Child:
- 16:744ce85aede2
File content as of revision 13:7a1fb885a8e4:
#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 CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
AjK::MODSERIAL* SnCommUsb::fgCpu = 0;
static const uint8_t __kMaxUChar = ~0;
#ifdef LED_ON_RECV
void __Recv(MODSERIAL_IRQ_INFO* s) {
static DigitalOut led4(LED4);
static DigitalOut led3(LED3);
for (uint8_t i=0; i<10; i++) {
led4=!led4;
led3=!led3;
wait(0.1);
}
}
#endif
SnCommWin::ECommWinResult SnCommUsb::SetupPort(AjK::MODSERIAL& cpu) {
fgCpu = &cpu;
// set up serial-usb port
fgCpu->baud( 230400 );
fgCpu->format( 8, Serial::None, 1 );
fgCpu->txBufferFlush();
fgCpu->rxBufferFlush();
#ifdef LED_ON_RECV
fgCpu->attach(&__Recv, MODSERIAL::RxIrq);
#endif
return kOkNoMsg;
}
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 res; // error
} else {
b += res;
}
} else {
wait_ms(10);
}
Watchdog::kick(); // don't reset; wait until timeout
}
return res; // timeout
}
int32_t SnCommUsb::ReceiveAll(char* const buf, const uint32_t mlen,
const uint32_t timeout_clock) {
return DoIO(buf, mlen, timeout_clock, &AjK::MODSERIAL::readable, &SnCommUsb::GetC);
}
int32_t SnCommUsb::SendAll(char* const data, const uint32_t length,
const uint32_t timeout_clock) {
return DoIO(data, length, timeout_clock, &AjK::MODSERIAL::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) {
ret = SendStatus(conf, evt, pow, seq, thmrate, evtrate, genBuf, timeout);
}
return ret;
}