STM32 Discovery Development On Linux

STM32 Discovery Development On Linux
STM32 Discovery Development On Linux_第1张图片


Summary

The STM32F4Discovery is a $15 development board, featuring a 168Mhz ARM Cortex M4 (STM32F407VGT6) The ARM is programmed via an STLINK/V2 interface connected to a PC's USB port (i.e. requires no JTAG plug, ICD, BDM, proprietary dongle, etc.)

There are four different Windows compilers+IDEs mentioned for use with the board in STM's materials. From what I've seen the evaluation versions are limited in executable size, with the licensed versions costing more than a student/hobbyist would like to spend. These instructions explain show to use ARM's free GNU Tools for ARM Embedded Processors toolchain, a port of GCC maintained by Advanced RISC Machines (ARM Ltd) for the Cortex-R/M families for "bare metal development" which includes glibc support.

These instructions have been tested for Ubuntu 12.04 LTS, GNU Tools for ARM Embedded Processors 4.7.3, and 2013.03.06 commit of STLINK. They may also work for other versions.

 


Allow Users Access to USB Devices

By default, only the superuser has access to USB devices. Change things so that you have access to the F4 Discovery USB STLINK device:

Create a new udev rule in /etc/udev/rules/45-usb-stlink-v2.rules:

SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="3748", MODE="0666"

 

The easiest thing to do at this point is reboot. Or you can try:

sudo service udev restart
Disconnect the F4 Discovery USB cable and reconnect it

 



STM32 Discovery Development On Linux_第2张图片


Download the GNU/ARM toolchain

I am using the GNU Tools for ARM Embedded Processors toolchain. This GNU toolchain is maintained by ARM for the Cortex-R/M families for "bare metal development." It includes glibc support.

On the download page, grab the Linux current installation tarball. Download this to a convenient spot and unpack it using something like this, but get the current release if there is a newer one. (You can download the tar.bz2 using your browser if you don't want to use wget.)

cd ~
wget https://launchpad.net/gcc-arm-embedded/4.7/4.7-2012-q4-major/+download/\
gcc-arm-none-eabi-4_7-2012q4-20121208-linux.tar.bz2
tar -xvjf gcc-arm-none-eabi-4_7-2012q4-20121208-linux.tar.bz2
rm gcc-arm-none-eabi-4_7-2012q4-20121208-linux.tar.bz2	            #Optional clean-up

 

The toolkit is now in the ~/gcc-arm-none-eabi-4_7-2012q4 directory. Add this directory to the end of the path with something like:

export PATH=$PATH:~/gcc-arm-none-eabi-4_7-2012q4/bin

 

As a sanity check, run the compiler to display the version. You should see something like this:

$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.7.3 20121207 (release)
[ARM/embedded-4_7-branch revision 194305]
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 

You need to add the toolchain directory to the search path in any console you are building in, so I'd recommend adding it to your ~/.bashrc or some convenient script you run to set up the build environment, etc

 


Download and Build STLINK

The F4 Discovery board uses an interface called STLINK for programming and debugging firmware. The software that talks to the STLINK interface is also called STLINK.

In order to retreive and build STLINK, you'll first need to install a couple of packages:

sudo apt-get install autoconf pkg-config libusb-1.0 git

 

Retreive a copy of the STLINK source:

cd ~
git clone https://github.com/texane/stlink.git

 

Build the code following the instructions in the README, with:

cd ~/stlink
./autogen.sh
./configure
make

 

As a sanity check, look in ~/st-link and you should see st-util, st-flash, etc...

 this the how to use STLINK

HOWTO
=====

First, you have to know there are several boards supported by the software.
Those boards use a chip to translate from USB to JTAG commands. The chip is
called stlink and there are 2 versions:
. STLINKv1, present on STM32VL discovery kits,
. STLINKv2, present on STM32L discovery and later kits.

2 different transport layers are used:
. STLINKv1 uses SCSI passthru commands over USB,
. STLINKv2 uses raw USB commands.

Common requirements
~~~~~~~~~~~~~~~~~~~

. libusb-1.0  (You probably already have this, but you'll need the
development version to compile)
. pkg-config

IF YOU HAVE AN STLINKv1
~~~~~~~~~~~~~~~~~~~~~~~
The STLINKv1's SCSI emulation is very broken, so the best thing to do
is tell your operating system to completely ignore it.

Options (do one of these before you plug it in)
   *) modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:i
or *)1. add "options usb-storage quirks=483:3744:i" to /etc/modprobe.conf
   *)2. modprobe -r usb-storage && modprobe usb-storage
or *)1. cp stlink_v1.modprobe.conf /etc/modprobe.d
   *)2. modprobe -r usb-storage && modprobe usb-storage

IF YOU HAVE AN STLINKv2
~~~~~~~~~~~~~~~~~~~~~~~

You're ready to go :)

COMPILING
~~~~~~~~~
This project was converted to Autotools by a well meaning individual. The
following steps will build the project for you.

$ ./autogen.sh
$ ./configure
$ make

USING THE GDBSERVER
~~~~~~~~~~~~~~~~~~~
To run the gdb server: (you do not need sudo if you have set up
permissions correctly)

$ make && [sudo] ./st-util

There are a few options:

./st-util - usage:

  -h, --help        Print this help
  -vXX, --verbose=XX    specify a specific verbosity level (0..99)
  -v, --verbose specify generally verbose logging
  -s X, --stlink_version=X
            Choose what version of stlink to use, (defaults to 2)
  -1, --stlinkv1    Force stlink version 1
  -p 4242, --listen_port=1234
            Set the gdb server listen port. (default port: 4242)

Then, in your project directory, someting like this...
(remember, you need to run an _ARM_ gdb, not an x86 gdb)

$ arm-none-eabi-gdb fancyblink.elf
...
(gdb) tar extended-remote :4242
...
(gdb) load
Loading section .text, size 0x458 lma 0x8000000
Loading section .data, size 0x8 lma 0x8000458
Start address 0x80001c1, load size 1120
Transfer rate: 1 KB/sec, 560 bytes/write.
(gdb)
...
(gdb) continue

Have fun!

Resetting the chip from GDB
===========================

You may reset the chip using GDB if you want. You'll need to use `target
extended-remote' command like in this session:
(gdb) target extended-remote localhost:4242
Remote debugging using localhost:4242
0x080007a8 in _startup ()
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) run
Starting program: /home/whitequark/ST/apps/bally/firmware.elf 

Remember that you can shorten the commands. `tar ext :4242' is good enough
for GDB.

Setting up udev rules
=====================

For convenience, you may install udev rules file, 49-stlinkv*.rules, located
in the root of repository. You will need to copy it to /etc/udev/rules.d,
and then either reboot or execute
$ udevadm control --reload-rules

Udev will now create a /dev/stlinkv2_XX or /dev/stlinkv1_XX file, with the appropriate permissions.
This is currently all the device is for, (only one stlink of each version is supported at 
any time presently)

Running programs from SRAM
==========================

You can run your firmware directly from SRAM if you want to. Just link
it at 0x20000000 and do
(gdb) load firmware.elf

It will be loaded, and pc will be adjusted to point to start of the
code, if it is linked correctly (i.e. ELF has correct entry point).

Writing to flash
================

The GDB stub ships with a correct memory map, including the flash area.
If you would link your executable to 0x08000000 and then do
(gdb) load firmware.elf
then it would be written to the memory.


FAQ
===

Q: My breakpoints do not work at all or only work once.

A: Optimizations can cause severe instruction reordering. For example,
if you are doing something like `REG = 0x100;' in a loop, the code may
be split into two parts: loading 0x100 into some intermediate register
and moving that value to REG. When you set up a breakpoint, GDB will
hook to the first instruction, which may be called only once if there are
enough unused registers. In my experience, -O3 causes that frequently.

Q: At some point I use GDB command `next', and it hangs.

A: Sometimes when you will try to use GDB `next' command to skip a loop,
it will use a rather inefficient single-stepping way of doing that.
Set up a breakpoint manually in that case and do `continue'.

Currently known working combinations of programmer and target
=============================================================

STLink v1 (as found on the 32VL Discovery board)

Known Working Targets:
* STM32F100xx (Medium Density VL)
* STM32F103 (according to jpa- on ##stm32)

No information:
* everything else!

STLink v2 (as found on the 32L and F4 Discovery boards)
Known Working Targets:
* STM32F100xx (Medium Density VL, as on the 32VL Discovery board)
* STM32L1xx (STM32L Discovery board)
* STM32F103VC, STM32F107RC, STM32L151RB, STM32F205RE and STM32F405RE on custom boards
  (https://github.com/UweBonnes/wiki_fuer_alex/layout/usps...)
* STM32F303xx (STM32F3 Discovery board)
* STM32F407xx (STM32F4 Discovery board)
* STM32F429I-DISCO (STM32F4 Discovery board with LCD)
* STM32F439VIT6 (discovery board reseated CPU)
* STM32L151CB (custom board)
* STM32L152RB (STM32L-Discovery board, custom board)

Please report any and all known working combinations so I can update this!

Download STM32 Libraries and Blinky Example

Download my modified version of the STM library code and blinky example project

cd ~
git clone https://github.com/rowol/stm32_discovery_arm_gcc

 

If you want the unmodified STM library source download that from STM's website (I've added Makefiles and linker files to build STM's example projects w/ the GNU/ARM toolchain, but left everything else intact..)

 


Compile Your Source Code

It's not important how this app works, the idea is just to get something that works, then try building it and programming it to the F4 Discovery board so that we can verify the build/program/run sequence works.

The important part of the project is the Makefile

Edit the Makefile, and set STLINK to point to where you have installed it:

STLINK=/home/rowol/stlink             #Example, change to your location

 

Make STM_COMMON point to where you installed the STM32 Library code:

STM_COMMON=/home/rowol/stm32_discovery_arm_gcc/STM32F4-DISCOVERY_FW_V1.1.0

 

Build the code by running make:

make

 


Program ARM's Flash and Run Code

There are at least two possible ways to program the flash

 

Makefile

make burn

The burn target programs the flash using st-flash. After the flash programming completes, press the black reset button on the F4 Discovery and the code will run.

 

GDB (debugger)

In the directory with your source code, I've also add a .gdbinit file which contains:

define reload
kill
monitor jtag_reset
load
end

target extended localhost:4242
load

This will execute whenever you start GDB in the source directory

 

To start up:

~/stlink/st-util                 #Start GDB server
arm-none-eabi-gdb main.elf       #In a second terminal window, start GDB

continue #Starts code running, or use other GDB commands to # set breakpoints, etc first...

 

    • If you need to make changes and recompile the code, you can reload the same application (i.e. .bin file) from within GDB with this sequence:
    • Hit ^C if the code is running, to get to a gdb prompt
    • reload      #Macro we defined in .gdbinit, it does kill/monitor/jtag_reset/load sequence
    • Start new code running again with 'run' or other debugger commands

转载于:https://www.cnblogs.com/marklin/p/STM32.html

你可能感兴趣的:(STM32 Discovery Development On Linux)