cancel
Showing results for 
Search instead for 
Did you mean: 

USB_virtual com port

silicon2
Associate II
Posted on June 16, 2010 at 09:23

USB_virtual com port

#virtual-com-port #seteptxvalid #usb
15 REPLIES 15
silicon2
Associate II
Posted on May 17, 2011 at 13:54

Hi,

I would like to read more about it what you explain but I couldn't find. Might you give me some advice.

r,r
tsuneo
Senior
Posted on May 17, 2011 at 13:54

> I would like to know how to check if USB on PC is avaliable to recive new data

Is it Windows ?

Just after enumeration, Windows CDC driver (usbser.sys) starts to poll the device bulk IN endpoint (EP).

This polling occurs regardless PC application opens the COM port or not. The data sent from the bulk IN EP is stored to the RX buffer on usbser.sys, until RX buffer overflows. The data is still there when a PC app opens the COM port.

If your device want to know the timing when a PC app opens / closes the COM port, the PC app explicitly sends a sign to the device.

For example, a) Toggle DTR signaling, or b) Send a specific character over the bulk OUT endpoint.

> or USB tx buffer on STM32 is empty.

a) when GetEPTxStatus() returns DEV_EP_TX_NAK, the endpoint is empty

OR

b) When a transaction completes on EP1, EP1_IN_Callback() is called from USB ISR (USB_Istr())

In this callback, you may raise a flag to show the endpoint is empty.

or, when the transfer size is greater than full-packet size, second (and later) packet is fed to the endpoint, in this callback.

Tsuneo

tsuneo
Senior
Posted on May 17, 2011 at 13:54

Unfortunately, the User manual of STM32_USB-FS-Device_Lib (UM0424) doesn't explain endpoints operation so much, other than the default.

The read/write operation of USB endpoint is almost similar to UART RX/TX. USB endpoint exchanges packet, but UART does a byte. Except for this difference of data size, the coding pattern is the same.

To send single byte to UART TX, 

- Check TX flag to know the TX register is empty

- Write to TX register

To send single packet to IN endpoint,

- Check STAT_TX flag to know IN buffer is empty

- Write to IN buffer, and validate it (to show the end of multi-byte write)

To send multi-bytes to UART TX,

- Fill a buffer with data

- Send the first byte to TX, using above procedure.

- On TX interrupt, next byte is passed to TX register from the buffer, until the last byte comes.

To send multi-packets to IN endpoint,

- Fill a buffer with data

- Send the first packet to IN endpoint, using above procedure.

- On IN endpoint interrupt, next packet is passed to IN endpoint from the buffer, until the last packet comes.

When you are aware of this analogy, USB endpoints become familiar with you at all.

For transfer of greater size, you may find this post helpful

http://www.cygnal.org/ubb/Forum9/HTML/001627.htm

Tsuneo

silicon2
Associate II
Posted on May 17, 2011 at 13:54

Hi,

I write simple test for virtual com port.

while (1)

 

{

 

    if(GetEPTxStatus(ENDP1) == EP_TX_NAK && BUTTON_PRESSED)

 

    {

 

         USB_SIL_Write(EP1_IN,''test'',4);

 

        SetEPTxValid(ENDP1);

 

    }

 

}

I plug my device into PC. Open port(virtual com port). When I press BUTTON the message is sent over usb and device EP status appear EP_TX_NAK so I am able to transmit data again . But if I press button after a while (20sec or 1minute later). My device stay in EP_TX_VALID mode. And I am not able to transmit data again. To see it on my PC.

I try to change code to:

while (1)

 

{

 

    if(BUTTON_PRESSED)

 

    {

 

         USB_SIL_Write(EP1_IN,''test'',4);

 

        SetEPTxValid(ENDP1);

 

    }

 

}

Now I don't understand. I received data if I press button. But only if I press Button once per second. If I press it later the IF statement is executed but I can't receive or see data on my PC.

r,S

chikos332
Associate II
Posted on May 17, 2011 at 13:54

Hi,

I'd like to know how is set the variable: BUTTON_PRESSED :

Because if it is set when a push button is pushed, then you will enter into the loop hundreds of time in each press... which will cause the USB IP be stuck with several overrun conditions.

So, I advise you to make sure that you don't have rebound condition on the push button and to use the EXTI interrupt instead of your infinite loop.

If you really need the infinite loop, then add a blocking variable:

while (1)

{

  if (BUTTON_PRESSED)

  {

     USB_SIL_Write(...);

     SET_EP_TX...

     BUTTON_PRESSED = 0; // assuming that this variable will be set by external mechanism/

  }

}
silicon2
Associate II
Posted on May 17, 2011 at 13:54

Hi,

I have the following. How often usb can send data(periodically(64 databuffer per ms)?

while (1)

{

  if (BUTTON_pushed)

  {

    while(BUTTON_pushed){};

    BUTTON_PRESSED = 1000;

  }

if(GetEPTxStatus(ENDP1) == EP_TX_NAK && BUTTON_PRESSED)

 

    {

 

         USB_SIL_Write(EP1_IN,''test'',4);

 

        SetEPTxValid(ENDP1);

 

    }

 

    BUTTON_PRESSED--;

 

}

 

}

r,S

chikos332
Associate II
Posted on May 17, 2011 at 13:54

What device are you using: STM32F105/7xxx or STM32F101/3xxx ?

If you use STM32F105/7xxx devices, no need to check on EP status, the write operation is managed by a FIFO, and you can write as many data as you want till the FIFO is full (and even when it is full it rolls back to first address I guess).

How often usb can send data(periodically(64 databuffer per ms)?

Yes, theorically it should be able to send, in full speed, 64 bytes/frame/endpoint (64B/ms).

tsuneo
Senior
Posted on May 17, 2011 at 13:54

> I have the following. How often usb can send data(periodically(64 databuffer per ms)? 

Transfer speed of bulk is proportional to the transfer size.

- When you send 4 bytes transfer, like above code, you'll get 4 bytes/ USB frame (1ms)

- When you send 300 bytes transfer, split into 5 packets (64 x 4 + 44 bytes), you'll get 300 bytes / frame.

Theoretical max is 19 full-size packets (64 bytes) / frame for full-speed bus.

Of course, bulk transfer is ''best effort'' type, full bandwidth is not always available.

If any other device on the bus has interrupt or isoc endpoint, the endpoint gets priority than the bulk endpoint. All of active bulk endpoints on the bus share the rest.

Tsuneo

silicon2
Associate II
Posted on May 17, 2011 at 13:54

Hi,

I use stm32f103rc

r,S