STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal_uart_ex.c
Go to the documentation of this file.
1 
43 /* Includes ------------------------------------------------------------------*/
44 #include "stm32l4xx_hal.h"
45 
55 #ifdef HAL_UART_MODULE_ENABLED
56 
57 /* Private typedef -----------------------------------------------------------*/
58 /* Private define ------------------------------------------------------------*/
59 #if defined(USART_CR1_FIFOEN)
60 
63 /* UART RX FIFO depth */
64 #define RX_FIFO_DEPTH 8U
65 
66 /* UART TX FIFO depth */
67 #define TX_FIFO_DEPTH 8U
68 
71 #endif /* USART_CR1_FIFOEN */
72 
73 /* Private macros ------------------------------------------------------------*/
74 /* Private variables ---------------------------------------------------------*/
75 /* Private function prototypes -----------------------------------------------*/
79 static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
80 #if defined(USART_CR1_FIFOEN)
82 #endif /* USART_CR1_FIFOEN */
83 
87 /* Exported functions --------------------------------------------------------*/
88 
170 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
171  uint32_t DeassertionTime)
172 {
173  uint32_t temp;
174 
175  /* Check the UART handle allocation */
176  if (huart == NULL)
177  {
178  return HAL_ERROR;
179  }
180  /* Check the Driver Enable UART instance */
181  assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
182 
183  /* Check the Driver Enable polarity */
184  assert_param(IS_UART_DE_POLARITY(Polarity));
185 
186  /* Check the Driver Enable assertion time */
187  assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
188 
189  /* Check the Driver Enable deassertion time */
190  assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
191 
192  if (huart->gState == HAL_UART_STATE_RESET)
193  {
194  /* Allocate lock resource and initialize it */
195  huart->Lock = HAL_UNLOCKED;
196 
197 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
199 
200  if (huart->MspInitCallback == NULL)
201  {
203  }
204 
205  /* Init the low level hardware */
206  huart->MspInitCallback(huart);
207 #else
208  /* Init the low level hardware : GPIO, CLOCK, CORTEX */
209  HAL_UART_MspInit(huart);
210 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
211  }
212 
213  huart->gState = HAL_UART_STATE_BUSY;
214 
215  /* Disable the Peripheral */
216  __HAL_UART_DISABLE(huart);
217 
218  /* Set the UART Communication parameters */
219  if (UART_SetConfig(huart) == HAL_ERROR)
220  {
221  return HAL_ERROR;
222  }
223 
224  if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
225  {
226  UART_AdvFeatureConfig(huart);
227  }
228 
229  /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
230  SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
231 
232  /* Set the Driver Enable polarity */
233  MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
234 
235  /* Set the Driver Enable assertion and deassertion times */
236  temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
237  temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
238  MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
239 
240  /* Enable the Peripheral */
241  __HAL_UART_ENABLE(huart);
242 
243  /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
244  return (UART_CheckIdleState(huart));
245 }
246 
277 {
278  /* Prevent unused argument(s) compilation warning */
279  UNUSED(huart);
280 
281  /* NOTE : This function should not be modified, when the callback is needed,
282  the HAL_UARTEx_WakeupCallback can be implemented in the user file.
283  */
284 }
285 
286 #if defined(USART_CR1_FIFOEN)
287 
293 {
294  /* Prevent unused argument(s) compilation warning */
295  UNUSED(huart);
296 
297  /* NOTE : This function should not be modified, when the callback is needed,
298  the HAL_UARTEx_RxFifoFullCallback can be implemented in the user file.
299  */
300 }
301 
308 {
309  /* Prevent unused argument(s) compilation warning */
310  UNUSED(huart);
311 
312  /* NOTE : This function should not be modified, when the callback is needed,
313  the HAL_UARTEx_TxFifoEmptyCallback can be implemented in the user file.
314  */
315 }
316 #endif /* USART_CR1_FIFOEN */
317 
349 #if defined(USART_CR3_UCESM)
350 
360 {
361  /* Process Locked */
362  __HAL_LOCK(huart);
363 
364  /* Set UCESM bit */
365  SET_BIT(huart->Instance->CR3, USART_CR3_UCESM);
366 
367  /* Process Unlocked */
368  __HAL_UNLOCK(huart);
369 
370  return HAL_OK;
371 }
372 
379 {
380  /* Process Locked */
381  __HAL_LOCK(huart);
382 
383  /* Clear UCESM bit */
384  CLEAR_BIT(huart->Instance->CR3, USART_CR3_UCESM);
385 
386  /* Process Unlocked */
387  __HAL_UNLOCK(huart);
388 
389  return HAL_OK;
390 }
391 #endif /* USART_CR3_UCESM */
392 
406 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
407 {
408  /* Check the UART handle allocation */
409  if (huart == NULL)
410  {
411  return HAL_ERROR;
412  }
413 
414  /* Check the address length parameter */
415  assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
416 
417  huart->gState = HAL_UART_STATE_BUSY;
418 
419  /* Disable the Peripheral */
420  __HAL_UART_DISABLE(huart);
421 
422  /* Set the address length */
423  MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
424 
425  /* Enable the Peripheral */
426  __HAL_UART_ENABLE(huart);
427 
428  /* TEACK and/or REACK to check before moving huart->gState to Ready */
429  return (UART_CheckIdleState(huart));
430 }
431 
445 {
446  HAL_StatusTypeDef status = HAL_OK;
447  uint32_t tickstart;
448 
449  /* check the wake-up from stop mode UART instance */
450  assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
451  /* check the wake-up selection parameter */
452  assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
453 
454  /* Process Locked */
455  __HAL_LOCK(huart);
456 
457  huart->gState = HAL_UART_STATE_BUSY;
458 
459  /* Disable the Peripheral */
460  __HAL_UART_DISABLE(huart);
461 
462  /* Set the wake-up selection scheme */
463  MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
464 
465  if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
466  {
467  UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
468  }
469 
470  /* Enable the Peripheral */
471  __HAL_UART_ENABLE(huart);
472 
473  /* Init tickstart for timeout managment*/
474  tickstart = HAL_GetTick();
475 
476  /* Wait until REACK flag is set */
477  if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
478  {
479  status = HAL_TIMEOUT;
480  }
481  else
482  {
483  /* Initialize the UART State */
484  huart->gState = HAL_UART_STATE_READY;
485  }
486 
487  /* Process Unlocked */
488  __HAL_UNLOCK(huart);
489 
490  return status;
491 }
492 
500 {
501  /* Process Locked */
502  __HAL_LOCK(huart);
503 
504  /* Set UESM bit */
505  SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
506 
507  /* Process Unlocked */
508  __HAL_UNLOCK(huart);
509 
510  return HAL_OK;
511 }
512 
519 {
520  /* Process Locked */
521  __HAL_LOCK(huart);
522 
523  /* Clear UESM bit */
524  CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
525 
526  /* Process Unlocked */
527  __HAL_UNLOCK(huart);
528 
529  return HAL_OK;
530 }
531 
532 #if defined(USART_CR1_FIFOEN)
533 
539 {
540  uint32_t tmpcr1;
541 
542  /* Check parameters */
543  assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
544 
545  /* Process Locked */
546  __HAL_LOCK(huart);
547 
548  huart->gState = HAL_UART_STATE_BUSY;
549 
550  /* Save actual UART configuration */
551  tmpcr1 = READ_REG(huart->Instance->CR1);
552 
553  /* Disable UART */
554  __HAL_UART_DISABLE(huart);
555 
556  /* Enable FIFO mode */
557  SET_BIT(tmpcr1, USART_CR1_FIFOEN);
558  huart->FifoMode = UART_FIFOMODE_ENABLE;
559 
560  /* Restore UART configuration */
561  WRITE_REG(huart->Instance->CR1, tmpcr1);
562 
563  /* Determine the number of data to process during RX/TX ISR execution */
565 
566  huart->gState = HAL_UART_STATE_READY;
567 
568  /* Process Unlocked */
569  __HAL_UNLOCK(huart);
570 
571  return HAL_OK;
572 }
573 
580 {
581  uint32_t tmpcr1;
582 
583  /* Check parameters */
584  assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
585 
586  /* Process Locked */
587  __HAL_LOCK(huart);
588 
589  huart->gState = HAL_UART_STATE_BUSY;
590 
591  /* Save actual UART configuration */
592  tmpcr1 = READ_REG(huart->Instance->CR1);
593 
594  /* Disable UART */
595  __HAL_UART_DISABLE(huart);
596 
597  /* Enable FIFO mode */
598  CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
599  huart->FifoMode = UART_FIFOMODE_DISABLE;
600 
601  /* Restore UART configuration */
602  WRITE_REG(huart->Instance->CR1, tmpcr1);
603 
604  huart->gState = HAL_UART_STATE_READY;
605 
606  /* Process Unlocked */
607  __HAL_UNLOCK(huart);
608 
609  return HAL_OK;
610 }
611 
625 HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
626 {
627  uint32_t tmpcr1;
628 
629  /* Check parameters */
630  assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
631  assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold));
632 
633  /* Process Locked */
634  __HAL_LOCK(huart);
635 
636  huart->gState = HAL_UART_STATE_BUSY;
637 
638  /* Save actual UART configuration */
639  tmpcr1 = READ_REG(huart->Instance->CR1);
640 
641  /* Disable UART */
642  __HAL_UART_DISABLE(huart);
643 
644  /* Update TX threshold configuration */
645  MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
646 
647  /* Determine the number of data to process during RX/TX ISR execution */
649 
650  /* Restore UART configuration */
651  WRITE_REG(huart->Instance->CR1, tmpcr1);
652 
653  huart->gState = HAL_UART_STATE_READY;
654 
655  /* Process Unlocked */
656  __HAL_UNLOCK(huart);
657 
658  return HAL_OK;
659 }
660 
674 HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
675 {
676  uint32_t tmpcr1;
677 
678  /* Check the parameters */
679  assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
680  assert_param(IS_UART_RXFIFO_THRESHOLD(Threshold));
681 
682  /* Process Locked */
683  __HAL_LOCK(huart);
684 
685  huart->gState = HAL_UART_STATE_BUSY;
686 
687  /* Save actual UART configuration */
688  tmpcr1 = READ_REG(huart->Instance->CR1);
689 
690  /* Disable UART */
691  __HAL_UART_DISABLE(huart);
692 
693  /* Update RX threshold configuration */
694  MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
695 
696  /* Determine the number of data to process during RX/TX ISR execution */
698 
699  /* Restore UART configuration */
700  WRITE_REG(huart->Instance->CR1, tmpcr1);
701 
702  huart->gState = HAL_UART_STATE_READY;
703 
704  /* Process Unlocked */
705  __HAL_UNLOCK(huart);
706 
707  return HAL_OK;
708 }
709 #endif /* USART_CR1_FIFOEN */
710 
730 {
731  assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
732 
733  /* Set the USART address length */
734  MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
735 
736  /* Set the USART address node */
737  MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
738 }
739 
740 #if defined(USART_CR1_FIFOEN)
741 
749 {
750  uint8_t rx_fifo_depth;
751  uint8_t tx_fifo_depth;
752  uint8_t rx_fifo_threshold;
753  uint8_t tx_fifo_threshold;
754  uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U};
755  uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U};
756 
757  if (huart->FifoMode == UART_FIFOMODE_DISABLE)
758  {
759  huart->NbTxDataToProcess = 1U;
760  huart->NbRxDataToProcess = 1U;
761  }
762  else
763  {
764  rx_fifo_depth = RX_FIFO_DEPTH;
765  tx_fifo_depth = TX_FIFO_DEPTH;
766  rx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
767  tx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
768  huart->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) / (uint16_t)denominator[tx_fifo_threshold];
769  huart->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) / (uint16_t)denominator[rx_fifo_threshold];
770  }
771 }
772 #endif /* USART_CR1_FIFOEN */
773 
777 #endif /* HAL_UART_MODULE_ENABLED */
778 
787 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
Set Wakeup from Stop mode interrupt flag selection.
HAL_StatusTypeDef HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef *huart)
Keep UART Clock enabled when in Stop Mode.
UART handle Structure definition.
void HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef *huart)
UART TX Fifo empty callback.
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
Check the UART Idle State.
void HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef *huart)
UART RX Fifo full callback.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
Configure the UART peripheral advanced features.
__HAL_UNLOCK(hrtc)
static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
Initialize the UART wake-up from stop mode parameters when triggered by address detection.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
Initialize the RS485 Driver enable feature according to the specified parameters in the UART_InitType...
HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
Set the RXFIFO threshold.
__HAL_LOCK(hrtc)
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
Initialize the UART MSP.
return HAL_OK
UART wake up from stop mode parameters.
static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart)
Calculate the number of data to process in RX/TX ISR.
USART_TypeDef * Instance
HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
By default in multiprocessor mode, when the wake up method is set to address mark, the UART handles only 4-bit long addresses detection; this API allows to enable longer addresses detection (6-, 7- or 8-bit long).
HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
Set the TXFIFO threshold.
HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
Enable UART Stop Mode.
HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart)
Disable the FIFO mode.
UART_AdvFeatureInitTypeDef AdvancedInit
void(* MspInitCallback)(struct __UART_HandleTypeDef *huart)
void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
UART wakeup from Stop mode callback.
HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart)
Enable the FIFO mode.
void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
Initialize the callbacks to their default values.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
Handle UART Communication Timeout.
HAL_StatusTypeDef HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef *huart)
Disable UART Clock when in Stop Mode.
HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
Disable UART Stop Mode.
HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
Configure the UART peripheral.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
__IO HAL_UART_StateTypeDef gState