Giter VIP home page Giter VIP logo

stm32cdcuart's Introduction

DMA-accelerated multi-UART USB CDC for STM32F072 microcontroller

This USB CDC ACM implementation was written out of frustration with the example USB CDC class to UART bridge implementation provided by ST Micro.

This code re-writes the USB CDC implementation to hopefully overcome these limitations, but tries to not otherwise 'reinvent the wheel' where ST's existing code (V1.2.1) is serviceable as-is.

Improvements over the ST-provided example code are:

  • Data loss and lockup issues in ST's example code are hopefully all addressed
  • Both receive and transmit UART data transfers are DMA-accelerated.
  • Fixed-value variables are declared as const wherever possible to reduce RAM usage.
  • USB descriptors are written as more-maintainable structs.

Testing was done with Linux, Windows 7, and Windows 10 hosts.

Build Requirements

One approach is to use Rowley Crossworks for ARM to compile this code. It is not free software, but has been my favorite go-to ARM development tool for a decade and counting.

OR

I've modified the build environment provided by Alex Taradov's mcu-starter-projects to work with the STM32F072/STM32F042. With this approach, the code can be built using only open-source software. In Ubuntu-derived distributions, this is likely achieved with as little as:

sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential

Sanity Checklist If Customizing

The STM32F072B Discovery Kit precludes the use of UART2, as the available pins for this are mapped to incompatible devices.

The only available pins in the device used in STM32F072B Discovery Kit for UART4 share de-bouncing circuitry that artificially restricts the maximum data rate.

config.h has a NUM_OF_CDC_UARTS value that is used throughout the code to control the number of CDC UARTs.

The Command and Data Interface numbers in the USB descriptor in usbd_desc.c must be continguous and start from zero.

An understanding of USB descriptors is important when modifying usb_desc.c. This data conveys the configuration of the device (including endpoint, etc.) to the host PC.

The UARTconfig array in stm32f0xx_hal_msp.c must be customized to suit the pin-mapping used in your application. The values provided were used on the STM32F072BDISCOVERY PCB.

The parameters array values in usbd_cdc.c must be consistent with both the UARTconfig array in stm32f0xx_hal_msp.c and the Command and Data Interface numbers in the USB descriptor in usbd_desc.c.

The DMA IRQ handlers in usbd_cdc.c must be consistent with the UARTconfig array in stm32f0xx_hal_msp.c.

USB transfers are handled via a distinct section of memory called "PMA". Read the ST documentation on this. At most, there is 1kBytes that must be shared across all endpoints. Consider the usage of this PMA memory when scaling up the number of UARTs and buffer sizes.

stm32cdcuart's People

Contributors

majbthrd avatar tim-nordell-nimbelink avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

stm32cdcuart's Issues

Maximum UART baudrate?

What do think a practical maximum baudrate would be with this code, assuming DMA is used to and from the UART? I notice that you code a default of 115.2k and I'd like to achieve 230.4k if possible. This code could be the reason I need to ditch the MCP2221A that I normally use and save some space on my board.

USBD_LL_Init() overwrites BTABLE?

Hello Peter,

first of all many thanks for providing a composite driver that tries to be "universal" and works around
the horrible limitations of the original code.

You try to compute the PMA addresses in USBD_LL_Init() and you start at 0.
"pma_address = 0;"

The examples of STM however use the "magic" constant 0x18 and one wonders why...
The reason is the BTABLE, that starts at 0 and 8 bytes are needed per endpoint.
Look at #define PCD_EP_TX_ADDRESS
With your definition I think you overwrite the BTABLE.

One easy fix would be to initialze pma_address to the max. endpoint number (there should be enough space...). Doing it dynamically would require a different approach (you would need to compute first the number of endpoints).

Regards,

Alexander

Maximum number of uarts?

Some STM32 devices have quite a few UARTS available, but I've never seen more than 2 for any CDC configuration. Is it reasonable to try 4-8 UARTs accepting that there will be some contention at FS (but maybe less of a problem with HS?)

Thanks!

SETUP EP0 intermittently overwrites CDC data EP

This issue may afflict users who implement multiple serial ports AND open and close one of those ports whilst one or more others are open.

Intermittently (seemingly during heavy traffic), an outgoing (PC to device) EP0 USB SETUP message somehow corrupts some of the data in an IN (device to PC) endpoint associated with CDC data.

The test setup is a STM32F072 DISCOVERY running the stock firmware. PA9 is jumper-ed to PA10 (first UART) and PC4 is jumper-ed to PC5 (second UART).

Attached is C source code for a command-line test utility. Two instances are run, such as:

./cdcloop /dev/ttyACM0
./cdcloop /dev/ttyACM1

On one of the instances (nominally the second), the user uses Ctrl-C to terminate the program and then runs it again. The user then repeats this until there is a "mismatch error" on the first instance.

Per the USB sniffer screenshot, the contents of the SETUP message at 0:54.246.463 previously appear earlier in time at 0:54:245.652 in the CDC data associated with first UART (EP1).

cdc3

It is not apparent how the message seemingly travels back in time, and user options (such as PMA memory mapping) have been exhausted. What is left is the domain of the ST stack. In addition to v1.2.1, I've tried the v1.5 stack, and this has the same behavior.

cdcloop.c.txt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.