STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_uart.c
Go to the documentation of this file.
1 
156 /* Includes ------------------------------------------------------------------*/
157 #include "stm32f7xx_hal.h"
158 
168 #ifdef HAL_UART_MODULE_ENABLED
169 
170 /* Private typedef -----------------------------------------------------------*/
171 /* Private define ------------------------------------------------------------*/
175 #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
176  USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
177 
180 /* Private macro -------------------------------------------------------------*/
181 /* Private variables ---------------------------------------------------------*/
182 /* Private function prototypes -----------------------------------------------*/
186 static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
187 static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
188 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
189 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
190 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
191 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
192 static void UART_DMAError(DMA_HandleTypeDef *hdma);
193 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
194 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
195 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
196 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
201 /* Exported functions --------------------------------------------------------*/
202 
244 {
245  /* Check the UART handle allocation */
246  if(huart == NULL)
247  {
248  return HAL_ERROR;
249  }
250 
251  if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
252  {
253  /* Check the parameters */
255  }
256  else
257  {
258  /* Check the parameters */
260  }
261 
262  if(huart->gState == HAL_UART_STATE_RESET)
263  {
264  /* Allocate lock resource and initialize it */
265  huart->Lock = HAL_UNLOCKED;
266 
267  /* Init the low level hardware : GPIO, CLOCK */
268  HAL_UART_MspInit(huart);
269  }
270 
271  huart->gState = HAL_UART_STATE_BUSY;
272 
273  /* Disable the Peripheral */
274  __HAL_UART_DISABLE(huart);
275 
276  /* Set the UART Communication parameters */
277  if (UART_SetConfig(huart) == HAL_ERROR)
278  {
279  return HAL_ERROR;
280  }
281 
283  {
284  UART_AdvFeatureConfig(huart);
285  }
286 
287  /* In asynchronous mode, the following bits must be kept cleared:
288  - LINEN and CLKEN bits in the USART_CR2 register,
289  - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
292 
293  /* Enable the Peripheral */
294  __HAL_UART_ENABLE(huart);
295 
296  /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
297  return (UART_CheckIdleState(huart));
298 }
299 
307 {
308  /* Check the UART handle allocation */
309  if(huart == NULL)
310  {
311  return HAL_ERROR;
312  }
313 
314  if(huart->gState == HAL_UART_STATE_RESET)
315  {
316  /* Allocate lock resource and initialize it */
317  huart->Lock = HAL_UNLOCKED;
318 
319  /* Init the low level hardware : GPIO, CLOCK */
320  HAL_UART_MspInit(huart);
321  }
322 
323  huart->gState = HAL_UART_STATE_BUSY;
324 
325  /* Disable the Peripheral */
326  __HAL_UART_DISABLE(huart);
327 
328  /* Set the UART Communication parameters */
329  if (UART_SetConfig(huart) == HAL_ERROR)
330  {
331  return HAL_ERROR;
332  }
333 
335  {
336  UART_AdvFeatureConfig(huart);
337  }
338 
339  /* In half-duplex mode, the following bits must be kept cleared:
340  - LINEN and CLKEN bits in the USART_CR2 register,
341  - SCEN and IREN bits in the USART_CR3 register.*/
344 
345  /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
347 
348  /* Enable the Peripheral */
349  __HAL_UART_ENABLE(huart);
350 
351  /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
352  return (UART_CheckIdleState(huart));
353 }
354 
355 
366 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
367 {
368  /* Check the UART handle allocation */
369  if(huart == NULL)
370  {
371  return HAL_ERROR;
372  }
373 
374  /* Check the parameters */
376  assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
378 
379  if(huart->gState == HAL_UART_STATE_RESET)
380  {
381  /* Allocate lock resource and initialize it */
382  huart->Lock = HAL_UNLOCKED;
383 
384  /* Init the low level hardware : GPIO, CLOCK */
385  HAL_UART_MspInit(huart);
386  }
387 
388  huart->gState = HAL_UART_STATE_BUSY;
389 
390  /* Disable the Peripheral */
391  __HAL_UART_DISABLE(huart);
392 
393  /* Set the UART Communication parameters */
394  if (UART_SetConfig(huart) == HAL_ERROR)
395  {
396  return HAL_ERROR;
397  }
398 
400  {
401  UART_AdvFeatureConfig(huart);
402  }
403 
404  /* In LIN mode, the following bits must be kept cleared:
405  - LINEN and CLKEN bits in the USART_CR2 register,
406  - SCEN and IREN bits in the USART_CR3 register.*/
409 
410  /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
412 
413  /* Set the USART LIN Break detection length. */
414  MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
415 
416  /* Enable the Peripheral */
417  __HAL_UART_ENABLE(huart);
418 
419  /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
420  return (UART_CheckIdleState(huart));
421 }
422 
423 
440 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
441 {
442  /* Check the UART handle allocation */
443  if(huart == NULL)
444  {
445  return HAL_ERROR;
446  }
447 
448  /* Check the wake up method parameter */
449  assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
450 
451  if(huart->gState == HAL_UART_STATE_RESET)
452  {
453  /* Allocate lock resource and initialize it */
454  huart->Lock = HAL_UNLOCKED;
455 
456  /* Init the low level hardware : GPIO, CLOCK */
457  HAL_UART_MspInit(huart);
458  }
459 
460  huart->gState = HAL_UART_STATE_BUSY;
461 
462  /* Disable the Peripheral */
463  __HAL_UART_DISABLE(huart);
464 
465  /* Set the UART Communication parameters */
466  if (UART_SetConfig(huart) == HAL_ERROR)
467  {
468  return HAL_ERROR;
469  }
470 
472  {
473  UART_AdvFeatureConfig(huart);
474  }
475 
476  /* In multiprocessor mode, the following bits must be kept cleared:
477  - LINEN and CLKEN bits in the USART_CR2 register,
478  - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
481 
482  if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
483  {
484  /* If address mark wake up method is chosen, set the USART address node */
485  MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
486  }
487 
488  /* Set the wake up method by setting the WAKE bit in the CR1 register */
489  MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
490 
491  /* Enable the Peripheral */
492  __HAL_UART_ENABLE(huart);
493 
494  /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
495  return (UART_CheckIdleState(huart));
496 }
497 
498 
518 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
519 {
520  uint32_t temp = 0x0;
521 
522  /* Check the UART handle allocation */
523  if(huart == NULL)
524  {
525  return HAL_ERROR;
526  }
527  /* Check the Driver Enable UART instance */
529 
530  /* Check the Driver Enable polarity */
532 
533  /* Check the Driver Enable assertion time */
534  assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
535 
536  /* Check the Driver Enable deassertion time */
537  assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
538 
539  if(huart->gState == HAL_UART_STATE_RESET)
540  {
541  /* Allocate lock resource and initialize it */
542  huart->Lock = HAL_UNLOCKED;
543 
544  /* Init the low level hardware : GPIO, CLOCK, CORTEX */
545  HAL_UART_MspInit(huart);
546  }
547 
548  huart->gState = HAL_UART_STATE_BUSY;
549 
550  /* Disable the Peripheral */
551  __HAL_UART_DISABLE(huart);
552 
553  /* Set the UART Communication parameters */
554  if (UART_SetConfig(huart) == HAL_ERROR)
555  {
556  return HAL_ERROR;
557  }
558 
560  {
561  UART_AdvFeatureConfig(huart);
562  }
563 
564  /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
565  SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
566 
567  /* Set the Driver Enable polarity */
568  MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
569 
570  /* Set the Driver Enable assertion and deassertion times */
571  temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
572  temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
574 
575  /* Enable the Peripheral */
576  __HAL_UART_ENABLE(huart);
577 
578  /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
579  return (UART_CheckIdleState(huart));
580 }
581 
588 {
589  /* Check the UART handle allocation */
590  if(huart == NULL)
591  {
592  return HAL_ERROR;
593  }
594 
595  /* Check the parameters */
597 
598  huart->gState = HAL_UART_STATE_BUSY;
599 
600  /* Disable the Peripheral */
601  __HAL_UART_DISABLE(huart);
602 
603  huart->Instance->CR1 = 0x0U;
604  huart->Instance->CR2 = 0x0U;
605  huart->Instance->CR3 = 0x0U;
606 
607  /* DeInit the low level hardware */
608  HAL_UART_MspDeInit(huart);
609 
611  huart->gState = HAL_UART_STATE_RESET;
612  huart->RxState = HAL_UART_STATE_RESET;
613 
614  /* Process Unlock */
615  __HAL_UNLOCK(huart);
616 
617  return HAL_OK;
618 }
619 
625 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
626 {
627  /* Prevent unused argument(s) compilation warning */
628  UNUSED(huart);
629 
630  /* NOTE : This function should not be modified, when the callback is needed,
631  the HAL_UART_MspInit can be implemented in the user file
632  */
633 }
634 
640 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
641 {
642  /* Prevent unused argument(s) compilation warning */
643  UNUSED(huart);
644 
645  /* NOTE : This function should not be modified, when the callback is needed,
646  the HAL_UART_MspDeInit can be implemented in the user file
647  */
648 }
649 
718 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
719 {
720  uint16_t* tmp;
721  uint32_t tickstart = 0U;
722 
723  /* Check that a Tx process is not already ongoing */
724  if(huart->gState == HAL_UART_STATE_READY)
725  {
726  if((pData == NULL ) || (Size == 0U))
727  {
728  return HAL_ERROR;
729  }
730 
731  /* Process Locked */
732  __HAL_LOCK(huart);
733 
736 
737  /* Init tickstart for timeout managment*/
738  tickstart = HAL_GetTick();
739 
740  huart->TxXferSize = Size;
741  huart->TxXferCount = Size;
742  while(huart->TxXferCount > 0U)
743  {
744  huart->TxXferCount--;
745  if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
746  {
747  return HAL_TIMEOUT;
748  }
749  if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
750  {
751  tmp = (uint16_t*) pData;
752  huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
753  pData += 2;
754  }
755  else
756  {
757  huart->Instance->TDR = (*pData++ & (uint8_t)0xFFU);
758  }
759  }
760  if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
761  {
762  return HAL_TIMEOUT;
763  }
764 
765  /* At end of Tx process, restore huart->gState to Ready */
766  huart->gState = HAL_UART_STATE_READY;
767 
768  /* Process Unlocked */
769  __HAL_UNLOCK(huart);
770 
771  return HAL_OK;
772  }
773  else
774  {
775  return HAL_BUSY;
776  }
777 }
778 
787 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
788 {
789  uint16_t* tmp;
790  uint16_t uhMask;
791  uint32_t tickstart = 0U;
792 
793  /* Check that a Rx process is not already ongoing */
794  if(huart->RxState == HAL_UART_STATE_READY)
795  {
796  if((pData == NULL ) || (Size == 0U))
797  {
798  return HAL_ERROR;
799  }
800 
801  /* Process Locked */
802  __HAL_LOCK(huart);
803 
806 
807  /* Init tickstart for timeout managment*/
808  tickstart = HAL_GetTick();
809 
810  huart->RxXferSize = Size;
811  huart->RxXferCount = Size;
812 
813  /* Computation of UART mask to apply to RDR register */
814  UART_MASK_COMPUTATION(huart);
815  uhMask = huart->Mask;
816 
817  /* as long as data have to be received */
818  while(huart->RxXferCount > 0U)
819  {
820  huart->RxXferCount--;
821  if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
822  {
823  return HAL_TIMEOUT;
824  }
825  if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
826  {
827  tmp = (uint16_t*) pData ;
828  *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
829  pData +=2U;
830  }
831  else
832  {
833  *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
834  }
835  }
836 
837  /* At end of Rx process, restore huart->RxState to Ready */
838  huart->RxState = HAL_UART_STATE_READY;
839 
840  /* Process Unlocked */
841  __HAL_UNLOCK(huart);
842 
843  return HAL_OK;
844  }
845  else
846  {
847  return HAL_BUSY;
848  }
849 }
850 
858 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
859 {
860  /* Check that a Tx process is not already ongoing */
861  if(huart->gState == HAL_UART_STATE_READY)
862  {
863  if((pData == NULL ) || (Size == 0U))
864  {
865  return HAL_ERROR;
866  }
867 
868  /* Process Locked */
869  __HAL_LOCK(huart);
870 
871  huart->pTxBuffPtr = pData;
872  huart->TxXferSize = Size;
873  huart->TxXferCount = Size;
874 
877 
878  /* Process Unlocked */
879  __HAL_UNLOCK(huart);
880 
881  /* Enable the UART Transmit Data Register Empty Interrupt */
883 
884  return HAL_OK;
885  }
886  else
887  {
888  return HAL_BUSY;
889  }
890 }
891 
899 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
900 {
901  /* Check that a Rx process is not already ongoing */
902  if(huart->RxState == HAL_UART_STATE_READY)
903  {
904  if((pData == NULL ) || (Size == 0U))
905  {
906  return HAL_ERROR;
907  }
908 
909  /* Process Locked */
910  __HAL_LOCK(huart);
911 
912  huart->pRxBuffPtr = pData;
913  huart->RxXferSize = Size;
914  huart->RxXferCount = Size;
915 
916  /* Computation of UART mask to apply to RDR register */
917  UART_MASK_COMPUTATION(huart);
918 
921 
922  /* Process Unlocked */
923  __HAL_UNLOCK(huart);
924 
925  /* Enable the UART Parity Error Interrupt */
927 
928  /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
929  SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
930 
931  /* Enable the UART Data Register not empty Interrupt */
933 
934  return HAL_OK;
935  }
936  else
937  {
938  return HAL_BUSY;
939  }
940 }
941 
949 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
950 {
951  uint32_t *tmp;
952 
953  /* Check that a Tx process is not already ongoing */
954  if(huart->gState == HAL_UART_STATE_READY)
955  {
956  if((pData == NULL ) || (Size == 0U))
957  {
958  return HAL_ERROR;
959  }
960 
961  /* Process Locked */
962  __HAL_LOCK(huart);
963 
964  huart->pTxBuffPtr = pData;
965  huart->TxXferSize = Size;
966  huart->TxXferCount = Size;
967 
970 
971  /* Set the UART DMA transfer complete callback */
972  huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
973 
974  /* Set the UART DMA Half transfer complete callback */
975  huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
976 
977  /* Set the DMA error callback */
978  huart->hdmatx->XferErrorCallback = UART_DMAError;
979 
980  /* Set the DMA abort callback */
981  huart->hdmatx->XferAbortCallback = NULL;
982 
983  /* Enable the UART transmit DMA channel */
984  tmp = (uint32_t*)&pData;
985  HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->TDR, Size);
986 
987  /* Clear the TC flag in the SR register by writing 0 to it */
989 
990  /* Process Unlocked */
991  __HAL_UNLOCK(huart);
992 
993  /* Enable the DMA transfer for transmit request by setting the DMAT bit
994  in the UART CR3 register */
996 
997  return HAL_OK;
998  }
999  else
1000  {
1001  return HAL_BUSY;
1002  }
1003 }
1004 
1014 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1015 {
1016  uint32_t *tmp;
1017 
1018  /* Check that a Rx process is not already ongoing */
1019  if(huart->RxState == HAL_UART_STATE_READY)
1020  {
1021  if((pData == NULL ) || (Size == 0U))
1022  {
1023  return HAL_ERROR;
1024  }
1025 
1026  /* Process Locked */
1027  __HAL_LOCK(huart);
1028 
1029  huart->pRxBuffPtr = pData;
1030  huart->RxXferSize = Size;
1031 
1032  huart->ErrorCode = HAL_UART_ERROR_NONE;
1034 
1035  /* Set the UART DMA transfer complete callback */
1036  huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
1037 
1038  /* Set the UART DMA Half transfer complete callback */
1039  huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
1040 
1041  /* Set the DMA error callback */
1042  huart->hdmarx->XferErrorCallback = UART_DMAError;
1043 
1044  /* Set the DMA abort callback */
1045  huart->hdmarx->XferAbortCallback = NULL;
1046 
1047  /* Enable the DMA channel */
1048  tmp = (uint32_t*)&pData;
1049  HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, *(uint32_t*)tmp, Size);
1050 
1051  /* Process Unlocked */
1052  __HAL_UNLOCK(huart);
1053 
1054  /* Enable the UART Parity Error Interrupt */
1055  SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1056 
1057  /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1058  SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1059 
1060  /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1061  in the UART CR3 register */
1062  SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1063 
1064  return HAL_OK;
1065  }
1066  else
1067  {
1068  return HAL_BUSY;
1069  }
1070 }
1071 
1078 {
1079  /* Process Locked */
1080  __HAL_LOCK(huart);
1081 
1082  if ((huart->gState == HAL_UART_STATE_BUSY_TX) &&
1084  {
1085  /* Disable the UART DMA Tx request */
1087  }
1088  if ((huart->RxState == HAL_UART_STATE_BUSY_RX) &&
1090  {
1091  /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1093  CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1094 
1095  /* Disable the UART DMA Rx request */
1097  }
1098 
1099  /* Process Unlocked */
1100  __HAL_UNLOCK(huart);
1101 
1102  return HAL_OK;
1103 }
1104 
1111 {
1112  /* Process Locked */
1113  __HAL_LOCK(huart);
1114 
1115  if(huart->gState == HAL_UART_STATE_BUSY_TX)
1116  {
1117  /* Enable the UART DMA Tx request */
1118  SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1119  }
1120  if(huart->RxState == HAL_UART_STATE_BUSY_RX)
1121  {
1122  /* Clear the Overrun flag before resuming the Rx transfer*/
1124 
1125  /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1126  SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1127  SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1128 
1129  /* Enable the UART DMA Rx request */
1130  SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1131  }
1132 
1133  /* If the UART peripheral is still not enabled, enable it */
1134  if ((huart->Instance->CR1 & USART_CR1_UE) == 0U)
1135  {
1136  /* Enable UART peripheral */
1137  __HAL_UART_ENABLE(huart);
1138  }
1139 
1140  return HAL_OK;
1141 }
1142 
1149 {
1150  /* The Lock is not implemented on this API to allow the user application
1151  to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1152  HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1153  indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1154  interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1155  the stream and the corresponding call back is executed. */
1156 
1157  /* Stop UART DMA Tx request if ongoing */
1158  if ((huart->gState == HAL_UART_STATE_BUSY_TX) &&
1160  {
1162 
1163  /* Abort the UART DMA Tx channel */
1164  if(huart->hdmatx != NULL)
1165  {
1166  HAL_DMA_Abort(huart->hdmatx);
1167  }
1168 
1169  UART_EndTxTransfer(huart);
1170  }
1171 
1172  /* Stop UART DMA Rx request if ongoing */
1173  if ((huart->RxState == HAL_UART_STATE_BUSY_RX) &&
1175  {
1177 
1178  /* Abort the UART DMA Rx channel */
1179  if(huart->hdmarx != NULL)
1180  {
1181  HAL_DMA_Abort(huart->hdmarx);
1182  }
1183 
1184  UART_EndRxTransfer(huart);
1185  }
1186 
1187  return HAL_OK;
1188 }
1189 
1196 {
1197  uint32_t isrflags = READ_REG(huart->Instance->ISR);
1198  uint32_t cr1its = READ_REG(huart->Instance->CR1);
1199  uint32_t cr3its = READ_REG(huart->Instance->CR3);
1200  uint32_t errorflags;
1201 
1202  /* If no error occurs */
1203  errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
1204  if (errorflags == RESET)
1205  {
1206  /* UART in mode Receiver ---------------------------------------------------*/
1207  if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1208  {
1209  UART_Receive_IT(huart);
1210  return;
1211  }
1212  }
1213 
1214  /* If some errors occur */
1215  if((errorflags != RESET) && ((cr3its & (USART_CR3_EIE | USART_CR1_PEIE)) != RESET))
1216  {
1217 
1218  /* UART parity error interrupt occurred -------------------------------------*/
1219  if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1220  {
1222 
1223  huart->ErrorCode |= HAL_UART_ERROR_PE;
1224  }
1225 
1226  /* UART frame error interrupt occurred --------------------------------------*/
1227  if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1228  {
1230 
1231  huart->ErrorCode |= HAL_UART_ERROR_FE;
1232  }
1233 
1234  /* UART noise error interrupt occurred --------------------------------------*/
1235  if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1236  {
1238 
1239  huart->ErrorCode |= HAL_UART_ERROR_NE;
1240  }
1241 
1242  /* UART Over-Run interrupt occurred -----------------------------------------*/
1243  if(((isrflags & USART_ISR_ORE) != RESET) &&
1244  (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
1245  {
1247 
1248  huart->ErrorCode |= HAL_UART_ERROR_ORE;
1249  }
1250 
1251  /* Call UART Error Call back function if need be --------------------------*/
1252  if(huart->ErrorCode != HAL_UART_ERROR_NONE)
1253  {
1254  /* UART in mode Receiver ---------------------------------------------------*/
1255  if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1256  {
1257  UART_Receive_IT(huart);
1258  }
1259 
1260  /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1261  consider error as blocking */
1262  if (((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) ||
1264  {
1265  /* Blocking error : transfer is aborted
1266  Set the UART state ready to be able to start again the process,
1267  Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1268  UART_EndRxTransfer(huart);
1269 
1270  /* Disable the UART DMA Rx request if enabled */
1271  if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1272  {
1274 
1275  /* Abort the UART DMA Rx channel */
1276  if(huart->hdmarx != NULL)
1277  {
1278  /* Set the UART DMA Abort callback :
1279  will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
1280  huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
1281 
1282  /* Abort DMA RX */
1283  if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1284  {
1285  /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
1286  huart->hdmarx->XferAbortCallback(huart->hdmarx);
1287  }
1288  }
1289  else
1290  {
1291  /* Call user error callback */
1292  HAL_UART_ErrorCallback(huart);
1293  }
1294  }
1295  else
1296  {
1297  /* Call user error callback */
1298  HAL_UART_ErrorCallback(huart);
1299  }
1300  }
1301  else
1302  {
1303  /* Non Blocking error : transfer could go on.
1304  Error is notified to user through user error callback */
1305  HAL_UART_ErrorCallback(huart);
1306  huart->ErrorCode = HAL_UART_ERROR_NONE;
1307  }
1308  }
1309  return;
1310 
1311  } /* End if some error occurs */
1312 
1313  /* UART in mode Transmitter ------------------------------------------------*/
1314  if(((isrflags & USART_ISR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1315  {
1316  UART_Transmit_IT(huart);
1317  return;
1318  }
1319 
1320  /* UART in mode Transmitter (transmission end) -----------------------------*/
1321  if(((isrflags & USART_ISR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1322  {
1323  UART_EndTransmit_IT(huart);
1324  return;
1325  }
1326 
1327 }
1328 
1338 HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
1339 {
1340  /* Wait until flag is set */
1341  while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
1342  {
1343  /* Check for the Timeout */
1344  if(Timeout != HAL_MAX_DELAY)
1345  {
1346  if((Timeout == 0U)||((HAL_GetTick()-Tickstart) >= Timeout))
1347  {
1348  /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1349  CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
1350  CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1351 
1352  huart->gState = HAL_UART_STATE_READY;
1353  huart->RxState = HAL_UART_STATE_READY;
1354 
1355  /* Process Unlocked */
1356  __HAL_UNLOCK(huart);
1357  return HAL_TIMEOUT;
1358  }
1359  }
1360  }
1361  return HAL_OK;
1362 }
1363 
1369 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1370 {
1371  UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1372 
1373  /* DMA Normal mode*/
1374  if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
1375  {
1376  huart->TxXferCount = 0U;
1377 
1378  /* Disable the DMA transfer for transmit request by setting the DMAT bit
1379  in the UART CR3 register */
1381 
1382  /* Enable the UART Transmit Complete Interrupt */
1383  SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
1384  }
1385  /* DMA Circular mode */
1386  else
1387  {
1388  HAL_UART_TxCpltCallback(huart);
1389  }
1390 }
1391 
1397 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
1398 {
1399  UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1400 
1402 }
1403 
1409 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1410 {
1411  UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1412 
1413  /* DMA Normal mode */
1414  if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
1415  {
1416  huart->RxXferCount = 0U;
1417 
1418  /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1419  CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1420  CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1421 
1422  /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1423  in the UART CR3 register */
1425 
1426  /* At end of Rx process, restore huart->RxState to Ready */
1427  huart->RxState = HAL_UART_STATE_READY;
1428  }
1429  HAL_UART_RxCpltCallback(huart);
1430 }
1431 
1437 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
1438 {
1439  UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1440 
1442 }
1443 
1449 static void UART_DMAError(DMA_HandleTypeDef *hdma)
1450 {
1451  UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1452  huart->RxXferCount = 0U;
1453  huart->TxXferCount = 0U;
1454  /* Stop UART DMA Tx request if ongoing */
1455  if ( (huart->gState == HAL_UART_STATE_BUSY_TX)
1456  &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) )
1457  {
1458  UART_EndTxTransfer(huart);
1459  }
1460 
1461  /* Stop UART DMA Rx request if ongoing */
1462  if ( (huart->RxState == HAL_UART_STATE_BUSY_RX)
1463  &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) )
1464  {
1465  UART_EndRxTransfer(huart);
1466  }
1468  HAL_UART_ErrorCallback(huart);
1469 }
1470 
1477 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
1478 {
1479  UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
1480  huart->RxXferCount = 0U;
1481  huart->TxXferCount = 0U;
1482 
1483  HAL_UART_ErrorCallback(huart);
1484 }
1485 
1491  __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
1492 {
1493  /* Prevent unused argument(s) compilation warning */
1494  UNUSED(huart);
1495 
1496  /* NOTE : This function should not be modified, when the callback is needed,
1497  the HAL_UART_TxCpltCallback can be implemented in the user file
1498  */
1499 }
1500 
1507 {
1508  /* Prevent unused argument(s) compilation warning */
1509  UNUSED(huart);
1510 
1511  /* NOTE: This function should not be modified, when the callback is needed,
1512  the HAL_UART_TxHalfCpltCallback can be implemented in the user file
1513  */
1514 }
1515 
1521 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
1522 {
1523  /* Prevent unused argument(s) compilation warning */
1524  UNUSED(huart);
1525 
1526  /* NOTE : This function should not be modified, when the callback is needed,
1527  the HAL_UART_RxCpltCallback can be implemented in the user file
1528  */
1529 }
1530 
1537 {
1538  /* Prevent unused argument(s) compilation warning */
1539  UNUSED(huart);
1540 
1541  /* NOTE: This function should not be modified, when the callback is needed,
1542  the HAL_UART_RxHalfCpltCallback can be implemented in the user file
1543  */
1544 }
1545 
1551  __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
1552 {
1553  /* Prevent unused argument(s) compilation warning */
1554  UNUSED(huart);
1555 
1556  /* NOTE : This function should not be modified, when the callback is needed,
1557  the HAL_UART_ErrorCallback can be implemented in the user file
1558  */
1559 }
1560 
1568 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
1569 {
1570  uint16_t* tmp;
1571 
1572  /* Check that a Tx process is ongoing */
1573  if (huart->gState == HAL_UART_STATE_BUSY_TX)
1574  {
1575 
1576  if(huart->TxXferCount == 0U)
1577  {
1578  /* Disable the UART Transmit Data Register Empty Interrupt */
1579  CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
1580 
1581  /* Enable the UART Transmit Complete Interrupt */
1582  SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
1583 
1584  return HAL_OK;
1585  }
1586  else
1587  {
1588  if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1589  {
1590  tmp = (uint16_t*) huart->pTxBuffPtr;
1591  huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
1592  huart->pTxBuffPtr += 2U;
1593  }
1594  else
1595  {
1596  huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0xFFU);
1597  }
1598 
1599  huart->TxXferCount--;
1600 
1601  return HAL_OK;
1602  }
1603  }
1604  else
1605  {
1606  return HAL_BUSY;
1607  }
1608 }
1609 
1616 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
1617 {
1618  /* Disable the UART Transmit Complete Interrupt */
1620 
1621  /* Tx process is ended, restore huart->gState to Ready */
1622  huart->gState = HAL_UART_STATE_READY;
1623 
1624  HAL_UART_TxCpltCallback(huart);
1625 
1626  return HAL_OK;
1627 }
1628 
1636 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
1637 {
1638  uint16_t* tmp;
1639  uint16_t uhMask = huart->Mask;
1640 
1641  /* Check that a Rx process is ongoing */
1642  if(huart->RxState == HAL_UART_STATE_BUSY_RX)
1643  {
1644 
1645  if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1646  {
1647  tmp = (uint16_t*) huart->pRxBuffPtr ;
1648  *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
1649  huart->pRxBuffPtr +=2;
1650  }
1651  else
1652  {
1653  *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
1654  }
1655 
1656  if(--huart->RxXferCount == 0)
1657  {
1658  /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
1659  CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1660 
1661  /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1662  CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1663 
1664  /* Rx process is completed, restore huart->RxState to Ready */
1665  huart->RxState = HAL_UART_STATE_READY;
1666 
1667  HAL_UART_RxCpltCallback(huart);
1668 
1669  return HAL_OK;
1670  }
1671 
1672  return HAL_OK;
1673  }
1674  else
1675  {
1676  /* Clear RXNE interrupt flag */
1678 
1679  return HAL_BUSY;
1680  }
1681 }
1682 
1688 static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
1689 {
1690  /* Disable TXEIE and TCIE interrupts */
1691  CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1692 
1693  /* At end of Tx process, restore huart->gState to Ready */
1694  huart->gState = HAL_UART_STATE_READY;
1695 }
1696 
1697 
1703 static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
1704 {
1705  /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1706  CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1707  CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1708 
1709  /* At end of Rx process, restore huart->RxState to Ready */
1710  huart->RxState = HAL_UART_STATE_READY;
1711 }
1712 
1750 {
1751  /* Process Locked */
1752  __HAL_LOCK(huart);
1753 
1754  huart->gState = HAL_UART_STATE_BUSY;
1755 
1756  /* Enable USART mute mode by setting the MME bit in the CR1 register */
1757  SET_BIT(huart->Instance->CR1, USART_CR1_MME);
1758 
1759  huart->gState = HAL_UART_STATE_READY;
1760 
1761  return (UART_CheckIdleState(huart));
1762 }
1763 
1771 {
1772  /* Process Locked */
1773  __HAL_LOCK(huart);
1774 
1775  huart->gState = HAL_UART_STATE_BUSY;
1776 
1777  /* Disable USART mute mode by clearing the MME bit in the CR1 register */
1778  CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
1779 
1780  huart->gState = HAL_UART_STATE_READY;
1781 
1782  return (UART_CheckIdleState(huart));
1783 }
1784 
1792 {
1794 }
1795 
1796 
1797 
1804 {
1805  uint32_t temp1= 0x00U, temp2 = 0x00U;
1806  temp1 = huart->gState;
1807  temp2 = huart->RxState;
1808 
1809  return (HAL_UART_StateTypeDef)(temp1 | temp2);
1810 }
1811 
1818 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
1819 {
1820  return huart->ErrorCode;
1821 }
1822 
1829 {
1830  uint32_t tmpreg = 0x00000000U;
1832  uint16_t brrtemp = 0x0000U;
1833  uint16_t usartdiv = 0x0000U;
1834  HAL_StatusTypeDef ret = HAL_OK;
1835 
1836  /* Check the parameters */
1844 
1845 
1846  /*-------------------------- USART CR1 Configuration -----------------------*/
1847  /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
1848  * the UART Word Length, Parity, Mode and oversampling:
1849  * set the M bits according to huart->Init.WordLength value
1850  * set PCE and PS bits according to huart->Init.Parity value
1851  * set TE and RE bits according to huart->Init.Mode value
1852  * set OVER8 bit according to huart->Init.OverSampling value */
1853  tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
1854  MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg);
1855 
1856  /*-------------------------- USART CR2 Configuration -----------------------*/
1857  /* Configure the UART Stop Bits: Set STOP[13:12] bits according
1858  * to huart->Init.StopBits value */
1859  MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
1860 
1861  /*-------------------------- USART CR3 Configuration -----------------------*/
1862  /* Configure
1863  * - UART HardWare Flow Control: set CTSE and RTSE bits according
1864  * to huart->Init.HwFlowCtl value
1865  * - one-bit sampling method versus three samples' majority rule according
1866  * to huart->Init.OneBitSampling */
1867  tmpreg = (uint32_t)huart->Init.HwFlowCtl | huart->Init.OneBitSampling ;
1869 
1870  /*-------------------------- USART BRR Configuration -----------------------*/
1871  UART_GETCLOCKSOURCE(huart, clocksource);
1872 
1873  /* Check UART Over Sampling to set Baud Rate Register */
1874  if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
1875  {
1876  switch (clocksource)
1877  {
1879  usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
1880  break;
1882  usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
1883  break;
1884  case UART_CLOCKSOURCE_HSI:
1885  usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
1886  break;
1888  usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
1889  break;
1890  case UART_CLOCKSOURCE_LSE:
1891  usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
1892  break;
1894  default:
1895  ret = HAL_ERROR;
1896  break;
1897  }
1898 
1899  brrtemp = usartdiv & 0xFFF0U;
1900  brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
1901  huart->Instance->BRR = brrtemp;
1902  }
1903  else
1904  {
1905  switch (clocksource)
1906  {
1908  huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
1909  break;
1911  huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
1912  break;
1913  case UART_CLOCKSOURCE_HSI:
1914  huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
1915  break;
1917  huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
1918  break;
1919  case UART_CLOCKSOURCE_LSE:
1920  huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
1921  break;
1923  default:
1924  ret = HAL_ERROR;
1925  break;
1926  }
1927  }
1928 
1929  return ret;
1930 
1931 }
1932 
1933 
1940 {
1941  /* Check whether the set of advanced features to configure is properly set */
1943 
1944  /* if required, configure TX pin active level inversion */
1946  {
1949  }
1950 
1951  /* if required, configure RX pin active level inversion */
1953  {
1956  }
1957 
1958  /* if required, configure data inversion */
1960  {
1963  }
1964 
1965  /* if required, configure RX/TX pins swap */
1967  {
1970  }
1971 
1972  /* if required, configure RX overrun detection disabling */
1974  {
1977  }
1978 
1979  /* if required, configure DMA disabling on reception error */
1981  {
1984  }
1985 
1986  /* if required, configure auto Baud rate detection scheme */
1988  {
1991  /* set auto Baudrate detection parameters if detection is enabled */
1993  {
1996  }
1997  }
1998 
1999  /* if required, configure MSB first on communication line */
2001  {
2004  }
2005 }
2006 
2007 
2008 
2015 {
2016  uint32_t tickstart = 0U;
2017 
2018  /* Initialize the UART ErrorCode */
2019  huart->ErrorCode = HAL_UART_ERROR_NONE;
2020 
2021  /* Init tickstart for timeout managment*/
2022  tickstart = HAL_GetTick();
2023 
2024  /* Check if the Transmitter is enabled */
2025  if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
2026  {
2027  /* Wait until TEACK flag is set */
2029  {
2030  /* Timeout Occurred */
2031  return HAL_TIMEOUT;
2032  }
2033  }
2034  /* Check if the Receiver is enabled */
2035  if((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2036  {
2037  /* Wait until REACK flag is set */
2039  {
2040  /* Timeout Occurred */
2041  return HAL_TIMEOUT;
2042  }
2043  }
2044 
2045  /* Initialize the UART State */
2046  huart->gState= HAL_UART_STATE_READY;
2047  huart->RxState= HAL_UART_STATE_READY;
2048 
2049  /* Process Unlocked */
2050  __HAL_UNLOCK(huart);
2051 
2052  return HAL_OK;
2053 }
2054 
2062 {
2063  /* Process Locked */
2064  __HAL_LOCK(huart);
2065  huart->gState = HAL_UART_STATE_BUSY;
2066 
2067  /* Clear TE and RE bits */
2069  /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2070  SET_BIT(huart->Instance->CR1, USART_CR1_TE);
2071 
2072  huart->gState= HAL_UART_STATE_READY;
2073  /* Process Unlocked */
2074  __HAL_UNLOCK(huart);
2075 
2076  return HAL_OK;
2077 }
2078 
2085 {
2086  /* Process Locked */
2087  __HAL_LOCK(huart);
2088  huart->gState = HAL_UART_STATE_BUSY;
2089 
2090  /* Clear TE and RE bits */
2092  /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2093  SET_BIT(huart->Instance->CR1, USART_CR1_RE);
2094 
2095  huart->gState = HAL_UART_STATE_READY;
2096  /* Process Unlocked */
2097  __HAL_UNLOCK(huart);
2098 
2099  return HAL_OK;
2100 }
2101 
2102 
2109 {
2110  /* Check the parameters */
2112 
2113  /* Process Locked */
2114  __HAL_LOCK(huart);
2115 
2116  huart->gState = HAL_UART_STATE_BUSY;
2117 
2118  /* Send break characters */
2120 
2121  huart->gState = HAL_UART_STATE_READY;
2122 
2123  /* Process Unlocked */
2124  __HAL_UNLOCK(huart);
2125 
2126  return HAL_OK;
2127 }
2128 
2143 {
2144  /* Check the UART handle allocation */
2145  if(huart == NULL)
2146  {
2147  return HAL_ERROR;
2148  }
2149 
2150  /* Check the address length parameter */
2152 
2153  huart->gState = HAL_UART_STATE_BUSY;
2154 
2155  /* Disable the Peripheral */
2156  __HAL_UART_DISABLE(huart);
2157 
2158  /* Set the address length */
2159  MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
2160 
2161  /* Enable the Peripheral */
2162  __HAL_UART_ENABLE(huart);
2163 
2164  /* TEACK and/or REACK to check before moving huart->gState to Ready */
2165  return (UART_CheckIdleState(huart));
2166 }
2167 
2176 #endif /* HAL_UART_MODULE_ENABLED */
2177 
2185 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#define CLEAR_BIT(REG, BIT)
Definition: stm32f7xx.h:180
#define MODIFY_REG(REG, CLEARMASK, SETMASK)
Definition: stm32f7xx.h:190
#define USART_CR3_DEP
Definition: stm32f745xx.h:7479
#define UART_CLEAR_OREF
#define IS_UART_ADVFEATURE_RXINV(RXINV)
HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
#define USART_CR3_RTSE
Definition: stm32f745xx.h:7472
#define USART_CR2_ADDM7
Definition: stm32f745xx.h:7440
#define HAL_UART_ERROR_PE
#define IS_UART_ADVFEATURE_AUTOBAUDRATE(AUTOBAUDRATE)
HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
#define USART_CR1_PEIE
Definition: stm32f745xx.h:7414
#define USART_CR1_TCIE
Definition: stm32f745xx.h:7412
#define IS_LIN_WORD_LENGTH(LENGTH)
#define USART_CR2_LBDL
Definition: stm32f745xx.h:7441
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
#define USART_CR2_ABRMODE
Definition: stm32f745xx.h:7457
#define assert_param(expr)
Include module&#39;s header file.
HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
#define IS_UART_ONE_BIT_SAMPLE(ONEBIT)
#define USART_CR2_DATAINV
Definition: stm32f745xx.h:7454
#define __HAL_UART_SEND_REQ(__HANDLE__, __REQ__)
Set a specific UART request flag.
#define IS_UART_ADVFEATURE_DATAINV(DATAINV)
void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
#define IS_UART_DRIVER_ENABLE_INSTANCE(__INSTANCE__)
Definition: stm32f745xx.h:9187
UART_AdvFeatureInitTypeDef AdvancedInit
#define UART_ADVFEATURE_AUTOBAUDRATE_ENABLE
#define USART_CR2_CLKEN
Definition: stm32f745xx.h:7446
#define HAL_UART_ERROR_NONE
#define HSI_VALUE
Internal High Speed oscillator (HSI) value. This value is used by the RCC HAL module to compute the s...
#define UART_ADVFEATURE_NO_INIT
void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
Definition: stm32f7xx.h:155
#define UART_ADVFEATURE_DMADISABLEONERROR_INIT
#define UART_MASK_COMPUTATION(__HANDLE__)
Reports the UART mask to apply to retrieve the received data according to the word length and to the ...
#define USART_ISR_REACK
Definition: stm32f745xx.h:7528
HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
#define UART_ADVFEATURE_SWAP_INIT
#define __HAL_UNLOCK(__HANDLE__)
#define IS_UART_DEASSERTIONTIME(TIME)
Check UART deassertion time.
#define IS_UART_BAUDRATE(BAUDRATE)
Check UART Baud rate.
#define UART_ADVFEATURE_RXINVERT_INIT
#define USART_CR3_OVRDIS
Definition: stm32f745xx.h:7476
#define IS_UART_HARDWARE_FLOW_CONTROL(CONTROL)
#define UART_ADVFEATURE_RXOVERRUNDISABLE_INIT
HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
__IO uint32_t TDR
Definition: stm32f745xx.h:940
#define USART_CR1_TXEIE
Definition: stm32f745xx.h:7413
__IO uint32_t BRR
Definition: stm32f745xx.h:933
__IO HAL_UART_StateTypeDef RxState
DMA_HandleTypeDef * hdmatx
#define USART_CR3_HDSEL
Definition: stm32f745xx.h:7467
#define UART_PARITY_NONE
#define IS_UART_ADVFEATURE_INIT(INIT)
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
#define USART_ISR_TC
Definition: stm32f745xx.h:7513
#define UART_CR1_DEDT_ADDRESS_LSB_POS
#define HAL_UART_TIMEOUT_VALUE
#define UART_FLAG_TXE
HAL_LockTypeDef Lock
void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
#define USART_ISR_TEACK
Definition: stm32f745xx.h:7527
__IO uint32_t RQR
Definition: stm32f745xx.h:936
DMA_HandleTypeDef * hdmarx
#define USART_CR1_TE
Definition: stm32f745xx.h:7409
#define UART_RXDATA_FLUSH_REQUEST
#define UART_FLAG_RXNE
HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
#define USART_ISR_FE
Definition: stm32f745xx.h:7508
#define USART_CR1_RXNEIE
Definition: stm32f745xx.h:7411
USART_TypeDef * Instance
#define IS_UART_LIN_BREAK_DETECT_LENGTH(LENGTH)
#define HAL_UART_ERROR_NE
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
#define __HAL_LOCK(__HANDLE__)
#define UART_CLEAR_PEF
#define IS_UART_INSTANCE(__INSTANCE__)
Definition: stm32f745xx.h:9177
#define NULL
Definition: usbd_def.h:53
#define DMA_SxCR_CIRC
Definition: stm32f745xx.h:3313
uint32_t HAL_RCC_GetPCLK1Freq(void)
HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
#define HAL_MAX_DELAY
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
#define USART_CR2_SWAP
Definition: stm32f745xx.h:7451
FlagStatus
Definition: stm32f7xx.h:152
__IO HAL_UART_StateTypeDef gState
#define IS_UART_ADDRESSLENGTH_DETECT(__ADDRESS__)
#define USART_CR2_STOP
Definition: stm32f745xx.h:7447
This file contains all the functions prototypes for the HAL module driver.
#define UART_WORDLENGTH_9B
#define HAL_UART_ERROR_ORE
#define USART_ISR_PE
Definition: stm32f745xx.h:7507
__IO uint32_t CR1
Definition: stm32f745xx.h:930
DMA_Stream_TypeDef * Instance
#define IS_UART_STOPBITS(STOPBITS)
UART_ClockSourceTypeDef
UART clock sources definition.
#define USART_CR2_MSBFIRST
Definition: stm32f745xx.h:7455
#define USART_CR2_ABREN
Definition: stm32f745xx.h:7456
#define IS_UART_ADVFEATURE_SWAP(SWAP)
uint32_t HAL_RCC_GetSysClockFreq(void)
#define IS_UART_PARITY(PARITY)
#define USART_ISR_RXNE
Definition: stm32f745xx.h:7512
#define LSE_VALUE
External Low Speed oscillator (LSE) value.
HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
#define USART_CR2_TXINV
Definition: stm32f745xx.h:7453
#define HAL_UART_ERROR_DMA
#define USART_ISR_TXE
Definition: stm32f745xx.h:7514
UART_InitTypeDef Init
#define UART_HWCONTROL_NONE
HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
#define UART_ADVFEATURE_DATAINVERT_INIT
#define IS_UART_WORD_LENGTH(__LENGTH__)
__IO uint32_t CR
Definition: stm32f745xx.h:392
#define UNUSED(x)
#define UART_ADVFEATURE_AUTOBAUDRATE_INIT
#define USART_CR3_DMAT
Definition: stm32f745xx.h:7471
#define USART_CR3_ONEBIT
Definition: stm32f745xx.h:7475
HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
#define IS_UART_MODE(MODE)
#define UART_OVERSAMPLING_8
#define UART_WAKEUPMETHOD_ADDRESSMARK
HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
#define SET_BIT(REG, BIT)
Definition: stm32f7xx.h:178
#define UART_ADVFEATURE_TXINVERT_INIT
#define USART_CR1_MME
Definition: stm32f745xx.h:7420
#define USART_CR3_DEM
Definition: stm32f745xx.h:7478
#define IS_UART_ADVFEATURE_DMAONRXERROR(DMA)
HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
#define HAL_IS_BIT_SET(REG, BIT)
uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
#define UART_FLAG_TC
#define READ_REG(REG)
Definition: stm32f7xx.h:188
#define __HAL_UART_ENABLE(__HANDLE__)
Enable UART.
#define UART_GETCLOCKSOURCE(__HANDLE__, __CLOCKSOURCE__)
Reports the UART clock source.
#define USART_ISR_ORE
Definition: stm32f745xx.h:7510
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_UART_StateTypeDef
HAL UART State structures definition.
DMA handle Structure definition.
HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
#define UART_CR2_ADDRESS_LSB_POS
__IO uint32_t RDR
Definition: stm32f745xx.h:939
#define UART_CR1_DEAT_ADDRESS_LSB_POS
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
#define USART_CR2_RXINV
Definition: stm32f745xx.h:7452
#define __HAL_UART_CLEAR_IT(__HANDLE__, __FLAG__)
Clears the specified UART ISR flag, in setting the proper ICR register flag.
#define UART_SENDBREAK_REQUEST
#define IS_UART_OVERRUN(OVERRUN)
HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
#define UART_CLEAR_FEF
#define __HAL_UART_DISABLE(__HANDLE__)
Disable UART.
#define IS_UART_ASSERTIONTIME(TIME)
Check UART assertion time.
__IO uint32_t CR2
Definition: stm32f745xx.h:931
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
#define UART_CLEAR_NEF
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
__IO uint32_t CR3
Definition: stm32f745xx.h:932
HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
__IO uint32_t ISR
Definition: stm32f745xx.h:937
#define IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(MODE)
HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
#define IS_UART_WAKEUPMETHOD(WAKEUP)
#define UART_ADVFEATURE_MSBFIRST_INIT
#define IS_UART_HWFLOW_INSTANCE(__INSTANCE__)
Definition: stm32f745xx.h:9197
#define USART_CR1_WAKE
Definition: stm32f745xx.h:7417
#define HAL_UART_ERROR_FE
#define USART_CR1_RE
Definition: stm32f745xx.h:7408
HAL_StatusTypeDef
HAL Status structures definition.
#define USART_CR3_IREN
Definition: stm32f745xx.h:7465
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
#define USART_CR3_CTSE
Definition: stm32f745xx.h:7473
#define UART_MUTE_MODE_REQUEST
#define USART_CR2_ADD
Definition: stm32f745xx.h:7461
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
#define USART_CR1_DEDT
Definition: stm32f745xx.h:7423
#define IS_UART_DE_POLARITY(POLARITY)
UART handle Structure definition.
#define IS_UART_ADVFEATURE_MSBFIRST(MSBFIRST)
uint32_t HAL_RCC_GetPCLK2Freq(void)
#define USART_CR1_UE
Definition: stm32f745xx.h:7407
#define IS_UART_ADVFEATURE_TXINV(TXINV)
#define USART_CR3_EIE
Definition: stm32f745xx.h:7464
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
#define __HAL_UART_GET_FLAG(__HANDLE__, __FLAG__)
Checks whether the specified UART flag is set or not.
#define UART_DIV_SAMPLING8(_PCLK_, _BAUD_)
BRR division operation to set BRR register in 8-bit oversampling mode.
#define USART_CR3_SCEN
Definition: stm32f745xx.h:7469
#define USART_CR3_DDRE
Definition: stm32f745xx.h:7477
#define USART_CR1_DEAT
Definition: stm32f745xx.h:7429
#define USART_ISR_NE
Definition: stm32f745xx.h:7509
#define UART_DIV_SAMPLING16(_PCLK_, _BAUD_)
BRR division operation to set BRR register in 16-bit oversampling mode.
#define USART_CR2_LINEN
Definition: stm32f745xx.h:7450
#define USART_CR3_DMAR
Definition: stm32f745xx.h:7470