I use the STM32F105 microcontroller with the STM32_USB-FS-Device_Lib_V3.2.1 USB library and adapted the VCP example for our purposes (integration with RTOS and serial API).
The problem is that if the USB cable is connected, but the port is not open on the Windows host, after a few minutes the device will automatically switch to the ISR USB interface until the port opens, and then everything starts working normally.
I have an interrupt handler and see that when an error occurs, the ISR handler exits and then returns immediately. This is because when I exit the interrupt, the IEPINT flag in OTG_FS_GINTSTS is not clear. OTG_FS_DAINT at this time contains 0x00000002 (set IEPINT1), while DIEPINT1 has 0x00000080 (TXFE). A line is called in OTGD_FS_Handle_InEP_ISR (), which clears the TXFE, but the bit is either not cleared or is immediately restored. When the COM port on the host is reopened, the state of OTG_FS_GINTSTS and OTG_FS_DAINT at the end of the interrupt is always zero, and further interrupts occur at normal speed. Note that the problem only occurs if data is output, but the host does not have a port. If the port is open or data is not output, the system starts endlessly. I believe,that the more data is output, the faster the problem arises, but at present it is anecdotal.
VCP code has a state variable that takes the following values:
UNCONNECTED,
ATTACHED,
POWERED,
SUSPENDED,
ADDRESSED,
CONFIGURED
and we use the CONFIGURED state to determine whether to put data in the driver buffer for sending. However, the CONFIGURED state is set when the cable is not connected when the host has a port, an open and connected application. I see that when Windows opens the port, there is an interrupt package, so it seems that there is some connection going on in this event; I wonder if it is possible, therefore, to determine whether a port host is open?
I may need one of two things:
- To prevent the USB code from getting into the ISR in the first instance
- To determine if the host has a port open from the end of the device, and only send data to send when it opens.