Jonathan Thomson's web journal

Getting Started with the STM32F4DISCOVERY in Linux November 17, 2011

Filed under: Electronics — jethomson @ 8:58 pm

Building an ARM toolchain
To compile code for the STM32F4DISCOVERY you’ll need an ARM toolchain that supports the Cortex-M3. I used the build script summon-arm-toolchain to build one. Before running the script you need to install several dependencies:

$ su -c "apt-get install flex bison libgmp3-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf texinfo build-essential libftdi-dev"

You might also need to run:

$ su -c "apt-get build-dep gcc-4.5"

Now make a directory to hold the files you’ll need to work with the STM32F4DISCOVERY:

$ mkdir ~/stm32f4discovery_root
$ cd ~/stm32f4discovery_root

Next clone the summon-arm-toolchain, read the README, and begin the build:

$ git clone https://github.com/esden/summon-arm-toolchain.git
$ cd summon-arm-toolchain
$ ./summon-arm-toolchain

 

Setting up the software to communicate with the STLINK
While the toolchain is compiling you can set up the software required to communicate with the STLINK, which is the on-board JTAG programmer/debugger on the lefthand side of the board. From a new shell run:

$ su -c "apt-get install libusb-1.0-0-dev"
$ cd ~/stm32f4discovery_root
$ git clone git://github.com/texane/stlink.git stlink_texane
$ cd ~/stm32f4discovery_root/stlink_texane
$ make

Now you should install the udev rules 49-stm32l-discovery.rules and restart udev. These steps will create a symbolic link called /dev/stm32l_stlinkN and give a normal user permission to access it.

$ su
# cp 49-stm32l-discovery.rules /etc/udev/rules.d/
# /etc/init.d/udev restart
# exit

 

Example projects and makefiles
Next, download the STM32F4DISCOVERY firmware package and an archive of makefiles for building some of the example projects found in the firmware package.

$ cd ~/stm32f4discovery_root
$ wget http://www.st.com/internet/com/SOFTWARE_RESOURCES/SW_COMPONENT/FIRMWARE/stm32f4discovery_fw.zip
$ unzip stm32f4discovery_fw.zip
$ wget http://jthomson.towhee.org/stm32f4discovery_files/STM32F4DISCOVERY_Makefiles.zip
$ unzip STM32F4DISCOVERY_Makefiles.zip
$ cd ~/stm32f4discovery_root/STM32F4DISCOVERY_Makefiles
$ cp Makefile.IO_Toggle ~/stm32f4discovery_root/STM32F4-Discovery_FW_V1.1.0/Project/Peripheral_Examples/IO_Toggle/
$ cd ~/stm32f4discovery_root/STM32F4-Discovery_FW_V1.1.0/Project/Peripheral_Examples/IO_Toggle

If the toolchain installation still hasn’t finished, then familiarize yourself with the example code and makefiles or take a break.

 

Compiling and Flashing
Once summon-arm-toolchain has finished building your ARM toolchain you can try it out on the IO_Toggle example by using the following commands.

$ cd ~/stm32f4discovery_root/STM32F4-Discovery_FW_V1.1.0/Project/Peripheral_Examples/IO_Toggle
$ make -f Makefile.IO_Toggle

If the build finished without error, then you’re ready to try downloading it to the board. Plug in the board and use stlink to write the blinking LED example, IO_Toggle.bin, to the STM32F4DISCOVERY’s flash memory.

$ PATH=~/stm32f4discovery_root/stlink_texane/flash:$PATH
$ flash write IO_Toggle.bin 0x8000000

Power cycle the board by removing then reinserting the USB plug. If the download was successful, the LEDs should light up in a clockwise pattern and pressing the pushbutton will not result in the board entering test mode.

You can easily modify one of the sample makefiles to work with another example by examining the .uvproj file (found in the example’s MDK-ARM directory) to determine which defines (-D), includes (-I), source files, etc. to add to the new makefile. Just open the .uvproj file in a text editor and look for lines like these:

<Define>USE_STDPERIPH_DRIVER,STM32F4XX</Define>
<IncludePath>
..\;
..\..\..\..\Libraries\CMSIS\Include;
..\..\..\..\Libraries\CMSIS\ST\STM32F4xx\Include;
..\..\..\..\Libraries\STM32F4xx_StdPeriph_Driver\inc;
..\..\..\..\Utilities\STM32F4-Discovery
</IncludePath>

<FilePath>..\main.c</FilePath>
<FilePath>..\..\..\..\Utilities\STM32F4-Discovery\stm32f4_discovery.c</FilePath>
<FilePath>..\..\..\..\Libraries\STM32F4xx_StdPeriph_Driver\src\stm32f4xx_rcc.c</FilePath>
etc.

 

Final Notes
There is a lot I don’t understand about compiling for the STM32F4DISCOVERY and I’m certain that the Makefiles I’ve provided could be improved. For example, I don’t know which -m options (see the Makefile’s MCUFLAGS variable) to set, when to set them, and why. If you have any corrections or improvements to share, please leave a comment.

 

Links
The STM32F4 high-performance discovery board (STM32F4DISCOVERY) and a range of of STMicroelectronics products are available at Newark
Alternate method of programming the STM32F4DISCOVERY using the Bus Blaster and OpenOCD
A fork of the summon-arm-toolchain that builds gcc for the Cortex-M4F
A comment discussing this fork

Advertisements
 

19 Responses to “Getting Started with the STM32F4DISCOVERY in Linux”

  1. Jope Says:

    Regarding your Makefile:
    You don’t need “-mthumb-interwork”, since Cortex-M3/4 are thumb-only processors. You also don’t need “-mlittle-endian”, since this is the default for ARM targets.

  2. rpx Says:

    I just want to say thank you for the text.

  3. Thank you so much… very clear and everything worked. The only minor deviations from your instructions were:

    1. The build ended up in ~/stm32f4discovery_root/summon-arm-toolchain/ for some reason.

    2. The latest version of the stlink_texane has two rule files 49-stlinkv1.rules (for older devices) and 49-stlinkv2.rules (for newer devices including the STM32F4-Discovery). The latter rules file creates a device of format /dev/stlinkv2_%n. )

  4. Thanks for your helpful post. I just compiled the Demonstration Project using Codesourcery Lite, flashed using st-flash and now have a working tool-chain.

  5. Witek Says:

    If you are compiling newer compiler, like gcc 4.7, then you will need small patch for libcmsis v1.10-3:

    --- src/core_cm3.c.orignal	2012-04-27 16:46:17.430297151 +0200
    +++ src/core_cm3.c	2012-04-27 16:42:45.851445817 +0200
    @@ -718,7 +718,7 @@
     {
        uint32_t result=0;
       
    -   __ASM volatile ("strexb %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
    +   __ASM volatile ("strexb %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );
        return(result);
     }
     
    @@ -735,7 +735,7 @@
     {
        uint32_t result=0;
       
    -   __ASM volatile ("strexh %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
    +   __ASM volatile ("strexh %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );
        return(result);
     }
     
    @@ -752,7 +752,7 @@
     {
        uint32_t result=0;
       
    -   __ASM volatile ("strex %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
    +   __ASM volatile ("strex %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );
        return(result);
     }
     
    

    Without this, compiler is going to generate output like strexb r0, r0, [r1], which is illegal, because r0 is used both as input and output register (and assembler will actually detect it and fail), and strexb says you cannot use same register as both input and output (it is one of few instructions in ARM which have this exception). Patch makes sure compiler will not generate such assembler code.

  6. Falco Says:

    Why I can’t find “flash” ?

    $ flash write IO_Toggle.bin 0x8000000
    No command ‘flash’ found,

    • Falco Says:

      I see now why. It should be “st-flash”
      ~/stm32f4discovery_root/stlink_texane/flash$ ls
      main.c main.o Makefile st-flash

      • jethomson Says:

        It was called ‘flash’ when I wrote this. This article is out of date. It looks like the process to build stlink-texane is now: ./autogen.sh; ./configure; make

  7. jacques Says:

    summon-arm-toolchain doesn’t compile. there is the following 2 errors in Makefiles.am

    > src/jtag/drivers/Makefile.am:8: `pkglibdir’ is not a legitimate directory for `DATA’
    > src/target/Makefile.am:166: `pkglibdir’ is not a legitimate directory for `DATA’
    > Makefile.am: installing `./INSTALL’

    Looking the files in gedit I see at the incriminate line:

    > nobase_dist_pkglib_DATA =

    There is nothing after the ‘=’
    ————-

    system Ubuntu 12.04
    gcc version 4.6.3

    Jacques

  8. monoem Says:

    when i run :
    make -f Makefile.IO_Toggle
    i get this error :
    /sat/bin/arm-none-eabi-gcc: not found

    • Here is something you may try to get further:

      Go to https://launchpad.net/gcc-arm-embedded en download the Linux install tarball. Difficult to find? Then check https://launchpad.net/gcc-arm-embedded/+download where you can get the files more directly. Choose the linux installation tarball.

      Once downloaded extract it. I presume you still have Ubuntu 12.04 but I don’t know how far you’ve update your system. Anyway the extracted contents is quite similar as the current content in your user home folder called ‘sat’. Check it to verify we’re on the same level.

      Now replace the ‘sat’ folder contents with the contents inside the folder you got after having extracted ‘gcc-arm-none-eabi-4_7-2013q3’ . Now that’s the name for the current release of the work of these folks, it’s September 2013 now so shortly what comes after ‘gcc-arm-none-eabi-‘ might be slightly different for you.

      The “/sat/bin/arm-none-eabi-gcc: not found” should now be overcome when you try the ‘make -f Makefile.IO_Toggle’ command.

      We’re not done yet.

      Another issue will pop up when getting into flashing the STM32F4 Discovery board. What also has changed sinced the original date of this blog (17 November 2011) is the contents of the stlink_texane folder so the following commands must be changed too:

      $ PATH=~/stm32f4discovery_root/stlink_texane/flash:$PATH
      $ flash write IO_Toggle.bin 0x8000000

      And executed these commands in ‘~/stm32f4discovery_root$ ‘

      But that didn’t help much since IO_Toggle.bin is not listed in ‘~/stm32f4discovery_root$ ‘ and the ‘flash’ command doesn’t exist (anymore).
      So I copied the produced files in ‘~/Project/Peripheral_Examples’ (IO_Toggle.bin, IO_Toggle.hex and IO_Toggle.elf) into ‘~/stm32f4discovery_root$ ‘ and changed the ‘flash’ command with ‘st-flash’ since that was closest of existing executable files listed in stlink_texane.

      Thus I changed the above with:

      $ PATH=~/stm32f4discovery_root/stlink_texane/:$PATH
      $ st-flash write IO_Toggle.bin 0x8000000

      And executed these commands again in ‘~/stm32f4discovery_root$ ‘ .

      The board successfully took the code and the IO_Toggle program executed. It keeps rolling the original demo (turning on leds 1,1+2,1+2+3,1+2+3+4 clockwise and then all off and again) but without simultaneous all 4 blinking and no more accellerometering (blue user button has no more effect).

      The next level is to get the toolchain working inside em::Blocks or Eclipse but that’s another story I still have to get myself through.

      Good luck.

  9. benzol Says:

    Nowhere arm-none-eabi-gcc is mentioned however my system returns the error that /home/benoit/sat/bin/arm-none-eabi-gcc is not found…

  10. Nicholas Buckeridge Says:

    Hi everyone!

    I have noticed that the link to http://jthomson.towhee.org/stm32f4discovery_files/STM32F4DISCOVERY_Makefiles.zip is brocken. So is the first one but I was able to download the files thanks to a bit of googling.
    can anyone email me the link or file that is meant to be leading tohttp://jthomson.towhee.org/stm32f4discovery_files/STM32F4DISCOVERY_Makefiles.zip.

    Thanks!

    Nicholas B


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s