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