How to detect when USB is disconnected? - c

I use a virtual com port for communication between the microcontroller and the computer.
I have mk. stm32h743 self-powered from its own 3.3V power supply. With the help of a cube, I raised a virtual com port. How can you understand that the USB is disconnected from the device ? When you connect Usb, it calls "CDC_Init_FS" and hUsbDeviceFS.dev_state is set to 3, which corresponds to "USBD_STATE_CONFIGURED". But when you disconnect the USB there is no callback "HAL_PCD_DisconnectCallback (PCD_HandleTypeDef * hpcd);"Why "HAL_PCD_DisconnectCallback (PCD_HandleTypeDef * hpcd);" not called when USB is disabled?
I have no way to track Vbus voltages. So I did it like this :
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef hpcd)
{
USBD_LL_Suspend((USBD_HandleTypeDef)hpcd->pData);
__HAL_PCD_GATE_PHYCLOCK(hpcd);
if (hpcd->Init.low_power_enable)
{
SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk |
SCB_SCR_SLEEPONEXIT_Msk));
}
SBD_LL_DevDisconnected(&hUsbDeviceFS);
}
Hello.
I have a problem with USB working properly - USB CDC (Virtual Com Port) with CubeMX HAL after disconnecting the USB cable and connecting again.
For the test I created a project similar to the one presented here - https://www.youtube.com/watch?v=AYICE0gU-Sg using STM32CubeIDE and STM32CubeMX.
The program writes data in the while (1) loop to the USB port after uploading to STM32.
while(1)
{
CDC_Transmit_FS(buffer, sizeof(buffer));
HAL_Delay(1000);
}
The data is written out, visible in Windows 7 (Tera Term).
When I disconnect the USB cable (OTG FS port) and reconnect, Windows sends the message "USB device not recognized".
Data is not received.
In order for the data to be transferred and the cable to be recognized, I must do a Reset on the board.
Hardware:
MCU: STM32H743VIT
Software: STM32CubeIDE v1.2
Firmware package: STM32Cube_FW_H7_V1.7.0
How to make the USB device work all the time correctly (after disconnecting the cable and reconnecting it), so that it is recognized by Windows and data is transferred?

Related

Replace a printer with a pc using serial port

I have to update my question (May 4th). The male db25 connector misled me to the parallel port so I rephrased it.
I have an old computer system that sends live data (temperature from some external devices) to a parallel dot printer. This computer must not be changed.
Instead of printing I would like to connect the printer cable in an (ubuntu os) pc serial port in order to receive those data and redirecting them in stdout.
Checking the db25 pins I discovered that it is used as RS232 serial cable. There are only three pin connected:
pin 2 (TXD)
pin 3 (RXD)
pin 7 (GND)
So my intentions are firstly to connect those pins to a db9 serial connector in order to plug it in a pc serial port and secondly to open and read the input data using:
chmod o+rw /dev/ttyS0
cat -v < /dev/ttyS0
I believe that it is a simple solution and it is going to work.

How does serial console communication work in a microcontroller?

My doubt is regarding the physical layer of the communication, I know from practice that for using for example teraterm to communicate with a MCU is simply enabling and configuring an UART peripheral, and then connect via USB the microcontroller and voila.
But it doesnt make sense to me yet that if USB connects to DN and DP, and an UART uses RX and TX, how does the host effectively communicates to the microcontroller?
There are two possibilities.
You connect to a USB/UART bridge such as devices made by FTDI or Prolific,
The microcontroller has a USB device controller and USB stack implementing the CDC/ACM device class (virtual COM port).
In the first instance, the bridge chip presents the CDC/ACM device to the host and exchanges data with a UART connected to the microcontroller UART. I/O control such as setting the baud rate have no impact on the USB connection, rather they are used to configure the UART link.
In the bridge arrangement, the bridge chip may be on the micro board, or it may be in a USB/serial adapter cable. Moreover internally the bridge chip is a microcontroller with a CDC/ACM device stack.
Unlike say RS-232, USB is not a peer-to-peer full duplex connection, and is not merely an electrical connection; USB requires quite complex device and host controllers and is more analogous to a device bus such as PCI than it is to UART serial connections. A CDC/ACM class device conforms to a specific protocol to allow a "virtual" UART to appear at the host. The UART you see at the PC is emulated, and is not physically the UART in the bridge.
The physical actual USB connection is a master-slave connection, with data and I/O control commands (such as baud rate and flow control) sent in USB packets to be unpacked, interpreted and transferred to the application layer via the CDC/ACM USB stack. In this arrangement the device, acting as a slave cannot initiate an exchange; rather the host continuously polls the device to which the device may return a packet containing its "tx" data. The polling and data rate of USB are fast enough to allow the simplex master-slave exchange to emulate a full-duplex UART connection, at higher throughput than can normally be achieved by a typical real UART, and certainly faster than a physical RS-xxx connection.
You can get an idea of how all this works by observing the raw USB data exchange using a tool such as WireShark. You will see that a lot more than just your application "serial" data is being exchanged.
You need to use a uart-usb interface IC that can convert uart to usb (and vice versa).

Microcontroller not recognized at USB port when connected via pins

Im having issues with my TI LAUNCHXL-F28379D. I have it connected to the laptop I am trying to send data to/from via pins P19 (SCIBRxD) and P18 (SCIBTxD), as well as GND and 5V via J16, using a USB Type B cord to which I sautered pin connectors. I'm sure power is being supplied to the board as when I plug the 5V cable of the USB cable, the RGB lights and 3 other small ones turn on. However, the Computer does not recognize ithe controller at the port - it only recognizes if if it is connected via the board's Mini-USB port. I've also written a C Program which opens the COM port and reads data - but this does not work without my knowledge of the COM port number, usually found in Device Manager. I cannot get any further in my project without the port number and it is very annoying. I will attempt to resauter a new USB cable, but if there are any other possible solutions, or if I'm doing something wrong, please let me know.
Absolutely all feedback is appreciated!
The SCIBRxD and SCIBTxD pins belong to the SCI peripheral in the microcontroller. This peripheral implements a UART port (a.k.a. COM port, a.k.a. TTL serial port). It is not a USB port. Nothing useful will happen if you connect it to a USB port, because it is not compatible with USB in any way.
USB-to-UART adapters (a.k.a. USB-to-serial adapters) do exist. There is one built into the LaunchPad. The UART side of the adapter is connected to the SCIB pins, and the USB side of the adapter is connected to the USB port.

Verify whether two usb devices are connected on the same hub

I have a microcontroller board which has a USB hub to which several CP2108 USB to serial modules are connected and one PIC Microcontroller is connected.
The USB hub is connected to Laptop USB Port..
The CP2108 devices are assigned /dev/ttyUSB0, /dev/ttyUSB1 ID's, whereas the PIC Microcontroller is assigned a /dev/usb/abc.
If I connect another USB device to Laptop USB Port, it will get /dev/ttyUSB2
I want to verify whether /dev/ttyUSB and /dev/usb/abc are on the same hub and not on different one..
Is there any way to find out through code.. I tried to look into sysfs , but I am unable to find out the difference

Stm32 Virtual COM Port - Can not connect

This is a code, part of my project;
//----------------------------------------------------------------------------
// USB: Virtual COM ----------------------------------------------------------
//----------------------------------------------------------------------------
USB_Interrupts_Config();
Set_USBClock();
USB_Init();
while (bDeviceState != CONFIGURED)
;
//----------------------------------------------------------------------------
// Main Loop -----------------------------------------------------------------
//----------------------------------------------------------------------------
while(1)
{
USB_printf("Main Function");
GPIO_SetBits(GPIOC, GPIO_Pin_10);
Wait(200);
GPIO_ResetBits(GPIOC, GPIO_Pin_10);
Wait(200);
}
After plugging my usb to the Circuit, Virtual COM Port Driver installation finishes correctly and LED blinking is also OK.
But when I try to connect COM port via Hyper Terminal or 3th party tools, I can not
achieve it.
Is there any opinion?
Thanks..
I had similar issue with a slightly different wording - it is possible to connect to the device until it's reset. My custom board would enumerate VCP correctly but would not connect after device reset. Turns out, that USB host keeps the enumerated address for the device, while device looses its address after reset/reprogramming. To avoid such issues, software reset should also perform USB physical layer reset (disconnecting pullup on D+/D- pin, depending on the speed used). In such a case it should be possible to connect to the device after disabling and then re-enabling it in the device manager or re-plugging
Not Necessarilly the answer but may be worth checking.
If your periodically send information out of the COM port and it appears your program does ( never used STM32 ), Windows (hyperterm so I assume windows XP) can detect that as a serial mouse and enumerates it as a mouse thus opening the port and preventing any other application from using it.
We have had many issues with this with our devices (CP2103's from SiLabs), and also is a common problem with GPS recievers.
This answer GPS Detected as serial mouse as a good description of the issue and possible workarrounds.

Resources