cancel
Showing results for 
Search instead for 
Did you mean: 

Cube IDE and stm32 - LED not blinking

Bbodz.1
Associate

I have a trouble because I am trying to run simple code with blinking LED (with Cube IDE and ST Link v.2 mini). It is STM32L011F4P6 - without devboard - just simple mC with some resistors and capacitors, and without external oscillator.

Here's how I do it so far:

  • Opening Cube IDE, New -> STM32 project
  • Selecting STM32L011F4PX and clicking NEXT
  • Name of project and settings

0693W000001r8MUQAY.png

Next step: 

0693W000001r8MZQAY.png

...and "Finish.

  • Now, the hardest thing (and I think this is the area where my faults can be visible:

a) Set PA7 as a GPIO output and rename to "LED1" (PA7 has been connected to the anode of LED and cathode of LED to the ground.

0693W000001r8MeQAI.png

b) Debug serial wire - selected as below: 

0693W000001r8MjQAI.png

And no other options selected in that window (maybe that's the problem?)

c) ALT+K (generate code)

  1. Now opening main.c. Code generated automatically.
  2. I added only two lines:
  3. HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  4. HAL_Delay(1000);

Just two lines in "while (1) {}". Now I have to set bin and hex options:

0693W000001r8MoQAI.png

And that's all.

  1. Now build all and I have my bin which is ready to upload. I use "ST-Link Utility.
  2. ST-Link Utility: Target->Program.
  3. What I see? I see that PA7 LED is switched on... But not blinking:C Why?:c

CODE:

--------------------------------------------------------------------------

/* USER CODE BEGIN Header */

/**

 ******************************************************************************

 * @file      : main.c

 * @brief     : Main program body

 ******************************************************************************

 * @attention

 *

 * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.

 * All rights reserved.</center></h2>

 *

 * This software component is licensed by ST under BSD 3-Clause license,

 * the "License"; You may not use this file except in compliance with the

 * License. You may obtain a copy of the License at:

 *            opensource.org/licenses/BSD-3-Clause

 *

 ******************************************************************************

 */

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#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 ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

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

void SystemClock_Config(void);

static void MX_GPIO_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();

 /* USER CODE BEGIN 2 */

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);

HAL_Delay(200);

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

/**

 * @brief System Clock Configuration

 * @retval None

 */

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Configure the main internal regulator output voltage 

 */

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 /** Initializes the CPU, AHB and APB busses clocks 

 */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;

 RCC_OscInitStruct.MSIState = RCC_MSI_ON;

 RCC_OscInitStruct.MSICalibrationValue = 0;

 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  Error_Handler();

 }

 /** Initializes the CPU, AHB and APB busses clocks 

 */

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

 {

  Error_Handler();

 }

}

/**

 * @brief GPIO Initialization Function

 * @param None

 * @retval None

 */

static void MX_GPIO_Init(void)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOA_CLK_ENABLE();

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);

 /*Configure GPIO pin : LED1_Pin */

 GPIO_InitStruct.Pin = LED1_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

 * @brief This function is executed in case of error occurrence.

 * @retval None

 */

void Error_Handler(void)

{

 /* USER CODE BEGIN Error_Handler_Debug */

 /* User can add his own implementation to report the HAL error return state */

 /* USER CODE END Error_Handler_Debug */

}

#ifdef USE_FULL_ASSERT

/**

 * @brief Reports the name of the source file and the source line number

 *     where the assert_param error has occurred.

 * @param file: pointer to the source file name

 * @param line: assert_param error line source number

 * @retval None

 */

void assert_failed(uint8_t *file, uint32_t line)

 /* USER CODE BEGIN 6 */

 /* User can add his own implementation to report the file name and line number,

   tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

 /* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

----------------------------------------------

UPDATE: I noticed that only main.h from stm32l0xx_it.c was included in code. Maybe here is the problem? (picture below)

0693W000001r8MtQAI.png

2 REPLIES 2

Well, you appear to have a deugger connected so... debug.

Try to break in to the program, and then single-step. Isn't it hanging in the delay? Isn't running too fast so that the LED blinks faster than you can perceive?

JW

TDK
Guru

Where are all the USER CODE BEGIN and USER CODE END tags? Doesn't look like code generated by CubeMX.

In any case, it seems you're not initializing the LED1 pin anywhere. This would usually be done in MX_GPIO_Init().

If you feel a post has answered your question, please click "Accept as Solution".