miyago

Uart_test

Nov 8th, 2019
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.38 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.   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26.  
  27. #include <string.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 huart5;
  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_UART5_Init(void);
  57. /* USER CODE BEGIN PFP */
  58.  
  59. /* USER CODE END PFP */
  60.  
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63.  
  64. /* USER CODE END 0 */
  65.  
  66. /**
  67.   * @brief  The application entry point.
  68.   * @retval int
  69.   */
  70. int main(void)
  71. {
  72.   /* USER CODE BEGIN 1 */
  73. uint8_t Tecken;
  74. char *hello = "Hello world!\n\r";
  75.   /* USER CODE END 1 */
  76.  
  77.  
  78.   /* MCU Configuration--------------------------------------------------------*/
  79.  
  80.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  81.   HAL_Init();
  82.  
  83.   /* USER CODE BEGIN Init */
  84.  
  85.   /* USER CODE END Init */
  86.  
  87.   /* Configure the system clock */
  88.   SystemClock_Config();
  89.  
  90.   /* USER CODE BEGIN SysInit */
  91.  
  92.   /* USER CODE END SysInit */
  93.  
  94.   /* Initialize all configured peripherals */
  95.   MX_GPIO_Init();
  96.   MX_UART5_Init();
  97.   /* USER CODE BEGIN 2 */
  98. HAL_UART_Transmit(&huart5, (uint8_t*)hello, strlen(hello), 5000);
  99.   /* USER CODE END 2 */
  100.  
  101.   /* Infinite loop */
  102.   /* USER CODE BEGIN WHILE */
  103.   while (1)
  104.   {
  105.  
  106.       HAL_UART_Receive(&huart5, &Tecken, 1, 5000);
  107.       HAL_UART_Transmit(&huart5, &Tecken, 1, 5000);
  108.  
  109.     /* USER CODE END WHILE */
  110.  
  111.     /* USER CODE BEGIN 3 */
  112.   }
  113.   /* USER CODE END 3 */
  114. }
  115.  
  116. /**
  117.   * @brief System Clock Configuration
  118.   * @retval None
  119.   */
  120. void SystemClock_Config(void)
  121. {
  122.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  123.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  124.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  125.  
  126.   /** Initializes the CPU, AHB and APB busses clocks
  127.   */
  128.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  129.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  130.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  131.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  132.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  133.   RCC_OscInitStruct.PLL.PLLM = 1;
  134.   RCC_OscInitStruct.PLL.PLLN = 10;
  135.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  136.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  137.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  138.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  139.   {
  140.     Error_Handler();
  141.   }
  142.   /** Initializes the CPU, AHB and APB busses clocks
  143.   */
  144.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  145.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  146.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  147.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  148.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  149.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  150.  
  151.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  152.   {
  153.     Error_Handler();
  154.   }
  155.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_UART5;
  156.   PeriphClkInit.Uart5ClockSelection = RCC_UART5CLKSOURCE_PCLK1;
  157.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  158.   {
  159.     Error_Handler();
  160.   }
  161.   /** Configure the main internal regulator output voltage
  162.   */
  163.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  164.   {
  165.     Error_Handler();
  166.   }
  167. }
  168.  
  169. /**
  170.   * @brief UART5 Initialization Function
  171.   * @param None
  172.   * @retval None
  173.   */
  174. static void MX_UART5_Init(void)
  175. {
  176.  
  177.   /* USER CODE BEGIN UART5_Init 0 */
  178.  
  179.   /* USER CODE END UART5_Init 0 */
  180.  
  181.   /* USER CODE BEGIN UART5_Init 1 */
  182.  
  183.   /* USER CODE END UART5_Init 1 */
  184.   huart5.Instance = UART5;
  185.   huart5.Init.BaudRate = 115200;
  186.   huart5.Init.WordLength = UART_WORDLENGTH_8B;
  187.   huart5.Init.StopBits = UART_STOPBITS_1;
  188.   huart5.Init.Parity = UART_PARITY_NONE;
  189.   huart5.Init.Mode = UART_MODE_TX_RX;
  190.   huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  191.   huart5.Init.OverSampling = UART_OVERSAMPLING_16;
  192.   huart5.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  193.   huart5.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  194.   if (HAL_UART_Init(&huart5) != HAL_OK)
  195.   {
  196.     Error_Handler();
  197.   }
  198.   /* USER CODE BEGIN UART5_Init 2 */
  199.  
  200.   /* USER CODE END UART5_Init 2 */
  201.  
  202. }
  203.  
  204. /**
  205.   * @brief GPIO Initialization Function
  206.   * @param None
  207.   * @retval None
  208.   */
  209. static void MX_GPIO_Init(void)
  210. {
  211.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  212.  
  213.   /* GPIO Ports Clock Enable */
  214.   __HAL_RCC_GPIOA_CLK_ENABLE();
  215.   __HAL_RCC_GPIOC_CLK_ENABLE();
  216.   __HAL_RCC_GPIOD_CLK_ENABLE();
  217.  
  218.   /*Configure GPIO pin Output Level */
  219.   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  220.  
  221.   /*Configure GPIO pin : LD2_Pin */
  222.   GPIO_InitStruct.Pin = LD2_Pin;
  223.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  224.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  225.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  226.   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  227.  
  228. }
  229.  
  230. /* USER CODE BEGIN 4 */
  231.  
  232. /* USER CODE END 4 */
  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.     HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
  243.   /* USER CODE END Error_Handler_Debug */
  244. }
  245.  
  246. #ifdef  USE_FULL_ASSERT
  247. /**
  248.   * @brief  Reports the name of the source file and the source line number
  249.   *         where the assert_param error has occurred.
  250.   * @param  file: pointer to the source file name
  251.   * @param  line: assert_param error line source number
  252.   * @retval None
  253.   */
  254. void assert_failed(char *file, uint32_t line)
  255. {
  256.   /* USER CODE BEGIN 6 */
  257.   /* User can add his own implementation to report the file name and line number,
  258.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  259.   /* USER CODE END 6 */
  260. }
  261. #endif /* USE_FULL_ASSERT */
  262.  
  263. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment