Advertisement
Guest User

Untitled

a guest
Aug 27th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.55 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2024 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. #include "usart.h"
  22. #include "gpio.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <stdio.h>
  27. /* USER CODE END Includes */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31.  
  32. /* USER CODE END PTD */
  33.  
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36.  
  37. /* USER CODE END PD */
  38.  
  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */
  41.  
  42. /* USER CODE END PM */
  43.  
  44. /* Private variables ---------------------------------------------------------*/
  45.  
  46. /* USER CODE BEGIN PV */
  47.  
  48. /* USER CODE END PV */
  49.  
  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);
  52. static void MPU_Config(void);
  53. /* USER CODE BEGIN PFP */
  54. #ifdef __GNUC__
  55. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  56. #define GETCHAR_PROTOTYPE int __io_getchar(void)
  57. #else
  58. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  59. #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
  60. #endif
  61.  
  62. PUTCHAR_PROTOTYPE
  63. {
  64.   HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  65.   return ch;
  66. }
  67.  
  68. GETCHAR_PROTOTYPE
  69. {
  70.   uint8_t ch = 0;
  71.  
  72.   /* Clear the Overrun flag just before receiving the first character */
  73.   __HAL_UART_CLEAR_OREFLAG(&huart2);
  74.  
  75.   /* Wait for reception of a character on the USART RX line and echo this
  76.    * character on console */
  77.   HAL_UART_Receive(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  78.   // HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  79.   return ch;
  80. }
  81. /* USER CODE END PFP */
  82.  
  83. /* Private user code ---------------------------------------------------------*/
  84. /* USER CODE BEGIN 0 */
  85.  
  86. /* USER CODE END 0 */
  87.  
  88. /**
  89.   * @brief  The application entry point.
  90.   * @retval int
  91.   */
  92. int main(void)
  93. {
  94.  
  95.   /* USER CODE BEGIN 1 */
  96.  
  97.   /* USER CODE END 1 */
  98.  
  99.   /* MPU Configuration--------------------------------------------------------*/
  100.   MPU_Config();
  101.  
  102.   /* MCU Configuration--------------------------------------------------------*/
  103.  
  104.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  105.   HAL_Init();
  106.  
  107.   /* USER CODE BEGIN Init */
  108.  
  109.   /* USER CODE END Init */
  110.  
  111.   /* Configure the system clock */
  112.   SystemClock_Config();
  113.  
  114.   /* USER CODE BEGIN SysInit */
  115.  
  116.   /* USER CODE END SysInit */
  117.  
  118.   /* Initialize all configured peripherals */
  119.   MX_GPIO_Init();
  120.   MX_USART2_UART_Init();
  121.   /* USER CODE BEGIN 2 */
  122.   // setvbuf(stdin, NULL, _IONBF, 0);
  123.  
  124.   printf("Welcome to Lab USART_Test!\n");
  125.   /* USER CODE END 2 */
  126.  
  127.   /* Infinite loop */
  128.   /* USER CODE BEGIN WHILE */
  129.   while (1)
  130.   {
  131.     /* USER CODE END WHILE */
  132.  
  133.     /* USER CODE BEGIN 3 */
  134.   }
  135.   /* USER CODE END 3 */
  136. }
  137.  
  138. /**
  139.   * @brief System Clock Configuration
  140.   * @retval None
  141.   */
  142. void SystemClock_Config(void)
  143. {
  144.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  145.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  146.  
  147.   /** Supply configuration update enable
  148.   */
  149.   HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  150.  
  151.   /** Configure the main internal regulator output voltage
  152.   */
  153.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  154.  
  155.   while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  156.  
  157.   __HAL_RCC_SYSCFG_CLK_ENABLE();
  158.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
  159.  
  160.   while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  161.  
  162.   /** Initializes the RCC Oscillators according to the specified parameters
  163.   * in the RCC_OscInitTypeDef structure.
  164.   */
  165.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  166.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  167.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  168.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  169.   RCC_OscInitStruct.PLL.PLLM = 5;
  170.   RCC_OscInitStruct.PLL.PLLN = 192;
  171.   RCC_OscInitStruct.PLL.PLLP = 2;
  172.   RCC_OscInitStruct.PLL.PLLQ = 2;
  173.   RCC_OscInitStruct.PLL.PLLR = 2;
  174.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
  175.   RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  176.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  177.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  178.   {
  179.     Error_Handler();
  180.   }
  181.  
  182.   /** Initializes the CPU, AHB and APB buses clocks
  183.   */
  184.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  185.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  186.                               |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  187.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  188.   RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  189.   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  190.   RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  191.   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  192.   RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  193.   RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  194.  
  195.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  196.   {
  197.     Error_Handler();
  198.   }
  199. }
  200.  
  201. /* USER CODE BEGIN 4 */
  202.  
  203. /* USER CODE END 4 */
  204.  
  205.  /* MPU Configuration */
  206.  
  207. void MPU_Config(void)
  208. {
  209.   MPU_Region_InitTypeDef MPU_InitStruct = {0};
  210.  
  211.   /* Disables the MPU */
  212.   HAL_MPU_Disable();
  213.  
  214.   /** Initializes and configures the Region and the memory to be protected
  215.   */
  216.   MPU_InitStruct.Enable = MPU_REGION_ENABLE;
  217.   MPU_InitStruct.Number = MPU_REGION_NUMBER0;
  218.   MPU_InitStruct.BaseAddress = 0x0;
  219.   MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
  220.   MPU_InitStruct.SubRegionDisable = 0x87;
  221.   MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  222.   MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
  223.   MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
  224.   MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  225.   MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  226.   MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  227.  
  228.   HAL_MPU_ConfigRegion(&MPU_InitStruct);
  229.   /* Enables the MPU */
  230.   HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  231.  
  232. }
  233.  
  234. /**
  235.   * @brief  This function is executed in case of error occurrence.
  236.   * @retval None
  237.   */
  238. void Error_Handler(void)
  239. {
  240.   /* USER CODE BEGIN Error_Handler_Debug */
  241.   /* User can add his own implementation to report the HAL error return state */
  242.   __disable_irq();
  243.   while (1)
  244.   {
  245.   }
  246.   /* USER CODE END Error_Handler_Debug */
  247. }
  248.  
  249. #ifdef  USE_FULL_ASSERT
  250. /**
  251.   * @brief  Reports the name of the source file and the source line number
  252.   *         where the assert_param error has occurred.
  253.   * @param  file: pointer to the source file name
  254.   * @param  line: assert_param error line source number
  255.   * @retval None
  256.   */
  257. void assert_failed(uint8_t *file, uint32_t line)
  258. {
  259.   /* USER CODE BEGIN 6 */
  260.   /* User can add his own implementation to report the file name and line number,
  261.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  262.   /* USER CODE END 6 */
  263. }
  264. #endif /* USE_FULL_ASSERT */
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement