USB-CDC demo hangs on an STM32F303

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
hackerfoo
Posts: 7
Joined: Thu May 09, 2013 9:42 pm

USB-CDC demo hangs on an STM32F303

Post by hackerfoo »

Running the demo in testhal/STM32F30x/USB_CDC from SVN revision 5693 on a STM32F3DISCOVERY board, the USB serial driver hangs while transmitting data.

I've used usbmon on Linux and Virtual USB Analyze to see what's happening, and it appears the transactions aren't being completed correctly. I've looked through the datasheet and usb.c/usb_serial.c/etc., but I have not been able to find the problem.

Here's a screenshot:
Image

In this capture, I connected using minicom, typed 'info', got a response, then typed 'help', and the connection hung. The device will not respond until I disconnect and reconnect. The red bar and errors happen when disconnecting.

Here's the data from usbmon:

Code: Select all

ffff8800da112480 3493345390 S Ii:2:038:2 -115:128 8 <
ffff8800364dd6c0 3493345466 S Co:2:038:0 s 21 22 0003 0000 0000 0
ffff8800364dd6c0 3493356309 C Co:2:038:0 0 0
ffff8800da112cc0 3493356384 S Bi:2:038:1 -115 128 <
ffff8800da112840 3493356402 S Bi:2:038:1 -115 128 <
ffff8800da112b40 3493356414 S Bi:2:038:1 -115 128 <
ffff8800da112900 3493356427 S Bi:2:038:1 -115 128 <
ffff8800da112d80 3493356439 S Bi:2:038:1 -115 128 <
ffff8800da1120c0 3493356452 S Bi:2:038:1 -115 128 <
ffff8800da112f00 3493356465 S Bi:2:038:1 -115 128 <
ffff8800da112300 3493356477 S Bi:2:038:1 -115 128 <
ffff8800da112a80 3493356489 S Bi:2:038:1 -115 128 <
ffff8800da112000 3493356502 S Bi:2:038:1 -115 128 <
ffff8800da112540 3493356514 S Bi:2:038:1 -115 128 <
ffff8800da112c00 3493356526 S Bi:2:038:1 -115 128 <
ffff8800da1129c0 3493356538 S Bi:2:038:1 -115 128 <
ffff8800da112600 3493356551 S Bi:2:038:1 -115 128 <
ffff880036762900 3493356563 S Bi:2:038:1 -115 128 <
ffff88004d888780 3493356575 S Bi:2:038:1 -115 128 <
ffff88004d888240 3496033117 S Bo:2:038:1 -115 1 = 69
ffff8800da112cc0 3496042974 C Bi:2:038:1 0 1 = 69
ffff8800da112cc0 3496043007 S Bi:2:038:1 -115 128 <
ffff88004d888240 3496043023 C Bo:2:038:1 0 1 >
ffff88004d888240 3496109951 S Bo:2:038:1 -115 1 = 6e
ffff8800da112840 3496111601 C Bi:2:038:1 0 1 = 6e
ffff8800da112840 3496111620 S Bi:2:038:1 -115 128 <
ffff88004d888240 3496111635 C Bo:2:038:1 0 1 >
ffff88004d888240 3496326056 S Bo:2:038:1 -115 1 = 66
ffff8800da112b40 3496344909 C Bi:2:038:1 0 1 = 66
ffff8800da112b40 3496344934 S Bi:2:038:1 -115 128 <
ffff88004d888240 3496344951 C Bo:2:038:1 0 1 >
ffff88004d888240 3496490045 S Bo:2:038:1 -115 1 = 6f
ffff8800da112900 3496497366 C Bi:2:038:1 0 1 = 6f
ffff8800da112900 3496497447 S Bi:2:038:1 -115 128 <
ffff88004d888240 3496497463 C Bo:2:038:1 0 1 >
ffff88004d888240 3497102668 S Bo:2:038:1 -115 1 = 0d
ffff8800da112d80 3497110884 C Bi:2:038:1 0 1 = 0d
ffff8800da112d80 3497110984 S Bi:2:038:1 -115 128 <
ffff88004d888240 3497110998 C Bo:2:038:1 0 1 >
ffff8800da1120c0 3497112472 C Bi:2:038:1 0 1 = 0a
ffff8800da1120c0 3497112489 S Bi:2:038:1 -115 128 <
ffff8800da112f00 3497113448 C Bi:2:038:1 0 128 = 4b65726e 656c3a20 20202020 2020322e 352e3275 6e737461 626c650d 0a436f6d
ffff8800da112f00 3497113468 S Bi:2:038:1 -115 128 <
ffff8800da112300 3497115517 C Bi:2:038:1 0 128 = 2041524d 76372d4d 450d0a43 6f726520 56617269 616e743a 20436f72 7465782d
ffff8800da112300 3497115534 S Bi:2:038:1 -115 128 <
ffff8800da112a80 3497117992 C Bi:2:038:1 0 76 = 63726f65 6c656374 726f6e69 63732053 544d3332 46332d44 6973636f 76657279
ffff8800da112a80 3497118069 S Bi:2:038:1 -115 128 <
ffff88004d888240 3499007344 S Bo:2:038:1 -115 1 = 68
ffff8800da112000 3499011544 C Bi:2:038:1 0 1 = 68
ffff8800da112000 3499011576 S Bi:2:038:1 -115 128 <
ffff88004d888240 3499011600 C Bo:2:038:1 0 1 >
ffff88004d888240 3499093373 S Bo:2:038:1 -115 1 = 65
ffff8800da112540 3499097949 C Bi:2:038:1 0 1 = 65
ffff8800da112540 3499097968 S Bi:2:038:1 -115 128 <
ffff88004d888240 3499097986 C Bo:2:038:1 0 1 >
ffff88004d888240 3499223965 S Bo:2:038:1 -115 1 = 6c
ffff8800da112c00 3499231458 C Bi:2:038:1 0 1 = 6c
ffff8800da112c00 3499231478 S Bi:2:038:1 -115 128 <
ffff88004d888240 3499231497 C Bo:2:038:1 0 1 >
ffff88004d888240 3499322804 S Bo:2:038:1 -115 1 = 70
ffff8800da1129c0 3499324449 C Bi:2:038:1 0 1 = 70
ffff8800da1129c0 3499324543 S Bi:2:038:1 -115 128 <
ffff88004d888240 3499324558 C Bo:2:038:1 0 1 >
ffff88004d888240 3499599221 S Bo:2:038:1 -115 1 = 0d
ffff8800da112600 3499618600 C Bi:2:038:1 0 1 = 0d
ffff8800da112600 3499618681 S Bi:2:038:1 -115 128 <
ffff88004d888240 3499618696 C Bo:2:038:1 0 1 >
ffff880036762900 3499621246 C Bi:2:038:1 0 1 = 0a
ffff880036762900 3499621265 S Bi:2:038:1 -115 128 <
ffff8800b8cfb000 3505740898 S Co:2:038:0 s 21 22 0000 0000 0000 0
ffff8800b8cfb000 3505755366 C Co:2:038:0 0 0
ffff8800da112480 3505756845 C Ii:2:038:2 -2:128 0
ffff8800da112cc0 3505761025 C Bi:2:038:1 -2 0
ffff8800da112840 3505761531 C Bi:2:038:1 -2 0
ffff8800da112b40 3505762654 C Bi:2:038:1 -2 0
ffff8800da112900 3505763567 C Bi:2:038:1 -2 0
ffff8800da112d80 3505764554 C Bi:2:038:1 -2 0
ffff8800da1120c0 3505765527 C Bi:2:038:1 -2 0
ffff8800da112f00 3505768333 C Bi:2:038:1 -2 0
ffff8800da112300 3505770688 C Bi:2:038:1 -2 0
ffff8800da112a80 3505772327 C Bi:2:038:1 -2 0
ffff8800da112000 3505774706 C Bi:2:038:1 -2 0
ffff8800da112540 3505776239 C Bi:2:038:1 -2 0
ffff8800da112c00 3505779233 C Bi:2:038:1 -2 0
ffff8800da1129c0 3505781215 C Bi:2:038:1 -2 0
ffff8800da112600 3505782627 C Bi:2:038:1 -2 0
ffff880036762900 3505784748 C Bi:2:038:1 -2 0
ffff88004d888780 3505786861 C Bi:2:038:1 -2 0
hackerfoo
Posts: 7
Joined: Thu May 09, 2013 9:42 pm

Re: USB-CDC demo hangs on an STM32F303

Post by hackerfoo »

I've checked several revisions all the way back to revision 4966. They all seem to have the same problem.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB-CDC demo hangs on an STM32F303

Post by Giovanni »

Hi,

Apparently it is not happening here, not sure about how to replicate the problem. Anybody else experiencing it?

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: USB-CDC demo hangs on an STM32F303

Post by Tectu »

Nope, I cannot help here either since I don't own any F3 device. I can just tell the the CDC demo works fine on F1 and F4 devices.


~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB-CDC demo hangs on an STM32F303

Post by Giovanni »

I'll try it again using Linux over the weekend (hopefully), just in case that particular setup triggers some bug.

Giovanni
hackerfoo
Posts: 7
Joined: Thu May 09, 2013 9:42 pm

Re: USB-CDC demo hangs on an STM32F303

Post by hackerfoo »

I'm running Ubuntu 12.04 as a guest on VirtualBox, with the host being Windows 7. I just tried running a test on the host, and the USB connection takes much longer to hang. It may be a bug in VirtualBox, or a timing issue, or likely a bug somewhere in my code.

I will do more testing to see if I can reproduce the problem on the host OS.

Thanks.
hackerfoo
Posts: 7
Joined: Thu May 09, 2013 9:42 pm

Re: USB-CDC demo hangs on an STM32F303

Post by hackerfoo »

I wrote a Python script to test USB serial communication using the USB-CDC demo. I haven't been able to reproduce any problems yet, but the script might be useful to someone.

Code: Select all

import serial
import sys

# requires pyserial

port = 'COM19'

# use the 'write' command and check its output
def writetest(n):
    ser = serial.Serial(port, timeout=0.1)
    i = 0
    next = '0';
    ser.write("\r")
    ser.readline()
    ser.write("\r")
    ser.readline()
    ser.readline()
    ser.write("write\r")
    ser.readline()
    while i < n:
        c = ser.read()
        if not c:
            continue
        if(c != next):
            print('{c} != {n}'.format(c=c, n=next))
        else:
            sys.stdout.write('.')
        if next == '9':
            next = 'a'
        else:
            if next == 'f':
                next = '0'
            else:
                next = chr(ord(next) + 1);
        i = i + 1
    ser.write("\r\n")
    ser.close()

teststr = 'l039un3vw[0uy3w5[0tyuhj0[49uyj3w[05eyu0[9euv[pe79uvm3w[097u3078'

# repeatedly write the test string and delete it
# while making sure the correct characters are echo'ed back
def echotest(n):
    ser = serial.Serial(port, timeout=0.1)
    i = 0
    next = '0';
    ser.readline()
    while i < n:
        ser.write(teststr)
        c = ser.read(len(teststr))
        if(c != teststr):
            sys.stdout.write('!')
        j = len(teststr)
        while j:
            ser.write('\b');
            c = ser.read(3)
            if(c != '\b \b'):
                sys.stdout.write('@')
            j = j - 1
        sys.stdout.write('.')
        i = i + 1
    ser.write("\r\n")
    ser.close()

wtN = 5000
etN = 5000

print("Running device to host test {d} iterations...".format(d=wtN))
writetest(wtN)
print("\nRunning echo test {d} iterations...".format(d=etN))
print("Test String Size: {d}".format(d=len(teststr)))
echotest(etN)
print("\nDone.")
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB-CDC demo hangs on an STM32F303

Post by Giovanni »

Hi,

I just fixed two important bugs on the USBv1 driver, the one used by the STM32F3xx, could you run your tests again?

The ticket is #410 on the tracker.

Giovanni
hackerfoo
Posts: 7
Joined: Thu May 09, 2013 9:42 pm

Re: USB-CDC demo hangs on an STM32F303

Post by hackerfoo »

Thanks, I think that fixed it. My tests pass now.

I noticed this while looking for the bug: The STM32F30x datasheet warns about spurious clearing of bits in USB_ISTR at the bottom of page 913. This might also apply to the CTR_RX/CTR_TX bits in USB_EPnR, so I made the following changes to os/hal/platforms/STM32/USBv1/stm32_usb.h to mask the CTR bits in your EPR_* macros:

Code: Select all

@@ -137,6 +137,8 @@
                                  EPR_STAT_RX_MASK | EPR_DTOG_RX |           \
                                  EPR_SETUP)
 
+#define EPR_CTR_MASK            (EPR_CTR_TX | EPR_CTR_RX)
+
 #define EPR_EA_MASK             0x000F
 #define EPR_STAT_TX_MASK        0x0030
 #define EPR_STAT_TX_DIS         0x0000
@@ -201,21 +203,23 @@
 #define RXCOUNT_COUNT_MASK      0x03FF
 #define TXCOUNT_COUNT_MASK      0x03FF
 
+#define SAFE_EPR(epr) (((epr) | EPR_CTR_MASK) & ~EPR_TOGGLE_MASK)
+
 #define EPR_SET(ep, epr)                                                    \
-  STM32_USB->EPR[ep] = (epr) & ~EPR_TOGGLE_MASK
+  STM32_USB->EPR[ep] = SAFE_EPR(epr)
 
 #define EPR_TOGGLE(ep, epr)                                                 \
-  STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] ^ ((epr) & EPR_TOGGLE_MASK))
+  STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] ^ ((epr) & EPR_TOGGLE_MASK)) | EPR_CTR_MASK
 
 #define EPR_SET_STAT_RX(ep, epr)                                            \
-  STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] &                                \
-                        ~(EPR_TOGGLE_MASK & ~EPR_STAT_RX_MASK)) ^           \
-                       (epr)
+  STM32_USB->EPR[ep] = ((STM32_USB->EPR[ep] &            \
+          ~(EPR_TOGGLE_MASK & ~EPR_STAT_RX_MASK)) ^   \
+         (epr)) | EPR_CTR_MASK
 
 #define EPR_SET_STAT_TX(ep, epr)                                            \
-  STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] &                                \
-                        ~(EPR_TOGGLE_MASK & ~EPR_STAT_TX_MASK)) ^           \
-                       (epr)
+  STM32_USB->EPR[ep] = ((STM32_USB->EPR[ep] &            \
+          ~(EPR_TOGGLE_MASK & ~EPR_STAT_TX_MASK)) ^   \
+         (epr)) | EPR_CTR_MASK


The USB driver seems to work without these modifications, though.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB-CDC demo hangs on an STM32F303

Post by Giovanni »

Thanks for finding this, I will look into it.

Giovanni
Post Reply