2 - Demo - STM32CubeMX Initialization Code Generation PDF
2 - Demo - STM32CubeMX Initialization Code Generation PDF
STM32L4+ 2
+ More Integration
STM32L4R5 Nucleo-144 3
• Features
• Flexible board power supply
• USB or external source
• Integrated ST-Link/V2-1 debugger
• Drag & drop device flash programming
• Virtual COM port
• For user application
• 3 LED
• Push button (blue)
• STM32L4R5 microcontroller
• USB OTG
• Connectors
• Arduino Uno
• ST Zio
• ST Morpho Extension - direct access to all
MCU I/Os
Patch for Atollic TrueSTUDIO v9.0.0 4
• Workaround
• Configure STM32L4R5 in single bank mode or
• Replace C:\Program Files (x86)\Atollic\TrueSTUDIO for STM32 9.0.0\Servers\ST-
LINK_gdbserver\ST-LINK_gdbserver.exe with a fixed version
• Fixed version can be found in the thumb drive content
• D:\Atollic TrueSTUDIO\TrueSTUDIO_Patch.zip
• Unzip ST-LINK_gdbserver.exe into the specified TrueSTUDIO folder
GPIO and EXTI Hands-on 5
• This hands-on describes how to use the GPIO HAL APIs. The User push button, configured
as input with interrupt, will be used to change the states of the LEDs.
• STM32CubeMX will be used to generate the initialization codes for the EXTI, GPIO and
System clock.
• This process will speed up the development as the initialization codes are generated by the
STM32CubeMX tool. The user then will only need to add the user codes as per application.
N
V
CORTEX-M4
I
C
MUX
Edge
GPIO
Pushbutton detect
event EXTI
Step 1: Open New Project 6
• MCU List
• Select [Part No.-> STM32L4R5ZI
(LQFP144) ] 2
• Click [Start Project] or double click
[STM32L4R5ZI] to continue
Step 3a: Project Settings 8
[Pinout] tab
• Left-click pin PC7 and set to
[GPIO_Output] mode
• Note : Drive LED
• Turn OFF – GPIO is LOW
• Turn ON – GPIO is HIGH
Hint – Pin PC7 can also be found by using [Find] feature in STM32CubeMx
Step 4: Configure GPIO 10
[Pinout] tab
• Left-click pin PC13 and set to
[GPIO_EXTI13] mode
• Note : USER button (Blue)
• Button not press – GPIO is LOW
• Button press – GPIO is HIGH
Step 5: Enable Debug Pins 11
[Pinout] tab
2
Step 7: Peripheral Configuration 13
[Configuration] tab
• Select [GPIO]
• Configure PC7
• [GPIO Mode] : Output Push Pull
• [Maximum output speed] : High
• [User label] : Any name (optional)
• Other settings use default
• Configure PC13
• [GPIO Mode] : External Interrupt Mode with Rising edge trigger detection
• [User label] : Any name (optional)
• Other settings use default
• Click [Apply] and [OK]
• NVIC
• EXTI line [15:10] interrupt (PC13 B1
USER)
• Enable
• Preemption Priority: 1
• Caution!!!
• HAL_Delay() function provides accurate delay (in
milliseconds) based on variable incremented in
System Tick Timer(SysTick) ISR.
• If HAL_Delay() is called from a peripheral ISR
process, then the SysTick interrupt must have
higher priority (numerically lower) than the
peripheral interrupt. Otherwise the caller ISR
process will be blocked.
Step 8: Generate Code 16
Editor view
Outline view
• Workspace
• Container that include project folders and information about project
• Project is a directory containing files that may be organized in sub-directories
• Can contain multiple projects and be located anywhere in the storage media
• Perspective
• Set of windows/views dedicated to a purpose
• Typically used perspective - C/C++ and Debug
• View
• Dedicated windows for specific purpose
• By default not all views are available in a perspective
Modifying generated code 21
}
/* USER CODE END 4 */
Tips: Useful project settings in TrueSTUDIO 23
C dialect and parallel build
= It is a mouse !
Tips: Using Code completion 24
Auto-complete a function
ctrl + space
Auto-complete a parameter
ctrl + space
Build project 25
3
2
4
Debug perspective 27
SFR view
Run control toolbar Peripheral registers
contents are
displayed here
1 2 3 4 5 6 7 8 9
1. Restart
2. Resume Fault Analyzer view
3. Suspend Display information if
4. Terminate a system fault occurs
5. Terminate & Relaunch
6. Step Into
7. Step Over
8. Step Return Terminal view
9. Instruction Stepping mode Serial port output
Breakpoint view
can be directed
Existing breakpoints are
to this view
shown or configured here
Verification 28
• Expected behavior:
• When blue button (B1 USER) is pressed an interrupt is triggered and will call the EXTI
IRQ handler in stm32l4xx_it.c file. The IRQ handler will then call the
HAL_GPIO_EXTI_Callback() function in main.c file where the global variable
(MODE_SELECTION) will be incremented.
• MODE_SELECTION == 0 (Default), Green LED will toggle
• MODE_SELECTION == 1, Green LED will turn off.
• MODE_SELECTION == 2, Green LED will turn on.
Discussion (Interrupts) 29
• Flow of interrupt
• Pushbutton event occurs
• EXTI detects valid edge N
CORTEX V
• EXTI generates interrupt request M4 I
• If the interrupt channel is enabled, the NVIC will C
MUX
Edge
GPIO
set as pending until its priority becomes the detect
highest compared to other pending interrupts)
EXTI
• Core executes EXTI IRQ Handler. Note that the
handler will eventually call a callback function Pushbutton event
where the user will have to add and write the
corresponding service routine.
Discussion (Callback) 30
• This flow of xxx_IRQhandler calls and xxx_Callback calls is similarly implemented for the
other peripherals when interrupt request is enabled
In main.c In stm3l4xx_it.c
Main() void EXTI15_10_IRQHandler(void)
{ {
… HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
…
while(1) } In stm32l4xx_hal_gpio.c
{ void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
… {
Interrupt … /* EXTI line interrupt detected */
trigger … if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
} {
} __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
In main.c HAL_GPIO_EXTI_Callback(GPIO_Pin);
}
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) }
{
…
… User Code to
… manage the interrupt
}
31
• This lab will show how to add printf capability to your STM32 code
UART printf 33
• For this hands-on, the STM32CubeMX will be used to generate the initialization codes for the
LPUART1. For STM32L4R5 NUCLEO-144 board, ST-LINK Virtual COM port feature is only
supported via LPUART pins
Default
LPUART Configuration 35
[Configuration] tab
• Select [LPUART1]
• [Baud Rate] : 115200 bit/s
• [Word length] : 8 bits (including Parity)
• [Parity] : None
• [Stop bits] : 1
• Other settings use default
UART printf 36
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if (MODE_SELECTION == 0) {
..
..
}
/* USER CODE END 3 */
Modifying the code 38
• Override _write() function used to send data over UART using HAL
function
/* USER CODE BEGIN 4 */
3
2
4
Terminal view 42
• Steps to configure
1. Open a Terminal
2. Select “Serial Terminal”
3. Select [Port] – refer to next slide to determine your port name
4. Change [Baud Rate] to the one configured on MCU (115200)
3
5. Click [OK]
4
1
5
Virtual COM Port name (COM#) 43
• Expected behaviour
• The message “Hello World” and incrementing count value will appear in the Terminal
View
• If you suspend/pause the program execution, the message will stop printing.