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

STM32CubeIntro Demo

STM32Cube is an STMicroelectronics software platform that consists of STM32CubeMX, a PC configuration tool, and STM32Cube embedded software packages. STM32CubeMX allows users to configure STM32 microcontrollers through a graphical interface and generate initialization code. The STM32Cube embedded software packages contain drivers, middleware, and over 150 peripheral examples to help users develop applications for STM32 microcontrollers.

Uploaded by

Niaz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
368 views

STM32CubeIntro Demo

STM32Cube is an STMicroelectronics software platform that consists of STM32CubeMX, a PC configuration tool, and STM32Cube embedded software packages. STM32CubeMX allows users to configure STM32 microcontrollers through a graphical interface and generate initialization code. The STM32Cube embedded software packages contain drivers, middleware, and over 150 peripheral examples to help users develop applications for STM32 microcontrollers.

Uploaded by

Niaz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

STM32Cube Overview

STM32CubeTM Introduction 2

• STMCubeTM is an STMicroelectronics original


initiative to ease developers life
• By reducing development efforts
• By reducing development time
• By reducing development cost, with free solutions

• STM32CubeTM applies on STM32 portfolio


STM32CubeMX

STM32CubeL0 STM32CubeH7

STM32CubeL1 STM32CubeF7

STM32CubeL4
STM32CubeF4
STM32CubeF0 STM32CubeF2
www.st.com/stm32cube STM32CubeF1 STM32CubeF3
STM32CubeTM Overview 3

• STM32CubeTM is a software development platform that combines


• A PC software configuration tool called STM32CubeMX
• STM32 embedded software bricks called STM32CubeFx/Lx

Graphical
User
STM32CubeMX Interface
Configuration tool on PC

User code STM32CubeH7.zip


STM32CubeF7.zip
STM32Cube STM32CubeF4.zip
Middleware STM32CubeF3.zip
STM32CubeF2.zip Firmware
STM32CubeF1.zip Library
STM32CubeF0.zip
STM32Cube STM32CubeL4.zip
STM32 Hardware abstraction layer
STM32CubeL1.zip
STM32CubeL0.zip
STM32Cube Work Flow 4

STM32CubeMX
Configuration tool on PC User Application

CMSIS-
DRIVER

STM32Cube Embedded Software deliverables

STM32F0 STM32F1 STM32F2 Middleware level


STM32F3 STM32F4 STM32F7 USB FAT
TCP CMSIS
STM32H0 STM32L0 STM32L1
Host / Gfx File RTOS
/IP Device Sys.
STM32L4

Hardware Abstraction Layer API


C code generation
for initialization,
depending on user
choices
STM32CubeFx/Lx FW Package 5

STM32 embedded
software
STM32CubeH7.zip Evaluation Discovery Nucleo Dedicated
boards boards boards boards

STM32CubeF7.zip Application level Demonstrations Utilities

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

Downloadable manually from www.st.com/stm32cube


or via STM32CubeMX download libraries menu
STM32Cube V1
STM32CubeFx/Lx FW Package 6

• Abstraction of STM32
• Through portable APIs

User Code • High STM32 coverage


• Most of peripherals covered
STM32Cube
Middleware
• Production Ready
• Quality: CodeSonarTM

• 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

User Code • USB Library


• Host & Device made by ST

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

• Customer focusing on his


User Code application differentiation

STM32Cube
Middleware

STM32CubeF0
STM32CubeF2
STM32Cube
STM32CubeL1
STM32CubeF1
STM32CubeF3
STM32CubeF4 CMSIS
Hardware Abstraction Layer
STM32Cube V1 – STM32CubeMX 9

Microcontroller configuration, step by step


• STM32Cube includes the
STM32CubeMX which is a graphical
software configuration tool that
allows generating C initialization
code using graphical wizards.

• Step 1: Select the microcontroller


• Through easy filtering capabilities
• Step 2: Configure the microcontroller
• Pin out wizard
• Clock tree wizard
• Peripherals and Middleware wizards
• Power consumption wizard
• Step 3: Initialization code generation
• Generates code for your favorite IDE !
STM32CubeTM V1 – Key Benefits 10

• Simplify and Speed up Application


Development for STM32!
• Through STM32CubeMX:
• MCU Selector
• Graphical Peripheral Configuration
• Power Consumption Wizard
• Peripheral Initialization Code Generation
• With automatic updater feature
• Ensuring the developer is aware of new versions
and fixes, as well as new components
• Through extensive set of “ready-to-run”
peripheral examples and application examples,
with ready project files for IAR, Keil and GCC
included in the STM32CubeFx/Lx packages
User Application
• More than Cost-friendly !
• 100% FREE embedded software!
• 100% FREE software tool !
• ST-branded, ST-supported !
• Users gain time with initialization code
generation, and remain focused on their key
application code
www.st.com/stm32cube
11

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

main.c is in charge of:


- Resetting the MCU to a known state by calling the HAL_init() function that resets all peripherals,
initializes the Flash memory interface and the SysTick.
- Configuring and initializing the system clock.
- Configuring and initializing the GPIOs that are not used by IPs.
- Defining and calling, for each configured IP, an IP initialization function that defines a handle
structure that will be passed to the corresponding IP HAL init function which in turn will call the IP HAL
MSP initialization function.
STM32CubeMX Repository 14

• Downloaded software and firmware releases will be stored in the


Repository folder. The Default folder is defined in STM32CubeMX-
>Help->Updater Settings->Repository Folder.

C:\Users\user name\STM32Cube
STM32CubeMX Documentation 15

Links to various STM32Cube offering

http://www.st.com/stm32cube
STM32Cube: STM32CubeF0 Firmware Package 16

HAL drivers for


STM32F0xx
product family
• Browse to
• C:\users\your
name\STM32Cube\Repository\
STM32Cube\STM32Cube_FW
_V1.8.0 or later HAL examples for
STM32F072
Discovery board

Project templates for


IAR, Keil and System
Workbench
STM32Cube FW Package Features 17

• STM32Cube gathers together, in a single package, all


the generic and highly portable embedded software
components required to develop an application on
STM32 microcontrollers.
• The package includes a low level hardware abstraction
layer (HAL) that covers the microcontroller hardware,
together with an extensive set of examples running on
STMicroelectronics boards.
• It also contains a set of middleware components (*)
with the corresponding examples. They come with very
permissive license terms:
• Full USB Host and Device stack supporting many classes.
• Host Classes: HID, MSC, CDC, Audio, MTP
• Device Classes: HID, MSC, CDC, Audio, DFU
• Graphics
• STemWin, a professional graphical stack solution available
in binary format and based on the emWin solution from ST's
partner SEGGER
• LibJPEG, an open source implementation on STM32 for
JPEG images encoding and decoding.
• CMSIS-RTOS implementation with FreeRTOS open source
solution
(*) middleware components availability depends on STM32 Series
• FAT File system based on open source FatFS solution
• TCP/IP stack based on open source LwIP solution
• SSL/TLS secure layer based on open source PolarSSL
• A demonstration implementing all these middleware
components is also provided
STM32CubeF0 Documentation 18

• The STM32Cube documentation vary from one STM32


series to another.
• More high-end MCUs(e.g. STM32F4) are supported by
more middleware libraries, like for STemWin graphics
library and LwIP TCP/IP Stack.

http://www.st.com/web/en/catalog/tools/PF260612
Demo: STM32CubeMX Overview
STM32Cube: STM32CubeMX 20

Step by step:

• MCU selector

• Pinout configuration

• Clock tree initialization

• Peripherals and
middleware parameters

• Code generation

• Power consumption
calculator
21
STM32CubeMX

Pinout Wizard Peripherals & Middleware


Wizard

Power Consumption
Wizard
Clock Tree wizard
22
STM32CubeMX

Generates Initialization C Code


based on user choices !
STM32CubeMX: MCU Selector 23

Easy Optional filtering:

• Core

• Series

• Line

• Package

• Advanced choices…

• Peripherals choices…
MCU selector continued 24

• Second tab provides shortcuts to


predefined boards equipped with
STM32 MCU.
• Predefined boards come with pinouts
already assigned to use the
connections and features of the
particular board.
• Alternative board configurations are not
covered.
STM32CubeMX: Pinout configuration 25

• 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

• Different possible states for a peripheral modes


• Dimmed:
the mode is not available because
it requires another mode to be set
(just put the mouse on top of the
dimmed mode to see why)

Dimmed:
The additional
periphery must
be selected
STM32CubeMX: Pinout configuration 27

• Different possible states for a peripheral modes


• Green:
Periphery is assigned to pinout

Green:
Periphery will
be functional
STM32CubeMX: Pinout configuration 28

• Different possible states for a peripheral modes


• Yellow:
Only some functionalities of
periphery can be used

Yellow:
On ADC only
some channels
can be used
STM32CubeMX: Pinout configuration 29

• Different possible states for a peripheral modes


• Red:
Signals required for this mode
can’t be mapped on the pinout
(see tooltip to see conflicts)

Red:
Periphery
cannot be used
in this pinout
setup
STM32CubeMX: Pinout configuration 30

• Ensure “Keep Current Signal Placement” is unchecked (by


default) 4. Pin conflict
2. I2C1 pin between I2C1 and
assignment LPTIM. I2C1_SCL
moved to alternative
position

1. I2C1
selected

3. LPTIM1 with
external trigger
selected
STM32CubeMX: Pinout configuration 31

• Keep User Placement renamed to Keep Current Signal


Placement and is unchecked by default

Keep Current Signal Placement


checked now CubeMX cannot
move selected signals to
different alternate pin

I2C1 cannot be moved


and LPTIM1 external
trigger cannot be used
STM32CubeMX: Pinout configuration 32

• Signals can be set/moved directly from the pinout view


• To see alternate pins for a signal Ctrl+Click on the signal, you can then
drag and drop the signal to the new pin (keep pressing the Ctrl key)

2. Show alternative 3. Move pin to new


positions position

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

• Global view of used


peripherals and
middleware

• Highlight of
configuration errors
+ Not configured
ⱱ OK
x Error

• Read only tree view on


the left with access to
IPs / Middleware
having no impact on
the pinout
STM32CubeMX: Peripheral and
middleware configuration 35

• 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 GPIO parameters

• Manage DMA
• Configure all the parameters of the
DMA request
• Runtime parameters (start address, …)
are not managed
NVIC Panel 37

• Manage all interruptions

• Manage priorities and sort


by priorities

• Search for a specific


interrupt in the list
DMA Panel 38

• Manage All DMA


requests including
Memory to Memory

• Set Direction and


priority

• Set specific parameters


GPIO Panel 39

• Most of the GPIO


parameters are set
by default to the
correct value

• You may want to


change the
maximum output
speed

• You can select


multiple pin at a time
to set the same
parameter
STM32CubeMX: Code generation 40

• Generation of all the C


initialization code

• Automatic integration with


partners toolchains

• User code can be added in


dedicated sections and will be
kept upon regeneration

• Required library code is


automatically copied or
referenced in the project
(updater)
STM32CubeMX: Updater 41

• 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

• Help->Install new libraries : Manage the content of the library


repository
• Click on the check button to see what is available
• Select the library you want to install and click install now
• The libraries will be automatically downloaded and unzipped
STM32CubeMX: Project settings 42

• Project -> Settings


• Set project name and location
• A full folder will be created named with the project name.
• Inside this folder you’ll find the saved configuration and all the generated code
• Select toolchain (Keil, IAR, Atollic, SW4STM32)
• You can choose to use the latest version of the firmware library or a specific one
STM32CubeMX: Code Generator settings 43

• Code generator options


• Either copy the full library or only the
necessary files or just reference the
files from the common repository
• Generate all peripherals initialization
in the stm32fYxx_hal_msp.c file or
one file per peripheral
• Keep user code or overwrite it (code
between User code comment
sections)
• Delete or keep files that are not
useful anymore
• Set free pins as analog, this settings
helps keep low consumption (if
SWD/JTAG is not selected in
pinout, this option will disable it)
• Enable full assert in project, this help
discover incorrect HAL function
parameter used in user code
Code Generator options :
STM32Cube Firmware Library package 44

• Copy all used libraries into the project folder

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

• Copy only the necessary library files

Project Driver
CubeMX repository
Folder

Copy only files for


peripherals selected
in CubeMX

Stored in
project_folder/Drivers/STM32XXXX_HAL_Driver folder
Code Generator options :
STM32Cube Firmware Library package 46

• Add necessary library files as reference in the toolchain project


configuration file

Project Driver
CubeMX repository
Folder

No Driver folder in project file


CubeMX not copy anything from
repository.
Project use drivers from
CubeMX repository
Code Generator options:
Generate peripheral initialization as a pair 47

of ‘.c/.h’ files per IP

• 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

of ‘.c/.h’ files per IP

• Generate dedicated initialization .c and .h file for each periphery

• 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

Src files Src files

Inc Folder Inc Folder


Backup Folder Backup Folder

Inc files Inc files


Old files are backuped
Keep User Code when re-generating 50

• Generated code contains USER CODE areas

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


/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
/* USER CODE BEGIN 2 */

/* USER CODE END 2 */


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

}
/* USER CODE END 3 */
}
Keep User Code when re-generating 51

• Generated code contains USER CODE areas

• This areas are reserved in new code generation, if this option is


selected

• Areas present in files generated by CubeMX


• Main.c
• Stm32l0xx_it.c
• Stm32l0xx_hal_msp.c

• Areas cover important areas used for:


• Includes
/* USER CODE BEGIN PFP */
• Variables
/* USER CODE END PFP */
• Function prototypes /* USER CODE BEGIN 0 */

• Functions /* USER CODE END 0 */


Code Generator options:
Delete previously generated files when 52

re-generating

CubeMX
Project Regenerating Project

gpio.c gpio.c

usart.c usart.c

spi.c

Spi.c not used in new


configuration it is deleted by Delete spi.c
CubeMX
Set all free pins as analog 53

• This settings optimize power consumption of unused pins

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

• If the JTAG/SWD is not selected in CubeMX, MX_GPIO_Init reconfigure


JTAG/SWD pins to analog and this disable debug possibilities
Enable Full Assert 54

• Feature very useful during debugging

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

If parameter is not in valid


range program jump into
void assert_failed(uint8_t* file, uint32_t line)
{
assert_failed function
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
Advanced options 55

• API Driver Selector


• Hardware Abstract Layer –use a high
abstraction level based on standalone
processes
• Low Layer – deeply knowledge Hardware
and process flow

• Generated function Call


• The option to generate Initialization
function Call of each periphery or not.
Warning and disclaimer 56

• Universal design for the entire STM32 family range at times prevents
the tool from focusing on specific features of a particular product.

• The STM32CubeMX GUI tool is not a replacement for the


reference manual or datasheet
• Always refer to written documentation for further information!
• Important features are often available on the product or in the HAL but not in the
GUI.

• The GUI helps start a project and initialize a working starting


configuration – but the configuration can be dynamically changed at
runtime (i.e. GPIO, NVIC priority or clock settings).
STM32CubeMX: Power consumption
General PCC
calculator 57

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

• MCU selection inherited from STM32CubeMX


• Use the direct link to the datasheet to get more detailed
information.

• Parameter selection
• Temperature and voltage choice may be limited, depending
on the selected MCU.

• Battery selection – select typical or define your own


• Battery is defined by capacity, voltage, self discharge and
current limitations.

• Information notes
• Purpose is to warn about estimation limitations.
Building a sequence 59

• A sequence is a set of ordered steps.


Compare
sequences, even Check automatically if
Load existing with different proposed power step
sequences and MCUs. transitions are valid.
adapt them.

Create new steps


by adding or
duplicating
existingones.
Hands-on Demo : GPIO and
EXTI with STM32CubeMX
GPIO and EXTI Hands-on Demo
• Part 1: Configure a GPIO in External Interrupt mode.

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

• This hands-on describes how to use the GPIO HAL APIs.


The User pushbutton, configured as input with interrupt, will be used to change
the states of the LEDs.
• For this hands-on, the STM32CubeMX will be used to generate the initialization
codes for the GPIOs 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.
Recommended especially for first time users of the STM32.
• Create an new project
• Target MCU: STM32F072RBT6
• Save this project in folder below with project name: Lab-GPIO_EXTI
• C:\..\STM32F0 Discovery Exercises\Lab-GPIO_EXTI
• In the Project->Settings, same as before, make sure that the following are set:
• Project
• Toolchain/IDE: MDK-ARM V5
• Firmware Package Name and Version: STM32Cube FW_F0 V1.9.0 (or download the STM32CubeF0 latest version)

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

• Use the STM32CubeMX to configure the GPIOs accordingly:


• Pinout Tab
• GPIOs
• PA0 (User pushbutton) – GPIO EXTI0
• PC9 (LD_R - Green) – GPIO output
• Sys (System)
• Enable Serial Wire Debug (SWD)
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 while still in firmware development stage
Step 3. Type
“pa0” in
Find column
Screenshots 66

Step 4. click on PA0


& select
“GPIO_EXT0”
Step 5. Type
“pc9” in
Find column Screenshots 67

Step 6. click on PC9


& select
“GPIO_Output”
Screenshots 68

Step 7. Select
“Debug Serial Wire”

Step 6. click on PA5


PA13 & PA14 will be
& select
used for SWD
“GPIO_Output”
Lab-GPIO_EXTI: STM32CubeMX Config 69

• Clock Configuration Tab


• System Clock using HSI (8MHz) as clock source:
• HSI as SYSCLK clock source
• SYSCLK = HCLK(AHB) = 8MHz
• APB1(PCLK1) = 8MHz
• APB2(PCLK2) = 8MHz
Step 8. Click on
“Clock Configuration”
tab
Screenshots 70

Verify the System


Step 7. Select
Clock selection is
“Debug Serial Wire”
“HSI” & 8MHz
Lab GPIO_EXTI: STM32CubeMX Config cont. 71

• STM32CubeMX configuration cont.:


• Configuration Tab
• GPIO
• PC9 (Output Push Pull mode, no pull-up/down, Fast output speed, User Label: LD_R (green) )
• PA0 (External Interrupt mode with the correct edge detection, no pull-up/down, User Label: B1
User) . Refer to schematics for the correct edge trigger.
• PA13 (Set as SWD pins. No further action. User Label: SWDIO)
• PA14 (Set as SWD pins. No further action. User Label: SWCLK)
• NVIC
• Enable External Line 4 to Line 15 interrupt with Software priority (Preemption Priortiy) set to 1.
• System tick timer - Care must be taken when using HAL_Delay(), this function provides accurate
delay (in milliseconds) based on variable incremented in SysTick ISR. This implies that 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.

• RCC – no further changes needed for this particular discovery board and exercise
Step 9. Click on
“Configuration”
tab Screenshots 72

Step 13. Click on


Step 10. Click on NVIC
GPIO

Step 14. Check to


enable EXTI0
interrupt and set
preemption priority 1

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

• Code Generator tab


• STM32Cube Firmware Library Package
• Select “Copy only the necessary library files”

• 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 17. Select “Copy only


the necessary library files”

Step 18. Select “Keep User


Code when re-generating”

Step 19.
Click “OK”
Step 20.
16. Key in
Screenshots 75

project name and


subdirectory

Step 21. Select


MDK-ARM V5
Toolchain for Keil
compiler

Step 22.
Click “OK”
Lab GPIO_EXTI: STM32CubeMX Project Setting 76

• Save the project once all configuration are done.

• To complete, perform the following:


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

• Open the KEIL MDK-ARM 5 Project (Lab-GPIO_EXTI.uvprojx)


• When the Code Generation is done, just click “Open Project”. Or you can manually
open from the specified folder.
• \..\STM32F0 Discovery Exercises\Lab-GPIO_EXTI\MDK-ARM

• Now you are ready to write some codes.


Lab GPIO_EXTI: Firmware Modification 77

• In KEIL environment, open main.c file.


• Study the generated GPIO configuration. Verify if the configurations done by
the STM32CubeMX tool are correct.
• In Main.c file -> MX_GPIO_Init ()
• In the STM32CubeMX Pinout configuration, you have only configured PA0(User
PB), PC9(LD_R), PA13(SWDIO) and PA14(SWCLK). How come there are
configurations for the other pins? What are these configurations for?

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

• For USER CODE BEGIN 3 / USER CODE END3


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

/* USER CODE BEGIN 3 */


if(MODE_SELECTION==0){
/* Toggle LEDs - Use the HAL functions from stm32l0xx_hal_gpio.c file */
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9); //LD_R (green) – PC9
HAL_Delay(100); //100ms
}
else if(MODE_SELECTION==1){
/* Turn OFF the LEDs */
/* - Use the HAL functions from stm32f0xx_hal_gpio.c file */
/* Hint: Highlight + right click on the function and use “Go to definition…” */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET); //Turn off LD_R(green)
HAL_Delay(100); //100ms
}
Lab GPIO_EXTI : Firmware modification
79
cont.
• Cont. For USER CODE BEGIN 3 / USER CODE END3
else if(MODE_SELECTION==2){
/* Turn ON the LED */
/* - Use the HAL functions from stm32f0xx_hal_gpio.c file */
/* Hint: Highlight + right click on the function and use “Go to definition…” */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET); //LD_R (green) – PC9

/* Delay - Use the HAL delay function from stm32l0xx_hal.c file */


HAL_Delay(1000); //1secs
}
}
/* USER CODE END 3 */
Lab GPIO_EXTI : Firmware modification
80
cont.
• For USER CODE BEGIN 4/ USER CODE END 4
/* USER CODE BEGIN 4 */
/**
* @brief EXTI line detection callback. The function will be call by EXTI0_IRQHandler in “stm32f0xx_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_0)
{
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 stm32l0xx_hal_gpio.c file */
/* Hint: Highlight + right click on the function and use “Go to definition…” */
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) != GPIO_PIN_RESET); //Blue pushbutton – PA0
}

}
/* USER CODE END 4 */
Lab GPIO_EXTI: Verification 81

• Build, then Download and Debug


• Run the code
• Expected behavior:
• When User button is pressed an interrupt is triggered and will call the EXTI IRQ handler
in stm32l0xx_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.
Lab GPIO_EXTI: Discussion (Interrupts) 82
• Flow of interrupt
• Pushbutton event occurs
• EXTI detects valid edge
• EXTI generates interrupt request N
V
• If the interrupt channel is enabled, the NVIC will acknowledge the interrupt CORTEX-M0+
I
request and checks the priority C
• When priority is higher, NVIC fetches EXTI Line interrupt vector.
(Otherwise the interrupt will be set as pending until its priority becomes the
highest compared to other pending interrupts)
• Core executes EXTI IRQ Handler. Note that the handler will eventually call
a callback function where the user will have to add and write the

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

• Check for the package F0 firmware


• eg> STM32Cube_FW_F0_V1.8.0

• Copy the whole directory content to other place to retain the original firmware
package

• Go to respective \Project subdirectory you copied and click into \STM32F072-


Discovery subdirectory
• eg>
C:\Users\..\STM32Cube_FW_F0_V1.8.0\Projects\STM32F072B-Discovery

• Select “RTC_Alarm” for Keil toolchain in \..\Examples\RTC\RTC_Alarm\MDK-


ARM subdirectory For IAR
• eg>
C:\Users\..\Projects\STM32F072B-Discovery\Examples\RTC\RTC_Alarm\MDK-ARM For Keil

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”

• b) Try to change the start up time from 12:00:00


• Hint : look for “stimestructure”
Demo 2: Running the Demonstration
project on the STM32F072 Discovery
Board
Hands-on sample code: Demostrations
95

• 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

• Check for the package F0 firmware


• eg> STM32Cube_FW_F0_V1.8.0

• Copy the whole directory content to other place to retain the original firmware
package

• Go to respective \Project subdirectory you copied and click into \STM32F072-


Discovery subdirectory
• eg>
C:\Users\..\STM32Cube_FW_F0_V1.8.0\Projects\STM32F072B-Discovery

• Select “Demonstrations” subdirectory in \STM32F072-Discovery subdirectory


• eg>
C:\Users\..\STM32Cube_FW_F0_V1.8.0\Projects\STM32F072B-
Discovery\Demonstrations
Hands-on sample code: Demostrations
(cont’1) 96

• 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

• Press User button B1 3rd time, all LED turns off


• Slide the touchsense buttons and observe the LEDs on/off changes; indicating the
position of the finger sliding

You might also like