Part 3 - Introduction To HAL Programming
Part 3 - Introduction To HAL Programming
Part 3 - Introduction To HAL Programming
HAL_Part1
STM32CubeMX utility – An Overview
STM32CubeMX is a graphical tool that configures initialization
code for peripherals and system clock. Upon installation, the
STM32CubeMX short cut available on the desktop. The
STM32CubeMX provides as way to create/launch a project
either by MCU or Board selectors. In this introductory course,
the Nucleo-L053R8 with the STM32L053R8T6 chip on board will
be used.
3
* Please refer to UM1718.pdf for detail of STM32CubeMX
Introduction to the STM32CubeMX utility
6
STM32CubeMX utility – A Blank Project
A blank project will
appear after the 'Start
Project' Tab is clicked.
The steps to
(1). Set the location where the
project will be located
[2]
(2). Name the project and
(3). Set the toolchain/IDE [1]
[3]
8
It is recommended place the project in a specific work folder
STM32CubeMX utility – Clock Configuration Tab
The steps to
(1). Select HSI [4]
(2). Set MUL Factor
(3). Set DIV Factor
[1] [2] [3]
(4). Select PLL
9
The system clock is limited to 32MHz for STM32L053R8T6
STM32CubeMX utility – Pinout & Configuration Tab
10
Make sure that the Pinout View is displayed. Otherwise click on the ‘Pinout view’ Tab
STM32CubeMX utility – Configure Pinout into GPIO_Output
11
Make sure that the Pinout View is displayed. Otherwise click on the ‘Pinout view’ Tab
STM32CubeMX utility – Setting GPIO mode and configuration
12
Make sure that the System View is displayed. Otherwise click on the ‘System view’ Tab
STM32CubeMX utility – Configure USART2
(1). Under ‘Connectivity Drop
Down Box’ select USART2
(2). In Mode list down table,
select ‘Asynchronous’
(3). Under USART ‘Parameter
Settings’ Tab, make sure the
parameter is set as: Set the
baud rate to 115200b/s, 8-bit,
no parity and 1 stop bit.
Note:
Pins PA2 and PA3 should turn
green as shown as soon as
USART2 is set to asynchronous
mode
13
By default, the USART is set to 115200b/s, 8-bit, no parity and 1 stop bit.
STM32CubeMX utility – Generate Code
[1]
14
STM32CubeMX utility – Re-accessing the Project
15
Double left click on the file to open the file
An Introduction
to HAL
Programming
PART 2
Editing & Writing Code
HAL_Part1
General-purpose inputs/outputs (GPIOs) – HAL API
18
KEIL®MDK ARM IDE – Expand the Project Folders
19
KEIL®MDK ARM IDE – Open File (main.c)
[6]
[1]
Double left click on the [7]
main.c to open the file.
[2]
[3]
[8]
[4]
[5] [9]
20
User code must be written in between “USER CODE BEGIN xxx” and “USER CODE END xxx”
[10]
[11]
[12]
[13]
[14]
21
User code must be written in between “USER CODE BEGIN xxx” and “USER CODE END xxx”
KEIL®MDK ARM IDE – Writing code
[3]
[4]
[1]
[2]
[5]
22
KEIL®MDK ARM IDE – Writing code
[6]
[7]
[8]
23
An Introduction to
HAL Programming
PART 3
Compiling, Downloading and
Testing
HAL_Part1
Compiling the project
25
Compile the project by clicking build icon or pressing <F7> . Executable file is ONLY generated if the code free from errors.
Connecting Nucleo-L053R8 to Computer
When Nucleo-L053R8 is
connected to a computer,
provided that the ST Link driver
is installed correctly, the
Nucleo-L053R8 will be showing
up on computer
26
Downloading the executable file
27
Project is downloaded by clicking load icon or pressing <F8>
Run the code – Press Reset Button (Black button)
28
Run the code – Setting up Serial Terminal Console
29
The step to set Serial Terminal Console (Tera Term)
Run the code – Displays in the Serial Terminal Console
30
An Introduction
to HAL
Programming
PART 4
Handling Multiple File
HAL_Part2
Add user/driver file – Creating the Project
[1]
[2]
33
Click on the ‘Pinout view’ Tab [1], Click on PIN and set to GPIO_Input or GPIO_Output[2],
Add user/driver file –GPIO mode & Internal Resistor
[1]
[2]
[3]
[4]
[5]
Click on the ‘System view’ Tab [1], Select GPIO [2], Click on PIN to be Modified [3], 34
Select GPIO Mode [4], Select GPIO Pull-up/Pull-down [5]
Add user/driver file –GPIO mode & Internal Resistor
35
Add user/driver file – Placing the Files in Directory
In general, the “driver” (already written code) must be placed accordingly
36
Any .h (header files) must be placed into Inc sub-folder and any .c (source files) must be placed into Src sub-folder
Add user/driver file – Adding the files to Project
Only the source file (.c) need to be Added ( because .h file is called by the .c file)
[1]
[2]
[3]
[4]
[5]
Click on the ‘Application User’ [1], Select Add Existing Files….. [2], Browse to Src sub-folder [3], 37
Select source file/s [4], Click ‘Add’ to accept [5]
Add user/driver file – Include the Added files to Project
Only the source file (.c) need to be Added ( because .h file i called by the .c file)
[2]
[1]
[3]
Click file buttonsNleds.c to open [1], Copy #include “butt….. [2], Place in main.c file [3] 38
Add user/driver file – Compile a project
40
Add user/driver file – EDIT CODE
41
Add user/driver file – EDIT CODE
42
An Introduction to
HAL Programming Using Sys Tick Timer
PART 5
HAL_Part3
HOW TO COPY A READY IOC FILE
44
HOW TO COPY A READY IOC FILE
(3) Rename
45
HOW TO COPY A READY IOC FILE
(5) Add the driver file/s
46
USING SYS TICK TIMER
47
USING SYS TICK TIMER
48
An Introduction to
HAL Programming Extended Interrupt
PART 6
HAL_Part4
Extended Interrupt
The Simple Example Project
50
Extended Interrupt – Setting the pin to GPIO_EXTI
Copy the previous project (HAL_Part3), open ioc file, left click on PA1 and set the pin to GPIO_EXTI 51
Extended Interrupt – Setting the pin to GPIO_EXTI
[1]
[2]
[3] [4]
Click on the ‘System view’ Tab [1], Select NVIC [2], Tick to Enable Interrupt [3], Select 52
GPIO Mode [4], Select GPIO Pull-up/Pull-down [5]
Extended Interrupt – Setting Preemption Priority
[1]
The Pre-emption Priority can be set from 0-3, 0 is the highest level, set it to 3. 53
Extended Interrupt – Select Trigger Edge/s
[1]
[3]
[2]
[4]
Click on the ‘System view’ Tab [1], Select GPIO [2], Select PA1 [3], Select Trigger edge 54
from list [4]; Select “External Interrupt Mode with Falling edge trigger detection”
Extended Interrupt – Codes
55
An Introduction to HAL Programming
PART 7
Designing the software driver
Multi 7-segment driver (multi-learning shield)
HAL_Part5
Designing Software Driver – Circuits
57
Designing Software Driver – 595/6 Logic Circuits
58
Designing Software Driver – Data Shifting
59
Designing Software Driver – Segment & Column Data
60
Designing Software Driver – Scanning Code
void displayData(void)
{
int8_t i;
static uint8_t seg=0;
parallelClk(0);
for(i=15; i>-1; i--){
serialClk(0);
if(buff[seg] & (1<<i))
dataIn(1);
else
dataIn(0);
serialClk(1);
}
parallelClk(1);
if(++seg>3) seg=0;
}
61
Designing Software Driver – GPIO Setting
Label Pin configuration STM32L053R8Tx Connection Int Resistor
SH_CP Digital PA8 Push Pull No
DS Digital PA9 Push Pull No
ST_CP Digital PB5 Push Pull No
62
Designing Software Driver – GPIO Setting
The Simple Example Project
63
Designing Software Driver – GPIO Setting
64
References
[1] STML0x3 Data Sheet, DocID025844 Rev 7, October 2016
[2] STML0x3 Reference Manual, DocID025274 Rev 3, RM0367, May 2015
[3] STM32™ Nucleo boards, DocID025838 Rev 5, NUCLEO-XXXXRX, September 2014
[4] Cortex®-M0 Programming manual, PM0223 Rev 5, October 2019
[5] General-purpose timer cookbook for STM32 microcontrollers, AN4776 Rev 3, July 2019
65