cancel
Showing results for 
Search instead for 
Did you mean: 

Program does not enter while(1) loop

N ORhan
Associate III

Hi everyone.

Up to now i have been programming STM32 mcus at register level but i have to use cube mx now. So i created a cube mx project only to see if timer 2 works fine. When i build without any problems and run the code on my F303RE board and pause i see program pointer shows at the least function call before while(1) loop which in here is "HAL_TIM_PWM_Start" function. I didn't understand why this problem occured. Can anyone please help me? Below is my simple code. Thank you.

#include "main.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

TIM_HandleTypeDef htim2;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

static void MX_TIM2_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_USART2_UART_Init();

 MX_TIM2_Init();

 /* USER CODE BEGIN 2 */

 HAL_TIM_Base_Start(&htim2);

 HAL_TIM_Base_Start(&htim2);

 HAL_TIM_PWM_Start(&htim2, HAL_TIM_ACTIVE_CHANNEL_1);

 HAL_TIM_PWM_Start(&htim2, HAL_TIM_ACTIVE_CHANNEL_2);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while(1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

4 REPLIES 4

Going to have to debug it an understand why/where it stopped.

Check it is not in ErrorHandler() or HardFault_Handler()

Check it is not in Default_Handler() due to lack of interrupt handler for things you have enabled.

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

Than you for your answer @Community member​. When i looked at disassembly to find where the program really is, i realized that it actually works in while(1), but in the C code window it shows the last function above while(1).

Most likely you have optimization flags set. Compiler code optimizations and C level line-by-line debugging are pretty much incompatible.

Yes @After Forever​ , i had made some changes in optimization settings on TrueStudio. After i load the default settings it worked.

Thank you for your answer.