source: S-port/trunk/Core/Src/usart.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 3.3 KB
Line 
1/**
2 ******************************************************************************
3 * @file usart.c
4 * @brief This file provides code for the configuration
5 * of the USART instances.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
10 * All rights reserved.</center></h2>
11 *
12 * This software component is licensed by ST under Ultimate Liberty license
13 * SLA0044, the "License"; You may not use this file except in compliance with
14 * the License. You may obtain a copy of the License at:
15 * www.st.com/SLA0044
16 *
17 ******************************************************************************
18 */
19
20/* Includes ------------------------------------------------------------------*/
21#include "usart.h"
22
23/* USER CODE BEGIN 0 */
24
25/* USER CODE END 0 */
26
27UART_HandleTypeDef huart2;
28
29/* USART2 init function */
30
31void MX_USART2_UART_Init(void)
32{
33
34 /* USER CODE BEGIN USART2_Init 0 */
35
36 /* USER CODE END USART2_Init 0 */
37
38 /* USER CODE BEGIN USART2_Init 1 */
39
40 /* USER CODE END USART2_Init 1 */
41 huart2.Instance = USART2;
42 huart2.Init.BaudRate = 115200;
43 huart2.Init.WordLength = UART_WORDLENGTH_8B;
44 huart2.Init.StopBits = UART_STOPBITS_1;
45 huart2.Init.Parity = UART_PARITY_NONE;
46 huart2.Init.Mode = UART_MODE_TX_RX;
47 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
48 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
49 if (HAL_UART_Init(&huart2) != HAL_OK)
50 {
51 Error_Handler();
52 }
53 /* USER CODE BEGIN USART2_Init 2 */
54
55 /* USER CODE END USART2_Init 2 */
56
57}
58
59void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
60{
61
62 GPIO_InitTypeDef GPIO_InitStruct = {0};
63 if(uartHandle->Instance==USART2)
64 {
65 /* USER CODE BEGIN USART2_MspInit 0 */
66
67 /* USER CODE END USART2_MspInit 0 */
68 /* USART2 clock enable */
69 __HAL_RCC_USART2_CLK_ENABLE();
70
71 __HAL_RCC_GPIOD_CLK_ENABLE();
72 /**USART2 GPIO Configuration
73 PD5 ------> USART2_TX
74 PD6 ------> USART2_RX
75 */
76 GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
77 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
78 GPIO_InitStruct.Pull = GPIO_NOPULL;
79 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
80 GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
81 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
82
83 /* USER CODE BEGIN USART2_MspInit 1 */
84 huart2.Instance->CR1 |= USART_CR1_RXNEIE; // RXNEIE
85 NVIC_EnableIRQ(USART2_IRQn);
86 NVIC_SetPriority(USART2_IRQn, 5);
87 /* USER CODE END USART2_MspInit 1 */
88 }
89}
90
91void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
92{
93
94 if(uartHandle->Instance==USART2)
95 {
96 /* USER CODE BEGIN USART2_MspDeInit 0 */
97
98 /* USER CODE END USART2_MspDeInit 0 */
99 /* Peripheral clock disable */
100 __HAL_RCC_USART2_CLK_DISABLE();
101
102 /**USART2 GPIO Configuration
103 PD5 ------> USART2_TX
104 PD6 ------> USART2_RX
105 */
106 HAL_GPIO_DeInit(GPIOD, GPIO_PIN_5|GPIO_PIN_6);
107
108 /* USER CODE BEGIN USART2_MspDeInit 1 */
109 huart2.Instance->CR1 &= USART_CR1_RXNEIE; // RXNEIE
110 NVIC_DisableIRQ(USART2_IRQn);
111 /* USER CODE END USART2_MspDeInit 1 */
112 }
113}
114
115/* USER CODE BEGIN 1 */
116
117/* USER CODE END 1 */
118
119/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.