STM32: ChibiOS/RT v16.1.x系统下USB接口的传输速率

使用基于ChibiOS/RT的实时系统已经有一段时间的,一直都好奇在STM32F1及STM32F4平台上USB接口的最大传输速率能达到多少。

  • 测试相关的代码

-device端使用的测试的代码参考的是ChibiOS/RT v16.1.x:testhal/STM32/STM32F7xx/USB_RAW,End Point会配置成bulk类型:

/**
 * @brief   IN EP1 state.
 */
static USBInEndpointState ep1instate;

/**
 * @brief   EP1 initialization structure (IN only)
 */
static const USBEndpointConfig ep1config = {
  USB_EP_MODE_TYPE_BULK,
  NULL,
  NULL,
  NULL,
  0x0040,
  0x0000,
  &ep1instate,
  NULL,
  1,
  NULL
};

/**
 * @brief   OUT EP2 state.
 */
static USBOutEndpointState ep2outstate;

/**
 * @brief   EP2 initialization structure (OUT only).
 */
static const USBEndpointConfig ep2config = {
  USB_EP_MODE_TYPE_BULK,
  NULL,
  NULL,
  NULL,
  0x0000,
  0x0040,
  NULL,
  &ep2outstate,
  1,
  NULL
};

同时会创建两个线程分别处理数据的接收与发送:

/*
 * USB writer. This thread writes data to the USB at maximum rate.
 * Can be measured using:
 *   dd if=/dev/xxxx of=/dev/null bs=512 count=10000
 */
static THD_WORKING_AREA(waWriter, 128);
static THD_FUNCTION(Writer, arg) {

  (void)arg;
  chRegSetThreadName("writer");
  while (true) {
    msg_t msg = usbTransmit(&USBD1, USBD1_STLINK_TX_EP,
                            txbuf, sizeof (txbuf) - 1);
    if (msg == MSG_RESET)
      chThdSleepMilliseconds(500);
  }
}

/*
 * USB reader. This thread reads data from the USB at maximum rate.
 * Can be measured using:
 *   dd if=bigfile of=/dev/xxx bs=512 count=10000
 */
static THD_WORKING_AREA(waReader, 128);
static THD_FUNCTION(Reader, arg) {

  (void)arg;
  chRegSetThreadName("reader");
  while (true) {
    msg_t msg = usbReceive(&USBD1, USBD1_STLINK_RX_EP,
                           rxbuf, sizeof (rxbuf));
    if (msg == MSG_RESET)
      chThdSleepMilliseconds(500);
  }
}

// ...

/*
 * Application entry point.
 */
int __attribute__((noreturn)) main(void) {
  // ...
  chThdCreateStatic(waWriter, sizeof(waWriter), HIGHPRIO, Writer, NULL);
  chThdCreateStatic(waReader, sizeof(waReader), HIGHPRIO, Reader, NULL);

  // ...
}

-host端的代码如下:

void handle_stlink_command(struct usb_ctx *ctx, const uint8_t *data, int32_t len)
{
    int result;
    struct timeval tv;
    int64_t start = 0, end = 0;
    int idx, count = 2048;
    uint8_t txbuf[1024], rxbuf[1024];

    // Write operation benchmark
    result = gettimeofday(&tv, NULL);
    if (result == 0) {
        start = seconds_to_microseconds(tv.tv_sec) + tv.tv_usec;
    }

    memset(txbuf, 0x00, sizeof(txbuf));
    for (idx = 0; idx < count; idx++) {
        W(ctx, txbuf, sizeof(txbuf));
    }

    result = gettimeofday(&tv, NULL);
    if (result == 0) {
        end = seconds_to_microseconds(tv.tv_sec) + tv.tv_usec;
    }

    printf("WRITE: Total transfer time: %11lldus, size=%lu, speed=%8.3fbyte/second\n", end - start, sizeof(txbuf) * count,
            (double)(sizeof(txbuf) * count) / (end - start) * seconds_to_microseconds(1));

    // Read operation benchmark
    result = gettimeofday(&tv, NULL);
    if (result == 0) {
        start = seconds_to_microseconds(tv.tv_sec) + tv.tv_usec;
    }

    for (idx = 0; idx < count; idx++) {
        R(ctx, rxbuf, sizeof(rxbuf));
    }

    result = gettimeofday(&tv, NULL);
    if (result == 0) {
        end = seconds_to_microseconds(tv.tv_sec) + tv.tv_usec;
    }

    printf(" READ: Total transfer time: %11lldus, size=%lu, speed=%8.3fbyte/second\n", end - start, sizeof(rxbuf) * count,
            (double)(sizeof(rxbuf) * count) / (end - start) * seconds_to_microseconds(1));
}

可以看到,这里分别测试写入与读取2MiB所需的时间。

- 相关的代码可以从这里下载:2016_11_26_chibios_v16.1.x_usb_benchmark.tar.gz

  • 在BRO-DBG-LINK – V2.1的传输速率

BRO-DBG-LINK – V2.1所使用的MCU为STM32F103CBT6,ARM Cortex M3核心,最大时钟频率为72MHz。

1. 系统的整体性能:

ch> test

*** ChibiOS/RT Test Suite
***
*** Compiled:     Nov 26 2016 - 18:38:32
*** Platform:     STM32F10x Performance Line Medium Density
*** Test Board:   BRO-DBG-LINK - V2.1

...
----------------------------------------------------------------------------
--- Test Case 12.1 (Messages performance #1)
--- Score : 264660 msgs/S, 529320 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.2 (Messages performance #2)
--- Score : 206101 msgs/S, 412202 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.3 (Messages performance #3)
--- Score : 206101 msgs/S, 412202 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.4 (Context Switch performance)
--- Score : 847568 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.5 (Threads performance, full cycle)
--- Score : 147596 threads/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.6 (Threads performance, create/exit only)
--- Score : 192312 threads/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.7 (Mass reschedule performance)
--- Score : 66410 reschedules/S, 398460 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.8 (Round-Robin voluntary reschedule)
--- Score : 504200 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.9 (Virtual Timers set/reset performance)
--- Score : 590990 timers/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.10 (Semaphores wait/signal performance)
--- Score : 1035976 wait+signal/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.11 (Mutexes lock/unlock performance)
--- Score : 568112 lock+unlock/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.12 (RAM Footprint)
--- System: 116 bytes
--- Thread: 68 bytes
--- Timer : 20 bytes
--- Semaph: 12 bytes
--- Mutex : 16 bytes
--- CondV.: 8 bytes
--- EventS: 4 bytes
--- EventL: 20 bytes
--- MailB.: 40 bytes
--- Result: SUCCESS
----------------------------------------------------------------------------

Final result: SUCCESS

 

2. USB接口的传输速率:

$ ./bro-dbg-link 

Device properties:
        bus number: 20
         port path: 1 (from root hub)
             speed: 12 Mbit/s (USB FullSpeed)

Reading device descriptor:
            length: 18
      device class: 0
               S/N: 3
           VID:PID: 0483:3748
         bcdDevice: 0100
   iMan:iProd:iSer: 1:2:3
          nb confs: 1

Reading string descriptors:
   String (0x01): "brobwind.com"
   String (0x02): "BRO-DBG-LINK - V2.1"
   String (0x03): "066AFF495650897167153325"
WRITE: Total transfer time:     4088864us, size=2097152, speed=512893.557byte/second
 READ: Total transfer time:     3146494us, size=2097152, speed=666504.370byte/second
...
WRITE: Total transfer time:     4092033us, size=2097152, speed=512496.356byte/second
 READ: Total transfer time:     3148294us, size=2097152, speed=666123.304byte/second
...
WRITE: Total transfer time:     4082853us, size=2097152, speed=513648.667byte/second
 READ: Total transfer time:     3107639us, size=2097152, speed=674837.714byte/second

可以看到写入的速率约为500KiB/second,而读取的速度可以达到650KiB/second。

  • 在STM32F4-discovery上的传输速率

STM32F4-discovery所使用的MCU为STM32F407VGT6,ARM Cortex M4核心,包含硬件浮点运算单元(FPU),最大时钟频率为168MHz。

1. 系统的整体性能:

ch> test

*** ChibiOS/RT Test Suite
***
*** Compiled:     Nov 26 2016 - 18:23:45
*** Platform:     STM32F407 High Performance with DSP and FPU
*** Test Board:   STMicroelectronics STM32F4-Discovery

...
----------------------------------------------------------------------------
--- Test Case 12.1 (Messages performance #1)
--- Score : 749995 msgs/S, 1499990 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.2 (Messages performance #2)
--- Score : 599998 msgs/S, 1199996 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.3 (Messages performance #3)
--- Score : 599998 msgs/S, 1199996 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.4 (Context Switch performance)
--- Score : 2434776 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.5 (Threads performance, full cycle)
--- Score : 428568 threads/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.6 (Threads performance, create/exit only)
--- Score : 552630 threads/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.7 (Mass reschedule performance)
--- Score : 197183 reschedules/S, 1183098 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.8 (Round-Robin voluntary reschedule)
--- Score : 1588640 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.9 (Virtual Timers set/reset performance)
--- Score : 1287926 timers/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.10 (Semaphores wait/signal performance)
--- Score : 3652164 wait+signal/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.11 (Mutexes lock/unlock performance)
--- Score : 1976464 lock+unlock/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 12.12 (RAM Footprint)
--- System: 120 bytes
--- Thread: 68 bytes
--- Timer : 20 bytes
--- Semaph: 12 bytes
--- Mutex : 16 bytes
--- CondV.: 8 bytes
--- EventS: 4 bytes
--- EventL: 20 bytes
--- MailB.: 40 bytes
--- Result: SUCCESS
----------------------------------------------------------------------------

Final result: SUCCESS

2. USB接口的传输速率:

$ ./bro-dbg-link 

Device properties:
        bus number: 20
         port path: 1 (from root hub)
             speed: 12 Mbit/s (USB FullSpeed)

Reading device descriptor:
            length: 18
      device class: 0
               S/N: 3
           VID:PID: 0483:3748
         bcdDevice: 0100
   iMan:iProd:iSer: 1:2:3
          nb confs: 1

Reading string descriptors:
   String (0x01): "brobwind.com"
   String (0x02): "BRO-DBG-LINK - V2.1"
   String (0x03): "FFFFFFFFFFFFFFFFFFFFFFFF"
WRITE: Total transfer time:     2263160us, size=2097152, speed=926647.696byte/second
 READ: Total transfer time:     3101735us, size=2097152, speed=676122.235byte/second
...
WRITE: Total transfer time:     2267679us, size=2097152, speed=924801.085byte/second
 READ: Total transfer time:     3097070us, size=2097152, speed=677140.652byte/second
...
WRITE: Total transfer time:     2258652us, size=2097152, speed=928497.174byte/second
 READ: Total transfer time:     3099147us, size=2097152, speed=676686.843byte/second

可以看到,在写入方面要比BRO-DBG-LINK – V2.1要好很多。

  • 相关的参考文档
  1. http://chibios.org/dokuwiki/doku.php
  2. http://www.st.com/en/microcontrollers/stm32f103cb.html
  3. http://www.st.com/en/microcontrollers/stm32f407vg.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注