This application note explains how to use the STM32H7 PWM in the FreeRTOS demo application.
Understanding PWM Interfaces
FreeRTOS PWM Implementation
The FreeRTOS BSP make uses of the STM32CubeH7 software component to provide a device driver for the STM32H7 PWM. The driver is configured (enabled / disabled) at the BSP build time, using the HAL_TIM_MODULE_ENABLED configuration option, defined in the stm32h7xx_hal_conf.h file.
FreeRTOS PWM C-Binding API
The PWM driver implements the following C-binding API:
Function | Description | Comments |
HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim) | ||
Initialize the TIM PWM mode according to the specified parameters by htim | htim is a pointer to a structure that contains the configuration information for TIM module; returns one of {HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT} | |
HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim) | ||
De-initialize the TIM peripheral | htim is a pointer to a structure that contains the configuration information for TIM module; returns one of {HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT} | |
HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel) | ||
Initialize the TIM PWM channel according to the specified parameters by sConfig | htim is a pointer to a structure that contains the configuration information for TIM module; sConfig is a TIM PWM configuration structure; Channel is a TIM channel to be configured; returns one of {HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT} | |
HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel) | ||
Starts the PWM signal generation | htim is a pointer to a structure that contains the configuration information for TIM module; Channel is a TIM channel to be enabled; returns one of {HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT} | |
HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel) | ||
Stops the PWM signal generation | htim is a pointer to a structure that contains the configuration information for TIM module; Channel is a TIM channel to be disabled; returns one of {HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT} |
The TIM_HandleTypeDef data structure used in this API has the following definition:
typedef struct { TIM_TypeDef *Instance; /* Register base address */ TIM_Base_InitTypeDef Init; /* TIM Time Base required parameters */ HAL_TIM_ActiveChannel Channel; /* Active channel */ DMA_HandleTypeDef *hdma[7]; /* DMA Handlers array */ HAL_LockTypeDef Lock; /* Locking object */ __IO HAL_TIM_StateTypeDef State; /* TIM operation state */ } TIM_HandleTypeDef;
The TIM_OC_InitTypeDef data structure used in HAL_TIM_PWM_ConfigChannel interface has the following definition:
typedef struct { uint32_t OCMode; /* Specifies the TIM mode. */ uint32_t Pulse; /* Specifies the pulse value to be loaded into the Capture Compare Register. */ uint32_t OCPolarity; /* Specifies the output polarity. */ uint32_t OCNPolarity; /* Specifies the complementary output polarity. */ uint32_t OCFastMode; /* Specifies the Fast mode state. */ uint32_t OCIdleState; /* Specifies the TIM Output Compare pin state during Idle state. */ uint32_t OCNIdleState; /* Specifies the TIM Output Compare pin state during Idle state. */ } TIM_OC_InitTypeDef;
PWM CLI Command
The FreeRTOS application implements the following PWM related CLI command:
Command | Description | Comments |
pwm_start <tim> <chnl> <period> <duty> | Start a PWM output signal | tim is the timer number, chnl is the channel number, period is the PWM output frequency (value in nanoseconds), duty is the duty cycle (percentage value) |
pwm_stop <tim> <chnl> | Stop a PWM output | tim is the timer number, chnl is the channel number |
Validating PWM Operation
Use the following step-wise procedure to validate the FreeRTOS PWM operation:
Connect an oscilloscope probe to the PWM pin on the carrier board:
On the STM32H7-BSB Rev 1A board: P7.4 (PB8).
On the STM32H7-BSB Rev 2A board: P7.5 (PB8).
From the FreeRTOS CLI, activate the 16 PWM output on the channel 1 to run at 1kHZ (value in nanoseconds), duty cycle 50%:
CLI> pwm_start 16 1 1000000 50
Validate that the PWM frequency and duty cycle match the parameters specified in the previous commands (1kHz and 50%).
Stop the 16 PWM on the channel 1:
CLI> pwm_stop 16 1
Activate the 16 PWM output on the channel 1 to run at 5kHZ (value in nanoseconds), duty cycle 20%:
CLI> pwm_start 16 1 5000000 20
Validate that the PWM frequency and duty cycle match the parameters specified in the previous commands (5kHz and 20%).
Stop the 16 PWM on the channel 1:
CLI> pwm_stop 16 1