26
\$\begingroup\$

I have an STM32 discovery board and would like to be able to program it on Linux.

What is the easiest way to do this?

\$\endgroup\$

closed as too broad by Brian Carlton, old_timer, winny, Lior Bilia, Bimpelrekkie Sep 5 '18 at 7:20

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ I installed Codesourcery on LinuxhUbuntu and little bit stacked: PATH is ok but the program does not appear on dasboard or software centre. Maybe it is the Ubuntu I would change somehow but do not know how. If you have an idea, please write. Thank you. Tom \$\endgroup\$ – Tom Rert Mar 22 '13 at 8:14
  • 1
    \$\begingroup\$ @TomRitzmann PeterJ is right, please don't use answers to ask questions. Moreover, software problems are off-topic here, SuperUser is the right place. \$\endgroup\$ – clabacchio Mar 22 '13 at 11:28
12
\$\begingroup\$

An easy way to program and debug the STM32 Discovery board (or any STM32 using an ST-Link programmer) is to use the 'stlink' project https://github.com/texane/stlink (however OpenOCD seems popular too)

ST Nucleo boards also appear as a USB flash device, so don't even need stlink - just copy the file over to them.

There are some good pages on how to develop for STM32 discovery on Linux, such as http://gpio.kaltpost.de/?page_id=131 and http://torrentula.to.funpic.de/2012/03/22/setting-up-the-stm32f4-arm-development-toolchain/ and http://jethomson.wordpress.com/2011/11/17/getting-started-with-the-stm32f4discovery-in-linux/

However I found the last link the most useful. It shows how to build ST's STM32 projects as-is - The only change is to add his Makefile, which seems like a perfect solution.

On recent versions of Ubuntu, there is a package you can install which contains an ARM compiler:

sudo apt-get install gcc-arm-none-eabi

Note that the processors are all a bit different. STM32F0..4 will all need different compiler flags, and the linker script will be slightly different for each (although only really because of the changed RAM and Flash sizes).

Edit: if you want to get started really quickly you could also look at http://www.espruino.com. It's a JavaScript interpreter that runs on the STM32 itself, so once you've got 'stlink' installed so you can flash to the board you can just download an image from that site, flash it on, and then connect with a terminal application and start programming.

\$\endgroup\$
8
\$\begingroup\$

If You are more into text editors and Makefiles instead of using a GUI, you could do:

  • Install a toolchain providing arm-none-eabi-gcc. On Archlinux, you would need community/arm-none-eabi-binutils, arm-none-eabi-gcc and arm-none-eabi-newlib (and arm-none-eabi-gdb if you want to debug) all from the community repo, or https://launchpad.net/gcc-arm-embedded (Which can be found in Archlinux' AUR as gcc-arm-none-eabi-bin).
  • Decide, if and what library you want to use to access the hardware. From the top of my head, there are three common options:
    1. None. You write everything from scratch. Not recommendable for beginners.
    2. STM32Cube: A C lib provided by ST itself.
    3. Libopencm3: An open source lib supporting quite many cortex-m cores by different vendors.
    4. STM32PLUS: A C++ lib. However, I cannot say much more about it because I haven't tested it.
  • Create or copy your first project.
    1. Without a lib, write your own makefile, linker script, startup code and get a simple makefile running. Good luck ;)
    2. With STM32Cube: Download and install STM32CubeMX. Once unzipped the *.exe file is actually just a java file and you can run it using "java -jar filename.exe". The installation needs sudo. When done, create a project and generate the code for "Truestudio". That should give you a starting point with a linker script, startup code, some trivial main-function (and a makefile if I remember correctly). Actually, even if you don't use the STM32Cube lib, the STM32CubeMX is great for calculating the values for the clock tree and validating if you can configure the chip the way you think.
    3. With libopencm3: Get the libopencm3 examples, find an example matching your board and use this as a starting point. The examples should be ready to run. Just type "make". Then use that example as a starting point for your own development.
    4. With STM32Plus: I don't know. Sorry.
  • Get your project to the board. Either use

    1. The serial bootloader: stm32flash works great.
    2. The debug port: You can use openocd to talk to the debugging adaptor provided on the board. Openocd is great, but documentation is not always the best. When in doubt, join the openocd irc channel. The people there are really nice.
  • Code in a text editor and use command line tools. This tutorial will provide lots of tips.

Enjoy

\$\endgroup\$
5
\$\begingroup\$

Eclipse, GCC, and OpenOCD is one toolchain. It's recommended by EMCU-IT and there's additional information here. Those pages also recommend using an RTOS like FreeRTOS.org, but that's up to you.

And for help with compiling the STM32 examples in Linux go here. That link points to a makefile for the examples which can be invoked with

git clone git://github.com/snowcap-electronics/stm32-examples.git
cd stm32-examples
wget http://www.st.com/internet/com/SOFTWARE_RESOURCES/SW_COMPONENT/FIRMWARE/stm32_f105-07_f2xx_usb-host-device_lib.zip
unzip stm32_f105-07_f2xx_usb-host-device_lib.zip

A couple minor code fixes are also documented, but most of the project should work with

make CROSS_COMPILE=/path/to/arm-2011.03/bin/arm-none-eabi-
\$\endgroup\$
  • 1
    \$\begingroup\$ Thanks! I hadn't seen those links before. I'm amazed there isn't a more definitive guide somewhere though as different discovery boards have subtly different build flags. \$\endgroup\$ – Gordon Williams May 31 '12 at 14:52
  • \$\begingroup\$ Yeah, ST made a very cheap board and gave them away, thousands of them, and the STM32 itself is an awesome chip, all the way up to F4, but programming them is a huge pain in the butt. Their drivers are incredibly poorly documented, and not named well, and finding a IDE and tool chain that play well together for free is not too easy. \$\endgroup\$ – hak8or May 31 '12 at 15:23
1
\$\begingroup\$

I have had success with https://github.com/JorgeAparicio/bareCortexM (see also the linked blog posts). I'm floored that I can simply single-step through the code or browse the device memory instead of inserting debugging statements in my code or guessing what is going on inside the chip.

The bareCortexM project is an Eclipse template for developing with the Cortex M series, especially STM32, in C++ without an OS. It is configured to use openocd, gcc, and has scripts to flash and debug to several targets including some of the discovery boards. By following the instructions and installing the recommended Eclipse plugins I was able to use my STM32VLDISCOVERY on Ubuntu.

As recommended I've combined the eclipse template with the same author's libstm32pp C++ template library for STM32 hardware. libstm32pp provides a surprisingly complete replacement for CMSIS and the often criticized STM32 drivers with a programming model that lets you say things like PB10::setMode(gpio::cr::GP_OPEN_DRAIN_2MHZ) and PINB::setLow() or PINB::setHigh() all mostly compiled inline due to the C++ templates. The setup is very nice.

\$\endgroup\$
  • \$\begingroup\$ I no longer recommend the unmaintained bareCortexM; check out stm32plus at andybrown.me.uk/wk \$\endgroup\$ – joeforker May 7 '14 at 17:10
  • \$\begingroup\$ The problem with such device drivers is that they are inefficient. For proper high speed device drivers that do not waste cpu cycles you need an rtos underneath so you can do proper scheduling. \$\endgroup\$ – Martin Aug 14 '18 at 17:06
0
\$\begingroup\$

Maybe it would be useful for someone: my short article (on russian) and simple project. All in linux and without unnecessary things like eclipse.

Libraries was taken from ST website, makefile — from one of many GPL examples in internet.

\$\endgroup\$
0
\$\begingroup\$

Here is a small but innovative template project for the quick start using STM32F0 Discovery board under Linux or any other OS:

https://github.com/dobromyslov/stm32f0-chibios-template

Note that the project uses ChibiOS - a free and open source real time operating system so it's not exactly a bare bone implementation from scratch.

\$\endgroup\$
0
\$\begingroup\$

I use vim and arm-none-eabi-gcc along with all the usual linux dev tools. Linux is in my opinion superior dev environment for embedded work by far. For debugging I use stlink and arm-none-eabi-gdb.

\$\endgroup\$
0
\$\begingroup\$

Consider platformio. If you're at all comfortable with the command-line, you'll find that platformio eases the development process considerably. pio init can be used to set up a project. pio run leverages the toolchain to compile. pio run --target upload sends the code to the device. Platformio takes care of downloading the toolchain components, libraries, etc. as needed.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.