There are two peripheral libraries which can be used. Firstly is the official ST libraries, or alternately libopencm3. There are more examples available for the ST libraries, however libopencm3 is nicer to use, but still in development.
STM32 Intro (Jon) slides here: stm32_intro_ukhas14.pdf
Ideally you should be able to apt-get install the tools, however the version in the repo is broken slightly and doesnt have the 'nano' version of newlib (printf etc)
export PATH=/usr/local/gcc-arm-none-eabi-4_8-2014q2/bin:$PATH
to your ~/.bashrc (assuming using bash).
$ source ~/.bashrc
$ sudo add-apt-repository ppa:terry.guo/gcc-arm-embedded
$ sudo apt-get update
$ sudo apt-get install gcc-arm-none-eabi
$ arm-none-eabi-gcc –version
cd
into the root of the repo$ git submodule update –init
to fetch libopencm3.cd
to the libopencm3 directory and run $ make
to build.We've not got things set up for the F1 (or other) series yet, give us a shout if this is what you're after and we can help.
Open up Makefile in firmware/src/. Adjust the last line for either the F0 or F4 as appropriate
include ../common/Makefile.f0.include
ORinclude ../common/Makefile.f4.include
Now open up common/stm32f0-discovery.ld or common/stm32f4-discovery. Adjust the RAM and ROM lengths as appropriate for your particular device. If you want to change the name of this file to satisfy your OCD, do so and then make the relevant change in firmware/common/Makefile.fx.include (where x=0 or 4 as appropriate).
st-util
and st-flash
binariesA libopencm3 LED blink example is provided in firmware/src/main.c.
cd
to this directory and build the firmware with $ make
- this should produce the main.elf
file.
If you're having issues then use $ make V=1
for more verbose build output.
$ st-flash erase
$ make bin
$ st-flash write main.bin 0x8000000
On Linux, st-flash needs root privileges ( sudo ./st-flash …
) to access the USB system until you set up udev rules
In theory you should be able to follow the linux instructions. You will need to have make installed, as well as python.
This guide will get a windows IDE based toolchain up and running. There is the option of either using ST or openlibcm3 libraries (see above). This guide uses 'coIDE,' however there are several available. This has the advantage that the ST libraries are 'built in', and so you just need to click on the ones you want and they are copied into the project. libopencm3 can still be used, but it requires a few more tweeks
These are the drivers and downloading program for ST-Link, which is found on the ST development boards
The STM32s also have a UART booloader, the tool to download is here: http://www.st.com/web/en/catalog/tools/PF257525 (you dont need this if you intend to use the SWD interface on the development boards)
(skip to the next step if you want to use libopencm3)
This example will now get an LED flashing. After project creation the 'repository' window should be showing, which has a selection of libraries that can be copied to the project. (if not, go view→repository)
With the repository showing, click 'GPIO'. A whole load of files should have been copied into the project. Also click 'C library' (for printf/sprintf etc).
Open main.c, and copy in the sample code below
Firstly add the libopencm3 files to the project directory. This can be fetched from the libopencm3 repository and compiled as per above instructions, or run git submodule add https://github.com/libopencm3/libopencm3 .\firmware\libopencm3
if you want to add it to an existing repository. Since it is unlikely for all the tools (make, python and others) to be set up correctly. As a result, a precompiled version is available here. Unzip the contents into a separate libopencm3 folder along side your project
The final file needed is part of the linker script. Copy this file (f4) or this file (f0) along side your project. Note that this file needs editing depending on how much flash/RAM the target has
Now set up the IDE with these files:
To run the program:
coIDE defaults to STLink to download programs, however if you are having issues, check 'Download' settings in the project configuration
#include "stm32f0xx.h" #include "stm32f0xx_gpio.h" #include "stm32f0xx_rcc.h" //include further headers as more peripherals are used int main(void) { //turn on GPIOC //IMPORTANT: every peripheral must be turned on before use RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); //init structure for GPIO GPIO_InitTypeDef GPIO_InitS; GPIO_InitS.GPIO_Pin = GPIO_Pin_9; //the pin we are configuring GPIO_InitS.GPIO_Mode = GPIO_Mode_OUT; //set to output mode GPIO_InitS.GPIO_OType = GPIO_OType_PP; //set to push/pull GPIO_InitS.GPIO_PuPd = GPIO_PuPd_NOPULL; //no pullup resistors GPIO_InitS.GPIO_Speed = GPIO_Speed_50MHz; //set to max speed GPIO_Init(GPIOC, &GPIO_InitS); //write this config to GPIOC while(1) //flash forever { GPIO_SetBits(GPIOC, GPIO_Pin_9); //set pin on int32_t i = 4800000; while(i) i--; //delay a bit GPIO_ResetBits(GPIOC, GPIO_Pin_9); //set pin off i = 4800000; while(i) i--; //delay a bit } }
#include <libopencm3/stm32/rcc.h> #include <libopencm3/stm32/gpio.h> #define LED_PORT GPIOC #define LED_PIN GPIO9 int main(void) { // Set clock to 48MHz (max) rcc_clock_setup_in_hsi_out_48mhz(); // IMPORTANT: every peripheral must be clocked before use rcc_periph_clock_enable(RCC_GPIOC); // Configure GPIO C.9 as an output gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN); // Flash the pin forever while(1) { gpio_set(LED_PORT, LED_PIN); int32_t i = 4800000; while(i) i--; gpio_clear(LED_PORT, LED_PIN); i = 4800000; while(i) i--; } }
while(1) //flash forever { GPIOC->BSRR |= (1<<9); int32_t i = 4800000; while(i) i--; GPIOC->BRR |= (1<<9); i = 4800000; while(i) i--; }