Skip to content

Tutorial1_HelloWorld

Olavi Kamppari edited this page Jun 15, 2016 · 9 revisions

Hello World

This tutorial page describes how to setup EmBitz IDE for STM32F103 and execute a program displaying text "Hello World". All detailed steps are shown here and it can look very long and tedious, but in practice all the required steps can be executed in 1 minute.

EmBitz has several benefits compared to other MCU IDEs. This includes

  • Very fast
  • Excellent editor
  • Excellent debugger
  • Large number of supported platforms

The default environment for STM32F103 includes the STM Standard Peripheral Library. In this tutorial step, the whole library is included. In the subsequent pages, only the required files will be included.

Setup Project

Once EmBitz IDE is started, select File menu and then New Project...

New Project

From the Category pane, select STmicro-ARM. If you are doing large number of projects with the same F103, you can save one as an User Template and skip over some of the subsequent selection steps.

Select Template

Enter a name (HelloWorld) for the project and select a folder where the project files will be stored. This folder contains all required files and can be later moved into a different location. If the external references are done with relative paths, then there are limitations in moving the folder around. For that reason, in this tutorial, all external references are with absolute paths.

Enter Project Title

EmBitz allows one project to have several configurations for different usage cases. The two default configurations are:

  • Debug, to enable functionality required for trouble shooting, assurance checks, and quality assurance testing
  • Release, to optimize for speed, memory usage, or any other criteria for the final environment

The default settings are good for this tutorial.

Accept the Default Configurations

Select the Cortex family. F103 is a member of Cortex M3 Device Family.

![Select M3](https://github.com/OliviliK/STM32F103/blob/master/BLOB/Images/HelloWorld/5-Select M3.png)

Select the STM32F10x Device Series.

Select STM32F10x

From the pull-down list select STM32F103CB processor as used in Blue Pill board.

![Select STM32F103C8](https://github.com/OliviliK/STM32F103/blob/master/BLOB/Images/HelloWorld/7-Select STM32F103C8.png)

The HelloWorld project uses printf call that prints to a pane in EmBitz IDE through ST-LINK connection. This feature is known as Semihosting. It works only in Debug configuration. For other configurations, the print functionality is available through USB or UART connection connected to applications such as KiTTY, Processing, and Python. By default, this feature is unselected.

By default, the STMicroelectronics Standard Peripheral Library is selected. This is proper for the HelloWorld project. In the subsequent tutorial steps, the SPL is not used in the default way.

![Enable Semihosting](https://github.com/OliviliK/STM32F103/blob/master/BLOB/Images/HelloWorld/8-Enable Semihosting.png)

The Serial Wire Debug is selected as the default ST-LINK interface. Use all the default settings.

![Use Default Debugger Interface](https://github.com/OliviliK/STM32F103/blob/master/BLOB/Images/HelloWorld/9-Default Debugger Interface.png)

Accept the debugger settings.

Accept Interface Options

Review Involved files

These steps are not required. They are shown to demonstrate what is involved in EmBitz development for STM32F103.

By default, the files used in the project are shown in the project tree organized by file types

  • Sources for all C-files
  • Header for all H-files
  • ASM for all S-files used in the initialization routines written in Assembly language
  • Others for all other files, such as
    • Text files (txt)
    • Loader files (ld)

Workspace Overview

There are several options how to show the included files. In this case, the files are shown in the way how they are stored in the subfolders of the project folder.

In Sources SPL src folder, there is misc.c for miscellaneous functions and there are several files for the peripheral devices.

In Sources src folder, there is main.c for the main program and system_stm32f10x.c for setting the clock speed and doing similar initialization steps. In a typical case, there is no need to modify this system file.

Source Files

In the Headers cmsis (Cortex Microcontroller Software Interface Standard) folder. there are core definition files created by ARM Limited. There is no need to modify them. The EmBitz Project Build Options can be used to setup #define constants to control how these files are used.

In Headers inc folder, there are standard system files from STMicroelectronics. There is no need to modify them. The EmBitz Project Build Options can be used to setup #define constants to control how these files are used. The comments in these files are promoting direct editing of them, but that is not recommended.

In Header SPL inc, there are the definition files for the Standard Peripheral Library (SPL).

Header Files

In the ASM Sources src folder, there is an assembly program that is executed on every startup. There is no need to modify this file. In the EmBitz Debug Interface Options, it can be selected if the debugger has its first breakpoint in the beginning of this assembly file or in the beginning of the main() function.

In the Others folder, readme.txt reminds that the memory locations and lengths in the loader files should be checked. That is not required, in case the normal project setup steps are taken.

In the Others folder, the loader files allow the loading either into flash memory or SRAM memory. The selection of the load target is done in EmBitz Project Build Options, by selecting the Linker script for the target device. There is no need to modify these system files.

ASM and Others

Setup System View Description File (SVD)

This step is required only once.

Start the SVD file selection by selecting the repository.

Find the SVD Setup Dialog

In the project setup, the vendor and device are already selected. You have to select where to store the SVD file.

Select SVD

In this tutorial, I have created CommonLibs folder for all STM32F103 related library items and similar common files. It is OK to store the SVD file somewhere else.

Store SVD in Library Folder

By default, the location of the SVD file is shown as relative path. I did modify manually the Destination path into absolute format.

Change the Relative Path to Absolut

In the subsequent tutorial steps, the SVD file location is set in the Debug Interface Options.

Confirm Absolute Path

Edit the Main Function

I did add the project name (Helloworld) in front of the file name (Main.c). This is a good practice for the typical cases where there are several Main.c files open in EmBitz editor.

Default Main Function

Before the while(1) line, I did add printf("\fHello World\n"); line, where

  • \f will clear the Debugger-Target pane. Without that, the pane will contain old text from past executions
  • \n will send a new line character to the Debugger - Target pane. Without that the "Hello World" text would not be shown.

Edited Main Function

Build Hello World Project

The project build is done with F7. The results are shown in the Build Messages pane.

Build with F7

Start Debugging Session

After a successful build, debugging session is started with F8. The layout is totally configurable in EmBitz. In this case the panes do show the following:

  • CPU registers
  • Project tree
  • Peripheral registers (as defined in SVD)
  • Files in EmBitz editor
    • The Assembly file is shown, because the Run to main() option was not selected
  • Dissembly pane. For assembly programs this is not relevant. For C-programs, the dissembly shows the machine code created by compiler
  • Logs & Others as general location for useful information
    • The sub-tabs are used to select what is shown
    • In this case, the Debugger - Target is selected to show what the program is printing Start Debugger with F8

Start Execution

The program execution is started with F5. This will lead to the infinite loop with the while(1) statement. Before that, the printf statement results are sent to the Debugger - Target pane.

Start Execution with F5

The infinite loop is stopped with the Debugger Stop icon or with a selection in Debugger menu. There is no function key for this.

After the debugger is stopped, the debugger session is ended with F8. This will reopen the EmBitz editor in the way it was before the debugger session started.