AnsweredAssumed Answered

STM32F103 Keil USB-CDC hangs up

Question asked by anpilogov.andrii on Aug 13, 2014
Hi guys,

Keil example USB-CDC stuck under load.
I just can’t believe such a basic example is unreliable.
I’m sure Keil USBstack is widely used in production so I guess I do something wrong.
Could you point me what is wrong here?

Here is my test environment:

I have NUCLEO-R103 board with external USB connector:
D- <-> PA11
D+ <-> PA12
Pull-up D+ to VCC by 1.5k resistor.

I took c:\Keil\ARM\Boards\Keil\MCBSTM32\RL\USB\Device\CDC_ACM\ example and commented out lcd init code:
<pre>
//  lcd_init  ();
//  lcd_clear ();
//  lcd_print ("USB Device Demo ");
//  set_cursor(0, 1);
//  lcd_print ("  www.keil.com  ");
</pre>

Connect RX and TX for loopback test:
PA9-PA10

As PC side software I used simple node.js script: 
<pre>
var uart   = process.argv[2];
var tx_count = 0;
var rx_count = 0;

var fs = require('fs');

//var tx = fs.readFileSync(txFile, 'utf8');
var tx = "0000000 ef 1a 33 d6 aa 18 02 f6 1d dc a5 12 01 52 4f 24\r\n0000010 c0 b5 64 e8 0c df 15 02 8d a8 92 2c 52 17 8c 6c\r\n0000020 10 1c 86 13 c6 a9 98 13 69 1b 25 5e 3e 94 ab 77\r\n0000030 07 4b 9f 30 b9 94 8c 5a b9 36 5e 2d 2b 9c d7 04\r\n0000040 25 bb b2 cd 55 f1 74 88 20 65 96 e0 89 ab 41 02\r\n0000050 d0 a2 a8 63 35 8d 10 c3 99 c0 36 df 9f 94 db bb\r\n0000060 41 cd f4 48 b2 42 c9 57 d9 1e ba 99 b8 78 99 10\r\n0000070 6b c9 a4 cf ea ce 65 e9 48 99 3c 10 87 82 d3 9d\r\n0000080 05 f9 ef ec 76 35 4f c3 24 67 3c 3a 6d e9 16 59\r\n0000090 5b 06 fd 0d c7 44 d6 38 5f 02 a7 0d 1c d3 95 fe\r\n00000a0 59 eb 96 da 49 5a 9f 76 43 a8 30 c2 5b 1f 76 5d\r\n00000b0 c5 bd 6b f8 dc c7 4e be 90 86 10 c9 e5 5f 83 f0\r\n00000c0 c7 10 dd eb d5 e0 b7 2b 01 06 8f eb 4c f0 f3 d9\r\n00000d0 e8 3a a4 a0 d4 dd af 43 9e 01 1b e1 b4 44 5a 9f\r\n00000e0 02 9b af 1c 67 46 17 85 f9 68 32 78 a7 eb a8 3b\r\n00000f0 87 58 b5 ff 7c 4d ff 21 02 05 29 b6 0c a2 a7 f1\r\n0000100 27 24 20 a8 d4 ad e9 c0 d5 ce e9 cf 16 98 cd a9\r\n0000110 5c 17 74 96 4c f2 5c d6 14 43 6c 35 5a 40 11 2a\r\n0000120 95 9c f2 bc ff d3 9b d5 fc 46 e8 14 25 18 0c c6\r\n0000130 83 39 96 aa 1c f5 96 bc f7 c3 49 16 01 30 e0 2d\r\n0000140 45 ea d4 a9 36 98 9a 59 34 0a 76 07 ae 70 b5 7d\r\n0000150 3d b0 37 ba a9 1a e9 09 7a 1d 92 34 b8 f5 cb a1\r\n0000160 f2 bf b9 fe 9a fd 7a b3 42 81 de f5 d5 36 ca 56\r\n0000170 d4 a2 f5 4b 1e c7 6c eb 65 a8 b8 44 4e 01 63 4b\r\n0000180 fa 9c e4 15 ae 3c c8 dc c5 57 34 49 cb 6b 21 63\r\n0000190 a8 9f c0 32 3e 5d 9e e0 50 b7 43 61 ca e9 48 80\r\n00001a0 de e4 72 d1 e2 9e 5f 01 30 65 69 82 3c 6b 63 6e\r\n00001b0 96 42 a4 b8 cf e5 7d 98 9f f9 41 f0 2a 39 53 ba\r\n00001c0 d4 b6 1c 8a 41 8d 58 b4 0f b7 81 d1 e2 85 41 71\r\n00001d0 9a 1e 3b 9c 49 38 2a d6 e5 2f 40 9c 2a 6c 11 51\r\n00001e0 3d 60 6f 7f a5 d6 13 f6 9d 4a 2f b4 63 eb 10 e2\r\n00001f0 4f 6b 77 97 7c ba 8d c3 01 3f f2 c2 b9 65 84 3c\r\n0000200 0f 13 4a 84 05 88 6c f9 f6 4b aa 1a c7 c4 70 25\r\n0000210 c4 3c d1 1c de 31 91 13 fd 6f ff 93 49 dd d9 a5\r\n0000220 7b fc 4d 17 93 fa 6d 87 37 15 f1 ca b2 81 7e 07\r\n0000230 33 81 c6 d4 74 fd ea 09 21 c8 cc 9c 8f fa 86 e5\r\n0000240 0b 8b 1b 81 15 2e f1 69 8e f6 c0 ea 3e 43 a4 9e\r\n0000250 97 a3 57 16 a6 a3 64 b3 0b 85 90 4c 31 4a 8f 29\r\n";

var SerialPort = require("serialport").SerialPort;
var serialPort = new SerialPort(uart, {
  baudrate: 115200,
  bufferSize: 128,
  flowControl: true,
  rtscts: true
}, false);


serialPort.open(function () {
  console.log('open');

       console.log('Send data');
       var offset = 0;
       var max_step = 32;
       var send = function()
       {
            var step = Math.round(Math.random() * max_step);
          serialPort.write(tx.slice(offset, offset+step), function(err, result)
          {
               console.log('write:', err, result, tx_count++);
          if (offset < tx.length)
               {
                    offset += step;
                    serialPort.drain(function() {
                         setImmediate(send);
                    });
               }
               else
               {
                    offset = 0;
                    setImmediate(send);
               }
          });
       }
     serialPort.on('data', function(data) {
       console.log('data received: ' + data);
     });
       send();
});
</pre>

It works ok some time. But after while USBD_CDC_ACM_PutChar or USBD_CDC_ACM_GetChar return “-1” all the time.
Usually it’s USBD_CDC_ACM_PutChar.
I test it on several PC and Mac. On some machines it needs more time to stuck (win7) and sometimes it stuck quite fast (MacOS).

Regards

Outcomes