Author Topic: STM32 without STM Libraries  (Read 26089 times)

0 Members and 1 Guest are viewing this topic.

Offline newbie666Topic starter

  • Regular Contributor
  • *
  • Posts: 73
STM32 without STM Libraries
« on: December 12, 2018, 09:26:22 am »
Hello,

I've got one of those stm32l152c discovery boards and I'm looking for a sane way of programming it. Ideally I'd like to use arm-none-eabi toolchain.

Are you aware of any examples on how to program those devices in a "bare bones" way? I've tried using CubeMX to generate makefile based projects but it includes lots of libraries I'll never use.

If not, can you help me to understand what I need exactly to create a bare bones template?
* I know I need a linker file (I can get that from CubeMX generated template)
* I need to initialize the clock (also done in CubeMX generated project but depends on many files so I guess I don't want to use it)
* I need to initialize interrupt table (what would be the simplest way to do it?)
* anything else I should have? I've seen other examples containing a header file with definitions of all the uC registers which seems like a great idea but I can't find one single file that would do that for my chip in those CubeMX projects.

Thanks for help,
Michal
 

Offline kapacuk

  • Newbie
  • Posts: 1
  • Country: gb
Re: STM32 without STM Libraries
« Reply #1 on: December 12, 2018, 10:28:50 am »
Have you looked at libopencm3?
 
The following users thanked this post: BravoV, newbie666, nugglix

Offline T.

  • Newbie
  • Posts: 3
  • Country: at
Re: STM32 without STM Libraries
« Reply #2 on: December 12, 2018, 11:25:12 am »
You also need to configure the GPIO, timer, UART, ...
It is a big step from cubeMX to bare bone. Why do you want bare bones?

With some changes you get smaller libs.
Cube MX option: Copy only the necessary library files.
Cube MX option: LL libraries instead of HAL
Don't define/use USE_FULL_LL_DRIVER, but this requires come changes.
 

Offline newbie666Topic starter

  • Regular Contributor
  • *
  • Posts: 73
Re: STM32 without STM Libraries
« Reply #3 on: December 12, 2018, 12:11:25 pm »
libopencm3 seems like a great suggestion! I've found a following tutorial explaining the process quite well:

https://rhye.org/post/stm32-with-opencm3-0-compiling-and-uploading/
 

Offline rhb

  • Super Contributor
  • ***
  • Posts: 3476
  • Country: us
Re: STM32 without STM Libraries
« Reply #4 on: December 13, 2018, 03:01:55 am »
If your goal is ARM embedded controllers, take a look at Mecrisp-Stellaris.

http://mecrisp.sourceforge.net/

Forth is a very different model from C.  The great beauty of it is you can interactively test your code before you commit it to memory.  C is close to the hardware, but forth is closer.

One of the first major applications for forth was controlling the railroad car mounted dishes at Kitt Peak Observatory almost 50 years ago.

Chuck Moore devised forth to simplify writing code to control weird hardware.  Matthias Koch has done a brilliant job implementing Mecrisp..
 
The following users thanked this post: MT, newbrain

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: STM32 without STM Libraries
« Reply #5 on: December 13, 2018, 05:03:26 am »
Or if you want to go even more bare bones, I have a few Github repositories holding the bare minimum device headers and latest CMSIS headers which they depend on. If you use that there will be no actual library available and everything must be done by direct access to registers using your own code and through readings of the manual. This can be the most laborious route for your purpose, but it will result in the smallest code size.
 

Offline mark03

  • Frequent Contributor
  • **
  • Posts: 708
  • Country: us
Re: STM32 without STM Libraries
« Reply #6 on: December 13, 2018, 04:37:08 pm »
If you use that there will be no actual library available and everything must be done by direct access to registers using your own code and through readings of the manual. This can be the most laborious route for your purpose, but it will result in the smallest code size.

 :-+
This is how I do most of my work, and it's a lot less work than people seem to think.  The best part is I only need ONE document open (the reference manual), and I can code straight from that, because the register and bit field names in the CMSIS headers follow the reference manual exactly, with very few exceptions.  When I am forced to use a library I am constantly hunting around for the right constants and function names; this wastes a lot of time and is no fun at all.  Obviously, writing your own drivers is more work in some sense, but if you stick with a family like STM32 you very quickly get to the point where you are just copy/pasting from previous projects with small modifications.
 

Offline newbie666Topic starter

  • Regular Contributor
  • *
  • Posts: 73
Re: STM32 without STM Libraries
« Reply #7 on: December 14, 2018, 10:31:51 am »
coming from smaller chips that's my favorite way of programming as well.

@technix can you link your github repo? I can't find a link on your website.

 

Offline rhb

  • Super Contributor
  • ***
  • Posts: 3476
  • Country: us
Re: STM32 without STM Libraries
« Reply #8 on: December 14, 2018, 01:17:48 pm »
If you use that there will be no actual library available and everything must be done by direct access to registers using your own code and through readings of the manual. This can be the most laborious route for your purpose, but it will result in the smallest code size.

 :-+
This is how I do most of my work, and it's a lot less work than people seem to think.  The best part is I only need ONE document open (the reference manual), and I can code straight from that, because the register and bit field names in the CMSIS headers follow the reference manual exactly, with very few exceptions.  When I am forced to use a library I am constantly hunting around for the right constants and function names; this wastes a lot of time and is no fun at all.  Obviously, writing your own drivers is more work in some sense, but if you stick with a family like STM32 you very quickly get to the point where you are just copy/pasting from previous projects with small modifications.

If that's the way you like to work you *really* should look at Mecrisp-Stellaris

http://mecrisp.sourceforge.net/

Matthias Koch has done a brilliant job in the implementation,  The great beauty of forth is the language is the operating system and the editor.  All you need is a dumb terminal window on a PC and a USB-serial adaptor with 3.3 V Tx & Rx lines.

There are over 30 ARM based boards it runs on, so it's almost certain you have a board that has a prebuilt image. The following list is *just* the STM32 based ARM ports.

STM L053 Discovery    STM32L053C8T6
STM Nucleo L152RE    STM32L152RE
STM Nucleo F207ZG    STM32F207ZG
STM Nucleo F303K8    STM32F303K8
STM Nucleo 401RE    STM32F401RE
STM Nucleo 411RE    STM32F411RET6
STM F0 Discovery    STM32F051R8
STM VL Discovery    STM32F100RB
Shenzhen LC Technology board    STM32F103C8T6 (aka the blue pill)
STM F3 Discovery    STM32F303VCT6
STM F4 Discovery    STM32F407VGT6
STM F429 Discovery    STM32F429ZIT6
Very small TSSOP20 packaged    STM32F030F4
STM L152 Discovery    STM32L152RBT6
STM L476 Discovery    STM32L476VG
STM F746 Discovery    STM32F746NG

Terry Porter has been building a very nice documentation site for Mecrisp.

http://hightechdoc.net/mecrisp-stellaris/_build/html/index.html

Forth is a very different mind set from assembly or C.  But once you understand the philosophy and how the outer interpreter works it's very intuitive.  If you're an HP calculator fan you'll feel right at home as it is RPN.

FWIW the STM32F030F4 has 16 KB of flash and 4 KB of SRAM.  That's about the size of the machine Chuck Moore developed forth on, except it's a much faster clock rate.
 
The following users thanked this post: pkplex, MT

Offline mark03

  • Frequent Contributor
  • **
  • Posts: 708
  • Country: us
Re: STM32 without STM Libraries
« Reply #9 on: December 14, 2018, 05:24:35 pm »
:-+
This is how I do most of my work, and it's a lot less work than people seem to think.  The best part is I only need ONE document open (the reference manual), and I can code straight from that, because the register and bit field names in the CMSIS headers follow the reference manual exactly, with very few exceptions.  When I am forced to use a library I am constantly hunting around for the right constants and function names; this wastes a lot of time and is no fun at all.  Obviously, writing your own drivers is more work in some sense, but if you stick with a family like STM32 you very quickly get to the point where you are just copy/pasting from previous projects with small modifications.

If that's the way you like to work you *really* should look at Mecrisp-Stellaris

I'm a big fan of RPN on my calculators, so I am "pre-biased" to appreciate this, but...  the thing I am missing when I browse those links is a concise argument for "why Forth?" apart from "it's cool and fun!"  I see that it is very memory-efficient, but I usually worry more about clock cycles than memory in my MCU projects these days.  Is the development time and ultimate code performance comparable between a seasoned Forth guru and a C/C++ expert, for applications like DSP and real-time control?  I'd be more interested in some case studies on real programming projects, than examples of blinky in 20 bytes.  E.g. my current project (an electrostatic field mill) involves managing a bunch of ping-pong buffer pairs and DMA streams, with just enough time to de-rotate IQ data coming in at ~ 3 MSPS, downsample, and re-modulate onto an optical link with error-correction coding. 
 

Offline rhb

  • Super Contributor
  • ***
  • Posts: 3476
  • Country: us
Re: STM32 without STM Libraries
« Reply #10 on: December 14, 2018, 09:33:09 pm »
Forth is intended for embedded control applications.  The Riyahd airport baggage system being one of the more well known applications.  The project was failing after expending a huge amount of time and money.  Forth, Inc rescued it.

The railroad car mounted radio telescope dishes at Kitt Peak were the first major application.  However, IIRC the Peachtree software was written in forth.  This link is a pretty good overview of the language:

https://users.ece.cmu.edu/~koopman/stack_computers/sec3_3.html

The place forth shines is when you're dealing with bare hardware with exotic peripherals such a variable baseline radio telescope.

I spent my career in reflection seismic processing.  For DSP work I tend to lean to a mixture of C & FORTRAN.  Because FORTRAN does not allow pointer aliasing a good compiler can generate faster code.  So the main program and everything except the pure numerical stuff will be C with tight encapsulation and scoping.

If you have to handle large multidimensional arrays FORTRAN notation is much easier to understand.  I've written C to access 3D arrays.  It's not pretty.  And the hard part of numerical work is getting the indexing correct.

I never made the transition to F9x for two reasons.  I could never find a book written for someone who had written parsers with lex and yacc and knew BNF notation and there simply wasn't anything I couldn't do with a mixture of C & F77.  I inherited a line oriented user defined format parser written in F77.  Because of the source of the data it was essential to use FORTRAN for the reads.  I wrote a FORTRAN routine which called a C routine which built a table of pointers to the FORTRAN internal reads and pointers to the structure fields where the results of the read were to be stored.  Once the table was initialized it simply executed the sequence of FORTRAN function calls until it hit a null function pointer and returned.  This produced a 6x speed up.  When you're reading 10-50 million lines of ASCII text that is a huge improvement.

That project was a port of 500K lines of VAX FORTRAN to Unix, so we had to replace all the VMS runtime library calls with code that called the standard C library.  You *really, really* don't want to pass strings between the two languages if you can possibly avoid it.  And you don't want to do any I/O in F77.  Just pure numerical work.

It is almost impossible to match a first class compiler with hand written assembly. In the late 90's there was a Japanese programmer whose hand coded square root was faster than the DEC Alpha compiler version.  He spent some time at UT Austin as a visiting scholar before returning to Japan. Sadly I can't find out his first name, but he created quite a stir in HPC, hence the invitation to Austin.  For reference, a 533 MHz  Alpha 164LX was 4-5 times faster than an x86 machine at the same clock rate.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9886
  • Country: us
Re: STM32 without STM Libraries
« Reply #11 on: December 15, 2018, 02:17:11 am »
Forth is excellent for top down design and bottom up coding. First we define the primitives, then we use them.

Decades ago, I was doing a lot of work with emerging hard disks and SCSI controllers.  I visited one of the controller manufacturers and look over their test setup.  It was written in Forth!

Why?  Well, first you define the SCSI ports, then you write code to send a message, then code to receive a message and then you move up to reading/writing a sector then you read/write an arbitrary number of sectors.  Eventually you reach the level where you read/write multiple sectors to random addresses and verify.

Each level is built on primitives defined at a lower level.  It was elegant!  If a new test idea came about, it was trivial to build it on top of the code that was already known to work.

I know exactly nothing about Forth but I did program my HP48GX calculator in a language that looks a  lot like Forth.  Always building on previous levels of code.
 

Offline rhb

  • Super Contributor
  • ***
  • Posts: 3476
  • Country: us
Re: STM32 without STM Libraries
« Reply #12 on: December 15, 2018, 02:44:41 am »
That's a very good description of where forth really is much better than the alternatives.  You're taking small steps and and every step is a progression built on tested code.

The best part is you can test it interactively before you compile it and move to the next word.
 

Online cncjerry

  • Supporter
  • ****
  • Posts: 1269
Re: STM32 without STM Libraries
« Reply #13 on: December 16, 2018, 05:05:59 am »
Save yourself some time and aggravation and use Eclipse/AC6 version from openstm32.org coupled with cubemx.  Both have been getting better over time. It is kept reasonably up to date with chips and firmware.

Jerry
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: STM32 without STM Libraries
« Reply #14 on: December 16, 2018, 03:03:48 pm »
coming from smaller chips that's my favorite way of programming as well.

@technix can you link your github repo? I can't find a link on your website.
CMSIS common headers: https://github.com/SushiBits/cmsis
Device-specific files and linker scripts: https://github.com/SushiBits/platform-stm32f0 for STM32F0 and similar for other series.
My repositories follows the UNIX folder structure though, not the ARM Keil one most other libraries use. I use stock Eclipse CDT + GNU MCU Eclipse plugin as my IDE, which largely follows UNIX folder structure.

I am also a contributor in this repository: https://github.com/dmitrystu/libusb_stm32. This is a minimalistic USB library, which is also otherwise dependency free.
« Last Edit: December 16, 2018, 03:05:37 pm by technix »
 
The following users thanked this post: boB

Offline richardman

  • Frequent Contributor
  • **
  • Posts: 427
  • Country: us
Re: STM32 without STM Libraries
« Reply #15 on: December 17, 2018, 02:44:49 am »
The problem with "I will just use the I/O registers and use the MCU reference manual" is that in fact the peripherals for different ST series can be different. Even the most basic of GPIO can have one single 32-bit register for both clear and set ops, or 2 16-bit registers, and it gets worse from there. Yea, it's not a big deal to know which one is which, but you end up with very specific MCU code.

There are better options. Libopencm3 is one, our JumpStart API is another. The key is to have higher level of functional API, not just wrappers for I/O registers.
// richard http://imagecraft.com/
JumpStart C++ for Cortex (compiler/IDE/debugger): the fastest easiest way to get productive on Cortex-M.
Smart.IO: phone App for embedded systems with no app or wireless coding
 
The following users thanked this post: andrewfernie

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: STM32 without STM Libraries
« Reply #16 on: December 17, 2018, 03:35:16 am »
The problem with "I will just use the I/O registers and use the MCU reference manual" is that in fact the peripherals for different ST series can be different. Even the most basic of GPIO can have one single 32-bit register for both clear and set ops, or 2 16-bit registers, and it gets worse from there. Yea, it's not a big deal to know which one is which, but you end up with very specific MCU code.

There are better options. Libopencm3 is one, our JumpStart API is another. The key is to have higher level of functional API, not just wrappers for I/O registers.
There are cases where you just cannot afford the library bloat and have to use the most minimalist option possible.

Also if you have existing drivers for other vendors or even other microcontroller series, you can try porting them over too. My POSIX-compatible UART driver was ported from an AVR UART driver.
 

Offline chickenHeadKnob

  • Super Contributor
  • ***
  • Posts: 1054
  • Country: ca
Re: STM32 without STM Libraries
« Reply #17 on: December 17, 2018, 04:05:47 am »
I would like to stay on topic but change the question slightly; If you are going with a minimal or bare metal approach is there an efficient way to utilize cubemx output, to leverage the pin planning and clock tree set up features and leave the bloat behind? I am looking to see what others are doing beyond the tedious manually going through the generated code. My current way is only good for a  a one time pass when setting up a project, and I keep thinking there must be a better way.
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: STM32 without STM Libraries
« Reply #18 on: December 17, 2018, 04:13:09 am »
I would like to stay on topic but change the question slightly; If you are going with a minimal or bare metal approach is there an efficient way to utilize cubemx output, to leverage the pin planning and clock tree set up features and leave the bloat behind? I am looking to see what others are doing beyond the tedious manually going through the generated code. My current way is only good for a  a one time pass when setting up a project, and I keep thinking there must be a better way.
For me I just screen cap CubeMX and maybe save the project file (without the code) as part of project documentation. That way I get some reference material when typing in the configuration.
 

Offline richardman

  • Frequent Contributor
  • **
  • Posts: 427
  • Country: us
Re: STM32 without STM Libraries
« Reply #19 on: December 17, 2018, 04:26:29 am »
CubeMX will definitely not be "minimal code", because even if you ask it to generate LL (low level) and not HAL code, it still do things like (sorry, just making up some names, I forgot their real names):

  make_port_into_altfunction(...);

and the code would be this hideous mess of

  if (io_pin > 15)
     modify_these_bits();
  else
     modify_other_bits();

When if you are optimizing for the smallest code, you would just write:

   // well, I know I am looking at port X pin Y
  portX->some_field &= ~some_bitmasks;
  portX->some_field |= some_bits;

Those two instructions will for sure much shorter than anything LL/HAL/CubeMX can do.

In theory, if the functions are static inline code, AND PROPERLY WRITTEN, the compiler could do a decent job of matching hand written code. The key here is properly written.
// richard http://imagecraft.com/
JumpStart C++ for Cortex (compiler/IDE/debugger): the fastest easiest way to get productive on Cortex-M.
Smart.IO: phone App for embedded systems with no app or wireless coding
 

Offline semir-t

  • Regular Contributor
  • *
  • Posts: 56
  • Country: ba
Re: STM32 without STM Libraries
« Reply #20 on: December 17, 2018, 08:34:30 am »
I think whatever API you use, at some point you will get restricted. This is why I use Reference manual and write my own drives for peripherals that I need. This way I have all the control, and I know what my program is doing.

I had opportunity to work with Arduino, and personally I think when you are starting with MCU programming you thing:  "This is cool" but later on when you start working on serious stuff you find out that libraries that are available can't do what you need (it can be speed, memory etc.). And also, you don't have idea how they work or what peripherals they use. For example, in Arduino you have ready library for servo control, but most of beginners don't know that when you start using this library, your Timer1 is taken.

So I started with bare-metal programing for AVR and when you learn how to do this stuff on one MCU then it easy to transfer to another. Currently, I am working also on STM32 and I didn't have any problems when migrating to this MCU.

I think this is way to go if you want to really know your stuff. Also, If you love Linux, Makefiles and VIM there is no IDE that could give you what these 3 do.
 

Offline emece67

  • Frequent Contributor
  • **
  • !
  • Posts: 614
  • Country: 00
Re: STM32 without STM Libraries
« Reply #21 on: December 17, 2018, 09:42:48 am »
.
« Last Edit: August 19, 2022, 02:05:34 pm by emece67 »
 
The following users thanked this post: chickenHeadKnob

Offline techman-001

  • Frequent Contributor
  • **
  • !
  • Posts: 748
  • Country: au
  • Electronics technician for the last 50 years
    • Mecrisp Stellaris Unofficial UserDoc
Re: STM32 without STM Libraries
« Reply #22 on: December 27, 2018, 12:03:53 am »
I wrote a CMSIS-SVD parser for Gnu AS some time ago so I didn't have to keep referring to the datasheet. I love datasheets as much as any electronics addict but as even the smallest STM32F chips have over 400 registers, the novelty soon wears off ;-)

http://hightechdoc.net/mecrisp-stellaris/_build/html/svd2gas.html?highlight=assembly

It should work on any STM32F which you have a SVD for.

Cheers,
Terry
 
The following users thanked this post: emece67

Offline techman-001

  • Frequent Contributor
  • **
  • !
  • Posts: 748
  • Country: au
  • Electronics technician for the last 50 years
    • Mecrisp Stellaris Unofficial UserDoc
Re: STM32 without STM Libraries
« Reply #23 on: December 27, 2018, 12:08:40 am »
Thanks for mentioning my Doc site. I'm a copious note keeper and the site was initially my private Mecrisp-Stellaris notes and code. They were already in py.sphinx form so rsync'ing to a cloud server was easy.

Cheers,
Terry
 

Offline techman-001

  • Frequent Contributor
  • **
  • !
  • Posts: 748
  • Country: au
  • Electronics technician for the last 50 years
    • Mecrisp Stellaris Unofficial UserDoc
Re: STM32 without STM Libraries
« Reply #24 on: December 27, 2018, 12:51:05 am »
I can't answer one of your questions because it appears only a seasoned Forth guru or a C/C++ expert would be qualified ;-)

I'm neither a seasoned Forth guru or a C/C++ expert, just a electronics technician and I believe that there is a mountain of Forth in real world applications, it's just that we never get to see most of them because the devices are all proprietary.

Forth was on the Philae lander recently, it's always been big in spacecraft mainly because so few MCU's are radiation hardened.

My Doc site http://hightechdoc.net/mecrisp-stellaris/_build/html/index.html does list every conceivable 'Blinky' from blocking loops, multitasking ,interrupt and assembly ones but they're designed to be grokked by someone who has never seen Forth before.

Luckily you don't need to go far to find complex Open Source Forth applications.

How about a 'tethered Forth' that implements, jtag, and a optimizing cross compiler hosted on a Ti Tiva Connected Launchpad connected to a target MSP430 ?

The source code is here: https://sourceforge.net/projects/mecrisp/files/mecrisp-across-0.6-experimental.tar.gz

And how to use it is here: http://hightechdoc.net/mecrisp-across/_build/html/intro.html


 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf