STM32 fast start. Part 1 software, materials, Cube MX

  • Tutorial
Recently, more and more often I come across holivars on the topic of Cube MX and HAL, as applied to STM32 controllers.

On the one hand, there are advocates who like the ease of configuration and the readability of the code.

On the other hand, adherents write everything with their hands, for which speed of work and careful use of resources are important.

In order to dot all the i - try to write “Hello world” with the three most frequently used paths CMSIS, LL, HAL. We will estimate the costs (controller resources, the volume of the executable file, and of course the developer’s time).

The article will consist of several parts:

STM32 fast start. Part 1 software, materials, Cube MX.
STM32 fast start. Part 2 Hello World on HAL, setting up debugging in Atollic TrueSTUDIO
STM32 fast start. Part 3 Hello World on LL
STM32 fast start. Part 4 Hello World on the CMSIS
STM32 fast start. Part 5 Summary, Comparison of HAL, LL, CMSIS.

First, let's decide what we will actually program, that is, find the right hardware.

An ideal option would be a budget board on the STM32F103C8T6 microcontroller.

image

This fee can be found on the entire well-known site at a price of 100 Russian rubles.

Search keywords: STM32F103C8T6 on ARM the STM32 the Minimum

https://ru.aliexpress.com/af/STM32F103C8T6-ARM-STM32-Minimum.html?SearchText=STM32F103C8T6+ARM+STM32+Minimum

To fill the firmware and play with debugging - you also need a programmer
For starters, and for future use, the Chinese clone of the ST-LINK V2 programmer is ideal.

You can buy on the same site at a price of 120 Russian rubles.

image

Search by keywords ST LINK Stlink ST 252dLink V2 Mini STM8 STM32:

https://en.aliexpress.com/af/ST-LINK-Stlink-ST%25252dLink-V2-Mini-STM8-STM32.html?SearchText=ST+ LINK + Stlink + ST% 252dLink + V2 + Mini + STM8 + STM32

You can use various IDEs to develop software for STM32.

The most popular are IAR, Keil, Coocox (Eclipse).

We will follow the path that recently has been completely free and fully provided by ST itself.

We will use Atollic TrueSTUDIO for STM32 or in the common people Tolik.

What are the advantages of this software: absolutely free, there is no limit on the size of the code, there is a good debugger, easy installation and configuration.

Cons: no auto code completion.

Versions for windows and linux are available.

Download here https://atollic.com/resources/download/
There shouldn’t be any problems with installing this software, everything is intuitively clear, we choose where to put it and click "next" all the time.

After installation, you can not run it, because in addition to the IDE itself, you need something else.

If you still started it, just close it.

Since TrueSTUDIO is a development and debugging tool, I would like not to assemble the project by hand (connecting the required libraries and prescribing the paths), but to get some pre-configured file in which you can immediately write code without unnecessary troubles.

To do this, use the Cube MX code generator program or in the common people “Kalokub”.
This software is the first stumbling block in holivars on what to write under STM: on registers and CMSIS or on HAL.

Defenders of the first ideology make the following arguments: Cube MX generates a huge, unnecessary amount of code, which also slows down the work of MK.

Defenders of the second - say that the automatically generated code reduces development time, allowing the developer to quickly switch to the gutter part of the device (to the main logic), giving the peripheral routine to the specialized software (Cube MX).

Oddly enough - both of these ideologies are true and applicable in practice, but only each in its own conditions.

Let's look at a couple of examples:

Example No. 1: It is required to develop a device that is as cheap as possible, since it is planned to produce in batches of 100500 pieces annually. Naturally, every extra ruble of the device’s price will result in hundreds of thousands of rubles of expenses on the final device. Moreover, in the planned development there are a couple of heavy calculations and work with peripherals (ADC, SPI, UART) at maximum speeds.

The device is a fully self-contained product; in the future, minimal changes are planned for the entire production period of this equipment. The development period until the finished sample is 1-2 years.

Example No. 2: A prototype of a device is required, which may be of interest to the customer and he will order 100 pieces of similar devices for the conversion of his facility. The first planned batch should be shipped to the customer in 2 months. The size of the first test batch of 10 pieces.

The exact technical specifications will be adjusted in the process of working on the project, but it is known that in the future several revisions of the hardware are planned, for which it is necessary to quickly adjust the entire applied logic.

In the first example, the ideal option would be to select the cheapest controller and write a hardware-dependent, but optimal code, where work with peripherals will be organized through access to the appropriate registers (CMSIS). The developer who is involved in this project should have good or excellent knowledge of the periphery of a particular MK family. Ideally - when trying to wake him up at night - he should immediately name the address of the required vector from the table of interrupt vectors.

In the second example, the choice of controller is determined by the available hardware, as well as the time required to write the functionality required by the customer. Therefore, the speed and optimality of the software itself is fading into the background. There is no time for manual initialization, just as there is no time for working out hardware dependencies.

In this case, a controller is selected that can be quickly put into production in the current region, it is initialized using Cube MX, the application logic is written in HAL, and the prototype is transferred to the customer for testing. Such a project can be conducted by any average developer who has acquired skills in working with the target programming language. An understanding of the intricacies of the work of the periphery is practically not required.

No matter how sad it may sound - in the realities of modern device development in Russia - example No. 1 is becoming less common, passing the baton to example No. 2.

We will return to the discussion of examples No. 1 and No. 2 at the very end of the series of articles, and now we will continue with the preparation of the workspace.

At this stage, take a short break, go to my.st.comand register an account on it, since the ST company policy does not allow downloading the necessary materials without registration.

After we got access to the site - download STM32 Cube MX.

At the very bottom of the page there is a version selection button, we need version 5.0.0

image

Along the way, until we leave here, download two more things that will come in handy later
https://www.st.com/en/development-tools/stsw- link008.html

ST-LINK V2 driver

image

and https://www.st.com/en/development-tools/stm32cubeprog.html Flasher
program.

image

Installing the driver, the flash driver and Cub itself does not cause any difficulties, we just agree with everything and click on.

After a complete installation of the necessary software, we can begin to create a project.
To do this, run Cube MX.

In the window that appears, click on the “ACCESS TO MCU SELECTOR” button.

image

The STM32F103C8T6 microcontroller is installed on our target board.

Enter its name in the search bar and double-click to select the only option found.

In the same table you can see the main filling of our MK (64 kilobytes of flash, 20 kilobytes of RAM, etc.).

image

Before us appeared a schematically depicted controller case with legs spread apart in different directions.

At this stage, you must definitely choose the method of connecting the debugger.

To do this, select the SYS item on the Pinout & Configuration tab in the left menu and set the Serial Wire value in the drop-down list under the name "Debug".

At the same time, we notice from the corner of our eyes that the program has reserved two pins for the debugging purposes on the controller mnemonic.

image

Once again, we recall that we want to blink an LED on our board.

To do this, you must first find out which particular leg it is connected to.

The electrical circuit diagram will help us with this:

image

or a more colorful and easier to understand board pinout. The

image

desired LED is on the PC13 leg.

Accordingly, it is necessary to configure this output to work in output mode.

For this:

  1. We find the conclusion on the mnemonic diagram
  2. We right-click on it, from the drop-down menu select the item “GPIO_Output”
  3. Go to the GPIO menu,
  4. In the list, select PC13
  5. We fill in the PC13-TAMPER-RTC Configuration table in accordance with the screenshot, we are especially interested in the GPIO mode and User Label parameters

image

We continue to configure the project, go to the Clock Configuration tab.

In fact, this is one of the most important tabs that allows you to configure the peripheral clock settings, but for now we will not touch anything here, since the main goal at the moment is not this.

image

Go to the Project Manager tab, under the Project tab.

Be sure to fill in the following parameters:

  1. Project name (it is better to use only Latin letters)
  2. The directory in which the project will be created (it is also better to use only the Latin alphabet)
  3. IDE in which we plan to work on the project (we plan to use TrueSTUDIO)

image

We get down below, under the Code Generator tab.

Here we definitely note the option Generate peripheral initialization as pair ...

Thus, we get a more structured project, in which for each type of periphery there is a pair of C and H files.

image

The last step remained. Sub tab Advanced Settings.

  1. Choose the type of HAL library for all peripheral modules
  2. We collect the project with the current settings

image

At initial launch, you may need to download the current version of the library for the selected MK family.

We give our consent to download the files:

image

We go to warm the kettle or make coffee:

image

After the code generator is finished, immediately open it:

image

Choose any folder where Atollic will store the workspace:

image

Upon successful opening, the main Atollic TrueSTUDIO for STM window will appear before us .

General information is of little interest to us, so immediately go to the file tree.

image

We find there the main.c file and the int main (void) function:

image

For self-testing - try to build an empty project

  1. In the menu Project -> Rebuild Project
  2. Below select the Console tab
  3. Upon successful assembly - should receive the inscription Build Finished

image

Continued in the next part.

PS: I previously published the article on my personal blog .

Also popular now: