1 /**
2 ******************************************************************************
3 * @file HAL_UART.c
4 * @author AE Team
5 * @version V1.1.0
6 * @date 28/08/2019
7 * @brief This file provides all the UART firmware functions.
8 ******************************************************************************
9 * @copy
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>© COPYRIGHT 2019 MindMotion</center></h2>
19 */
20
21 /* Includes ------------------------------------------------------------------*/
22 #include "HAL_uart.h"
23 #include "HAL_rcc.h"
24
25 /** @addtogroup StdPeriph_Driver
26 * @{
27 */
28
29 /** @defgroup UART
30 * @brief UART driver modules
31 * @{
32 */
33
34 /** @defgroup UART_Private_TypesDefinitions
35 * @{
36 */
37
38 /**
39 * @}
40 */
41
42 /** @defgroup UART_Private_Defines
43 * @{
44 */
45
46
47
48 /* UART UE Mask */
49 #define GCR_UE_Set ((uint16_t)0x0001) /* UART Enable Mask */
50 #define GCR_UE_Reset ((uint16_t)0xFFFE) /* UART Disable Mask */
51
52 //#define CCR_CLEAR_Mask ((uint32_t)0xFFFFFF30) /* UART CCR Mask */
53 #define CCR_CLEAR_Mask ((uint32_t)0xFFFFFFC9) /* UART CCR Mask */
54 #define GCR_CLEAR_Mask ((uint32_t)0xFFFFFFE0) /* UART GCR Mask */
55 /**
56 * @}
57 */
58
59 /** @defgroup UART_Private_Macros
60 * @{
61 */
62
63 /**
64 * @}
65 */
66
67 /** @defgroup UART_Private_Variables
68 * @{
69 */
70
71 /**
72 * @}
73 */
74
75 /** @defgroup UART_Private_FunctionPrototypes
76 * @{
77 */
78
79 /**
80 * @}
81 */
82
83 /** @defgroup UART_Private_Functions
84 * @{
85 */
86
87 /**
88 * @brief Deinitializes the UARTx peripheral registers to their
89 * default reset values.
90 * @param UARTx: Select the UART or the UART peripheral.
91 * This parameter can be one of the following values:
92 * UART1, UART2, UART3.
93 * @retval : None
94 */
UART_DeInit(UART_TypeDef * UARTx)95 void UART_DeInit(UART_TypeDef* UARTx)
96 {
97 /* Check the parameters */
98 assert_param(IS_UART_ALL_PERIPH(UARTx));
99 switch (*(uint32_t*)&UARTx)
100 {
101 case UART1_BASE:
102 RCC_APB2PeriphResetCmd(RCC_APB2Periph_UART1, ENABLE);
103 RCC_APB2PeriphResetCmd(RCC_APB2Periph_UART1, DISABLE);
104 break;
105 case UART2_BASE:
106 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART2, ENABLE);
107 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART2, DISABLE);
108 break;
109 case UART3_BASE:
110 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART3, ENABLE);
111 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART3, DISABLE);
112 break;
113 default:
114 break;
115 }
116 }
117
118 /**
119 * @brief Initializes the UARTx peripheral according to the specified
120 * parameters in the UART_InitStruct .
121 * @param UARTx: Select the UART or the UART peripheral.
122 * This parameter can be one of the following values:
123 * UART1, UART2, UART3.
124 * @param UART_InitStruct: pointer to a UART_InitTypeDef structure
125 * that contains the configuration information for the
126 * specified UART peripheral.
127 * @retval : None
128 */
UART_Init(UART_TypeDef * UARTx,UART_InitTypeDef * UART_InitStruct)129 void UART_Init(UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct)
130 {
131 uint32_t tmpreg = 0x00, apbclock = 0x00;
132 uint32_t tmpreg1 = 0x00;
133 uint32_t UARTxbase = 0;
134 RCC_ClocksTypeDef RCC_ClocksStatus;
135 /* Check the parameters */
136 assert_param(IS_UART_ALL_PERIPH(UARTx));
137 assert_param(IS_UART_BAUDRATE(UART_InitStruct->UART_BaudRate));
138 assert_param(IS_UART_WORD_LENGTH(UART_InitStruct->UART_WordLength));
139 assert_param(IS_UART_STOPBITS(UART_InitStruct->UART_StopBits));
140 assert_param(IS_UART_PARITY(UART_InitStruct->UART_Parity));
141 assert_param(IS_UART_MODE(UART_InitStruct->UART_Mode));
142 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(UART_InitStruct->UART_HardwareFlowControl));
143
144 UARTxbase = (*(uint32_t*)&UARTx);
145 /*---------------------------- UART CCR Configuration -----------------------*/
146 /* get UART CCR values */
147 tmpreg = UARTx->CCR;
148 /* Clear spb,psel,pen bits */
149 tmpreg &= CCR_CLEAR_Mask;
150 /* Configure the UART Word Length,the UART Stop Bits,Parity ------------*/
151 /* Set the char bits according to UART_WordLength value */
152 /* Set spb bit according to UART_StopBits value */
153 /* Set PEN bit according to UART_Parity value */
154 tmpreg |= (uint32_t)UART_InitStruct->UART_WordLength | (uint32_t)UART_InitStruct->UART_StopBits | UART_InitStruct->UART_Parity;
155
156 /* Write to UART CCR */
157 UARTx->CCR = tmpreg;
158
159 /*---------------------------- UART GCR Configuration -----------------------*/
160 /* get UART GCR values */
161 tmpreg = UARTx->GCR;
162 /* Clear TXEN and RXEN ,autoflowen, mode ,uarten bits */
163 tmpreg &= GCR_CLEAR_Mask;
164 /* Set autorlowen bit according to UART_HardwareFlowControl value */
165 /* Set rxen,txen bits according to UART_Mode value */
166 tmpreg |= UART_InitStruct->UART_HardwareFlowControl | UART_InitStruct->UART_Mode ;
167 /* Write to UART GCR */
168 UARTx->GCR = tmpreg;
169 /*---------------------------- UART BRR Configuration -----------------------*/
170 /* Configure the UART Baud Rate -------------------------------------------*/
171 RCC_GetClocksFreq(&RCC_ClocksStatus);
172 if (UARTxbase == UART1_BASE)
173 {
174
175 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
176
177 }
178 else
179 {
180
181 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
182 }
183 /* Determine the UART_baud*/
184
185 tmpreg = (apbclock / UART_InitStruct->UART_BaudRate) / 16;
186 tmpreg1 = (apbclock / UART_InitStruct->UART_BaudRate) % 16;
187 UARTx->BRR = tmpreg;
188 UARTx->FRA = tmpreg1;
189 }
190
191 /**
192 * @brief Fills each UART_InitStruct member with its default value.
193 * @param UART_InitStruct: pointer to a UART_InitTypeDef structure
194 * which will be initialized.
195 * @retval : None
196 */
UART_StructInit(UART_InitTypeDef * UART_InitStruct)197 void UART_StructInit(UART_InitTypeDef* UART_InitStruct)
198 {
199 /* UART_InitStruct members default value */
200 UART_InitStruct->UART_BaudRate = 9600;
201 UART_InitStruct->UART_WordLength = UART_WordLength_8b;
202 UART_InitStruct->UART_StopBits = UART_StopBits_1;
203 UART_InitStruct->UART_Parity = UART_Parity_No ;
204 UART_InitStruct->UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
205 UART_InitStruct->UART_HardwareFlowControl = UART_HardwareFlowControl_None;
206 }
207
208
209 /**
210 * @brief Enables or disables the specified UART peripheral.
211 * @param UARTx: Select the UART or the UART peripheral.
212 * This parameter can be one of the following values:
213 * UART1, UART2, UART3.
214 * @param NewState: new state of the UARTx peripheral.
215 * This parameter can be: ENABLE or DISABLE.
216 * @retval : None
217 */
UART_Cmd(UART_TypeDef * UARTx,FunctionalState NewState)218 void UART_Cmd(UART_TypeDef* UARTx, FunctionalState NewState)
219 {
220 /* Check the parameters */
221 assert_param(IS_UART_ALL_PERIPH(UARTx));
222 assert_param(IS_FUNCTIONAL_STATE(NewState));
223
224 if (NewState != DISABLE)
225 {
226 /* Enable the selected UART by setting the uarten bit in the GCR register */
227 UARTx->GCR |= GCR_UE_Set;
228 }
229 else
230 {
231 /* Disable the selected UART by clearing the uarten bit in the GCR register */
232 UARTx->GCR &= GCR_UE_Reset;
233 }
234 }
235
236 /**
237 * @brief Enables or disables the specified UART interrupts.
238 * @param UARTx: Select the UART or the UART peripheral.
239 * This parameter can be one of the following values:
240 * UART1, UART2, UART3.
241 * @param UART_IT: specifies the UART interrupt sources to be
242 * enabled or disabled.
243 * This parameter can be one of the following values:
244 *
245 * @arg UART_IT_ERR: Error interrupt(Frame error,)
246 * @arg UART_IT_PE: Parity Error interrupt
247 * @arg UART_OVER_ERR: overrun Error interrupt
248 * @arg UART_TIMEOUT_ERR: timeout Error interrupt
249 * @arg UART_IT_RXIEN: Receive Data register interrupt
250 * @arg UART_IT_TXIEN: Tansmit Data Register empty interrupt
251 * @param NewState: new state of the specified UARTx interrupts.
252 * This parameter can be: ENABLE or DISABLE.
253 * @retval : None
254 */
UART_ITConfig(UART_TypeDef * UARTx,uint16_t UART_IT,FunctionalState NewState)255 void UART_ITConfig(UART_TypeDef* UARTx, uint16_t UART_IT, FunctionalState NewState)
256 {
257 /* Check the parameters */
258 assert_param(IS_UART_ALL_PERIPH(UARTx));
259 assert_param(IS_UART_CONFIG_IT(UART_IT));
260 assert_param(IS_FUNCTIONAL_STATE(NewState));
261
262 if (NewState != DISABLE)
263 {
264 /* Enable the UART_IT interrupt */
265 UARTx->IER |= UART_IT;
266 }
267 else
268 {
269 /* Disable the UART_IT interrupt */
270 UARTx->IER &= ~ UART_IT;
271 }
272 }
273
274 /**
275 * @brief Enables or disables the UART�s DMA interface.
276 * @param UARTx: Select the UART or the UART peripheral.
277 * This parameter can be one of the following values:
278 * UART1, UART2, UART3 .
279 * @param UART_DMAReq: specifies the DMA request.
280 * This parameter can be any combination of the following values:
281 * @arg UART_DMAReq_EN: UART DMA transmit request
282 *
283 * @param NewState: new state of the DMA Request sources.
284 * This parameter can be: ENABLE or DISABLE.
285 * @note The DMA mode is not available for UART5.
286 * @retval : None
287 */
UART_DMACmd(UART_TypeDef * UARTx,uint16_t UART_DMAReq,FunctionalState NewState)288 void UART_DMACmd(UART_TypeDef* UARTx, uint16_t UART_DMAReq, FunctionalState NewState)
289 {
290 /* Check the parameters */
291 assert_param(IS_UART_1234_PERIPH(UARTx));
292 assert_param(IS_UART_DMAREQ(UART_DMAReq));
293 assert_param(IS_FUNCTIONAL_STATE(NewState));
294 if (NewState != DISABLE)
295 {
296 /* Enable the DMA transfer */
297 UARTx->GCR |= UART_DMAReq;
298 }
299 else
300 {
301 /* Disable the DMA transfer */
302 UARTx->GCR &= ~UART_DMAReq;
303 }
304 }
305
306
307 /**
308 * @brief Transmits single data through the UARTx peripheral.
309 * @param UARTx: Select the UART or the UART peripheral.
310 * This parameter can be one of the following values:
311 * UART1, UART2, UART3.
312 * @param Data: the data to transmit.
313 * @retval : None
314 */
UART_SendData(UART_TypeDef * UARTx,uint16_t Data)315 void UART_SendData(UART_TypeDef* UARTx, uint16_t Data)
316 {
317 /* Check the parameters */
318 assert_param(IS_UART_ALL_PERIPH(UARTx));
319 assert_param(IS_UART_DATA(Data));
320
321 /* Transmit Data */
322 UARTx->TDR = (Data & (uint16_t)0x00FF);
323 }
324
325 /**
326 * @brief Returns the most recent received data by the UARTx peripheral.
327 * @param UARTx: Select the UART or the UART peripheral.
328 * This parameter can be one of the following values:
329 * UART1, UART2, UART3.
330 * @retval : The received data.
331 */
UART_ReceiveData(UART_TypeDef * UARTx)332 uint16_t UART_ReceiveData(UART_TypeDef* UARTx)
333 {
334 /* Check the parameters */
335 assert_param(IS_UART_ALL_PERIPH(UARTx));
336
337 /* Receive Data */
338 return (uint16_t)(UARTx->RDR & (uint16_t)0x00FF);
339 }
340
341
342 /**
343 * @brief Checks whether the specified UART flag is set or not.
344 * @param UARTx: Select the UART or the UART peripheral.
345 * This parameter can be one of the following values:
346 * UART1, UART2, UART3.
347 * @param UART_FLAG: specifies the flag to check.
348 * This parameter can be one of the following values:
349 * @arg UART_FLAG_TXEMPTY:Transmit data register empty flag
350 * @arg UART_FLAG_TXFULL:Transmit data buffer full
351 * @arg UART_FLAG_RXAVL:RX Buffer has a byte flag
352 * @arg UART_FLAG_OVER:OverRun Error flag
353 * @arg UART_FLAG_TXEPT: tx and shifter are emptys flag
354 * @retval : The new state of UART_FLAG (SET or RESET).
355 */
UART_GetFlagStatus(UART_TypeDef * UARTx,uint16_t UART_FLAG)356 FlagStatus UART_GetFlagStatus(UART_TypeDef* UARTx, uint16_t UART_FLAG)
357 {
358 FlagStatus bitstatus = RESET;
359 /* Check the parameters */
360 assert_param(IS_UART_ALL_PERIPH(UARTx));
361 assert_param(IS_UART_FLAG(UART_FLAG));
362 if ((UARTx->CSR & UART_FLAG) != (uint16_t)RESET)
363 {
364 bitstatus = SET;
365 }
366 else
367 {
368 bitstatus = RESET;
369 }
370 return bitstatus;
371 }
372
373 /**
374 * @brief Clears the UARTx's pending flags.
375 * @param UARTx: Select the UART or the UART peripheral.
376 * This parameter can be one of the following values:
377 * UART1, UART2, UART3, UART4 or UART5.
378 * @param UART_FLAG: specifies the flag to clear.
379 * This parameter can be any combination of the following values:
380 * @arg UART_FLAG_TXEMPTY:Transmit data register empty flag
381 * @arg UART_FLAG_TXFULL:Transmit data buffer full
382 * @arg UART_FLAG_RXAVL:RX Buffer has a byte flag
383 * @arg UART_FLAG_OVER:OverRun Error flag
384 * @arg UART_FLAG_TXEPT: tx and shifter are emptys flag
385 * @retval : None
386 */
UART_ClearFlag(UART_TypeDef * UARTx,uint16_t UART_FLAG)387 void UART_ClearFlag(UART_TypeDef* UARTx, uint16_t UART_FLAG)
388 {
389
390 }
391
392 /**
393 * @brief Checks whether the specified UART interrupt has occurred or not.
394 * @param UARTx: Select the UART or the UART peripheral.
395 * This parameter can be one of the following values:
396 * UART1, UART2, UART3.
397 * @param UART_IT: specifies the UART interrupt source to check.
398 * This parameter can be one of the following values:
399 * @arg UART_IT_ERR: Error interrupt(Frame error,)
400 * @arg UART_IT_PE: Parity Error interrupt
401 * @arg UART_OVER_ERR: overrun Error interrupt
402 * @arg UART_TIMEOUT_ERR: timeout Error interrupt
403 * @arg UART_IT_RXIEN: Receive Data register interrupt
404 * @arg UART_IT_TXIEN: Tansmit Data Register empty interrupt
405 * @retval : The new state of UART_IT (SET or RESET).
406 */
UART_GetITStatus(UART_TypeDef * UARTx,uint16_t UART_IT)407 ITStatus UART_GetITStatus(UART_TypeDef* UARTx, uint16_t UART_IT)
408 {
409 FlagStatus bitstatus = RESET;
410 /* Check the parameters */
411 assert_param(IS_UART_ALL_PERIPH(UARTx));
412 assert_param(IS_UART_FLAG(UART_FLAG));
413 assert_param(IS_UART_PERIPH_FLAG(UARTx, UART_FLAG)); /* The CTS flag is not available for UART4 and UART5 */
414 if ((UARTx->ISR & UART_IT) != (uint16_t)RESET)
415 {
416 bitstatus = SET;
417 }
418 else
419 {
420 bitstatus = RESET;
421 }
422 return bitstatus;
423 }
424
425 /**
426 * @brief Clears the UARTx�s interrupt pending bits.
427 * @param UARTx: Select the UART or the UART peripheral.
428 * This parameter can be one of the following values:
429 * UART1, UART2, UART3, UART4 or UART5.
430 * @param UART_IT: specifies the interrupt pending bit to clear.
431 * This parameter can be one of the following values:
432 * @arg UART_IT_ERR: Error interrupt(Frame error,)
433 * @arg UART_IT_PE: Parity Error interrupt
434 * @arg UART_OVER_ERR: overrun Error interrupt
435 * @arg UART_TIMEOUT_ERR: timeout Error interrupt
436 * @arg UART_IT_RXIEN: Receive Data register interrupt
437 * @arg UART_IT_TXIEN: Tansmit Data Register empty interrupt
438
439 * @retval : None
440 */
UART_ClearITPendingBit(UART_TypeDef * UARTx,uint16_t UART_IT)441 void UART_ClearITPendingBit(UART_TypeDef* UARTx, uint16_t UART_IT)
442 {
443
444 /* Check the parameters */
445 assert_param(IS_UART_ALL_PERIPH(UARTx));
446 assert_param(IS_UART_CLEAR_IT(UART_IT));
447 assert_param(IS_UART_PERIPH_IT(UARTx, UART_IT)); /* The CTS interrupt is not available for UART4 and UART5 */
448 /*clear UART_IT pendings bit*/
449 UARTx->ICR = UART_IT;
450 }
451 /**
452 * @}
453 */
454
455 /**
456 * @}
457 */
458
459 /**
460 * @}
461 */
462
463 /*-------------------------(C) COPYRIGHT 2019 MindMotion ----------------------*/
464