cancel
Showing results for 
Search instead for 
Did you mean: 

PWM mod. Where in the code is the microcontroller pin enabled and disabled?

MMust.5
Senior II

STM32F407

There are only two lines in the code that I wrote.

HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);

TIM4->CCR2=3;

The pin is activated when the number in the CNT register and the CCR2 register match.

The pin is deactivated at the end of counting in the CNT register.

Where can I find in the code what causes this behavior?

In the code, I did not find a mention of the GPIOD PIN 13 pin.

Interrupts are disabled.

In the Out Compare CH2 mode, the pin is not activated without writing a code to activate the pin.


_legacyfs_online_stmicro_images_0693W00000blFqTQAU.png

3 REPLIES 3

Often this is handled in the MSP files.

Initialize clocks, pins and peripherals. Selects the AF (Alternate Function) via mux settings in the pin initialization. Refer to Data Sheet for STM32 part in question.

Suggest Find-in-Files "HAL_GPIO_Init(" or "GPIO_PIN_13"

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

The stm32f4xx_hal_msp.c file contains the initialization of pin 13.

But there is no pin activation and deactivation in this file.

/* USER CODE BEGIN Header */
 
/**
 
 ******************************************************************************
 
 * @file     stm32f4xx_hal_msp.c
 
 * @brief    This file provides code for the MSP Initialization
 
 *        and de-Initialization codes.
 
 ******************************************************************************
 
 * @attention
 
 *
 
 * Copyright (c) 2023 STMicroelectronics.
 
 * All rights reserved.
 
 *
 
 * This software is licensed under terms that can be found in the LICENSE file
 
 * in the root directory of this software component.
 
 * If no LICENSE file comes with this software, it is provided AS-IS.
 
 *
 
 ******************************************************************************
 
 */
 
/* USER CODE END Header */
 
 
 
/* Includes ------------------------------------------------------------------*/
 
#include "main.h"
 
/* USER CODE BEGIN Includes */
 
 
 
/* USER CODE END Includes */
 
 
 
/* Private typedef -----------------------------------------------------------*/
 
/* USER CODE BEGIN TD */
 
 
 
/* USER CODE END TD */
 
 
 
/* Private define ------------------------------------------------------------*/
 
/* USER CODE BEGIN Define */
 
 
 
/* USER CODE END Define */
 
 
 
/* Private macro -------------------------------------------------------------*/
 
/* USER CODE BEGIN Macro */
 
 
 
/* USER CODE END Macro */
 
 
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
 
 
 
/* USER CODE END PV */
 
 
 
/* Private function prototypes -----------------------------------------------*/
 
/* USER CODE BEGIN PFP */
 
 
 
/* USER CODE END PFP */
 
 
 
/* External functions --------------------------------------------------------*/
 
/* USER CODE BEGIN ExternalFunctions */
 
 
 
/* USER CODE END ExternalFunctions */
 
 
 
/* USER CODE BEGIN 0 */
 
 
 
/* USER CODE END 0 */
 
 
 
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
 
          /**
 
 * Initializes the Global MSP.
 
 */
 
void HAL_MspInit(void)
 
{
 
 /* USER CODE BEGIN MspInit 0 */
 
 
 
 /* USER CODE END MspInit 0 */
 
 
 
 __HAL_RCC_SYSCFG_CLK_ENABLE();
 
 __HAL_RCC_PWR_CLK_ENABLE();
 
 
 
 /* System interrupt init*/
 
 
 
 /* USER CODE BEGIN MspInit 1 */
 
 
 
 /* USER CODE END MspInit 1 */
 
}
 
 
 
/**
 
* @brief TIM_Base MSP Initialization
 
* This function configures the hardware resources used in this example
 
* @param htim_base: TIM_Base handle pointer
 
* @retval None
 
*/
 
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
 
{
 
 if(htim_base->Instance==TIM4)
 
 {
 
 /* USER CODE BEGIN TIM4_MspInit 0 */
 
 
 
 /* USER CODE END TIM4_MspInit 0 */
 
  /* Peripheral clock enable */
 
  __HAL_RCC_TIM4_CLK_ENABLE();
 
 /* USER CODE BEGIN TIM4_MspInit 1 */
 
 
 
 /* USER CODE END TIM4_MspInit 1 */
 
 }
 
 
 
}
 
 
 
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
 
{
 
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 
 if(htim->Instance==TIM4)
 
 {
 
 /* USER CODE BEGIN TIM4_MspPostInit 0 */
 
 
 
 /* USER CODE END TIM4_MspPostInit 0 */
 
 
 
  __HAL_RCC_GPIOD_CLK_ENABLE();
 
  /**TIM4 GPIO Configuration
 
  PD13   ------> TIM4_CH2
 
  */
 
  GPIO_InitStruct.Pin = GPIO_PIN_13;
 
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
  GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
 
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
 
 
 
 /* USER CODE BEGIN TIM4_MspPostInit 1 */
 
 
 
 /* USER CODE END TIM4_MspPostInit 1 */
 
 }
 
 
 
}
 
/**
 
* @brief TIM_Base MSP De-Initialization
 
* This function freeze the hardware resources used in this example
 
* @param htim_base: TIM_Base handle pointer
 
* @retval None
 
*/
 
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
 
{
 
 if(htim_base->Instance==TIM4)
 
 {
 
 /* USER CODE BEGIN TIM4_MspDeInit 0 */
 
 
 
 /* USER CODE END TIM4_MspDeInit 0 */
 
  /* Peripheral clock disable */
 
  __HAL_RCC_TIM4_CLK_DISABLE();
 
 
 
  /* TIM4 interrupt DeInit */
 
  HAL_NVIC_DisableIRQ(TIM4_IRQn);
 
 /* USER CODE BEGIN TIM4_MspDeInit 1 */
 
 
 
 /* USER CODE END TIM4_MspDeInit 1 */
 
 }
 
 
 
}
 
 
 
/* USER CODE BEGIN 1 */
 
 
 
/* USER CODE END 1 */
 

The TIM *hardware* has counters and comparators, it's those that drive the pin high and low, it's not done in software.

Perhaps look at the Reference Manual, and the block diagrams of the TIM units.

The gate level portion of the design is not expressed in the materials available to you.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..