ARM microcontrollers STM32F. Quick start with STM32-Discovery

When studying imageany unfamiliar business, especially when it comes to microcontrollers, the question arises - "Where to start." Searches for articles on the keywords “Getting Started” are underway, inevitably there is a dilemma in choosing a development environment and a debugger. To help you decide on the answers to your questions, I will share my experience in mastering the 32-bit STM32F family of controllers from ST Microelectronics.

Controller selection


There were essentially two options - STM32F or NXP (LPC1xxx). For STM32F microcontrollers, my choice fell for several reasons.

Firstly, the cheap STM32VL-Discovery debug board. Some of you who are involved in electronics have probably managed to grab the free Discovery , which is distributed free of charge when participating in a contest from EBV-Electronik. Those who did not have time to submit an application or did not even know about such an opportunity should not be upset - the fee costs about 500 rubles. The price is more than affordable.

Secondly, the controllers themselves are pretty cheap. The simplest stone STM32F100C4T6 with 16 kilobytes on board costs less than 40 rubles! The price of stones of the older line with hardware USB (STM32F103) is also quite humane, about 120 rubles.

Thirdly, controllers are actively starting to gain interest from electronic engineers, articles on the development of STM32 appear on the Internet one after another. If that goes on, popularity may well reach the level of Atmel controllers.

LQFP48The only inconvenience for the ham radio compared to the Atmel is its small body. You can forget about DIP. The only housing available is the LQFP with 0.5 mm pitch . Nevertheless, with a certain dexterity, making a board with LUT is not difficult (we already read about this), so the small case is not a minus. In the photo on the left is my motherboard for the LQFP48 chassis.

A few words about the STM32VL-Discovery debug board: on board it has the ST-Link debugger and debugger (target) controller STM32F100RBT6 (128 kb flash, 8 kb RAM). All pins are wired to pins, there are two LEDs and a button. The debugger's SWD connector is pulled out, so the board can also be used like a regular ST-Link. This is in our favor, we don’t need to buy a programmer.

Choosing a development environment


Here, not everything is as simple as with Atmelovskaya AVR Studio. There are several options:
  • IAR . A commercial. Limit on the amount of executable code. Haha.
  • Keil . Same. Haha.
  • Atollic TrueStudio . Based on Eclipse, but nonetheless commercial. There is no code size limit in the free Lite version, but the “Get PRO version” ad regularly pops up. Haha.
  • Pure Eclipse with ARM plugin and ARM-GCC compiler. It seems like it’s cross-platform happiness, but out of nowhere an even more suitable candidate appears:
  • CooCox IDE (CoIDE). Also based on Eclipse, but free. It includes everything for working with ARM, allows you to download the necessary libraries without leaving the cash desk. It knows how to work with various debuggers (though not with ST-Link, but it costs installing GNU Debugger). The only negative - the assembly is only under Windows. Linux users will have to install Eclipse and configure it by hand. As usual. Well, nothing, we don’t get used to it.
Well, my choice is made. You can challenge it, offer to write in a notebook, compile with makefiles, or even stop at commercial solutions by opposing the holy GCC. But personally, I think CoIDE is simply an ideal development environment, at least for a quick start. Start with her. And there it will be seen.

Actually, Getting Started!


So, we need:
  1. STM32VL-Discovery Board
  2. STM32 ST-Link Utility
  3. CoIDE development environment is the latest version (currently 1.3.0).
Launch CoIDE. We are kindly offered to choose the manufacturer of the controller, then the chip itself. We select STM32F100RBT6 , it is he who is on Discovery. Next, we see the page for selecting plug-in libraries, which played a decisive role in choosing the development environment:

Coide

There is also a CMSIS library for the ARM Cortex M3 core, and STM32 Peripheral Library (a library for working with peripherals from ST Microelectronics), as well as CooCox's own developments for the selected microcontroller. We just need to tick off the required libraries and the thing in the hat. For the simplest LED blinker - and this is exactly what “Hello World" looks like on microcontrollers - we need CMSIS Core, CMSIS Boot, RCC (peripheral clock control) and GPIO (general-purpose input-output port control).

Without further explanation, I will write the code for the simplest program:

#include 
#include 
#include 
#include 
void Delay(volatile uint32_t nCount) {
	for (; nCount != 0; nCount--);
}
int main(void) {
	/* SystemInit() вызывается из startup_stm32f10x_md_vl.c */
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init( GPIOC , &GPIO_InitStructure);
	while (1) {
		GPIOC->ODR |= GPIO_ODR_ODR9;
		Delay(0x1FFFF);
		GPIOC->ODR &= ~GPIO_ODR_ODR9;
		Delay(0x7FFFF);
	}
	return 0;
}


We compile (F7), we observe the executable files appearing in the Debug directory - bin, hex, elf.

Controller firmware

We have prepared the program, now we are going to load it into the chip. We connect the board (I won’t explain about installing drivers), start the ST-Link Utility. The controller in Discovery is connected via SWD, so the first thing to do is switch the operating mode of the debugger from JTAG to SWD:

image

Then everything is transparent: the Target menu -> Program & Verify ... select the hex file, click Program and the firmware is completed. It remains only to press the Reset button on the Discovery board - the one with the black cap - and enjoy the blinking LED.

image

Congratulations. Your first program is written, compiled, flashed into a chip, and works successfully. The beginning has been made, and then it's up to you: we climb into the template code, as usual, change something, supplement it, study new peripherals, connect new libraries. And do not forget to read the Reference Manual, it has a lot of useful information.

Update: Starting with version 1.3.0, the CooCox IDE development environment supports ST-Link , so programming and debugging can be done directly from CoIDE, without using the ST-Link Utility. Select an adapter through the menu Debug -> Debug Configuration (ST-Link, SWD port). Downloading the program to the controller via the menu Flash -> Program Download.

That's all. I wish you success in the development of ST Microelectronics microcontrollers.

Related Resources

Also popular now: