How does the event flag work on STM32? Part2
This is the part 2 of the article to explain How the Event Flag work on STM32 with a working example. Welcome back!1. MATERIALS:To develop this article’s demo, the following materials were necessary:- STM32CubeIDE version 1.10.0 or higher- X-CUBE-AZRTOS-G4 1.0.0 or higher- STM32G474 Discovery Kit (B-G474E-DPOW1)2. DEVELOPMENT:Let us start creating a new STM32 Project on STM32CubeIDE for the STM32G474RET microcontroller.Give your project name, just remember to avoid space and special characters – the name given by this article is “EV_FLAG_G474”.To import the Azure RTOS to the project, click on “Software Packs”, and then “Select Components”.In “Software Packs Component Selector” menu, locate STMicroelectronics.X-CUBE-AZRTOS-G4 → RTOS ThreadX → ThreadX, check “Core” box and click OK. Add the Azure RTOS ThreadX checking the box inside Software Packs → STMicroelectronics.X-CUBE-AZRTOS-G4. As this demo is just meant to create small timers and GPIOs managements according to the Event Flags, the default settings are fine. By default, the HAL driver uses the Systick as its primary time base, but this timer should be left to the AzureRTOS only. If the HAL library does not have a separate time source, the compilation and code execution might fail because both libraries want to use the SysTick_Handler interrupt. To prevent this, we can simply select a different time base for the HAL by clicking in the System Core → SYS and selecting the time base Source as TIM6: Go to clock configurations and adjust the settings as shown in the image to achieve the HLCK at 170MHz.Configure the following pins as table shown:
Component | Pin | Configuration | Label |
Joystick Select | PC13 | GPIO External Interrupt mode with Falling Edge trigger detection and pull-up | JOYSTICK_SELECT |
Joystick Up | PB10 | JOYSTICK_UP |
Joystick Right | PB2 | JOYSTICK_RIGHT |
Joystick Down | PC5 | JOYSTICK_DOWN |
Joystick Left | PC4 | JOYSTICK_LEFT |
RGB led Red | PC6 | TIM3_CH1 | PWM_RED |
RGB led GREEN | PC8 | TIM3_CH3 | PWM_GREEN |
RGB led BLUE | PA8 | TIM1_CH1 | PWM_BLUE |
Enable the GPIO External interrupts on NVIC tab and give them a 14 Preemption Priority (this priority will be 1 level lower than thread priorities).Set timer 1 and 3 configurations as the following settings:
- TIM1_CHANNEL1: PWM Generation CH1.
- TIM3_CHANNEL1: PWM Generation CH1.
- TIM3_CHANNEL3: PWM Generation CH3.
- Prescaler: 169, that sets the timer frequency to 1MHz = 170Mhz/(169 + 1).
- Counter Period: 99, which sets the PWM frequency to 100KHz = 1MHz/(99 + 1) and a resolution of 1%.
- Auto-reload preload: Enable.
The other settings are default values.The last step before the code generation is to enable the “Generate peripheral initialization as a pair of ‘.c/.h’ files per peripheral” in Project Manager → Code Generator. This should be helpful to HAL functions and resources management.All set, we can press Alt+K to generate the code or click on the following button.In part 3 we will cover the step by step of the code implementation using the STM32CubeIDE.