To detect the disorder of the bulk IN endpoint by the firmware, there are a couple of options. Here is one of the ways. TX timeout
- Just after the firmware passes data to CDC bulk IN EP by putrsUSBUSART(), putsUSBUSART() or putUSBUSART(), start a timer to count down timeout.
- USBUSARTIsTxTrfReady() is examined in ProcessIO(), to stop this timer on the TX completion.
- This timer is examined in ProcessIO(), too. When the timer expires, call detach() (below)
Another value is loaded to the timer to make a period of disconnection (around 2-3 sec)
- When the timer expires, again, call attach() (below)
Usually, the data on the bulk IN EP is retrieved by the host at the current USB frame or next one.
5 ms is enough for the timeout.
void detach( void )
U1CON = 0; // Disable USB engine & detach from bus
U1IE = 0; // Mask all USB interrupts
USBDeviceState = POWERED_STATE; // Move to powered state, temporarily
void attach( void )
USBDeviceState = DETACHED_STATE; // Move to the detached state
// stack moves it to ATTACHED_STATE
// and initialize the USB engine
Some USB engine on other MCUs (NXP LPC family, AT90USB, SAM3U, EZ-USB series, etc) has a bit for NAK sent (Unfortunately PIC USB engine doesn't have this bit). For these engines, check and drop this bit in SOF interrupt. It provides an easy way of integrity check of polling IN transactions. This function is desirable for the next generation of PIC USB engine
Host app side also has a couple of options.
a) COM Port polling
SerialPort.GetPortNames lists up all COM ports. Checking the list periodically, disconnection and re-connection of the target COM port is known. When the target COM port disappears from the list, close the COM port. When it appears again, open the COM port.
b) DSR/DTR handshake
The firmware notifies coming detach to host app by DSR. Seeing DSR change, Host closes the COM port. CloseHandle() (WinAPI) drops DTR automatically. Seeing DTR change, the firmware actually detaches.
Serial_State notification (DCD/DSR/RI etc) support http://www.microchip.com/forums/fb.ashx?m=480514
post edited by chinzei - 2011/01/25 09:22:21