I've been working on a communication protocol to configure devices. I am using an MSP430F6638 as the processor and HID as the interface. For the most part it works great but there have been some intermittent errors. Some of the 64 byte HID packets are out of order. Upon further testing it appears that as the device is sending packets, occasionally one will go "missing". Immediately after the next packet arrives at the PC the "missing" one can be read.
I am using HIDAPI from signal11 (I am using https://github.com/trezor/cython-hidapi for my testing right now) and Windows 7 (I've also run my script in Linux with same results).
Key points I've noticed:
- If "missing" packet is the last one in a transmission block, no amount of waiting will make it show up.
- It appears that the "missing" packet is always available to read after the next transmission block starts
- If its in the middle of a transmission block it will appear after n+1
The following is my TX code on the MSP.
static char out_buf_index = 0; void usb_flush(void){ char error; uint16_t bytesSent, bytesReceived; set_LED(Warn3, 1);//debug if(out_buf_index > 0){ packet_cnt++; //debug while (USBHID_intfStatus(HID0_INTFNUM, &bytesSent, &bytesReceived) & kUSBHID_waitingForSend) //Wait till last tx is finished __no_operation(); if(x_send){ usb_out_buf_y[0] = packet_cnt; //debug error = hidSendDataInBackground((unsigned char*)usb_out_buf_y, out_buf_index +1, HID0_INTFNUM, 0xFFFF); //Send usb_out_buf_x[1] = '\0'; if(error == 0){ x_send = 0; } else //usb_out_buf_y[0] = '\0'; //Clear other buffer //debug usb_out_buf_y[1] = '\0'; //Clear other buffer } else{ usb_out_buf_x[0] = packet_cnt; //debug error = hidSendDataInBackground((unsigned char*)usb_out_buf_x, out_buf_index+1, HID0_INTFNUM, 0xFFFF); //Send usb_out_buf_y[0] = '\0'; if(error == 0){ x_send = 1; } else //usb_out_buf_x[0] = '\0'; //Clear other buffer //debug usb_out_buf_x[1] = '\0'; //Clear other buffer } out_buf_index = 0; } else _NOP(); set_LED(Warn3, 0);//debug }
EDIT: Fixed some wording