L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
usart.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file usart.c
5 * @brief This file provides code for the configuration
6 * of the USART instances.
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2025 STMicroelectronics.
11 * All rights reserved.
12 *
13 * This software is licensed under terms that can be found in the LICENSE file
14 * in the root directory of this software component.
15 * If no LICENSE file comes with this software, it is provided AS-IS.
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20/* Includes ------------------------------------------------------------------*/
21#include "usart.h"
22
23/* USER CODE BEGIN 0 */
24#include <stdio.h>
25#include <string.h>
26
27
28/* USER CODE END 0 */
29
30UART_HandleTypeDef huart1;
31DMA_HandleTypeDef hdma_usart1_tx;
32
33/* USART1 init function */
34
36{
37
38 /* USER CODE BEGIN USART1_Init 0 */
39 //huart1.Instance = NULL;
40// return;
41 /* USER CODE END USART1_Init 0 */
42
43 /* USER CODE BEGIN USART1_Init 1 */
44
45 /* USER CODE END USART1_Init 1 */
46 huart1.Instance = USART1;
47 huart1.Init.BaudRate = 115200;
48 huart1.Init.WordLength = UART_WORDLENGTH_8B;
49 huart1.Init.StopBits = UART_STOPBITS_1;
50 huart1.Init.Parity = UART_PARITY_NONE;
51 huart1.Init.Mode = UART_MODE_TX_RX;
52 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
53 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
54 huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
55 huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
56 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
57 if (HAL_UART_Init(&huart1) != HAL_OK)
58 {
60 }
61 if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
62 {
64 }
65 if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
66 {
68 }
69 if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
70 {
72 }
73 /* USER CODE BEGIN USART1_Init 2 */
74
75 /* USER CODE END USART1_Init 2 */
76
77}
78
79void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
80{
81
82 GPIO_InitTypeDef GPIO_InitStruct = {0};
83 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
84 if(uartHandle->Instance==USART1)
85 {
86 /* USER CODE BEGIN USART1_MspInit 0 */
87
88 /* USER CODE END USART1_MspInit 0 */
89
90 /** Initializes the peripherals clocks
91 */
92 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1;
93 PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
94 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
95 {
97 }
98
99 /* USART1 clock enable */
100 __HAL_RCC_USART1_CLK_ENABLE();
101
102 __HAL_RCC_GPIOB_CLK_ENABLE();
103 /**USART1 GPIO Configuration
104 PB6 ------> USART1_TX
105 PB7 ------> USART1_RX
106 */
107 GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
108 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
109 GPIO_InitStruct.Pull = GPIO_NOPULL;
110 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
111 GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
112 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
113
114 /* USART1 DMA Init */
115 /* USART1_TX Init */
116 hdma_usart1_tx.Instance = DMA1_Channel1;
117 hdma_usart1_tx.Init.Request = DMA_REQUEST_USART1_TX;
118 hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
119 hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
120 hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
121 hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
122 hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
123 hdma_usart1_tx.Init.Mode = DMA_NORMAL;
124 hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW;
125 if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
126 {
128 }
129
130 __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx);
131
132 /* USART1 interrupt Init */
133 HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);
134 HAL_NVIC_EnableIRQ(USART1_IRQn);
135 /* USER CODE BEGIN USART1_MspInit 1 */
136
137 /* USER CODE END USART1_MspInit 1 */
138 }
139}
140
141void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
142{
143
144 if(uartHandle->Instance==USART1)
145 {
146 /* USER CODE BEGIN USART1_MspDeInit 0 */
147
148 /* USER CODE END USART1_MspDeInit 0 */
149 /* Peripheral clock disable */
150 __HAL_RCC_USART1_CLK_DISABLE();
151
152 /**USART1 GPIO Configuration
153 PB6 ------> USART1_TX
154 PB7 ------> USART1_RX
155 */
156 HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
157
158 /* USART1 DMA DeInit */
159 HAL_DMA_DeInit(uartHandle->hdmatx);
160
161 /* USART1 interrupt Deinit */
162 HAL_NVIC_DisableIRQ(USART1_IRQn);
163 /* USER CODE BEGIN USART1_MspDeInit 1 */
164
165 /* USER CODE END USART1_MspDeInit 1 */
166 }
167}
168
169/* USER CODE BEGIN 1 */
170
171
172/* USER CODE END 1 */
void Error_Handler(void)
Default error handler called by HAL on unrecoverable errors. Disables interrupts and enters an infini...
Definition main.c:505
DMA_HandleTypeDef hdma_usart1_tx
Definition usart.c:31
void MX_USART1_UART_Init(void)
Initialise USART1 (huart1) as configured by STM32CubeMX. Sets baud rate, word length,...
Definition usart.c:35
void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
Definition usart.c:79
void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
Definition usart.c:141
This file contains all the function prototypes for the usart.c file.
UART_HandleTypeDef huart1
Definition usart.c:30