1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-20 10:54:14 +01:00

USB HID: basic COM functionality over USB HID

This fixes the COM interface over USB HID to the point where
it can establish and maintain solid communications with the UAVObject
Browser in the GCS.

Tested only on Linux.  The USB HID interface is still disabled for now
until it is tested successfully by a wider group.

Edit telemetry.c and set ALLOW_HID_TELEMETRY to 1 to enable telemetry
over the USB HID interface and report your results in the forum.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@656 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
stac 2010-05-24 16:33:40 +00:00 committed by stac
parent 69de42db49
commit 6b2760da1a
6 changed files with 53 additions and 78 deletions

View File

@ -52,7 +52,7 @@
// Private constants
#define STACK_SIZE 200
#define TASK_PRIORITY (tskIDLE_PRIORITY+1)
#define TASK_PRIORITY (tskIDLE_PRIORITY+2)
// Private types

View File

@ -28,11 +28,14 @@
#include "gcstelemetrystats.h"
#include "telemetrysettings.h"
// Set this to 1 to enable telemetry via the USB HID interface
#define ALLOW_HID_TELEMETRY 0
// Private constants
#define MAX_QUEUE_SIZE 20
#define STACK_SIZE configMINIMAL_STACK_SIZE
#define TASK_PRIORITY_RX (tskIDLE_PRIORITY + 2)
#define TASK_PRIORITY_TX (tskIDLE_PRIORITY + 1)
#define TASK_PRIORITY_TX (tskIDLE_PRIORITY + 2)
#define TASK_PRIORITY_TXPRI (tskIDLE_PRIORITY + 2)
#define REQ_TIMEOUT_MS 250
#define MAX_RETRIES 2
@ -303,17 +306,17 @@ static void telemetryRxTask(void* parameters)
// Task loop
while (1)
{
// TODO: Disabled since the USB HID is not fully functional yet
inputPort = telemetryPort; // force input port, remove once USB HID is tested
#if ALLOW_HID_TELEMETRY
// Determine input port (USB takes priority over telemetry port)
//if(!PIOS_USB_HID_CheckAvailable())
//{
// inputPort = telemetryPort;
//}
//else
//{
// inputPort = COM_USB_HID;
//}
if(PIOS_USB_HID_CheckAvailable())
{
inputPort = COM_USB_HID;
}
else
#endif /* ALLOW_HID_TELEMETRY */
{
inputPort = telemetryPort;
}
// Block until data are available
// TODO: Currently we periodically check the buffer for data, update once the PIOS_COM is made blocking
@ -338,17 +341,17 @@ static int32_t transmitData(uint8_t* data, int32_t length)
{
COMPortTypeDef outputPort;
// TODO: Disabled since the USB HID is not fully functional yet
outputPort = telemetryPort; // force input port, remove once USB HID is tested
// Determine input port (USB takes priority over telemetry port)
//if(!PIOS_USB_HID_CheckAvailable())
//{
// outputPort = telemetryPort;
//}
//else
//{
// outputPort = COM_USB_HID;
//}
#if ALLOW_HID_TELEMETRY
if(PIOS_USB_HID_CheckAvailable())
{
outputPort = COM_USB_HID;
}
else
#endif /* ALLOW_HID_TELEMETRY */
{
outputPort = telemetryPort;
}
// TODO: Update once the PIOS_COM is made blocking (it is implemented as a busy loop for now!)
//PIOS_LED_Toggle(LED2);
@ -393,7 +396,7 @@ static int32_t setUpdatePeriod(UAVObjHandle obj, int32_t updatePeriodMs)
/**
* Called each time the GCS telemetry stats object is updated.
* Trigger a flight telemetry stats update if a connection is not
* yet established.
* yet established.
*/
static void gcsTelemetryStatsUpdated()
{
@ -409,7 +412,7 @@ static void gcsTelemetryStatsUpdated()
}
/**
* Update telemetry statistics and handle connection handshake
* Update telemetry statistics and handle connection handshake
*/
static void updateTelemetryStats()
{
@ -546,5 +549,5 @@ static void updateSettings()
{
PIOS_COM_ChangeBaud(telemetryPort, 38400);
}
}
}

View File

@ -205,33 +205,37 @@ static void TaskHIDTest(void *pvParameters)
{
uint8_t byte;
uint8_t line_buffer[128];
uint16_t line_ix;
uint16_t line_ix = 0;
for(;;)
{
/* HID Loopback Test */
/*if(PIOS_COM_ReceiveBufferUsed(COM_USB_HID) != 0) {
#if 0
if(PIOS_COM_ReceiveBufferUsed(COM_USB_HID) != 0) {
byte = PIOS_COM_ReceiveBuffer(COM_USB_HID);
if(byte == '\r' || byte == '\n' || byte == 0) {
PIOS_COM_SendFormattedString(COM_USB_HID, "RX: %s\r", line_buffer);
PIOS_COM_SendFormattedString(COM_DEBUG_USART, "RX: %s\r", line_buffer);
line_ix = 0;
} else if(line_ix < (64 - 1)) {
} else if(line_ix < sizeof(line_buffer)) {
line_buffer[line_ix++] = byte;
line_buffer[line_ix] = 0;
}
}*/
}
#endif
/* HID Loopback Test */
if(PIOS_COM_ReceiveBufferUsed(COM_USART2) != 0) {
byte = PIOS_COM_ReceiveBuffer(COM_USART2);
/*if(byte == '\r' || byte == '\n' || byte == 0) {
#if 0
if(byte == '\r' || byte == '\n' || byte == 0) {
PIOS_COM_SendFormattedString(COM_DEBUG_USART, "RX: %s\r", line_buffer);
line_ix = 0;
} else if(line_ix < (128 - 1)) {
} else if(line_ix < sizeof(line_buffer)) {
line_buffer[line_ix++] = byte;
line_buffer[line_ix] = 0;
}*/
}
#endif
PIOS_COM_SendChar(COM_DEBUG_USART, (char)byte);
}
}

View File

@ -146,12 +146,11 @@ static const uint8_t PIOS_USB_ConfigDescriptor[PIOS_USB_SIZ_CONFIG_DESC] = {
/* 18 */
0x09, /* bLength: HID Descriptor size */
0x21, /* bDescriptorType: HID */
0x10, /* bcdHID: HID Class Spec release number */
0x01, 0x00, /* bCountryCode: Hardware target country */
0x10, 0x01, /* bcdHID: HID Class Spec release number */
0x00, /* bCountryCode: Hardware target country */
0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
0x22, /* bDescriptorType */
PIOS_USB_HID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor */
0x00,
PIOS_USB_HID_SIZ_REPORT_DESC, 0x00, /* wItemLength: Total length of Report descriptor */
/******************** Descriptor of Custom HID endpoints ******************/
/* 27 */
@ -160,8 +159,8 @@ static const uint8_t PIOS_USB_ConfigDescriptor[PIOS_USB_SIZ_CONFIG_DESC] = {
0x81, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
(PIOS_USB_HID_DATA_LENGTH + 1), /* wMaxPacketSize: 2 Bytes max */
0x00, 2,//0x20, /* bInterval: Polling Interval (2 ms) */
(PIOS_USB_HID_DATA_LENGTH), 0x00, /* wMaxPacketSize */
0x02, /* bInterval: Polling Interval (2 ms) */
/* 34 */
0x07, /* bLength: Endpoint Descriptor size */
@ -170,7 +169,7 @@ static const uint8_t PIOS_USB_ConfigDescriptor[PIOS_USB_SIZ_CONFIG_DESC] = {
0x01, /* bEndpointAddress: */
/* Endpoint Address (OUT) */
0x03, /* bmAttributes: Interrupt endpoint */
(PIOS_USB_HID_DATA_LENGTH + 1), /* wMaxPacketSize: 2 Bytes max */
(PIOS_USB_HID_DATA_LENGTH), /* wMaxPacketSize */
0x00, 8,//0x20, /* bInterval: Polling Interval (8 ms) */
/* 41 */
};
@ -255,7 +254,7 @@ int32_t PIOS_USB_Init(uint32_t mode)
if(mode != 2) {
/* Note: usually no need to duplicate this for external drivers */
pInformation = &My_Device_Info;
pInformation->Ctrl_Info.Usb_wLength = (PIOS_USB_HID_DATA_LENGTH + 1); /* TODO: Is this required? */
pInformation->Ctrl_Info.Usb_wLength = (PIOS_USB_HID_DATA_LENGTH); /* TODO: Is this required? */
/* Following hooks/pointers should be replaced by external drivers */
memcpy(&Device_Table, (DEVICE *) &My_Device_Table, sizeof(Device_Table));
@ -411,8 +410,8 @@ static void PIOS_USB_CB_Reset(void)
SetEPType(ENDP1, EP_INTERRUPT);
SetEPTxAddr(ENDP1, PIOS_USB_ENDP1_TXADDR);
SetEPRxAddr(ENDP1, PIOS_USB_ENDP1_RXADDR);
SetEPTxCount(ENDP1, (PIOS_USB_HID_DATA_LENGTH + 1));
SetEPRxCount(ENDP1, (PIOS_USB_HID_DATA_LENGTH + 1));
SetEPTxCount(ENDP1, (PIOS_USB_HID_DATA_LENGTH));
SetEPRxCount(ENDP1, (PIOS_USB_HID_DATA_LENGTH));
SetEPTxStatus(ENDP1, EP_TX_NAK);
SetEPRxStatus(ENDP1, EP_RX_VALID);
@ -426,7 +425,7 @@ static void PIOS_USB_CB_Reset(void)
}
/**
* Update the device state to configured
* Update the device state to configured
*/
static void PIOS_USB_CB_SetConfiguration(void)
{

View File

@ -89,7 +89,7 @@ static uint8_t rx_buffer[PIOS_USB_HID_DATA_LENGTH] = {0};
* Initialises USB COM layer
* \param[in] mode currently only mode 0 supported
* \return < 0 if initialisation failed
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
*/
int32_t PIOS_USB_HID_Init(uint32_t mode)
{
@ -148,8 +148,8 @@ int32_t PIOS_USB_HID_TxBufferPutMoreNonBlocking(uint8_t *buffer, uint16_t len)
}
/* Copy bytes to be transmitted into transmit buffer */
UserToPMABufferCopy((uint8_t*) buffer, GetEPTxAddr(EP1_IN & 0x7F), (PIOS_USB_HID_DATA_LENGTH + 1));
SetEPTxCount(ENDP1, (PIOS_USB_HID_DATA_LENGTH + 1));
UserToPMABufferCopy((uint8_t*) buffer, GetEPTxAddr(EP1_IN & 0x7F), len);
SetEPTxCount(ENDP1, len);
/* Send Buffer */
SetEPTxValid(ENDP1);
@ -189,23 +189,8 @@ uint8_t PIOS_USB_HID_RxBufferGet(void)
return -1;
}
/* This stops returning bytes after the first occurrence of '\0' */
if(rx_buffer[rx_buffer_ix] == 0) {
/* Clean the buffer */
for(uint8_t i = 0; i < PIOS_USB_HID_DATA_LENGTH; i++) {
rx_buffer[i] = 0;
}
rx_buffer_new_data_ctr = 0;
rx_buffer_ix = 0;
return -1;
}
/* There is still data in the buffer */
uint8_t b = rx_buffer[rx_buffer_ix++];
//PIOS_COM_SendFormattedString(COM_DEBUG_USART, "b: %c\r", b);
//PIOS_COM_SendFormattedString(COM_DEBUG_USART, "ix: %u\r", rx_buffer_ix);
//PIOS_COM_SendFormattedString(COM_DEBUG_USART, "ctr: %u\r\r", rx_buffer_new_data_ctr);
if(--rx_buffer_new_data_ctr == 0) {
rx_buffer_ix = 0;
}
@ -311,7 +296,6 @@ static uint8_t *PIOS_USB_HID_GetProtocolValue(uint16_t Length)
*/
void PIOS_USB_HID_EP1_OUT_Callback(void)
{
#if 1
uint32_t DataLength = 0;
/* Read received data (63 bytes) */
@ -321,21 +305,7 @@ void PIOS_USB_HID_EP1_OUT_Callback(void)
PMAToUserBufferCopy((uint8_t *) rx_buffer, GetEPRxAddr(ENDP1 & 0x7F), DataLength);
/* We now have data waiting */
rx_buffer_new_data_ctr = PIOS_USB_HID_DATA_LENGTH;
#else
// FOR DEBUGGING USE ONLY
uint32_t DataLength = 0;
/* Read received data (63 bytes) */
/* Get the number of received data on the selected Endpoint */
DataLength = GetEPRxCount(ENDP1 & 0x7F);
/* Use the memory interface function to write to the selected endpoint */
PMAToUserBufferCopy((uint8_t *) rx_buffer, GetEPRxAddr(ENDP1 & 0x7F), DataLength);
/* Send it back */
PIOS_COM_SendFormattedStringNonBlocking(COM_USB_HID, "Received: %s", rx_buffer);
#endif
rx_buffer_new_data_ctr = DataLength;
SetEPRxStatus(ENDP1, EP_RX_VALID);
}

View File

@ -213,7 +213,6 @@ void RawHIDWriteThread::run()
//make a temporary copy so we don't need to lock the mutex
//during actual device access
memcpy(buffer, m_writeBuffer.constData(), size);
m_writeBuffer.remove(0, size);
m_writeBufMtx.unlock();
qDebug() << "Data to write";