Or, if wasting a few cycles isn't a problem (and you know that len < 256-2),
Code: Select all
uchar usbFunctionWrite( uchar* data, uchar len )
{
if ( usbCrc16( data, len + 2 ) != 0x4FFE )
{
// CRC error
}
...
}
This relies on the property that the checksum of some data with its usb CRC appended is 0x4FFE, which is just the CRC of two zero bytes alone. This occurs because the appended CRC XORs itself and the previous data out of the CRC, leaving the equivalent of two zero bytes and no data.
OK, and to check the CRC of normal incoming requests:
Code: Select all
uchar usbFunctionSetup( uchar data [8] )
{
if ( usbCrc16( data, 8 + 2 ) != 0x4FFE )
{
// report error to host somehow, perhaps returning a zero-length reply (return 0;)
}
...
}
Though this still doesn't check ones handled internally by V-USB, like descriptor reading.
Added to V-USB WikiEDIT: And if you just want to ignore any packets with an invalid CRC (including those handled by V-USB internally), just add this to usbconfig.h:
Code: Select all
/* Check CRC of all received data */
#define USB_RX_USER_HOOK( data, len ) { \
if ( usbCrc16( data, len + 2 ) != 0x4FFE )\
return;\
}
This gets inserted into usbProcessRx() at the beginning, which is called from usbPoll(). The downside is that you don't get to take any application-specific action. I'm not sure how well this works, ignoring packets. The host might not notice, thus leaving the CRC error still harmful to reliable functioning.