HAL GPIO Use Cases

This chapter covers various use cases for the GPIO HAL module.

Process

User application toggling an IO

@startuml



hide footbox

skinparam ParticipantPadding 150

skinparam DefaultFontSize 26

skinparam ArrowFontSize 24

skinparam NoteFontSize 22

skinparam boxPadding 80

skinparam backgroundColor #FBFBFB

skinparam DiagramBorderColor #black

skinparam DiagramBorderThickness 2

skinparam Padding 6

scale 1600 Width

!pragma teoz true



|||

|||

== Initialization && Configuration==

|||

|||



"User Application" -> "System Driver":  **HAL_Init()**

note right: **Configure System Clock**

"System Driver" --> "User Application": hal status: HAL_OK

note left:  **return**



 group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL == HAL_CLK_ENABLE_NO) **

|||

"User Application" -> "RCC": **Enable the current GPIO port Clock**

|||

|||

end group

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_2, &p_gpio_config)**

note right: **Initialize the GPIO Pin to use**\n**in   OUTPUT mode**



"HAL GPIO Driver" --> "User Application": hal status: HAL_OK

|||

note left:  **return**

note right: **GPIO pin PA2 initialized in OUTPUT mode successfully **

group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO) **

|||

"HAL GPIO Driver" -> "RCC": **Enable the current GPIO port Clock**



|||

|||

end group

|||



|||

|||

== Process ==

|||

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_TogglePin(HAL_GPIOA, HAL_GPIO_PIN_2)**

note right: **Toggle the actual state of the GPIO Pin PA2**

|||

"HAL GPIO Driver" -> "STM32 Hardware": **Change the logical state of the GPIO pin PA2 from HIGH to LOW and vice-versa**

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_ReadPin(HAL_GPIOA, HAL_GPIO_PIN_2)**

note right: **Retrieve the actual state of the GPIO Pin PA2**



"HAL GPIO Driver" <- "STM32 Hardware": **Read the logical state of the GPIO pin PA2 (either HIGH or LOW)**

"User Application" <- "HAL GPIO Driver":  **return the actual logical state of the GPIO Pin PA2**







@enduml

Called functions:

User application configuring a GPIO pin in INPUT mode

@startuml



hide footbox

skinparam ParticipantPadding 150

skinparam DefaultFontSize 26

skinparam ArrowFontSize 24

skinparam NoteFontSize 22

skinparam boxPadding 80

skinparam backgroundColor #FBFBFB

skinparam DiagramBorderColor #black

skinparam DiagramBorderThickness 2

skinparam Padding 6

skinparam SequenceGroupBorderThickness 5

scale 1600 Width

!pragma teoz true

|||

|||

== Initialization && Configuration==

|||

|||



"User Application" -> "System Driver":  **HAL_Init()**

note right: **Configure System Clock**

"System Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

 group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL == HAL_CLK_ENABLE_NO) **

|||

"User Application" -> "RCC": **Enable the current GPIO port Clock**

|||

|||

end group

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_10, &p_gpio_config)**

note right: **Initialize the GPIO Pin to use**\n**in   INPUT mode**

"HAL GPIO Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

note right: **GPIO pin PA10 initialized in INPUT mode successfully **

group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO) **

|||

"HAL GPIO Driver" -> "RCC": **Enable the current GPIO port Clock**



|||

|||

end group

|||

|||

== Process ==

|||

|||

"HAL GPIO Driver" <- "STM32 Hardware": **Signal input on the GPIO pin PA10 (either HIGH or LOW)**

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_ReadPin(HAL_GPIOA, HAL_GPIO_PIN_10)**

note right: **Retrievee the actual state of the GPIO Pin PA10**

|||

"User Application" <- "HAL GPIO Driver":  **return the actual logical state of the GPIO Pin PA10**





@enduml

Called functions:

User application configuring a GPIO pin in Alternate mode

@startuml



hide footbox

skinparam ParticipantPadding 150

skinparam DefaultFontSize 26

skinparam ArrowFontSize 24

skinparam NoteFontSize 22

skinparam boxPadding 80

skinparam backgroundColor #FBFBFB

skinparam DiagramBorderColor #black

skinparam DiagramBorderThickness 2

skinparam Padding 6

skinparam SequenceGroupBorderThickness 5

scale 1600 Width

!pragma teoz true

|||

|||

== Initialization && Configuration==

|||

|||



"User Application" -> "System Driver":  **HAL_Init()**

note right: **Configure System Clock**

"System Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

 group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL == HAL_CLK_ENABLE_NO) **

|||

"User Application" -> "RCC": **Enable the current GPIO port Clock**

|||

|||

end group

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_11, &p_gpio_config)**

note right: **Initialize the GPIO Pin to use**\n**in   Alternate mode**

"HAL GPIO Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

note right: **GPIO pin PA11 initialized in Alternate mode successfully **

group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO) **

|||

"HAL GPIO Driver" -> "RCC": **Enable the current GPIO port Clock**



|||

|||

end group

|||

|||

== De-initialization ==



"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_11, &p_gpio_config)**

note right: **De-initialize the GPIO pin PA11 and reset its configuration**

|||

hnote across: **GPIO pin PA11 de-initialized and configuration reset successfully **



|||

|||





@enduml

Called functions:

User application Configuring GPIO pin in INPUT mode to use with EXTI line through EXTI mux

@startuml



hide footbox

skinparam ParticipantPadding 150

skinparam DefaultFontSize 26

skinparam ArrowFontSize 24

skinparam NoteFontSize 22

skinparam boxPadding 80

skinparam backgroundColor #FBFBFB

skinparam DiagramBorderColor #black

skinparam DiagramBorderThickness 2

skinparam Padding 6

scale 1600 Width

!pragma teoz true



|||

|||

== Initialization ==

|||

|||



"User Application" -> "System Driver":  **HAL_Init()**

note right: **Configure System Clock**

"System Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

 group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL == HAL_CLK_ENABLE_NO) **

|||

"User Application" -> "RCC": **Enable the current GPIO port Clock**

|||

|||

end group

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_2 ,&p_gpio_config)**

note right: **Initialize the GPIO Pin to use**

"HAL GPIO Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO) **

|||

"HAL GPIO Driver" -> "RCC": **Enable the current GPIO port Clock**



|||

|||

end group

"User Application" -> "HAL EXTI Driver":  **HAL_EXTI_Init(&hexti, HAL_EXTI_LINE_2)**

note right: **Initialize the EXTI.**\n**Store Line into Handle**

"HAL EXTI Driver" --> "User Application": hal status: HAL_OK

note left:  **return**



|||

|||

== Configuration ==

|||

|||



"User Application" -> "HAL EXTI Driver":  **HAL_EXTI_SetConfig(&hexti, &p_exti_config)**

note right: **Configure the EXTI Line**\n**stored in the handle**

"HAL EXTI Driver" --> "User Application": hal status: HAL_OK

note left:  **return**



|||

|||

== NVIC Settings ==

|||

|||



"User Application" -> "System Driver":  **HAL_CORTEX_NVIC_SetPriority(EXTI2_IRQn, \

HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0)**

note right: **Configure EXTI line**\n**IRQ priority**

"System Driver" --> "User Application": Priority set

"User Application" -> "System Driver":  **HAL_CORTEX_NVIC_EnableIRQ( EXTI2_IRQn)**

note right: **Enable EXTI line**\n**interrupt in the NVIC **

"System Driver" --> "User Application": Interrupt enabled



|||

|||

== Register Callback ==

|||

|||



"User Application" -> "HAL EXTI Driver":  **HAL_EXTI_RegisterTriggerCallback(&hexti, &user_cb)**

note right:  **Register the user defined callback**\n**function on rising edge trigger**

"HAL EXTI Driver" --> "User Application": hal status: HAL_OK

note left:  **return**



|||

|||

== Start the EXTI  ==

|||

|||



"User Application" -> "HAL EXTI Driver":  **HAL_EXTI_Enable(&hexti, HAL_EXTI_MODE_INTERRUPT)**

note right:  **Activate interrupt mode for**\n**the current EXTI **

"HAL EXTI Driver" --> "User Application": hal status: HAL_OK

note left:  **return**



|||

|||

== Interrupt Service Routine ==

|||

|||



 "EXTI_IRQHandler" <- :  **Interrupt**

note left:  **Receive an interrupt signal**

"EXTI_IRQHandler" --> "HAL EXTI Driver": "**HAL_EXTI_IRQHandler(&hexti)**"

note left:  **Call the IRQ Handler to manage upcoming interrupts**

"HAL EXTI Driver" -> "User Application": "**p_rising_callback()**"



@enduml

Called functions:

User application writing multiple GPIO pins in one shot

@startuml



hide footbox

skinparam ParticipantPadding 150

skinparam DefaultFontSize 26

skinparam ArrowFontSize 24

skinparam NoteFontSize 22

skinparam boxPadding 80

skinparam backgroundColor #FBFBFB

skinparam DiagramBorderColor #black

skinparam DiagramBorderThickness 2

skinparam Padding 6

scale 1600 Width

!pragma teoz true



|||

|||

== Initialization && Configuration==

|||

|||



"User Application" -> "System Driver":  **HAL_Init()**

note right: **Configure System Clock**

"System Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

 group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL == HAL_CLK_ENABLE_NO) **

|||

"User Application" -> "RCC": **Enable the current GPIO port Clock**

|||

|||

end group

"User Application" -> "HAL GPIO Driver":         **HAL_GPIO_Init****(HAL_GPIOB**, **HAL_GPIO_PIN_2** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_3** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_5** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_6** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_14, &p_gpio_config)**

note right: **Initialize the GPIO Pins to use**\n**in   OUTPUT mode**

"HAL GPIO Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

note right: **GPIO pins PB2, PB3, PB5, PB6 and PB14 initialized in OUTPUT mode successfully **

group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO) **

|||

"HAL GPIO Driver" -> "RCC": **Enable the current GPIO port Clock**



|||

|||

end group

|||

|||

== Process ==

|||

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_WriteMultipleStatePin****(HAL_GPIOB, HAL_GPIO_PIN_2** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_3** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_14,** \

 \n\t\t\t\t\t\t    **HAL_GPIO_PIN_5** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_6)**

note right: **Reset GPIO pin PB2**, **PB3 and PB14 and set GPIO pins PB5 and PB6**



"HAL GPIO Driver" -> "STM32 Hardware": **Change the logical state of the pin PB2**, **PB3 and PB14 from HIGH to LOW and GPIO pins PB5 and PB6 from LOW to HIGH**

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_ReadPin(HAL_GPIOB, HAL_GPIO_PIN_2)**

note right: **Retrieve the actual state of the GPIO Pin PB2**

"HAL GPIO Driver" <- "STM32 Hardware": **Read the logical state of the GPIO pin PB2 (either HIGH or LOW)**

"User Application" <- "HAL GPIO Driver":  **return the actual logical state of the GPIO Pin PB2**

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_ReadPin(HAL_GPIOB, HAL_GPIO_PIN_3)**

note right: **Retrieve the actual state of the GPIO Pin PB3**

"HAL GPIO Driver" <- "STM32 Hardware": **Read the logical state of the GPIO pin PB3 (either HIGH or LOW)**

"User Application" <- "HAL GPIO Driver":  **return the actual logical state of the GPIO Pin PB3**

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_ReadPin(HAL_GPIOB, HAL_GPIO_PIN_5)**

note right: **Retrieve the actual state of the GPIO Pin PB5**

"HAL GPIO Driver" <- "STM32 Hardware": **Read the logical state of the GPIO pin PB5 (either HIGH or LOW)**

"User Application" <- "HAL GPIO Driver":  **return the actual logical state of the GPIO Pin PB5**

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_ReadPin(HAL_GPIOB, HAL_GPIO_PIN_6)**

note right: **Retrieve the actual state of the GPIO Pin PB6**

"HAL GPIO Driver" <- "STM32 Hardware": **Read the logical state of the GPIO pin PB6 (either HIGH or LOW)**

"User Application" <- "HAL GPIO Driver":  **return the actual logical state of the GPIO Pin PB6**

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_ReadPin(HAL_GPIOB, HAL_GPIO_PIN_14)**

note right: **Retrieve the actual state of the GPIO Pin PB14**

"HAL GPIO Driver" <- "STM32 Hardware": **Read the logical state of the GPIO pin PB14 (either HIGH or LOW)**

"User Application" <- "HAL GPIO Driver":  **return the actual logical state of the GPIO Pin PB14**



@enduml

Called functions:

User application enabling the speed optimization feature

@startuml



hide footbox

skinparam ParticipantPadding 150

skinparam DefaultFontSize 26

skinparam ArrowFontSize 24

skinparam NoteFontSize 22

skinparam boxPadding 80

skinparam backgroundColor #FBFBFB

skinparam DiagramBorderColor #black

skinparam DiagramBorderThickness 2

skinparam Padding 6

scale 1600 Width

!pragma teoz true



|||

|||

== Initialization && Configuration==

|||

|||



"User Application" -> "System Driver":  **HAL_Init()**

note right: **Configure System Clock**

"System Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

 group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL == HAL_CLK_ENABLE_NO) **

|||

"User Application" -> "RCC": **Enable the current GPIO port Clock**

|||

|||

end group

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_Init(HAL_GPIOG, **HAL_GPIO_PIN_2** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_4** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_5** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_10** \

 \n\t\t\t\t\t\t  | **HAL_GPIO_PIN_14**,&p_gpio_config)**

note right: **Initialize the GPIO Pins to use**\n**in   OUTPUT mode**

"HAL GPIO Driver" --> "User Application": hal status: HAL_OK

note left:  **return**

note right: **GPIO pins PG2, PG4, PG5, PG10 and PG14 initialized in OUTPUT mode successfully **

group #LightBlue **#if defined (USE_HAL_GPIO_CLK_ENABLE_MODEL) && (USE_HAL_GPIO_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO) **

|||

"HAL GPIO Driver" -> "RCC": **Enable the current GPIO port Clock**



|||

|||

end group

|||

|||

== Process ==

|||

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_EnableHighSpeedLowVoltage****(HAL_GPIOG**, **HAL_GPIO_PIN_2** \

 \n\t\t\t\t\t\t\t\t\t  | **HAL_GPIO_PIN_4**  \

 \n\t\t\t\t\t\t\t\t\t  | **HAL_GPIO_PIN_5**  \

 \n\t\t\t\t\t\t\t\t\t  | **HAL_GPIO_PIN_10** \

 \n\t\t\t\t\t\t\t\t\t  | **HAL_GPIO_PIN_14**)

note right: **Enable the speed optimization feature for GPIO pins PG2**, **PG4**,**PG5**,**PG10 and PG14**



"HAL GPIO Driver" -> "STM32 Hardware": **Enable the HSLV feature for the dedicated GPIO pins**

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_TogglePin(HAL_GPIOG, HAL_GPIO_PIN_2)**

note right: **Toggle the actual state of the GPIO Pin PG2**

|||

"HAL GPIO Driver" -> "STM32 Hardware": **Change the logical state of the GPIO pin PG2 from HIGH to LOW and vice-versa**

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_ReadPin(HAL_GPIOG, HAL_GPIO_PIN_2)**

note right: **Retrieve the actual state of the GPIO Pin PG2**

"HAL GPIO Driver" <- "STM32 Hardware": **Read the logical state of the GPIO pin PG2 (either HIGH or LOW)**

"User Application" <- "HAL GPIO Driver":  **return the actual logical state of the GPIO Pin PG2**

|||

|||

"User Application" -> "HAL GPIO Driver":  **HAL_GPIO_DisableHighSpeedLowVoltage****(HAL_GPIOG**, **HAL_GPIO_PIN_2**)

note right: **Disable the speed optimization feature for GPIO pin PG2**



"HAL GPIO Driver" -> "STM32 Hardware": **Disable the HSLV feature for the GPIO pin PG2**

|||

|||

@enduml

Called functions: