L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
usart_if.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file usart_if.c
5 * @author MCD Application Team
6 * @brief Configuration of UART driver interface for hyperterminal communication
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2026 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
21/* Includes ------------------------------------------------------------------*/
22#include "usart_if.h"
23
24/* USER CODE BEGIN Includes */
25#include "stm32_seq.h"
26/* USER CODE END Includes */
27
28/* External variables ---------------------------------------------------------*/
29/**
30 * @brief DMA handle
31 */
32extern DMA_HandleTypeDef hdma_usart1_tx;
33
34/**
35 * @brief UART handle
36 */
37extern UART_HandleTypeDef huart1;
38
39/**
40 * @brief buffer to receive 1 character
41 */
42uint8_t charRx;
43
44/* USER CODE BEGIN EV */
45
46char uart_req_buf[100];
48static UART_HandleTypeDef *_curUart = NULL; // aktualny uart
49
50/* USER CODE END EV */
51
52/* Private typedef -----------------------------------------------------------*/
53/**
54 * @brief Trace driver callbacks handler
55 */
56const UTIL_ADV_TRACE_Driver_s UTIL_TraceDriver =
57{
62};
63
64/* USER CODE BEGIN PTD */
65
66/* USER CODE END PTD */
67
68/* Private define ------------------------------------------------------------*/
69/* USER CODE BEGIN PD */
70
71/* USER CODE END PD */
72
73/* Private macro -------------------------------------------------------------*/
74/* USER CODE BEGIN PM */
75
76/* USER CODE END PM */
77
78/* Private variables ---------------------------------------------------------*/
79/**
80 * @brief TX complete callback
81 * @return none
82 */
83static void (*TxCpltCallback)(void *);
84/**
85 * @brief RX complete callback
86 * @param rxChar ptr of chars buffer sent by user
87 * @param size buffer size
88 * @param error errorcode
89 * @return none
90 */
91static void (*RxCpltCallback)(uint8_t *rxChar, uint16_t size, uint8_t error);
92
93/* USER CODE BEGIN PV */
94
95/* USER CODE END PV */
96
97/* Private function prototypes -----------------------------------------------*/
98
99/* USER CODE BEGIN PFP */
100
101/* USER CODE END PFP */
102
103/* Exported functions --------------------------------------------------------*/
104
105UTIL_ADV_TRACE_Status_t vcom_Init(void (*cb)(void *))
106{
107 /* USER CODE BEGIN vcom_Init_1 */
108
109 /* USER CODE END vcom_Init_1 */
110 TxCpltCallback = cb;
111 MX_DMA_Init();
113 LL_EXTI_EnableIT_0_31(LL_EXTI_LINE_26);
114 return UTIL_ADV_TRACE_OK;
115 /* USER CODE BEGIN vcom_Init_2 */
116
117 /* USER CODE END vcom_Init_2 */
118}
119
120UTIL_ADV_TRACE_Status_t vcom_DeInit(void)
121{
122 /* USER CODE BEGIN vcom_DeInit_1 */
123 if (huart1.Instance == NULL)
124 return UTIL_ADV_TRACE_HW_ERROR; // MT 4.2.2026 - suppress UART
125
126 /* USER CODE END vcom_DeInit_1 */
127 /* ##-1- Reset peripherals ################################################## */
128 __HAL_RCC_USART1_FORCE_RESET();
129 __HAL_RCC_USART1_RELEASE_RESET();
130
131 /* ##-2- MspDeInit ################################################## */
133
134 /* ##-3- Disable the NVIC for DMA ########################################### */
135 /* USER CODE BEGIN 1 */
136 HAL_NVIC_DisableIRQ(DMA1_Channel5_IRQn);
137
138 return UTIL_ADV_TRACE_OK;
139 /* USER CODE END 1 */
140 /* USER CODE BEGIN vcom_DeInit_2 */
141
142 /* USER CODE END vcom_DeInit_2 */
143}
144
145void vcom_Trace(uint8_t *p_data, uint16_t size)
146{
147 /* USER CODE BEGIN vcom_Trace_1 */
148 if (huart1.Instance == NULL)
149 return;
150
151 /* USER CODE END vcom_Trace_1 */
152 HAL_UART_Transmit(&huart1, p_data, size, 1000);
153 /* USER CODE BEGIN vcom_Trace_2 */
154
155 /* USER CODE END vcom_Trace_2 */
156}
157
158UTIL_ADV_TRACE_Status_t vcom_Trace_DMA(uint8_t *p_data, uint16_t size)
159{
160 /* USER CODE BEGIN vcom_Trace_DMA_1 */
161 if (huart1.Instance == NULL)
162 return UTIL_ADV_TRACE_HW_ERROR;
163
164 HAL_UART_Transmit(&huart1, p_data, size, 10000); // MT 13.1.2026 - no DMA
165 HAL_UART_TxCpltCallback(&huart1); // must be called
166 return UTIL_ADV_TRACE_OK;
167
168
169 /* USER CODE END vcom_Trace_DMA_1 */
170 HAL_UART_Transmit_DMA(&huart1, p_data, size);
171 return UTIL_ADV_TRACE_OK;
172 /* USER CODE BEGIN vcom_Trace_DMA_2 */
173
174 /* USER CODE END vcom_Trace_DMA_2 */
175}
176
177UTIL_ADV_TRACE_Status_t vcom_ReceiveInit(void (*RxCb)(uint8_t *rxChar, uint16_t size, uint8_t error))
178{
179 /* USER CODE BEGIN vcom_ReceiveInit_1 */
180
181 if (huart1.Instance == NULL)
182 return UTIL_ADV_TRACE_HW_ERROR;
183
184
185 /* USER CODE END vcom_ReceiveInit_1 */
186 UART_WakeUpTypeDef WakeUpSelection;
187
188 /*record call back*/
189 RxCpltCallback = RxCb;
190
191 /*Set wakeUp event on start bit*/
192 WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_STARTBIT;
193
194 HAL_UARTEx_StopModeWakeUpSourceConfig(&huart1, WakeUpSelection);
195
196 /* Make sure that no UART transfer is on-going */
197 while (__HAL_UART_GET_FLAG(&huart1, USART_ISR_BUSY) == SET);
198
199 /* Make sure that UART is ready to receive) */
200 while (__HAL_UART_GET_FLAG(&huart1, USART_ISR_REACK) == RESET);
201
202 /* Enable USART interrupt */
203 __HAL_UART_ENABLE_IT(&huart1, UART_IT_WUF);
204
205 /*Enable wakeup from stop mode*/
206 HAL_UARTEx_EnableStopMode(&huart1);
207
208 /*Start LPUART receive on IT*/
209 HAL_UART_Receive_IT(&huart1, &charRx, 1);
210
211 return UTIL_ADV_TRACE_OK;
212 /* USER CODE BEGIN vcom_ReceiveInit_2 */
213
214 /* USER CODE END vcom_ReceiveInit_2 */
215}
216
217void vcom_Resume(void)
218{
219 /* USER CODE BEGIN vcom_Resume_1 */
220 if (huart1.Instance == NULL)
221 return;
222
223 /* USER CODE END vcom_Resume_1 */
224 /*to re-enable lost UART settings*/
225 if (HAL_UART_Init(&huart1) != HAL_OK)
226 {
228 }
229
230 /*to re-enable lost DMA settings*/
231 if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
232 {
234 }
235 /* USER CODE BEGIN vcom_Resume_2 */
236
237 /* USER CODE END vcom_Resume_2 */
238}
239
240void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
241{
242 /* USER CODE BEGIN HAL_UART_TxCpltCallback_1 */
243
244 /* USER CODE END HAL_UART_TxCpltCallback_1 */
245 /* buffer transmission complete*/
246 if (huart->Instance == USART1)
247 {
248 TxCpltCallback(NULL);
249 }
250 /* USER CODE BEGIN HAL_UART_TxCpltCallback_2 */
251
252 /* USER CODE END HAL_UART_TxCpltCallback_2 */
253}
254
255void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
256{
257 /* USER CODE BEGIN HAL_UART_RxCpltCallback_1 */
258
259 /* USER CODE END HAL_UART_RxCpltCallback_1 */
260 if (huart->Instance == USART1)
261 {
262 if ((NULL != RxCpltCallback) && (HAL_UART_ERROR_NONE == huart->ErrorCode))
263 {
264 RxCpltCallback(&charRx, 1, 0);
265 }
266 HAL_UART_Receive_IT(huart, &charRx, 1);
267 }
268 /* USER CODE BEGIN HAL_UART_RxCpltCallback_2 */
270 /* USER CODE END HAL_UART_RxCpltCallback_2 */
271}
272
273/* USER CODE BEGIN EF */
274
275/**
276 * @brief called if char is ready from UART
277 * @retval -1 - other UART, 0 - char is accepted, next receiving must start, 1 - data ready
278 */
279int HAL_UART_RxCharMT(UART_HandleTypeDef *huart, uint8_t charRx)
280{
281 int ret = -1;
282 if (_curUart != NULL && huart->Instance == _curUart->Instance) //
283 {
285 if (*uart_req_w_ptr == '\n' || *uart_req_w_ptr == '\r' || uart_req_w_ptr == uart_req_buf + sizeof(uart_req_buf) - 2) //
286 {
287 ret = 1;
288 UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_Uart_RX), CFG_SEQ_Prio_0); // start of tasksensors_Work
289 }
290 else
291 {
293 ret = 0;
294 }
295 }
296 return ret;
297}
298
299UTIL_ADV_TRACE_Status_t Uart_Info(const char *strInfo)
300{
301 UTIL_ADV_TRACE_Status_t status = UTIL_ADV_TRACE_OK;//vcom_Trace_DMA((uint8_t*) strInfo, strlen(strInfo));
302 vcom_Trace((uint8_t*) strInfo, strlen(strInfo));
303 return status;
304}
305
306UTIL_ADV_TRACE_Status_t Uart_NextReceving()
307{
309 memset(uart_req_buf, 0, sizeof(uart_req_buf));
310 return UTIL_ADV_TRACE_OK;
311}
312
313UTIL_ADV_TRACE_Status_t Uart_StartReceving(UART_HandleTypeDef *uart) //
314{
315 _curUart = uart;
317 return vcom_ReceiveInit(NULL);
318}
319
320/* USER CODE END EF */
321
322/* Private Functions Definition -----------------------------------------------*/
323
324/* USER CODE BEGIN PrFD */
325
326/* USER CODE END PrFD */
void MX_DMA_Init(void)
Initialise DMA1 channels used by USART1 (TX via DMA) and other peripherals. Must be called before MX_...
Definition dma.c:39
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 HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
Definition usart.c:141
UART_HandleTypeDef huart1
Definition usart.c:30
void MX_USART1_UART_Init(void)
Initialise USART1 (huart1) as configured by STM32CubeMX. Sets baud rate, word length,...
Definition usart.c:35
const UTIL_ADV_TRACE_Driver_s UTIL_TraceDriver
Trace driver callbacks handler.
Definition usart_if.c:56
UTIL_ADV_TRACE_Status_t vcom_Trace_DMA(uint8_t *p_data, uint16_t size)
send buffer p_data of size size to vcom using DMA
Definition usart_if.c:158
void vcom_Resume(void)
Resume the UART and associated DMA (used by LPM).
Definition usart_if.c:217
static void(* RxCpltCallback)(uint8_t *rxChar, uint16_t size, uint8_t error)
RX complete callback.
Definition usart_if.c:91
UTIL_ADV_TRACE_Status_t Uart_Info(const char *strInfo)
Write to UART1.
Definition usart_if.c:299
int HAL_UART_RxCharMT(UART_HandleTypeDef *huart, uint8_t charRx)
called if char is ready from UART
Definition usart_if.c:279
void vcom_Trace(uint8_t *p_data, uint16_t size)
send buffer p_data of size size to vcom in polling mode
Definition usart_if.c:145
static void(* TxCpltCallback)(void *)
TX complete callback.
Definition usart_if.c:83
char * uart_req_w_ptr
Definition usart_if.c:47
UTIL_ADV_TRACE_Status_t Uart_NextReceving()
continue with next reading if data arrived
Definition usart_if.c:306
UTIL_ADV_TRACE_Status_t vcom_ReceiveInit(void(*RxCb)(uint8_t *rxChar, uint16_t size, uint8_t error))
init receiver of vcom
Definition usart_if.c:177
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
Definition usart_if.c:240
UTIL_ADV_TRACE_Status_t Uart_StartReceving(UART_HandleTypeDef *uart)
start reading
Definition usart_if.c:313
UTIL_ADV_TRACE_Status_t vcom_DeInit(void)
DeInit the UART and associated DMA.
Definition usart_if.c:120
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
Definition usart_if.c:255
static UART_HandleTypeDef * _curUart
Definition usart_if.c:48
UTIL_ADV_TRACE_Status_t vcom_Init(void(*cb)(void *))
Init the UART and associated DMA.
Definition usart_if.c:105
uint8_t charRx
buffer to receive 1 character
Definition usart_if.c:42
Header for USART interface configuration.
UTIL_ADV_TRACE_Status_t vcom_Trace_DMA(uint8_t *p_data, uint16_t size)
send buffer p_data of size size to vcom using DMA
Definition usart_if.c:158
UTIL_ADV_TRACE_Status_t vcom_ReceiveInit(void(*RxCb)(uint8_t *rxChar, uint16_t size, uint8_t error))
init receiver of vcom
Definition usart_if.c:177
UTIL_ADV_TRACE_Status_t vcom_DeInit(void)
DeInit the UART and associated DMA.
Definition usart_if.c:120
UTIL_ADV_TRACE_Status_t vcom_Init(void(*cb)(void *))
Init the UART and associated DMA.
Definition usart_if.c:105
char uart_req_buf[]
Definition usart_if.c:46
@ CFG_SEQ_Prio_0
@ CFG_SEQ_Task_Uart_RX