APIs

HAL

Gérer une interruption externe (→ EXTI) avec STM32CubeMX

Procédure :

  1. Dans STM32CubeMX cliquer sur la broche à configurer comme source d’interruption

  2. Affecter à cette broche le rôle “GPIO_EXTI<n>” (<n> = n° de broche)

    exti cfg 01
    Exemple

    Seules les broches des GPIOs numérotées de 0 à 4 (PA0…​PA4, PB0…​PB4, …​) peuvent bénéficier d’une interruption externe dédiée (→ EXTI0 à EXTI4) au niveau du contrôleur d’interruption (→ NVIC). Les autres se partagent — selon leur numéro (5 à 9 ou 10 à 15) — 2 interruptions externes (→ EXTI[5..9] et EXTI[10-15]) et par conséquent 2 vecteurs d’interruption.

  3. Sélectionner la rubrique GPIO et dans l’onglet nommé également GPIO, définir les caractéristiques de l’interruption associée à la broche : déclenchement sur front montant ou sur front descendant et activation ou non des résistances de pull-up/pull-down

    exti cfg 02
  4. Sélectionner la catégorie NVIC et dans l’onglet nommé également NVIC sélectionner la ligne d’interruption concernée (→ EXTI0 …​EXTI4, EXTI[5..9], EXTI[10-15]) puis l’activer en cochant la case “Enabled” et en ajustant si nécessaire le couple “Preemption priority” : “Sub Priority”

    exti cfg 03
    Preemption priority / Sub Priority
    • La Preemption Priority (ou Group Priority) permet à une interruption de suspendre une interruption en cours de traitement si sa priorité de groupe est supérieure (i.e possède une valeur inférieure)

      La Sub Priority permet, quant à elle, d’ordonner les interruptions en attente en vue de leur traitement (plus la valeur est petite, plus grande est la sous-priorité).

    • Le nombre de Preemption Priorities et Sub Priorities dépend du nombre de bits accordés pour leur codage. Ce nombre de bit est configurable au niveau global (→ voir liste déroulante “Priority Group” dans le screenshot de STM32CubeMX ci-dessus).

      Ainsi, si 0 bit est accordé pour le codage de la propriété de groupe alors 15 niveaux de sous-priorité sont disponibles. Si 1 bit est accordé pour le codage de la propriété de groupe alors 2 niveaux de priorité de groupe et 8 niveaux de sous-priorité sont disponibles. etc…​

  5. Ajuster si nécessaire les options de l’onglet “Code generation”.

    exti cfg 04
    Code generation

    Se référer au chapitre “§4.4.14 NVIC Configuration window” de UM1718 STM32CubeMX for STM32 configuration and initialization C code generation link pour avoir des précisions sur les options disponibles.

  6. Sauvegarder pour lancer la génération de code

  7. Dans la section /* USER CODE BEGIN 4 / …​ / USER CODE END 4 */ du fichier main.c, ajouter la fonction de traitement de l’interruption qui doit obligatoirement avoir le prototype suivant void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) dans le cas d’une interruption sur un MCU STM32F4 déclenchée sur front montant. Voir dans la fonction void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin) du fichier stm32f4xx_it.c le nom du callback à utiliser.

    Exemple : main.c
    // [...]
    /* USER CODE BEGIN 4 */
    void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) (1)
    {
       if(GPIO_Pin == INT2_ADXL375_Pin) {
          shock = true; (2)
       }
    }
    /* USER CODE END 4 */
    [...]
    1 Le paramètre indique la broche qui a provoqué l’interruption
    2 Ici, la routine se contente de changer la valeur d’une variable globale dont le contenu est testé dans la “super-loop” du programme principal pour lancer ou non une action (ex. : allumer une DEL, envoyer un message sur liaison série, débloquer une tâche FreeRTOS, …​).

Modes basse consommation

Ressources

Low Layer ( → LL)

Présentation

What is the Cube Low-Layer API?

With the advent of the STCube initiative, ST completely redesigned the SDK for the STM32 series introducing the Hardware Abstraction Layer (HAL) library and throwing out of the windows the old Standard Peripheral Library (SPL), which was very popular in the ST community despite of the fact it lacked many features related to more recent and powerful STM32 MCUs. However, the HAL library has attracted a lot of criticism over the years, both because it suffered of too many bugs during the first years and - more important - because it does not represent a good example of well optimized code for the development of embedded applications. The CubeHAL is really a not-performant library, but for one simple reason: it is designed to be abstract and to simplify the porting of user code between MCUs of the same series and MCUs of different STM32 series. This led to a library full of if and then and full of unnecessary code when working with a very specific microcontroller. But this is the price to pay when you want to streamline the development process and - more important - the adoption of a given complex microcontroller architecture like the STM32 portfolio. The HAL APIs are split into two categories: generic APIs, which provide common and generic functions for all the STM32 series, and extension APIs, which include specific and customized functions for a given line or part number. The HAL drivers include a complete set of ready-to-use APIs that simplify the user application implementation. The HAL drivers are feature-oriented instead of IP-oriented. For example, the timer APIs are split into several categories following the IP functions, such as basic timer, capture and pulse width modulation (PWM). The HAL driver layer implements run-time failure detection by checking the input values of all functions. Such dynamic checking enhances the firmware robustness. In the recent years, ST answered to strong criticism of the library’s performances by introducing the Cube Low-Layer (shortened LL) set of drivers. As the name suggest, the LL library is born to be very optimized, leaving to the programmer the responsibility to deal with very specific characteristics of the given STM32 series and the given P/N. The LL drivers offer hardware services based on the available features of the STM32 peripherals. These services reflect exactly the hardware capabilities and provide atomic operations that must be called by following the programming model described in the product line reference manual. As a result, the LL services are not based on standalone processes and do not require any additional memory resources to save their states, counter or data pointers. All operations are performed by changing the content of the associated peripheral registers. Unlike the HAL, LL APIs are not provided for peripherals for which optimized access is not a key feature, or for those requiring heavy software configuration and/or a complex upper-level stack (such as USB). LLbased code is essentially a sequence of C macros that will be expanded in a series of statements with a very limited usage of branches and unpredictable statements from the performance point-of-view. This book will not cover topics related to the LL library. It would require a completely different approach to the text and a strong focus on just few STM32 P/N. This book aims to be generic and to provide an overview of the most relevant features to start designing powerful and complex electronic boards. If you need to control every single aspect of a given peripheral to reach the most optimized code, then the LL library is what you need. But, at the first instance, I suggest you start designing the firmware by using the CubeHAL and then moving to the next step, unless you are a very experienced firmware developer.

— Carmine Noviello
Mastering STM32 - Second Edition §4.1.3 Project Manager

Utiliser l’API LL plutôt que HAL

Cela se fait au niveau de chaque périphérique dans STM32CubeMX dans l’onglet “Project Manager”

select low layer

🕮 Sources : § 4.9.3 Choosing between HAL and LL based code generation for a given peripheral instance dans UM1718 - User manual - STM32CubeMX for STM32 configuration and initialization C code generation link

🞄  🞄  🞄