Serial communication with host computer

Post here first, or if you can't find a relevant section!
DrMN
Posts: 10
Joined: Sun Dec 04, 2022 3:37 pm

Re: Serial communication with host computer

Post by DrMN »

@fpiSTM

So... I tried the STMCubeProgrammer. It is not very intuitive, at least not for someone who is just being introduced to the Nucleo and STM32 family.

And the manual is not very accessible. In general the STM manuals would be much improved if they would include a table of acronyms and references to manuals where what they refer to is described.

So I would love to try the STMCubeProgrammer, but so far, I have no idea how to use it.


For the folks at STM, what would really help a lot, is a single self-contained document that describes common tasks in terms of simple numbered steps.
DrMN
Posts: 10
Joined: Sun Dec 04, 2022 3:37 pm

Re: Serial communication with host computer

Post by DrMN »

@GonzoG Thank you, that is a big help.

I should probably be more careful with language. For the application, I plan to communicate with the pc by serial over usb. The data rate going back to the pc needs to be about 1MB/s, faster is better of course.

From your previous description, it seems like serial over usb can be via the already present usb connector or by wiring a usb connector to pins PA11/PA12. I vote for simplicity - almost always. So that would seem to point to using the onboard connector.

For flashing the application to the board, for the moment, I prefer to use the onboard usb connector.

Aside, I guessed that SWD means software debugging? That sounds useful, An ICE might be even better. Arduino famously does not support either. So, I am curious about how to use a debugger with the Nucleo. But, apart from DMA, what I am doing is simple enough for the holy and time-honoured print statement and "blink the led". So, setting it up might only be a distraction for the moment.

Thank you again.
DrMN
Posts: 10
Joined: Sun Dec 04, 2022 3:37 pm

Re: Serial communication with host computer

Post by DrMN »

@ag123 I apologize, wasn't clear. Please see above to GonzoG. I meant serial over usb.
GonzoG
Posts: 325
Joined: Wed Jan 15, 2020 11:30 am
Answers: 24
Location: Prudnik, Poland

Re: Serial communication with host computer

Post by GonzoG »

DrMN wrote: Tue Dec 06, 2022 3:37 pm @GonzoG Thank you, that is a big help.

I should probably be more careful with language. For the application, I plan to communicate with the pc by serial over usb. The data rate going back to the pc needs to be about 1MB/s, faster is better of course.

From your previous description, it seems like serial over usb can be via the already present usb connector or by wiring a usb connector to pins PA11/PA12. I vote for simplicity - almost always. So that would seem to point to using the onboard connector.
Thinking like this, every serial interface (UART, SPI, IIC, CAN) can be called Serial over USB if you use USB adapter, which in case of Nucleo32 boards you have in form of ST-Link.

You won't achieve 1MB/s using ST-Link to get data from Nucleo. Although UART on L432KC can go up to 10Mbps, UART on ST-Link (F103CB) can go only up to 4Mbps, but ST-Link is not designed for high speed serial communication, so I doubt you will get ever 1Mbps.
If you want such high speeds (1MB/s is really high speed for those small MCUs), you'd be better with hardware USB, which can go up to 12Mbps, but it's a theoretical speed. I was able to achieve about 600kB/s with STM32F411CE (without using DMA).

DrMN wrote: Tue Dec 06, 2022 3:37 pm For flashing the application to the board, for the moment, I prefer to use the onboard usb connector.
Using ST-Link is the best way for STM32.
DrMN wrote: Tue Dec 06, 2022 3:37 pm Aside, I guessed that SWD means software debugging? That sounds useful, An ICE might be even better. Arduino famously does not support either. So, I am curious about how to use a debugger with the Nucleo. But, apart from DMA, what I am doing is simple enough for the holy and time-honoured print statement and "blink the led". So, setting it up might only be a distraction for the moment.

Thank you again.
Yes, SWD allows debugging.
Arduino IDE 1.x does not have it, 2.0 does have debugger. But there's also bad news, stm32duino does not support debugging in Arduino IDE 2 for now, you'll have to ask @fpiSTM if and when it will be available.

If you want debugging you will have to move to "real" IDE (as many think of Arduino IDE as a child's toy).
STM has whole set of tools for this, and all is described on their wiki:
https://wiki.st.com/stm32mcu/wiki/Categ ... ep_by_step
But be warned, it's not an Arduino language, where every thing is simple.
DrMN
Posts: 10
Joined: Sun Dec 04, 2022 3:37 pm

Re: Serial communication with host computer

Post by DrMN »

@GonzoG Hmmm...
You won't achieve 1MB/s using ST-Link to get data from Nucleo.
I just now tried it with a Teensy 4.0 into a python program on an i7 desktop, fedora 35.

For a 4KB binary transfer, the Teensy reports 18 usecs elapsed time to complete the write, and the pc side reports 88 usecs to complete the read.

I'll try this now on the STM32L432 through its usb/st-link and report back.
GonzoG
Posts: 325
Joined: Wed Jan 15, 2020 11:30 am
Answers: 24
Location: Prudnik, Poland

Re: Serial communication with host computer

Post by GonzoG »

Python ?? No wonder it took so long to read...
DrMN wrote: Tue Dec 06, 2022 7:41 pm I'll try this now on the STM32L432 through its usb/st-link and report back.
You don't have to try it. It's written in Nucleo32 manual (page 19, under table) that it's not good high baud rates.


edit:
There's one big difference between Tensy 4.0... it has 600 MHz chip with high speed USB 2.0 (480Mbps). STM32L432 is 80MHz chip with 12Mbps USB.
DrMN
Posts: 10
Joined: Sun Dec 04, 2022 3:37 pm

Re: Serial communication with host computer

Post by DrMN »

@GonzoG
There's one big difference between Tensy 4.0... it has 600 MHz chip with high speed USB 2.0 (480Mbps). STM32L432 is 80MHz chip with 12Mbps USB.
Yes, but also the STM32L432 has a 5MS/s ADC (if I read it right). That's what brought me to the part.
ag123
Posts: 1421
Joined: Thu Dec 19, 2019 5:30 am
Answers: 19

Re: Serial communication with host computer

Post by ag123 »

DrMN wrote: Tue Dec 06, 2022 3:37 pm @GonzoG Thank you, that is a big help.

I should probably be more careful with language. For the application, I plan to communicate with the pc by serial over usb. The data rate going back to the pc needs to be about 1MB/s, faster is better of course.

From your previous description, it seems like serial over usb can be via the already present usb connector or by wiring a usb connector to pins PA11/PA12. I vote for simplicity - almost always. So that would seem to point to using the onboard connector.

For flashing the application to the board, for the moment, I prefer to use the onboard usb connector.

Aside, I guessed that SWD means software debugging? That sounds useful, An ICE might be even better. Arduino famously does not support either. So, I am curious about how to use a debugger with the Nucleo. But, apart from DMA, what I am doing is simple enough for the holy and time-honoured print statement and "blink the led". So, setting it up might only be a distraction for the moment.

Thank you again.
I'm thinking you can actually get one of those boards, like the stm32f401/f411 "black pill" boards
e.g.
https://www.aliexpress.com/item/1005001456186625.html
https://stm32-base.org/boards/STM32F401 ... -Pill-V3.0
https://github.com/WeActStudio/WeActStu ... iSTM32F4x1

note that there are others making similar boards, but that the configuration (e.g. pinouts, buttons, etc) may be different
https://www.aliexpress.com/wholesale?ca ... =stm32f401

or a stm32f405 feather from Adafruit
https://www.adafruit.com/product/4382
or a Pyboard
https://store.micropython.org/product/PYBLITEv1.0
or a stm32F405 dev board from Olimex
https://www.olimex.com/Products/ARM/ST/STM32-H405/
STM32F405 is a 168 Mhz chip and has considerably more resources 1 M flash, 512k sram and is consequently more expensive

the 3rd party boards mostly have the usb connector directly connected at PA11/PA12 all done on board.

the stm32f4xx series has a DFU boot loader built in
https://learn.adafruit.com/adafruit-stm ... er-details
what it takes is to set boot0, press reset it it should be in usb (DFU) mode waiting for the upload.

of course, as you have the L432 Nucleo, you can also buy the usb breakout modules and connect PA11/PA12 accordingly along with the usb power lines. And I think you can try setting boot0, pressing reset to try uploads as well.

on stm32 the normal usb is full speed 12 Mbps, but that because usb (host/pc) multiplex the connections by polling every device for 1 ms each (round robin) the bandwidth is heavily shared by all the connected usb devices (including hubs (there are hidden ones)) e.g. your mouse, keyboard, etc.
in my tests on my own pc (running Linux) usb serial gives about 1.x Mbps maximum. Note that for USB, there are other protocol overheads as well.

--- the difficult way:
Accordingly, the stm32f405 can interface a USB high speed ULPI transceiver (480 Mbps), but that I've never tried it prior.
There are modules like such
https://www.aliexpress.com/wholesale?ca ... xt=usb3300
you would need to check the pin accessibility on the dev boards if you want to do that as well, preferably get a board where all the pins goes to the headers.

At those speeds, I'd think signal quality / signals etc is critical, any stray capacitance or impedance mismatch is effectively a short circuit or open circuit.
And I think usb high speeds actually use signals like 0.3v. Shielded cables are necessary as well to prevent signal degradation and possible interference.

---
another solution which I kind of thought about is to get a separate USB host adapter
https://www.aliexpress.com/wholesale?ca ... ss+adapter
they are quite cost-effective the idea is to reduce the multiplexing so that you can have as much of the 12 Mbps USB full speed as is possible.
but again I've never tried this prior.

Note that this may not help if, after all there are many ports on the hub internal to the host adapter, as it may still poll the 'empty' ports. But I'm not sure if this is indeed the case.

----
as i'm using Linux
I do use various 3rd party, open sourced utilities
https://github.com/stlink-org/stlink
^ this is for st-link, you have a st-link on your Nucleo board, so you can try it.

https://dfu-util.sourceforge.net/
^ open sourced dfu-util, this can work with ST chips with DFU boot loader, set boot0, press reset:

Code: Select all

dfu-util -a 0 -s 0x8000000 -RD sketch.bin
https://pypi.org/project/stm32loader/
^ this is for upload over serial, you need a separate usb-uart dongle to connect to PA9/PA10 for the flashing, again set boot0, press reset

The trouble with the open sourced utilities is that in Windows there may be different additional driver requirements, hence they may not work out of the box. But that stm32cubeprogrammer would have the driver issues accounted for as it is developed to work natively in Windows.

---

There is another thing which I don't understand fully is about the usb polling on the host/pc.
can this be controlled influenced by software?
e.g. that there are mouses which allows setting polling rates, but that normally HID uses interrupt requests, so I'm not too sure if it'd work with USB (CDC) serial.
A thing is we may not have access to the os driver layers that control polling even if it is software driven.
if it is hardware driven, e.g. by the host controller/adapter, it is likely we have even less control over it unless there is some registers we can set in the host controller/adapter to influence polling.

but that if these are possible, then we can 'optimize' the usb bandwidth e.g. to use as much of the 12 Mbps as is possible

it seemed some of the answers may be found in the host controllers specifications
https://www.intel.sg/content/www/xa/en/ ... ation.html
some interesting reading here as well
https://learn.microsoft.com/en-us/windo ... allocation

--
note that optimizing usb transfers on the device can be tricky as well, i've tried something like to read from ADC then transferring to usb sequently, e.g. read into a 2KB buffer then sending it. I'd think things like Serial.write(buffer, size) rather than Serial.print() or Serial.write() a single char at a time can improve throughput. But i did it sequentially. Other more 'difficult' things may involve double buffers and DMA. This would likely improve the throughput to usb as well.

--
oh and of course, another *cheap* and 'simple minded* way is to temporary disconnect other devices, e.g. mouse / keyboard to see if it could improve on all that usb multiplexing. There is a chance this works, because polling is all controlled by the host / os / apps etc.

-- swd / debug
Nucleo boards has st-link on board
https://www.st.com/resource/en/user_man ... ronics.pdf
https://www.st.com/resource/en/applicat ... ronics.pdf
so you can already do that if you want
and as usual, this is an 'advanced' topic, don't forget to review the wiki
https://github.com/stm32duino/wiki/wiki
there are some tips on how you can do that
https://github.com/stm32duino/wiki/wiki/How-to-debug
Last edited by ag123 on Wed Dec 07, 2022 9:17 am, edited 4 times in total.
ag123
Posts: 1421
Joined: Thu Dec 19, 2019 5:30 am
Answers: 19

Re: Serial communication with host computer

Post by ag123 »

here is something that turns up in a random google search
https://community.st.com/s/question/0D5 ... characters
apparently, st-link may be able to do pretty high baud rates.

Hence, even for Nucleo, you can try higher baud rates.
try with things like Serial.begin(115200) - 115200 baud for a start and use a serial monitor or write an app to interface with it.
then you can try higher baud rates. You would need to set baud rates in your serial monitor as well to match that declared in your Serial.begin(baud);

Note that for Nucleo, the interface uses one of the uart on board. Hence, you should choose u(s)art for Serial instead and not usb-cdc serial.

Note that USB is still USB, even if you can have high baud rates, that is really between the on chip uart to st-link uart (on board).
Then from the st-link (which acts as a bridge, i.e. usb-uart dongle) to your host/pc, it is still limited by all that usb polling and multiplexing.
So there'd be some upper bound no matter how.

This is 'simpler' on Nucleo as you can use the on board st-link to do all that bridging. The com port (VCP) is already there.

if you prefer to 'optimize to bare metal', then you may need to play with USB on PA11/PA12 on board itself. Which needs USB (CDC) Serial to use PA11/PA12 pins for USB.

--
for the maximum optimization with uart i.e. Serial via st-link, you can even venture to DMA with uart, but that that is an 'advanced' topic.
the basic Serial won't have that.
GonzoG
Posts: 325
Joined: Wed Jan 15, 2020 11:30 am
Answers: 24
Location: Prudnik, Poland

Re: Serial communication with host computer

Post by GonzoG »

ag123 wrote: Wed Dec 07, 2022 8:08 am here is something that turns up in a random google search
https://community.st.com/s/question/0D5 ... characters
apparently, st-link may be able to do pretty high baud rates.

Hence, even for Nucleo, you can try higher baud rates.
Don't think so.
It's about ST-Link V3. It has 15MHz VCP serial connection and USB 2.0 high speed
Nucleo32 boards have ST-Link V2, which uses F103CB MCU (or at least that's what is in all datasheets).
Post Reply

Return to “General discussion”