cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure STM32F103 USB endpoints.

Bags
Associate II

Been reading thru the F103 datasheet to try and figure out why my TinyUSB based code isn't working. The datasheet says.. "For mono-directional/single-buffer endpoints, a single register can be used to implement two distinct endpoints".

The F103 has 8 physical endpoint registers. There are no flags in those registers to set mono or bidirectional mode. Each register only holds one EP address value.

My application requires creating EP0 (IN+OUT) + 4 * (Bulk IN+Bulk OUT+Interrupt In)=2+4*(1+1+1)= 14 EP addresses in total.

The datasheet continues on to say "The number of registers is 8, allowing up to 16 mono-directional/single-buffer or up to 7 double-buffer endpoints(a) in any combination. For example the USB peripheral can be programmed to have 4 double buffer endpoints and 8 single-buffer/mono-directional endpoints."

So how can you get 14 EP addresses into 8 endpoint registers?

 

9 REPLIES 9
TDK
Super User

> The datasheet continues on to say "The number of registers is 8, allowing up to 16 mono-directional/single-buffer or up to 7 double-buffer endpoints(a) in any combination. For example the USB peripheral can be programmed to have 4 double buffer endpoints and 8 single-buffer/mono-directional endpoints."

Can you link to the datasheet that says this? I can't find it. "STM32F103" unfortunately doesn't narrow down a single datasheet.

Here is a F103 datasheet that does not say this. Checked a few others as well.

https://www.st.com/resource/en/datasheet/stm32f103ze.pdf

 

> So how can you get 14 EP addresses into 8 endpoint registers?

I suspect it is not possible to have 14 independent endpoint addresses.

 

In any case:

TDK_0-1764872324278.png

If you feel a post has answered your question, please click "Accept as Solution".
gbm
Principal

In F103, you may configure up to 8 pairs of IN+OUT endpoints (or 8 double-buffered, single direction endpoints), so the maximum possible no. of CDC VCPs is 3 (Control IN+OUT, 3*CDC data IN+OUT, 3*CDC notification IN). Both endpoints in a pair must be of the same type, so you cannot combine interrupt IN with bulk OUT in a single pair. With 3 CDCs, one pair remains free, so you may add some other device, like MSC or printer.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
FBL
ST Employee

Hi @Bags 

Did you check How to configure the packet memory area in STM32 article? Also I would recommend How to select suitable endpoints for your STM32.

When assigning endpoint types and directions in STM32 USB device descriptors:

  • Each endpoint is uniquely identified by its number and direction.
  • The endpoint type (control, bulk, interrupt, isochronous) is set in the descriptor's bmAttributes field.
  • The direction (IN/OUT) is set in the bEndpointAddress field.

@gbm could you please clarify the source or reference for the statement that both endpoints in a pair must be of the same type? I'm not sure where this requirement comes from.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


Bags
Associate II

Hiya,

RM0008 Reference manual Feb 2021 Rev 21, Page 624 at the bottom.

Can't remember where I got the PDF from, I think it was the STM website.

Being a newbie at STM32 and of the guts of USB its a steep learning curve and I was depending on STM documentation being a definitively correct source to knowledge but I'm coming to the conclusion that parts of that document regarding USB are porkies!

But having said that, its forced me into doing a deep dive on the subject matter and it exercises my grey matter :o)

Bags
Associate II

Hiya,

Thanks for the much clearer explanation than that in the RM0008 document page 624. Perhaps that paragraph applies to the other MCUs listed in the RM0008 title, F101xx, F105xx, F107xx.

I'll have a look at the other MCU USB specs after I finish this reply - I'll either have to find an MCU that can support 4 VCPs or think of another way to configured\controll my device without having to write a vendor specific driver and host app for it.

At least its been a good learning exercise, or rather learning to know whats not. LOL.

For anyone else with similar USB queries, another useful project someone else pointed me towards as an example USB app was the blue pill serial monster code.

 


@Bags wrote:

Can't remember where I got the PDF from :o)


All documentation will always be available on the 'Documentation' tab of the Product Page for the chip in question; eg, for STM32F103C4:

https://www.st.com/en/microcontrollers-microprocessors/stm32f103c4.html#documentation 

So that's always the place to check for the definitive version.

So Feb 2021 Rev 21 is, indeed, the latest:

AndrewNeil_0-1764927524453.png

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
Bags
Associate II

I checked the other types of MCU referenced in RM0008 and they too don't match up with that paragraph I described.

There are only a max of 8 physical EP registers, so the described capability of "16 mono-directional/single-buffer" is false.

I came across another useful STM document for comparing USB capabilities across MCU types... 

Application note AN4879 - Introduction to USB hardware and PCB guidelines using STM32 MCUs.

It contains several tables listing the USB capabilities for a large number of STM32 MCUs.

 

@FBL It's not a requirement, it's how the USB hardware is designed. I assume we are taking about USB device peripheral like the one in F103, not the OTG one found in F105.

In USB device peripheral, a single register controls a pair of endpoints. There is only one EP type field shared by both endpoints. A "pair" may be used as single-buffered IN+OUT or double-buffered IN or OUT. No matter how it's used, there is a single EP type setting for the pair. This is also true for all the other variants of the base design like USB_DRD. The variants of this peripheral are found in STM32 L0, F0, G, H5, C MCUs.

With OTG_FS peripheral present in more robust F10x and F4x1, there is control pair plus independent 3xIN and 3xOUT endpoints, not "paired" like in USB device. So with these bigger MCUs you can only implement a single CDC VCP. More robust MCUs, lile F4, U5, H7 (at least the models I had a chance to write firmware for) have 5 or 8 independent pairs of IN+OUT EPs.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Bags
Associate II

An update as to my STM32F103 multi CDC project progress.....

Finally... I have an STM32F103 up and running with four CDCs!

For the STM32 USB stack I used the TinyUSB project, retaining the STM32Cube HAL for its GUI IDE functionality.

The USB device works out of the box on Win10 H2 (I don't have a Win11 installation to try it on).

For Ubuntu 25.10 with kernel 6.17.0, I had to patch out a single line in "drivers/usb/core/config.c" in order to prevent the kernel from rejecting the USB device due to my use of duplicate endpoints in the CDC notification interfaces.

Not bad for my first big STM32 project, (and first deep dive into USB internals) though I would put 3 months as a bit more of a diversion than I would have liked from the project I'm working on that needed this device as a debugging tool.

Now to finish the software, polish the hardware design, get a PCB manufactured and then back to the real work!