I've been reading a lot about USB communication for a while. I've already made two projects with PIC18F4550 using USB and it all works perfectly. But I still have one unanswered question in my mind. How does the USB stack know if there is new data or not?
I mean, I'm using Microchip's USB HID examples to guide me. So my chip is working in polling mode. My device will receive data constantly and transmit it to the computer through USB. But the computer will also send back some data. The thing is that the computer output data will only happens sometimes. Here's part of the code:
void QueryPCData(void)
{
if (!HIDRxHandleBusy(lastReceived))
{
lastReceived = HIDRxPacket(HID_EP,(BYTE*)&hid_out, HID_OUT_LEN);
if ((hid_out[0]==0xEF)&&(hid_out[1]==0xEF)&&(hid_out[2]==0xEF))
LED_ON;
else
LED_OFF;
}
//If the last transmision is complete
if(!HIDTxHandleBusy(lastTransmission))
{
lastTransmission = HIDTxPacket(HID_EP, (BYTE*)&hid_in, HID_IN_LEN);
}
return;
}
Looking at the code it seems that every loop it tries to update all data (TX and RX). But what if I did not send any data from the computer? My hid_out array will keep last value until it actually receives new data?
If I want to send a single command through USB then I should send it one time and then send blank spaces or something to prevent repeating the last command (sending it twice)?