Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Part 3 - Introduction To HAL Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

• Part 1 - Creating a Project and Start-up Code --- 2

• Part 2 - Editing / Code Writing ---- 16


• Part 3 - Compiling / Downloading / Testing ---24

Introduction to HAL • Part 4 – Handling multiple file --- 31


• Part 5 - Using Sys Tick Timer --- 43
Programming • Part 6 – An extended interrupt --- 49
• Part 7 – Designing the driver (Multi 7-segment) --- 56
• References --- 65
An Introduction
to HAL
Programming
PART 1
Creating a Project and Generate Start-
up Code

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.

The fundamental initialization steps required


1. Enable clock to system core and peripherals
2. Configure the pins required by the peripheral
3. Configure peripheral hardware
4. Configure the corresponding register for peripherals

3
* Please refer to UM1718.pdf for detail of STM32CubeMX
Introduction to the STM32CubeMX utility

We will create the project with


(1). One active high LED at PA5
(2). One active low push button at PC13
(3). Serial comm through USART2 at 9600b/s,
8-bit, with no parity and 1 stop bit in
asynchronous mode in polling mode

Double click on STM32CubeMX short cut

* Please refer to UM1718.pdf for detail of STM32CubeMX 4


STM32CubeMX utility – Creating a Project
The STM32CubeMX allows
the creating of new project
or loading of existing
project.

The project can be created


either by MCU selector or
board selector or Cross
Selector (MCU + Board).

We will create the project by the


MCU selector. Click on ACCES TO
MCU SELECTOR button
5
STM32CubeMX utility – PART NUMBER SELECTION

(1). Type STM32L053R8 in


the part number box
(2). Accept the chosen
package which is
STM32L053R8Tx. Left click on
the correct package.
(3). Start the Creation of
Project by clicking the ‘Start
Project’ tab

6
STM32CubeMX utility – A Blank Project
A blank project will
appear after the 'Start
Project' Tab is clicked.

If you do not see a window similar to the one 7


shown, please click on the 'Project Manager' Tab
STM32CubeMX utility – Project Manager Tab

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 show how the system


clock can be retrieved from HSI
(High-Speed Internal) clock
source, via the PLLCLK (Phase
Lock Loop) to achieve 24MHz

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

The step to set PA5


into GPIO_Output

10
Make sure that the Pinout View is displayed. Otherwise click on the ‘Pinout view’ Tab
STM32CubeMX utility – Configure Pinout into GPIO_Output

The step to set PC13


into GPIO_Input

11
Make sure that the Pinout View is displayed. Otherwise click on the ‘Pinout view’ Tab
STM32CubeMX utility – Setting GPIO mode and configuration

The step to set 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]

Click on Generate Code Tab to


generate the source code [1]

To start writing code, select [3] [2]


'Open Project’ [2]

Select 'Open Folder' to view


the generated file [3]

14
STM32CubeMX utility – Re-accessing the Project

Project file (.ioc) can be accessed


again on D:\TRAINING No01
2021\HAL PROJECTS\HAL_Part1

The project (uVision5 Project ) can be


accessed on D:\TRAINING No01 2021\HAL
PROJECTS\HAL_Part1\MDK-ARM

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

 The delay ms can be called by HAL_Delay(x); where x is the number delay in ms


 The pin can be toggled by HAL_GPIO_Toggled(GPIOx, GPIO_PIN_y), where x is port, y pin number.
 The pin can be written by HAL_GPIO_WritePin(GPIOx, GPIO_PIN_y, GPIO_PIN_z), where x is port, y pin number
and z condition (RESET or SET).
 The pin can be monitored by HAL_GPIO_ReadPin(GPIOx, GPIO_PIN_y), where x is port, y pin number.

 GPIO_PIN_RESET - APPLY LOGIC LOW TO THE INTENDED PIN


 GPIO_PIN_SET - APPLY LOGIC HIGH TO THE INTENDED PIN

API – Application Programming Interface


17
KEIL®MDK ARM IDE – Open the uVision IDE/Project

Double left Click on the project (uVision5


Project ) located at D:\TRAINING No01
2021\HAL PROJECTS\HAL_Part1\MDK-ARM

The uVision IDE host the


project named HAL_Part1
will appear once clicked

18
KEIL®MDK ARM IDE – Expand the Project Folders

In the KEIL MDK-ARM V5


IDE, expand the HAL_Part1 [1]
folder [1]. Next, expand the
Application/USER sub-
folder [2] [2]

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

Label Pin configuration STM32L053R8Tx Connection Int Resistor


PB1 Active low PA1 NA No
PB2 Active low PA4 NA No
PB3 Active low PB0 NA No
LED4 Active low PB6 Open Drain No
LED3 Active low PA7 Open Drain No
LED2 Active low PA6 Open Drain No
LED1 Active low PA5 Push Pull No
BUZZER Active low PB3 Open Drain No
32
Add user/driver file – Configure GPIO

[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

To confirm the step was followed correctly 39


Add user/driver file – Compile a project
The Simple Example Project

o The LEDs shifted upwards during startup.


o The buzzer sounds if pb1 is pressed.
o Shift LEDs downward at intervals of 100ms continuously in case of downward direction.
o Shift LEDs upward at intervals of 150ms continuously in case of upward direction.
o Change direction of LEDs shift downward if pb2 is pressed.
o Change direction of LEDs shift upward if pb3 is pressed.

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

(1) Copy and Rename

(2) Delete any HAL_Part Subfolders/files

44
HOW TO COPY A READY IOC FILE

(3) Rename

(4) Double click ioc file renamed, and Generate Code

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

o The LEDs shifted upwards during startup.


o The buzzer toggles sounds if pb1 is pressed – Extended Interrupt (PA1)
o Shift LEDs downward at intervals of 100ms continuously in case of downward direction.
o Shift LEDs upward at intervals of 150ms continuously in case of upward direction.
o Change direction of LEDs shift downward if pb2 is pressed.
o Change direction of LEDs shift upward if pb3 is pressed.

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

Pull down latch clk


set x to 7
a:
Pull down shift clk
check bit-x of Y
if test bit =1
data shift in =1
else
data shift in 0
Pull up shift clk
reduce x
if x >-1 goto a:
Pull up latch clk

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

o The LEDs shifted upwards during startup.


o The buzzer toggles sounds if pb1 is pressed
o Shift LEDs downward at intervals of 100ms continuously in case of downward direction.
o Shift LEDs upward at intervals of 150ms continuously in case of upward direction.
o Change direction of LEDs shift downward if pb2 is pressed.
o Change direction of LEDs shift upward if pb3 is pressed.
o Numbers from 0 to 9999 are displayed continuously at 50ms intervals

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

You might also like