Skip to content

Commit

Permalink
Merge pull request #10 from OSVR/additional-usb-improvements
Browse files Browse the repository at this point in the history
Additional usb improvements
  • Loading branch information
VRguy authored Nov 2, 2016
2 parents ea9b29c + e4b76a7 commit e001bd1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
4 changes: 3 additions & 1 deletion Source code/Embedded/src/Console.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ void dWrite(const char *const Data, uint8_t DebugMask)
Write(Data);
}

static const char CR[] = "\n\r";
/// @todo This corresponds to LF+CR, which is the reverse of the typically-expected order. If it is safe to do so, it
/// should be flipped to the normal order.
static const char CR[] = {0x0A, 0x0D, 0x00};

void WriteLn(const char *const Data)

Expand Down
28 changes: 16 additions & 12 deletions Source code/Embedded/src/SerialStateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ static EEPROM_type EEPROM;
static uint8_t I2CAddress = 0; // selected I2C address
static bool NXPLeftSide = true; // selected eye (left or right)

// todo: move USBActive as well as TRUE and FALSE
static bool USBActive = false; // true if USB is connected

static uint8_t BufferPos = 0; /* position of character to be received in new buffer. When command is completed,
this also shows the length of the command */
static uint8_t ReadyBufferPos = 0; // copy of BufferPos for command being executed
Expand Down Expand Up @@ -271,21 +268,28 @@ static uint8_t HexPairToDecimal(uint8_t startIndex)
return HexDigitToDecimal(startIndex) * 16 + HexDigitToDecimal(startIndex + 1);
}

uint8_t ParseHexDigit(const char *buf)
{
if (!buf)
{
return 0;
}
return ParseHexDigitDirectly(*buf);
}

enum
{
BITS_PER_HEX_DIGIT = 4,
HEX_DIGITS_PER_BYTE = CHAR_BIT / BITS_PER_HEX_DIGIT
};

uint8_t ParseHexDigitNibble(const char *buf)
{
uint8_t ret = 0;
if (!buf)
{
return ret;
}
char c = *buf;
if ('\0' == c)
{
return ret;
}
ret = ParseHexDigitDirectly(c);
return ret;
}

/// Utility function for parsing the first two characters of a given C string as
/// a pair of hex digits.
uint8_t ParseHexDigits2_8(const char *buf)
Expand Down
27 changes: 21 additions & 6 deletions Source code/Embedded/src/USB.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@
#endif

#include "USB.h"
#include <udi_cdc.h>

// Make our internal buffer the size of the endpoint buffer for full-speed.
static const uint8_t RX_BUF_SZ = UDI_CDC_DATA_EPS_FS_SIZE;

static volatile bool main_b_cdc_enable = false;
static volatile bool main_b_cdc_opened = false;

#define USB_CDC_ECHO_ON

#undef USB_USE_UART

#ifdef USB_USE_UART
Expand Down Expand Up @@ -70,13 +77,21 @@ bool usb_cdc_is_active(void) { return main_b_cdc_enable && main_b_cdc_opened; }
bool usb_cdc_should_tx(void) { return main_b_cdc_enable && main_b_cdc_opened && udi_cdc_is_tx_ready(); }
void main_cdc_rx_notify()
{
while (udi_cdc_is_rx_ready())
char buf[RX_BUF_SZ];

do
{
char ch = udi_cdc_getc();
// echo on
udi_cdc_putc(ch);
ProcessIncomingChar(ch);
}
uint8_t recvd = (uint8_t)udi_cdc_read_no_polling(buf, sizeof(buf));
for (uint8_t i = 0; i < recvd; ++i)
{
char ch = buf[i];
#ifdef USB_CDC_ECHO_ON
udi_cdc_putc(ch);
#endif
ProcessIncomingChar(ch);
}
// drain the buffer
} while (udi_cdc_is_rx_ready());
}

void main_cdc_set_dtr(bool b_enable)
Expand Down

0 comments on commit e001bd1

Please sign in to comment.