Recent

Author Topic: STM32, startup and enable USB as Virtual COM port  (Read 739 times)

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
STM32, startup and enable USB as Virtual COM port
« on: June 23, 2019, 11:41:06 pm »
I think I can build a bootable image with the embedded version as installed through fpcupdeluxe.  :D

I checked the device and cpu configuration files, and they look pretty good, but they're only available for the common STM32F controllers. The ones I had weren't available.

I installed Arduino and the STM32 support, the devices I had were available and the simple program (flash the led) worked right away. But Arduino isn't a real development platform, it's for light hobby usage only.

I installed the (brand new) official STM32 development platform. At the start you have to run the STM32CubeMX tool, which generates the startup and initialization code. This is a mess of lots of .h files, with either a HAL (the default) or a Low Level interface. And I am talking about the LL interface, the HAL one is a maze. This generates a project with a lot of source files with in total about 20 lines of C code to initialize the clock sources. To enable the USB you have to use the HAL.

I hated that, so I installed the Open Source STM32 development platform, which allows you to program to the bare metal. When you create a project, it basically generates a single file with the startup code in assembly. I hunted down, translated and added the configuration of the clock sources from the CubeMX project.

This does work, and the debugging is flawless. But getting the USB to work like this is far too complex.

Next up, as I knew what I needed, I looked closer at the free pascal / Lazarus interface. It was not complete. For starters, all the FPUs are defined as: "fputype:fpu_soft". And while the larger controllers can have multiple banks of RAM and Flash (with different starting addresses), only continuous chunks seem to be supported. Fortunately, all the register names and addresses are there.  :)

The FPU should work if you fill in the right value, the startup code is there, and the supplied device files look like they are from before ST chopped them up and packaged them into LL and HAL chunks. But I would have to make my own for the devices I had.

I bought some new, supported devices and when they finally arrived, I could start programming them. Well, after generating and translating the CubeMX code that configures the clock sources. And activating the USB...

Ok, let me put it like this: a quarter of the Reference Manual for the STM32F401 is about the USB. It's complex. DIY is going to take a lot of time. Especially because the ST Virtual COM Port Driver has to recognize and understand it.

What would be the simplest way to get this to work? I'm going to try to generate the CubeMX project and see if I can get the IDE to compile it to assembly, and import that, but that will still be very time-consuming.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: STM32, startup and enable USB as Virtual COM port
« Reply #1 on: June 24, 2019, 12:20:45 am »
I now have 5 different kinds of STM32 boards, but it seems the board I was most enthusiastic about is already depreciated.

https://www.aliexpress.com/item/33014539711.html

They make a much improved, green "blue pill" board, and then they replace it with a black one that confirms to the much worse "official" specs.  :(

lucamar

  • Hero Member
  • *****
  • Posts: 1827
Re: STM32, startup and enable USB as Virtual COM port
« Reply #2 on: June 24, 2019, 12:23:57 am »
USB programming is complex. With normal OSs this complexity is hidden besides succesive layers of system code: device drivers, kernel libs, mid-layer libs, etc. but for embedded code w/out OS DIY is the way ... unless someone happens to have done it before for your particular platform, which you can google for.

Bare metal (and "almost-bare metal") programming has always been like this. Thank the gods for "the internets" and the acumulated expertise therein. ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: STM32, startup and enable USB as Virtual COM port
« Reply #3 on: June 24, 2019, 01:03:42 am »
USB programming is complex. With normal OSs this complexity is hidden besides succesive layers of system code: device drivers, kernel libs, mid-layer libs, etc. but for embedded code w/out OS DIY is the way ... unless someone happens to have done it before for your particular platform, which you can google for.

Bare metal (and "almost-bare metal") programming has always been like this. Thank the gods for "the internets" and the acumulated expertise therein. ;)

I wrote a low-level USB interface (Windows) and an USB driver (Linux) for some devices, and it was not a fun or fast experience. But I was hoping the microcontroller part would be better organized.

ST/Philips/Microchip: just !@#$%^& give us one assembly file for each complex device to configure, instead of a really hard to understand, use and massive library and documentation for each and every one that only works with C!

Edit: and I tend to always write a mini-OS with command-line interface for microcontrollers to make larger projects manageable.

Atmel did it right: turn on the power, and it actually works right away. And Microchip is the worst.
« Last Edit: June 24, 2019, 01:09:28 am by SymbolicFrank »

avra

  • Hero Member
  • *****
  • Posts: 1570
    • Additional info
Re: STM32, startup and enable USB as Virtual COM port
« Reply #4 on: June 24, 2019, 09:31:58 am »
What would be the simplest way to get this to work?
The simplest way would be to forget STM32 internal USB and use something external, like FTDI or CP210x. Or use MikroElektronika Pascal for STM32 and use their USB CDC implementation.

I'm going to try to generate the CubeMX project and see if I can get the IDE to compile it to assembly, and import that, but that will still be very time-consuming.
Chances for incorporating generated asm and making it work properly with your pascal code are very slim, and even worse tied to a specific MCU. It is not a simple copy of generated asm for a single function. It is a whole framework. If there is a USB lib for some STM32 GCC based compiler then you might want to try to translate headers and link to it, but on STM32 embedded target you are on uncharted territory. You could also read all the USB docs and try to do it yourself, but some 15 years ago when I checked such an effort for an AVR would be 6+ months so STM32 would probably be similar. What I would do would be to find a STM32 USB CDC lib with acceptable license an try to convert it to FPC. Also lots of work, but doable. Quick search pointed me to something like this: https://github.com/ayushgaud/USB_Serial_Bridge/tree/master/Src. It would need some adaptation, but looks doable.

Since I am new to STM32, this link introduced me to USB CDC on it: https://damogranlabs.com/2018/02/stm32-usb-cdc/

ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: STM32, startup and enable USB as Virtual COM port
« Reply #5 on: June 24, 2019, 03:11:12 pm »
CDC for the Virtual COM. "STM32 USB CDC old StdPeripheral library" looks to be the magic sentence.  :) There are still some sites that support those. I'm checking it out.

The MikroElektronika Pascal does look interesting, but it is expensive. If fpc / Lazaus is not an option, I'll use the Open Source STM32 development platform (Assembler / C) and strip the old libraries. But I would prefer to use Lazarus as my IDE (and Pascal, ofc.).

The problem isn't so much the porting of the USB libraries, but that I would have to port everything to be able to use those bits. Those old libraries look much better.

avra

  • Hero Member
  • *****
  • Posts: 1570
    • Additional info
Re: STM32, startup and enable USB as Virtual COM port
« Reply #6 on: June 24, 2019, 03:37:07 pm »
https://github.com/search?q=stm32+usb+cdc
Good old github shows 60 repos. Too bad it's taken by Microsoft now...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: STM32, startup and enable USB as Virtual COM port
« Reply #7 on: July 02, 2019, 04:22:20 pm »
Ok. I downloaded and sorted the promising ones. The potentially useful ones are build on top of the old STM libraries. This is the coding style used:

Code: C  [Select]
  1. #define USB_OTG_WRITE_REG32(reg,value) (*(__IO uint32_t *)reg = value)
  2.  
  3. USB_OTG_WRITE_REG32( &pdev->regs.GREGS->GINTMSK, int_mask.d32);

In other words: mostly useless.

Yes, I could translate all that and it might even work, but I still have to translate much more than I would need and most of it would be unstructured and use pointers to pointers with constants instead of records and classes.

The Cortex M4 has the best thing for programming microcontrollers in higher languages ever: bit-banding. You can directly read and write bits like bytes. And they never use it! They probably ported the libraries from older devices that don't support that.

If $HPPEMIT would work I would probably use that and precompile the libraries, but otherwise I have to write all of it myself. And make sure that bit-banding works. Fortunately, more than 90% of the source is definitions and declarations, and only 10% is actual code. But even so, a lot of work.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: STM32, startup and enable USB as Virtual COM port
« Reply #8 on: July 03, 2019, 04:39:10 pm »
The simplest way would be to forget STM32 internal USB and use something external, like FTDI or CP210x. Or use MikroElektronika Pascal for STM32 and use their USB CDC implementation.

After more investigation and posting on the ST boards, it seems you are right. It's crazy.

MiR

  • New member
  • *
  • Posts: 39
Re: STM32, startup and enable USB as Virtual COM port
« Reply #9 on: July 03, 2019, 06:19:13 pm »
Stupid question, what's the goal are you trying to reach with your USB Driver?

Do you want to roll your own board that acts as a virtual com port server
or
do you have some project that needs to communicate via COM Port and it's a hobby project and you can do it with a NucleoF334 or a Nucleo F401 Board?

Michael

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: STM32, startup and enable USB as Virtual COM port
« Reply #10 on: July 03, 2019, 08:17:02 pm »
In short: I want to make it easy to use a microcontroller. I want something like Borland Pascal or Free Pascal running on the microcontroller itself.

In general, except for Arduino, getting an IDE and toolchain up and running is something that often takes weeks. If you know what you're doing. I want to be able to turn it on, connect with Putty or something like that, have it show the device status, and start programming.

It is for building electronics, so it should fit snugly on a breadboard, about the size of a 40-pin DIP or a little bigger. And it needs to have an USB port, to be able to connect to the terminal emulator (remote keyboard and screen, and optionally mass storage). Possibly a 4-pins debug connector.

Devices in that size have something in the order of 256 kB Flash and 64 kB RAM. About half that should be usable for storing and running user programs.

Laksen

  • Hero Member
  • *****
  • Posts: 614
    • J-Software
Re: STM32, startup and enable USB as Virtual COM port
« Reply #11 on: July 04, 2019, 12:50:10 am »
It shouldn't be too hard to port the original STM32F10x FW library USB stack. I had it almost completed at some point

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: STM32, startup and enable USB as Virtual COM port
« Reply #12 on: July 14, 2019, 05:53:29 pm »
Well, it's a mess. I really hate C libraries and header files. It is possible to do them right, but that rarely happens.

I looked into just writing it all myself in Pascal, but some parts (especially the USB) are very badly documented. And while there is a switch to generate different files with the startup code for each device, they all start by including many library headers. To find the full definition of a single function you have to reference a lot of different files.

Next plan: write a program that first combines all the referenced headers and code into just two files (one .h and one .c) and clean them up.

avra

  • Hero Member
  • *****
  • Posts: 1570
    • Additional info
Re: STM32, startup and enable USB as Virtual COM port
« Reply #13 on: July 14, 2019, 11:41:53 pm »
I want something like Borland Pascal or Free Pascal running on the microcontroller itself.
Take a look at this P-Code interpreter for MEGA AVR:
https://community.atmel.com/projects/pchip
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: STM32, startup and enable USB as Virtual COM port
« Reply #14 on: July 14, 2019, 11:55:48 pm »
Thanks for the link, but the problem isn't the compiler (I want to build a very custom one anyway), but the initialization and USB communication. Shouldn't be too hard or long, if I could extract it from the C library. A few hundred lines at most.