STM32CubeIntro Demo
STM32CubeIntro Demo
STM32CubeTM Introduction 2
STM32CubeL0 STM32CubeH7
STM32CubeL1 STM32CubeF7
STM32CubeL4
STM32CubeF4
STM32CubeF0 STM32CubeF2
www.st.com/stm32cube STM32CubeF1 STM32CubeF3
STM32CubeTM Overview 3
Graphical
User
STM32CubeMX Interface
Configuration tool on PC
STM32CubeMX
Configuration tool on PC User Application
CMSIS-
DRIVER
STM32 embedded
software
STM32CubeH7.zip Evaluation Discovery Nucleo Dedicated
boards boards boards boards
STM32CubeF4.zip
STM32CubeF3.zip TCP/IP
USB
Host & Graphics
FAT
File RTOS CMSIS
Device System
STM32CubeF2.zip
Middleware level Utilities
STM32CubeF1.zip
STM32CubeF0.zip Hardware Abstraction Layer API
HAL level
STM32CubeL4.zip
STM32CubeL1.zip STM32H7
Hardware
STM32CubeL0.zip
• Abstraction of STM32
• Through portable APIs
• Complete
STM32CubeL4
STM32CubeF0
STM32CubeF2
STM32CubeF7
STM32Cube
STM32CubeL1
STM32CubeF1
STM32CubeF3
STM32CubeF4
STM32CubeL0 • >150 peripheral examples!
CMSIS
Hardware
Hardware Abstraction
Abstraction Layer
layer
• Permissive terms
• Open source BSD license
STM32Cube V1
STM32CubeFx/Lx FW Package 7
• TCP/IP stack
• LwIP open source standard
STM32Cube
Middleware • Graphics
• STemWin from ST and
SEGGER
• File System
• FatFS open source standard
STM32CubeF0
STM32CubeF2
STM32Cube
STM32CubeL1
STM32CubeF1
STM32CubeF3
STM32CubeF4 CMSIS
Hardware Abstraction Layer
• RTOS
• FreeRTOS open source
standard (with CMSIS-RTOS
abstraction)
• >40 examples !
STM32Cube V1
STM32CubeFx/Lx FW Package 8
STM32Cube
Middleware
STM32CubeF0
STM32CubeF2
STM32Cube
STM32CubeL1
STM32CubeF1
STM32CubeF3
STM32CubeF4 CMSIS
Hardware Abstraction Layer
STM32Cube V1 – STM32CubeMX 9
STM32CubeMX Overview
STM32CubeMX for STM32 configuration
12
and initialization C code generation
• The STM32CubeMX, a graphical software
configuration tool that allows to generate C
initialization C code using graphical wizards.
• STM32CubeMX has the following key
features:
• Easy microcontroller selection covering whole
STM32 portfolio.
• Board selection from a list of STMicroelectronics
boards.
• Easy microcontroller configuration (pins, clock
tree, peripherals, middleware) and generation of
the corresponding initialization C code.
• Generation of configuration reports.
• Generation of IDE ready projects for a selection
of integrated development environment tool
chains.
STM32CubeMX projects include the generated
initialization C code, STM32 HAL drivers, the
middleware stacks required for the user
configuration, and all the relevant files needed to
open and build the project in the selected IDE.
• Power consumption calculation for a user-
defined application sequence.
• Self-updates allowing the user to keep the
STM32CubeMX up-to-date.
• Downloading and updating STM32Cube™
firmware packages allowing the download from
www.st.com of the MCU firmware package
required for the development of the user
application
STM32CubeMX C Code generation
13
overview
• During the C code generation process, STM32CubeMX performs the following
actions:
• It downloads the relevant STM32Cube firmware package if it is missing from the STM32CubeMX
repository.
• It copies from the firmware package, the relevant files in Drivers/CMSIS and
Drivers/STM32xx_HAL_Driver folders and in the Middleware folder if a middleware was selected.
• It generates a Projects folder that contains the toolchain specific files that match the user project
settings.
• It generates the initialization C code ( .c/.h files) corresponding to the user MCU configuration and
stores it in the Inc and Src folders. By default, the following files are included:
Files Description
stm32f4xx_hal_conf.h this file defines the enabled HAL modules and sets some parameters (e.g. External High Speed
oscillator frequency) to pre-defined default values or according to user configuration (clock tree).
stm32f4xx_hal_msp.c this file defines all initialization functions to configure the IP instances according to the user
(MSP=MCU Support package) configuration (pin allocation, enabling of clock, use of DMA and Interrupts).
C:\Users\user name\STM32Cube
STM32CubeMX Documentation 15
http://www.st.com/stm32cube
STM32Cube: STM32CubeF0 Firmware Package 16
http://www.st.com/web/en/catalog/tools/PF260612
Demo: STM32CubeMX Overview
STM32Cube: STM32CubeMX 20
Step by step:
• MCU selector
• Pinout configuration
• Peripherals and
middleware parameters
• Code generation
• Power consumption
calculator
21
STM32CubeMX
Power Consumption
Wizard
Clock Tree wizard
22
STM32CubeMX
• Core
• Series
• Line
• Package
• Advanced choices…
• Peripherals choices…
MCU selector continued 24
• Pinout from:
• Peripheral tree
• Manually
• Automatic signal
remapping
• Management of
dependencies
between
peripherals and/or
middleware
(FatFS, LWIP,
FREERTOS, USB
etc)
STM32CubeMX: Pinout configuration 26
Dimmed:
The additional
periphery must
be selected
STM32CubeMX: Pinout configuration 27
Green:
Periphery will
be functional
STM32CubeMX: Pinout configuration 28
Yellow:
On ADC only
some channels
can be used
STM32CubeMX: Pinout configuration 29
Red:
Periphery
cannot be used
in this pinout
setup
STM32CubeMX: Pinout configuration 30
1. I2C1
selected
3. LPTIM1 with
external trigger
selected
STM32CubeMX: Pinout configuration 31
1. Ctrl+Click on pin
STM32CubeMX: Clock tree 33
• Immediate
display of all
clock values
• Management of
all clock
constraints
• Highlight of
errors
STM32CubeMX: Peripheral and
middleware configuration 34
• Highlight of
configuration errors
+ Not configured
ⱱ OK
x Error
• Parameters with
management of
dependencies and
constraints
• Interrupts
• GPIO
• DMA
STM32CubeMX: Peripheral and
middleware configuration 36
• Manage Interruptions
• priorities can only be set in the NVIC
global view
• Manage DMA
• Configure all the parameters of the
DMA request
• Runtime parameters (start address, …)
are not managed
NVIC Panel 37
• Help->Updater settings
• Choose location of STM32CubeFx firmware libraries repository
• Choose manual or automatic check
• Configure connection parameters
• Try to “Use System Proxy Parameters” first
• If it doesn’t work check with IT department
• Alternatively, manually check and download from ST website
Project Driver
CubeMX repository
Folder
Copy all driver files In project are used only peripheral files selected in CubeMX
from CubeMX
repository
Stored in
project_folder/Drivers/STM32XXXX_HAL_Driver folder
Code Generator options :
STM32Cube Firmware Library package 45
Project Driver
CubeMX repository
Folder
Stored in
project_folder/Drivers/STM32XXXX_HAL_Driver folder
Code Generator options :
STM32Cube Firmware Library package 46
Project Driver
CubeMX repository
Folder
• By default this option is not used. All peripheral initialization code are
generated in main.c
Main.c
MX_GPIO_Init
MX_SPI_Init
Initialization done directly in
MX_USART_Init main.c file
…
MX_XXX_Init
Code Generator optios:
Generate peripheral initialization as a pair 48
• Advantage is that with .h file we can call MX_XXX init functions from
every file in project not only from main.c
Main.c gpio.c
Initialization in separated .c
and .h files called from main
spi.c
usart.c
xxx.c
Backup previously generated files when
re-generating 49
• Backup old files from Src and Inc folder into Backup folder
CubeMX
Project folder Project folder
Regenerating
Src Folder Src Folder
Backup Folder Backup Folder
• This areas are reserved in new code generation, if this option is selected
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */ Here can user put his code,
/* USER CODE BEGIN 0 */
code will be preserved during
/* USER CODE END 0 */
int main(void)
project generation
{
/* USER CODE BEGIN 1 */
}
/* USER CODE END 3 */
}
Keep User Code when re-generating 51
re-generating
CubeMX
Project Regenerating Project
gpio.c gpio.c
usart.c usart.c
spi.c
CubeMX
Not used pins(grays) Project
will be in project Generating
configured as analog MX_GPIO_Init
Initialization of unused
pins is in MX_GPIO_Init
• Function input parameters are checked if they are in correct range, if not
application jump into assert_failed function in main.c
This function trying to
/* USER CODE BEGIN 2 */
configure not existing
HAL_GPIO_TogglePin(GPIOA,(0x1<<17));
pin PA17
/* USER CODE END 2 */
• Universal design for the entire STM32 family range at times prevents
the tool from focusing on specific features of a particular product.
configuration
panel
• Power step
definitions
• Battery selection
• Creation of
consumption graph
Sequence
configuration
• Display of
• Average
consumption
• Average DMIPS Result
overview
• Battery lifetime
General PCC parameters 58
• Parameter selection
• Temperature and voltage choice may be limited, depending
on the selected MCU.
• Information notes
• Purpose is to warn about estimation limitations.
Building a sequence 59
N
V
CORTEX-M0+
I
C
MUX
Edge
GPIO
detect
EXTI
Pushbutton
event
STM32 Nucleo User Pushbutton and LEDs 62
Active High
Active Low
GPIO_EXTI 63
• Code Generator
• Copy only the necessary library files (to reduce the size of the project folder)
• Keep User Code when re-generating
• Delete previously generated files when not re-generated
Screenshots 64
Step 1. Select
New Project
Step 2. Type
“STM32F072RB”
Select
“STM32F072RBTx”
Lab-GPIO_EXTI: STM32CubeMX Config 65
• Objective:
• Configure the GPIOS for LEDs
• Configure the GPIO for the User pushbutton as input with interrupt(EXTI).
Step 7. Select
“Debug Serial Wire”
• RCC – no further changes needed for this particular discovery board and exercise
Step 9. Click on
“Configuration”
tab Screenshots 72
Step
Step 11. Change
8. Click on
“Clock Configuration”
accordingly for PA0 &
PC9
tab
Step 15.
Click “Apply”
Step 12.
Click “Apply”
Lab GPIO_EXTI: STM32CubeMX Project Setting 73
• Generated files
• Select “ Keep User Code when re-generating” & “Delete previously generated files when not re-
generated”
• Project tab
• Project name
• Enter the project name and the subdirectory
• Toolchain/IDE
• Select “MDK-ARM V5” for Keil V5 compiler
Step
Step 16.
16. Click
Click
“Project”
“Project” and
select
and
select “Setting”
“Setting” Screenshots 74
Step 19.
Click “OK”
Step 20.
16. Key in
Screenshots 75
Step 22.
Click “OK”
Lab GPIO_EXTI: STM32CubeMX Project Setting 76
• main.c
• The STM32CubeMX tool only generates the initialization code, further modifications
of the user files (e.g. main.c, stm32F0xx_hal_msp.c, stm32l0xx_it.c) are needed to
complete the implementation.
• In the main.c file, you will find sections for user code. Copy the highlighted codes
below to the corresponding USER CODE sections in the main.c file.
• It is important the codes are copied within the USER CODE sections. This will allow
you to regenerate another initialization code using STM32CubeMX tool without
deleting the user codes.
Lab GPIO_EXTI: Firmware modification
78
cont.
• For USER CODE BEGIN 0/USER CODE END 0
/* USER CODE BEGIN 0 */
uint8_t MODE_SELECTION;
/* USER CODE END 0 */
}
/* USER CODE END 4 */
Lab GPIO_EXTI: Verification 81
MUX
Edge
corresponding service routine. GPIO
detect
In main.c
Main() In stm3fl0xx_it.c EXTI
{ void EXTI0_1_IRQHandler(void)
Pushbutton event
… {
… HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
while(1)
{
}
Interrupt … In stm32f0xx_hal_gpio.c
trigger … void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
… {
… /* EXTI line interrupt detected */
} if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
} {
In main.c __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
HAL_GPIO_EXTI_Callback(GPIO_Pin);
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) }
{ }
… User Code to manage the
…
… interrupt This flow of xxx_IRQhandler calls and xxx_Callback
…
} calls is similarly implemented for the other peripherals
when interrupt request is enabled.
Demo 1: Running Sample code on
the STM32F072 Discovery Board
Hands-on sample code: RTC Alarm 84
• Objective:
• Understand where to look for example codes in STM32Cube
• Run “RTC_Alarm” example code on STM32F072 Discovery board which trigger
turning on LED LD5 when the clock reaches the alarm setting
• Description:
• LED LD5 is connected to PC9 and and RTC is set to 00:20:00 at initial
• Alarm will be generated after 30 seconds on 02:20:30 and turns on LED LD5
• Procedure:
• Use window explorer to locate \STM32Cube\Repository subdirectory
• eg> c:\users\le-yan chin\STM32Cube\Repository
• Copy the whole directory content to other place to retain the original firmware
package
For AC6
For Atollic
Hands-on sample code: RTC Alarm
(cont’1) 85
• Procedure:
• Click to run the “Project.uvprojx” for Keil toolchain platform
Hands-on sample code: RTC Alarm
(cont’2) 86
• Procedure:
• In Keil IDE, click the “Rebuild” ( ) icon to rebuild all the target files in the project
Hands-on sample code: RTC Alarm
(cont’3) 87
• Procedure:
• When build finish without error, click the “Start/Stop Debug” ( ) icon to go into
debug mode
Hands-on sample code: RTC Alarm
(cont’4) 88
• Procedure:
• In Debug mode use the “Find” command to locate “aShowTime” variable
Hands-on sample code: RTC Alarm
(cont’5) 89
• Procedure:
• Move the cursor pointer to the variable “aShowTime”, right click the mouse and
select {Add “aShowTime” } to “Watch 1”
Hands-on sample code: RTC Alarm
(cont’6) 90
• Procedure:
• Expand the variable “aShowTime” in the Watch 1 to see detail value
Hands-on sample code: RTC Alarm
(cont’7)
91
• Procedure:
• Click on “View” menu and check
the “Periodic Window Update”
Hands-on sample code: RTC Alarm
(cont’8)
92
• Procedure:
• Click the “Run” ( ) icon to run the project, observe the eShowTime variable
change in Watch 1 window
Hands-on sample code: RTC Alarm
(cont’9)
93
• Procedure:
• a) Try to modify the code only to turn on the LED LD5 when eShowTime is
02:20:20
• Hint : look for “salarmstructure.AlarmTime.Seconds”
• Objective:
• Understand where to look for example codes in STM32Cube
• Run “Demostrations” example code of STM32F072 on Discovery board to test the
touchsense button, USB HID interface and Gyro sensor.
• Description:
• LED LD5 is connected to PC9 and 4 touchsense buttons are connected to
TS_G1_IO3,TS_G1_IO4, TS_G2_IO3,TS_G2_IO4,TS_G3_IO2, TS_G3_IO3
• Using User button to select different application options in the project such as
Sliding position, HID, Gyro movement.
• Procedure:
• Use window explorer to locate \STM32Cube\Repository subdirectory
• eg> c:\users\le-yan chin\STM32Cube\Repository
• Copy the whole directory content to other place to retain the original firmware
package
• Procedure:
• Select project file “Project.uvprojx” to run on Keil toolchain
eg> C:\Users\..\STM32Cube_FW_F0_V1.8.0\Projects\STM32F072B-
Discovery\Demonstrations\MDK-ARM\project.uvprojx
Hands-on sample code: Demostrations
(cont’2) 97
• Procedure:
• In Keil IDE, click the “Rebuild” ( ) icon to rebuild all the target files in the
project.
Hands-on sample code: Demostraions
(cont’3) 98
• Procedure:
• When build finish without error, click the “Start/Stop Debug” ( ) icon to go into
debug mode
Hands-on sample code: Demostrations
(cont’4) 99
• Procedure:
• Click the “Run” ( ) icon to run the project.
Hands-on sample code: Demostrations
(cont’5) 100
• Procedure:
• Press User button B1 1st time
• Change the Discovery board level position to see the LEDs on change.
• eg LED LD4 turns on shows the board level swing to left, opposite swing direction will turn
on LED LD5; similarly LED LD3 turns on shows board level swing to front and opposite
direction will turn on LED LD6
• Press User button B1 2nd time, LED LD3 and LD6 turns on
• Connect a mini-USB cable at the USB User socket
• The movement of the Discovery board will take control of the PC mouse pointer