stm32f103rb_hal_blink

Non-blocking, event triggered blink example using HAL library
Index Commits Files Refs README
Core/Src/main.c (7310B)
   1 /* USER CODE BEGIN Header */
   2 /**
   3   ******************************************************************************
   4   * @file           : main.c
   5   * @brief          : Main program body
   6   ******************************************************************************
   7   * @attention
   8   *
   9   * Copyright (c) 2025 STMicroelectronics.
  10   * All rights reserved.
  11   *
  12   * This software is licensed under terms that can be found in the LICENSE file
  13   * in the root directory of this software component.
  14   * If no LICENSE file comes with this software, it is provided AS-IS.
  15   *
  16   ******************************************************************************
  17   */
  18 /* USER CODE END Header */
  19 /* Includes ------------------------------------------------------------------*/
  20 #include "main.h"
  21 
  22 /* Private includes ----------------------------------------------------------*/
  23 /* USER CODE BEGIN Includes */
  24 
  25 /* Application includes. */
  26 #include "logger.h"
  27 #include "app.h"
  28 
  29 /* USER CODE END Includes */
  30 
  31 /* Private typedef -----------------------------------------------------------*/
  32 /* USER CODE BEGIN PTD */
  33 
  34 /* USER CODE END PTD */
  35 
  36 /* Private define ------------------------------------------------------------*/
  37 /* USER CODE BEGIN PD */
  38 
  39 /* USER CODE END PD */
  40 
  41 /* Private macro -------------------------------------------------------------*/
  42 /* USER CODE BEGIN PM */
  43 
  44 /* USER CODE END PM */
  45 
  46 /* Private variables ---------------------------------------------------------*/
  47 UART_HandleTypeDef huart2;
  48 
  49 /* USER CODE BEGIN PV */
  50 
  51 /* USER CODE END PV */
  52 
  53 /* Private function prototypes -----------------------------------------------*/
  54 void SystemClock_Config(void);
  55 static void MX_GPIO_Init(void);
  56 static void MX_USART2_UART_Init(void);
  57 /* USER CODE BEGIN PFP */
  58 
  59 /* USER CODE END PFP */
  60 
  61 /* Private user code ---------------------------------------------------------*/
  62 /* USER CODE BEGIN 0 */
  63 #if (1 == LOGGER_CONFIG_USE_SEMIHOSTING)
  64 extern void initialise_monitor_handles(void);
  65 #endif
  66 
  67 /* USER CODE END 0 */
  68 
  69 /**
  70   * @brief  The application entry point.
  71   * @retval int
  72   */
  73 int main(void)
  74 {
  75     /* USER CODE BEGIN 1 */
  76 #if (1 == LOGGER_CONFIG_USE_SEMIHOSTING)
  77     initialise_monitor_handles();
  78 #endif
  79     /* USER CODE END 1 */
  80 
  81     /* MCU Configuration------------------------------------------------------*/
  82 
  83     /* Reset of all peripherals, Initializes Flash interface and Systick. */
  84     HAL_Init();
  85 
  86     /* USER CODE BEGIN Init */
  87 
  88     /* USER CODE END Init */
  89 
  90     /* Configure the system clock */
  91     SystemClock_Config();
  92 
  93     /* USER CODE BEGIN SysInit */
  94 
  95     /* USER CODE END SysInit */
  96 
  97     /* Initialize all configured peripherals */
  98     MX_GPIO_Init();
  99     MX_USART2_UART_Init();
 100     /* USER CODE BEGIN 2 */
 101 
 102     /* Application Init */
 103     app_init();
 104 
 105     /* USER CODE END 2 */
 106 
 107     /* Infinite loop */
 108     /* USER CODE BEGIN WHILE */
 109 
 110     LOGGER_INFO("Running main loop...");
 111     while (1)
 112     {
 113         /* USER CODE END WHILE */
 114 
 115         /* USER CODE BEGIN 3 */
 116 
 117         /* Application Update */
 118         app_update();
 119     }
 120     /* USER CODE END 3 */
 121 }
 122 
 123 /**
 124   * @brief System Clock Configuration
 125   * @retval None
 126   */
 127 void SystemClock_Config(void)
 128 {
 129     RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 130     RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 131 
 132     /** Initializes the RCC Oscillators according to the specified parameters
 133      * in the RCC_OscInitTypeDef structure.
 134      */
 135     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 136     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 137     RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 138     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 139     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
 140     RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
 141     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 142     {
 143         Error_Handler();
 144     }
 145 
 146     /** Initializes the CPU, AHB and APB buses clocks
 147     */
 148     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 149         |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
 150     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 151     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 152     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
 153     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 154 
 155     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
 156     {
 157         Error_Handler();
 158     }
 159 }
 160 
 161 /**
 162   * @brief USART2 Initialization Function
 163   * @param None
 164   * @retval None
 165   */
 166 static void MX_USART2_UART_Init(void)
 167 {
 168     /* USER CODE BEGIN USART2_Init 0 */
 169 
 170     /* USER CODE END USART2_Init 0 */
 171 
 172     /* USER CODE BEGIN USART2_Init 1 */
 173 
 174     /* USER CODE END USART2_Init 1 */
 175     huart2.Instance = USART2;
 176     huart2.Init.BaudRate = 115200;
 177     huart2.Init.WordLength = UART_WORDLENGTH_8B;
 178     huart2.Init.StopBits = UART_STOPBITS_1;
 179     huart2.Init.Parity = UART_PARITY_NONE;
 180     huart2.Init.Mode = UART_MODE_TX_RX;
 181     huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 182     huart2.Init.OverSampling = UART_OVERSAMPLING_16;
 183     if (HAL_UART_Init(&huart2) != HAL_OK)
 184     {
 185         Error_Handler();
 186     }
 187     /* USER CODE BEGIN USART2_Init 2 */
 188 
 189     /* USER CODE END USART2_Init 2 */
 190 }
 191 
 192 /**
 193   * @brief GPIO Initialization Function
 194   * @param None
 195   * @retval None
 196   */
 197 static void MX_GPIO_Init(void)
 198 {
 199     GPIO_InitTypeDef GPIO_InitStruct = {0};
 200     /* USER CODE BEGIN MX_GPIO_Init_1 */
 201     /* USER CODE END MX_GPIO_Init_1 */
 202 
 203     /* GPIO Ports Clock Enable */
 204     __HAL_RCC_GPIOC_CLK_ENABLE();
 205     __HAL_RCC_GPIOD_CLK_ENABLE();
 206     __HAL_RCC_GPIOA_CLK_ENABLE();
 207     __HAL_RCC_GPIOB_CLK_ENABLE();
 208 
 209     /*Configure GPIO pin Output Level */
 210     HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
 211 
 212     /*Configure GPIO pin : B1_Pin */
 213     GPIO_InitStruct.Pin = B1_Pin;
 214     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
 215     GPIO_InitStruct.Pull = GPIO_NOPULL;
 216     HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
 217 
 218     /*Configure GPIO pin : LD2_Pin */
 219     GPIO_InitStruct.Pin = LD2_Pin;
 220     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 221     GPIO_InitStruct.Pull = GPIO_NOPULL;
 222     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 223     HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
 224 
 225     /* EXTI interrupt init*/
 226     HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
 227     HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
 228 
 229     /* USER CODE BEGIN MX_GPIO_Init_2 */
 230     /* USER CODE END MX_GPIO_Init_2 */
 231 }
 232 
 233 /* USER CODE BEGIN 4 */
 234 
 235 /* USER CODE END 4 */
 236 
 237 /**
 238   * @brief  This function is executed in case of error occurrence.
 239   * @retval None
 240   */
 241 void Error_Handler(void)
 242 {
 243     /* USER CODE BEGIN Error_Handler_Debug */
 244     /* User can add his own implementation to report the HAL error return state */
 245     __disable_irq();
 246     while (1)
 247     {
 248         ;
 249     }
 250     /* USER CODE END Error_Handler_Debug */
 251 }
 252 
 253 #ifdef  USE_FULL_ASSERT
 254 /**
 255   * @brief  Reports the name of the source file and the source line number
 256   *         where the assert_param error has occurred.
 257   * @param  file: pointer to the source file name
 258   * @param  line: assert_param error line source number
 259   * @retval None
 260   */
 261 void assert_failed(uint8_t *file, uint32_t line)
 262 {
 263     /* USER CODE BEGIN 6 */
 264     /*
 265      * User can add his own implementation to report the file name and line
 266      * number, i.e. `printf("Error: file %s on line %d\r\n", file, line)
 267      */
 268     /* USER CODE END 6 */
 269 }
 270 #endif /* USE_FULL_ASSERT */