Académique Documents
Professionnel Documents
Culture Documents
2
Introduction
3
Objectifs
• Accéder aux registres GPIO (ou Autres) comme
prédéfinis dans la bibliothèque HAL STM32
4
Présentation des APIs GPIAO
• Les APIs GPIO du HAL sont les suivants:
– HAL_GPIO_Init()
– HAL_GPIO_DeInit()
– HAL_GPIO_ReadPin()
– HAL_GPIO_WritePin()
– HAL_GPIO_TogglePin ().
• En plus des modes standards (input, output, analog), chaque
broche (PINE) peut être configurée en mode
– EXTI
– EVENT.
• Lorsque le mode EXTI est utilisé:
– L’utilisateur doit appeler la fonction HAL_GPIO_EXTI_IRQHandler() dans le
fichier stm32f4xx_it.c
– L’utilisateur doit implémenter la fonction HAL_GPIO_EXTI_Callback()
5
HAL_GPIO_INIT
6
Description du HAL_GPIO_Init
Function void HAL_GPIO_Init (GPIO_TypeDef * GPIOx,
Name GPIO_InitTypeDef * GPIO_Init)
Return none
values
7
GPIO_InitTypeDef
uint32_t GPIO_InitTypeDef::Pin
Specifies the GPIO pins to be configured. This parameter can be
any value of GPIO_pins_define
typedef struct {
uint32_t GPIO_InitTypeDef::Mode
uint32_t Pin;
Specifies the operating mode for the selected pins. This
uint32_t Mode; parameter can be a value of GPIO_mode_define
uint32_t Pull; uint32_t GPIO_InitTypeDef::Pull
uint32_t Speed; Specifies the Pull-up or Pull-Down activation for the selected pins.
uint32_t Alternate; This parameter can be a value of GPIO_pull_define
}GPIO_InitTypeDef; uint32_t GPIO_InitTypeDef::Speed
Specifies the speed for the selected pins. This parameter can be a
value of GPIO_speed_define
uint32_t GPIO_InitTypeDef::Alternate
Peripheral to be connected to the selected pins. This parameter
can be a value of GPIO_Alternate_function_selection
8
typedef struct {
uint32_t
uint32_t
Pin;
Mode;
GPIO_InitTypeDef::PIN
uint32_t Pull;
uint32_t Speed;
uint32_t Alternate;
}GPIO_InitTypeDef;
10
GPIO_InitTypeDef::Pull
typedef struct {
uint32_t Pin;
uint32_t Mode;
uint32_t Pull;
uint32_t Speed;
uint32_t Alternate;
}GPIO_InitTypeDef;
/* No Pull-up or Pull-down */
#define GPIO_NOPULL ((uint32_t)0x00000000U )
/* Pull-up activation */
#define GPIO_PULLUP ((uint32_t)0x00000001U )
/*Pull-down activation */
#define GPIO_PULLDOWN ((uint32_t)0x00000002U)
11
GPIO_InitTypeDef::SPEED
typedef struct {
uint32_t Pin;
uint32_t Mode;
uint32_t Pull;
uint32_t Speed;
uint32_t Alternate;
}GPIO_InitTypeDef;
12
GPIO_InitTypeDef:: Alternate
GPIO_AF0_RTC_50Hz
typedef struct { GPIO_AF0_MCO
uint32_t Pin; GPIO_AF0_TAMPER
uint32_t Mode; GPIO_AF0_SWJ
uint32_t Pull; GPIO_AF0_TRACE
uint32_t Speed; GPIO_AF1_TIM1
uint32_t Alternate; ……
}GPIO_InitTypeDef; GPIO_AF3_TIM11
GPIO_AF4_I2C1
….
GPIO_AF4_I2C3
Peripheral to be connected to the selected pins. GPIO_AF5_SPI1
Possible values: GPIO_AFx_PPP, where GPIO_AF5_SPI2
AFx: is the alternate function index ……
PPP: is the peripheral instance
Example: use GPIO_AF1_TIM1 to connect TIM1 IOs on AF1.
These values are defined in the GPIO extended driver, since the AF mapping
may change between product lines.
13
Exemples d’utilisation de HAL_GPIO_Init
1) Configurer GPIOs en output push-pull pour commander les LEDs
connectées aux broches 12, 13,14 et 15
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
2) Configurer PA0 en entrée EXTI avec front descendant
GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Pin = GPIO_PIN_0;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
3) Configurer USART3 Tx (PC10, mapped on AF7) en alternate function:
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
HAL_GPIO_DeInit()
15
HAL_GPIO_DeInit
17
HAL_GPIO_ReadPin
/**
* Description: lit l’état de broche configurée en entrée.
* Paramètres1: GPIOx: where x can be (A..I)
* Paramètres2: GPIO_Pin: specifies the port bit to read.
GPIO_PIN = GPIO_PIN_x where x can be (0..15).
* Retour : GPIO_PinState = The input port pin value.
*/
18
HAL_GPIO_WritePin()
19
HAL_GPIO_WritePin
void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx,
uint16_t GPIO_Pin,
GPIO_PinState PinState)
/**
Description: Sets or clears the selected data port bit.
* @param1: GPIOx: where x can be (A..I)
* @param2: GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* @param3: PinState: specifies the value to be written to the selected bit.
* This parameter can be one of the GPIO_PinState enum values:
* @arg GPIO_PIN_RESET: to clear the port pin
* @arg GPIO_PIN_SET: to set the port pin
* @retval None
*/
20
Code de la fonction
Remarque: This function uses GPIOx_BSRR register to allow atomic
read/modify accesses. In this way, there is no risk of an IRQ occurring
between the read and the modify access.
if(PinState != GPIO_PIN_RESET)
{
GPIOx->BSRR = GPIO_Pin;
}
else
{
GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
}
}
21
HAL_GPIO_TogglePin()
22
HAL_GPIO_TogglePin
void HAL_GPIO_TogglePin( GPIO_TypeDef* GPIOx,
uint16_t GPIO_Pin)
{
GPIOx->ODR ^= GPIO_Pin;
}
/**
Description: Toggles the specified GPIO pins..
* @param1: GPIOx: where x can be (A..I)
* @param2: GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* @retval None
*/
23
Activation des horloges des GPIO
24
Les Macros d’activation/reset
Un ensemble de macros est défini dans le fichier
stm32f4xx_hal_rcc.h. Ces macros permettent d’exécuter des
opérations élémentaires dans les registres de RCC.
25
__PPP_CLK_ENABLE()/
__PPP_CLK_DISABLE
#define __HAL_RCC_GPIOA_CLK_ENABLE() do { \
SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN); \
}while(0)
#define __HAL_RCC_GPIOA_CLK_DISABLE() \
(RCC->AHB1ENR &= ~(RCC_AHB1ENR_GPIOAEN))
26
__PPP_FORCE_RESET() et
__PPP_RELEASE_RESET()
#define __HAL_RCC_GPIOB_FORCE_RESET() \
(RCC->AHB1RSTR |= (RCC_AHB1RSTR_GPIOBRST))
#define __HAL_RCC_GPIOH_RELEASE_RESET() \
(RCC->AHB1RSTR &= ~(RCC_AHB1RSTR_GPIOHRST))
Ces deux macros sont aussi définies pour tous les GPIOs
27
PROGRAMMATION AVEC LES
APIS
28
28
Exemple
29
29
Exemple …code
int main(void) {
GPIO_InitTypeDef ma_structure;
__HAL_RCC_GPIOD_FORCE_RESET();
__HAL_RCC_GPIOD_RELEASE_RESET();
__HAL_RCC_GPIOD_CLK_ENABLE();
ma_structure.Pin = GPIO_Pin_12;
ma_structure.Mode = GPIO_MODE_OUTPUT_PP;
ma_structure.Pull = GPIO_NOPULL;
ma_structure.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &ma_structure);
30
30
Suite ..
int main(void) {
GPIO_InitTypeDef ma_structure;
…………
…………
HAL_GPIO_Init(GPIOD, &ma_structure);
31
31
Clignotez les 4 LEDS en
même temps en utilisant le
HAL ?
Défiler les 4 leds en utilisant
le HAL ?
En utilisant le HAL, inverser
les leds deux par deux à
chaque appuie sur le bouton
user?