0% found this document useful (0 votes)
161 views

2 - Demo - STM32CubeMX Initialization Code Generation PDF

Uploaded by

dar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views

2 - Demo - STM32CubeMX Initialization Code Generation PDF

Uploaded by

dar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

STM32CubeMX Hands-on

STM32L4+ 2

High integration with high memory size in small packages

Parallel Interface Connectivity


Cortex-M4
FSMC 8-/16-bit 120 MHz USB OTG Crystal less,
(TFT-LCD, SRAM, NOR, NAND) 1x SD/SDIO/MMC, 3 x SPI,
FPU
4 x I²C, 1x CAN, 2 x Octo SPI,
Display MPU 5 x USART + 1 x ULP UART Package size down
ETM
DSI MIPI
LCD TFT Controller DMA
to 5.24 x 5.24 mm
ART Accelerator™ Digital
Timers
17 timers including: Chrom-ART™ AES (256-bit),zSHA (256-bit),
2 x 16-bit advanced motor control TRNG, 2 x SAI, DFSDM (8 channels),
timers Camera I/F, Chrom-GRC™
2 x ULP timers Up to
7 x 16-bit-timers 2-Mbyte Flash
2 x 32-bit timers with ECC
Dual Bank Analog
I/Os 1 x 16-bit ADC, 2 x DAC,
640-Kbyte RAM 2 x comparators, 2 x op amps
Up to 136 I/Os
1 x temperature sensor
Touch-sensing controller

+ 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

• Atollic TrueSTUDIO v9.0.0 is unable to erase the flash when


STM32L4R5 is configured in dual-bank mode (default from factory)

• 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

• Click on New Project


Step 2: Select MCU 7

• Use [MCU Selector] to select


STM32L4R5ZI device
1
• MCU Filter
• Type “STM32L4R5ZI” in [Part Number
Search]
3

• MCU List
• Select [Part No.-> STM32L4R5ZI
(LQFP144) ] 2
• Click [Start Project] or double click
[STM32L4R5ZI] to continue
Step 3a: Project Settings 8

Configure project settings


• Select [Project->Settings…]
• [Project] tab
• [Project Name] : Any name. For example GPIO_EXTI
• [Project Location] : Location to store project folders. In
the case of Atollic TrueSTUDIO, the workspace folder
location. For example C:\ASEAN Workshop\workspace
• [Application Main Location] : Src
• [Toolchain Folder Location] : Will automatically be
generated
• [Toolchain / IDE] : TrueSTUDIO
• [Generate Under Root] : Checked

• [Code Generator] tab


• Keep default configuration

• Click [OK] to finish


Step 4: Configure GPIO 9

[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

• Select [Configuration >Peripherals]


tree, expand the [SYS] sub-tree
• Set the [Debug] to “Serial Wire”
Although the SWD debug pins are active after reset, it is a good
practice to make sure the debug pins are reserved for debug
purposes while assigning pins for your application. This avoids
assigning it for other alternate function by mistake during
firmware development stage
Step 6: Clock Configuration 12

[Clock Configuration] tab


• Set [HCLK (MHz)] to 120
• Click OK when [Clock Wizard]
message pop out to automatically
find the correct clock sources
• The appropriate clock source and 1
PLL values will be set automatically

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]

Note: Refer to next slide for picture of configuration


Step 7: Peripheral Configuration 14
Step 7: Peripheral Configuration 15

• 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

• Select [Project->Generate Code]

• Select [Open Project] to launch TrueSTUDIO and import project into


TrueSTUDIO automatically

• Selecting [Open Folder] will open folder containing STM32CubeMx


generated code.
• You will need to start TrueSTUDIO and import project manually
Starting TrueSTUDIO 17

• Starting TrueSTUDIO will start the [Eclipse Launcher]

• Select the workspace path:


• For example C:\ASEAN Workshop\workspace
as specified in the STM32CubeMX Project Setting i.e. [Project Location]

• Click [OK] to proceed


First TrueSTUDIO start 18

• [Information Center] panel


contains links to various
information related to
TrueSTUDIO.
• Close the [Information Center] panel
• C/C++ perspective will appear

Please note content of [Information Center]


panel might take some time to appear. Be
patient.
C/C++ Perspective 19

Editor view
Outline view

Project Explorer view

Build analyzer view


Eclipse definition 20

• 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

• Add the following code to main.c


/* USER CODE BEGIN 0 */ /* Infinite loop */
uint8_t MODE_SELECTION; /* USER CODE BEGIN WHILE */
while (1)
/* USER CODE END 0 */
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */


if (MODE_SELECTION == 0) {
/* Toggle LEDs - Use the HAL functions from stm32l4xx_hal_gpio.c file */
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_7); //LD1 (green) – PC7
HAL_Delay(100); //100ms
} else if (MODE_SELECTION == 1) {
/* Turn OFF the LEDs - Use the HAL functions from stm32l4xx_hal_gpio.c file
*/
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET); //Turn off LD1 (green)
HAL_Delay(100); //100ms
} else if (MODE_SELECTION == 2) {
/* Turn ON the LED - Use the HAL functions from stm32l4xx_hal_gpio.c file */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET);//LD1 (green) – PC7
HAL_Delay(1000);//1secs
}
}
/* USER CODE END 3 */
Modifying generated code 22

/* USER CODE BEGIN 4 */


/**
* @brief EXTI line detection callback. The function will be call by EXTI15_10_IRQHandler in “stm32l4xx_it.c” .
* @param GPIO_Pin: Specifies the pins connected EXTI line
* @retval None
*/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_13)
{
MODE_SELECTION++;
if(MODE_SELECTION > 2) MODE_SELECTION=0;
/* Debounce - wait until the button is released . Read the GPIO to get the state. Refer to the schematics. */
/* - Use the HAL functions from stm32l4xx_hal_gpio.c file */
while(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) != GPIO_PIN_RESET);//Blue pushbutton – PC13
}

}
/* USER CODE END 4 */
Tips: Useful project settings in TrueSTUDIO 23
C dialect and parallel build

C/C++ Build->Settings->Tools Settings->C Compiler->General->C standard

1. Configure C standard to C99 to


avoid possible compilation
errors

C/C++ Build->Behavior tab

2. Enable parallel build to make


use of your machine potential
and to shorten compilation time

= It is a mouse !
Tips: Using Code completion 24

• You can complete a function or parameter by using CTRL + SPACE


keys after a typing a few characters of the function or parameter.
Mouse over to view
function description

Auto-complete a function
ctrl + space

Auto-complete a parameter
ctrl + space
Build project 25

• Select [Project > Build Project] or [Project > Build All]

• Build result is displayed in the [Console] window


Starting the debugger 26

1. Select project in [Project Explorer] view


2. Click on [Debug] button ( ) or press F11 to start debug session
3. [Debug Configuration] dialog box will appear when debugging project the first time
4. Click [OK] to accept default configuration

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

acknowledge the interrupt request and checks


the priority
• When priority is higher, NVIC fetches EXTI Line
interrupt vector. (Otherwise the interrupt will be

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

Using printf over UART


Using printf over UART 32

• Printf is often used to print debug messages to PC monitor when


debugging a program. For embedded systems this is often not
possible.

• Typically the serial port or UART is used instead to print debug


message to PC

• 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

• Open the previous project STM32CubeMX project (GPIO_EXTI.ioc)


• For example : C:\ASEAN Workshop\workspace\GPIO_EXTI.ioc

• Use STM32CubeMX to configure the LPUART1


• Pinout tab
• LPUART1 – Asynchronous mode on PG7 & PG8
• Clock configuration tab
• No change
• Configuration tab
• LPUART1
• Baud Rate : 115200 bit/s
• Word length : 8 bits (including Parity)
• Parity: None
• Stop bits: 1
Pinout Configuration 34

• On STM32L4R5ZI, multiple pins


support LPUART1 functionality

• Please ensure that PG8 and


PG7 is selected as LPUART1
pins
• By default PC0 and PC1 will be
selected

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

• Save the project once all configuration are done.

• To complete, perform the following:


• Generate Code
• This will generate a project based on the Toolchain/IDE selected and all the necessary user
and library files.
• Generate Report (optional)
• This will create a .pdf, .txt, and .jpg file

• Open Atollic TrueSTUDIO


• When Code Generation is done, just click [Open Project]
Modifying the code 37

• In main.c source file, add code to print “Hello World” messages


/* USER CODE BEGIN 2 */
int count = 0;
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */


//Send message to UART port
printf("\n\rHello World %d", count++);

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 */

int _write(int file, char *ptr, int len)


{
HAL_UART_Transmit(&hlpuart1,(uint8_t *)ptr,len,HAL_MAX_DELAY);
return len;
}
..
..
..
Build project 40

• Select [Project > Build Project] or [Project > Build All]

• Build result is displayed in the [Console] window


Start the debugger 41

1. Select project in Project Explorer view


2. Click on Debug button ( ) or press F11 to start debug session
3. [Debug Configuration] dialog box will appear when debugging project the first time
4. Click [OK] to accept default configuration

3
2

4
Terminal view 42

• Allows I/O communication with target using Serial communication

• 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

• If you have successfully installed


the driver, you should be able to
find the COM Port number from
Windows Device Manager

• If not please install refer to the


installation instructions again
Verification 44

• 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.

Close COM port


when done

You might also like