1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-01 09:24:10 +01:00

Updated HID code to do packet processing in OS indepdent layer. Should now work on all platforms. Please test and get back to me. Maximum object size of 62 bytes currently.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1174 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
peabody124 2010-07-28 18:55:04 +00:00 committed by peabody124
parent 02a78f0470
commit 7e6afe5a75
5 changed files with 27 additions and 45 deletions

View File

@ -46,14 +46,14 @@ const struct pios_com_driver pios_usb_com_driver = {
.rx_avail = PIOS_USB_HID_RxBufferUsed,
};
// TODO: Eventually replace the transmit and receive buffers with bigger ring bufers
// so there isn't hte 64 byte cap in place by the USB interrupt packet definition
/* Rx/Tx status */
static volatile uint8_t rx_buffer_new_data_ctr = 0;
static volatile uint8_t rx_buffer_ix;
static uint8_t transfer_possible = 0;
static uint8_t rx_buffer[PIOS_USB_HID_DATA_LENGTH+2] = {0};
static uint8_t transmit_remaining;
static uint8_t *p_tx_buffer;
static uint8_t tx_buffer[PIOS_USB_HID_DATA_LENGTH+2] = {0};
/**
@ -107,7 +107,6 @@ int32_t PIOS_USB_HID_Init(uint32_t mode)
USB_Init();
USB_SIL_Init();
PIOS_LED_On(LED2);
return 0; /* No error */
}
@ -142,29 +141,6 @@ int32_t PIOS_USB_HID_CheckAvailable(uint8_t id)
return transfer_possible ? 1 : 0;
}
/**
* Transmits the next byte in the buffer in report 1
*/
void PIOS_USB_HID_TxNextByte()
{
uint8_t buf[3];
if( transmit_remaining > 0 ) {
transmit_remaining--;
buf[0] = 1; // report ID 1
buf[1] = 1; // *p_tx_buffer;
buf[2] = 1;
p_tx_buffer++;
UserToPMABufferCopy((uint8_t*) buf, GetEPTxAddr(EP1_IN & 0x7F), 3);
SetEPTxCount((EP1_IN & 0x7F), 3);
/* Send Buffer */
SetEPTxValid(ENDP1);
PIOS_LED_Toggle( LED2 );
}
}
/**
* Puts more than one byte onto the transmit buffer (used for atomic sends)
* \param[in] *buffer pointer to buffer which should be transmitted

View File

@ -102,7 +102,7 @@ const uint8_t PIOS_HID_ConfigDescriptor[PIOS_HID_SIZ_CONFIG_DESC] =
0x03, /* bmAttributes: Interrupt endpoint */
0x40, /* wMaxPacketSize: 2 Bytes max */
0x00,
0x20, /* bInterval: Polling Interval (32 ms) */
0x08, /* bInterval: Polling Interval (32 ms) */
/* 34 */
0x07, /* bLength: Endpoint Descriptor size */
@ -113,7 +113,7 @@ const uint8_t PIOS_HID_ConfigDescriptor[PIOS_HID_SIZ_CONFIG_DESC] =
0x03, /* bmAttributes: Interrupt endpoint */
0x40, /* wMaxPacketSize: 2 Bytes max */
0x00,
0x20, /* bInterval: Polling Interval (20 ms) */
0x08, /* bInterval: Polling Interval (20 ms) */
/* 41 */
}
; /* CustomHID_ConfigDescriptor */

View File

@ -51,7 +51,6 @@ extern int32_t PIOS_USB_HID_RxBufferUsed(uint8_t id);
extern int32_t PIOS_USB_HID_CB_Data_Setup(uint8_t RequestNo);
extern int32_t PIOS_USB_HID_CB_NoData_Setup(uint8_t RequestNo);
extern void PIOS_USB_HID_EP1_OUT_Callback(void);
extern void PIOS_USB_HID_TxNextByte(void);
#endif /* PIOS_USB_HID_H */

View File

@ -47,7 +47,7 @@
#define BUFFER_SIZE 64
#define printf qDebug
//#define printf qDebug
#define printf
typedef struct hid_struct hid_t;
@ -240,11 +240,10 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
if (!hid || !hid->open) return -1;
#if 1
#warning "Send timeout not implemented on MACOSX"
uint8_t *report_buf = (uint8_t *) malloc(BUFFER_SIZE);
memcpy(&report_buf[2], buf,len);
report_buf[1] = len;
report_buf[0] = 2; // report ID
IOReturn ret = IOHIDDeviceSetReport(hid->ref, kIOHIDReportTypeOutput, 0, (uint8_t *)report_buf, BUFFER_SIZE);
uint8_t *report_buf = (uint8_t *) malloc(len);
memcpy(&report_buf[0], buf,len);
// Note: packet processing done in OS indepdent code
IOReturn ret = IOHIDDeviceSetReport(hid->ref, kIOHIDReportTypeOutput, 2, (uint8_t *)report_buf, len);
result = (ret == kIOReturnSuccess) ? len : -1;
#endif
#if 0
@ -321,15 +320,14 @@ static void input_callback(void *context, IOReturn ret, void *sender, IOHIDRepor
buffer_t *n;
hid_t *hid;
printf("input_callback, report id: %i buf: %x %x\n", id, data[0], data[1]);
printf("input_callback, report id: %i buf: %x %x, len: %d\n", id, data[0], data[1], len);
if (ret != kIOReturnSuccess || len < 1) return;
hid = (hid_t*)context;
if (!hid || hid->ref != sender) return;
n = (buffer_t *)malloc(sizeof(buffer_t));
if (!n) return;
if (len > BUFFER_SIZE) len = BUFFER_SIZE;
/* Real data starts at third byte, second byte is valid data size */
//len = data[1];
// Note: packet preprocessing done in OS independent code
memcpy(n->buf, &data[0], len);
n->len = len;
n->next = NULL;

View File

@ -137,16 +137,20 @@ void RawHIDReadThread::run()
{
//here we use a temporary buffer so we don't need to lock
//the mutex while we are reading from the device
// Want to read in regular chunks that match the packet size the device
// is using. In this case it is 64 bytes (the interrupt packet limit)
// although it would be nice if the device had a different report to
// configure this
char buffer[READ_SIZE] = {0};
int ret = hiddev->receive(hidno, buffer, READ_SIZE, READ_TIMEOUT);
if(ret > 0) //read some data
{
m_readBufMtx.lock();
/* First byte is report ID, can be ignored */
/* Second byte is the number of valid bytes */
// Note: Preprocess the USB packets in this OS independent code
// First byte is report ID, second byte is the number of valid bytes
m_readBuffer.append(&buffer[2], buffer[1]);
// m_readBuffer.append(buffer, ret);
m_readBufMtx.unlock();
emit m_hid->readyRead();
@ -210,15 +214,20 @@ void RawHIDWriteThread::run()
if(!m_running)
return;
else
size = qMin(WRITE_SIZE, m_writeBuffer.size());
size = qMin(WRITE_SIZE-2, m_writeBuffer.size());
//NOTE: data size is limited to 2 bytes less than the
//usb packet size (64 bytes for interrupt) to make room
//for the reportID and valid data length
}
//make a temporary copy so we don't need to lock the mutex
//during actual device access
memcpy(buffer, m_writeBuffer.constData(), size);
memcpy(&buffer[2], m_writeBuffer.constData(), size);
buffer[1] = size; //valid data length
buffer[0] = 1; //reportID
m_writeBufMtx.unlock();
int ret = hiddev->send(hidno, buffer, size, WRITE_TIMEOUT);
int ret = hiddev->send(hidno, buffer, WRITE_SIZE, WRITE_TIMEOUT);
if(ret > 0)
{