Chapter2-STM32_GPIOdriverHAL
Chapter2-STM32_GPIOdriverHAL
STM32
Programmation GPIO par HAL
1
PLAN
■ Introduction
■ Programmation
2
Introduction
3
Objectifs
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 parameter can
uint32_t Mode; 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. This
uint32_t Alternate; 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
GPIO_InitTypeDef::PIN
#define GPIO_PIN_0 ((uint16_t)0x0001U)
#define GPIO_PIN_1 ((uint16_t)0x0002U)
typedef struct { #define GPIO_PIN_2 ((uint16_t)0x0004U)
uint32_t Pin; #define GPIO_PIN_3 ((uint16_t)0x0008U)
uint32_t Mode; #define GPIO_PIN_4 ((uint16_t)0x0010U)
#define GPIO_PIN_5 ((uint16_t)0x0020U)
uint32_t Pull;
#define GPIO_PIN_6 ((uint16_t)0x0040U)
uint32_t Speed;
#define GPIO_PIN_7 ((uint16_t)0x0080U)
uint32_t Alternate; #define GPIO_PIN_8 ((uint16_t)0x0100U)
}GPIO_InitTypeDef; #define GPIO_PIN_9 ((uint16_t)0x0200U)
#define GPIO_PIN_10 ((uint16_t)0x0400U)
#define GPIO_PIN_11 ((uint16_t)0x0800U)
#define GPIO_PIN_12 ((uint16_t)0x1000U)
#define GPIO_PIN_13 ((uint16_t)0x2000U)
#define GPIO_PIN_14 ((uint16_t)0x4000U)
#define GPIO_PIN_15 ((uint16_t)0x8000U)
#define GPIO_PIN_All ((uint16_t)0xFFFFU)
9
GPIO_InitTypeDef::MODE 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;
15
HAL_GPIO_DeInit
16
HAL_GPIO_ReadPin()
17
HAL_GPIO_ReadPin
GPIO_PinState HAL_GPIO_ReadPin( GPIO_TypeDef*
GPIOx,
uint16_t GPIO_Pin)
typedef enum
{
GPIO_PIN_RESET = 0,
GPIO_PIN_SET
}GPIO_PinState;
/**
* 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
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
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
Exemple
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
Suite ..
int main(void) {
GPIO_InitTypeDef ma_structure;
…………
…………
HAL_GPIO_Init(GPIOD, &ma_structure);
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, allumer la led
PD12 tant que le bouton user est
appuyé?
En utilisant le HAL, inverser les
leds deux par deux à chaque
appuie sur le bouton user?