Subversion Repositories FuelGauge

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f0xx_hal_uart_ex.c
  4.   * @author  MCD Application Team
  5.   * @brief   Extended UART HAL module driver.
  6.   *          This file provides firmware functions to manage the following extended
  7.   *          functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
  8.   *           + Initialization and de-initialization functions
  9.   *           + Peripheral Control functions
  10.   *
  11.   *
  12.   @verbatim
  13.   ==============================================================================
  14.                ##### UART peripheral extended features  #####
  15.   ==============================================================================
  16.  
  17.     (#) Declare a UART_HandleTypeDef handle structure.
  18.  
  19.     (#) For the UART RS485 Driver Enable mode, initialize the UART registers
  20.         by calling the HAL_RS485Ex_Init() API.
  21.  
  22.   @endverbatim
  23.   ******************************************************************************
  24.   * @attention
  25.   *
  26.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  27.   * All rights reserved.</center></h2>
  28.   *
  29.   * This software component is licensed by ST under BSD 3-Clause license,
  30.   * the "License"; You may not use this file except in compliance with the
  31.   * License. You may obtain a copy of the License at:
  32.   *                        opensource.org/licenses/BSD-3-Clause
  33.   *
  34.   ******************************************************************************
  35.   */
  36.  
  37. /* Includes ------------------------------------------------------------------*/
  38. #include "stm32f0xx_hal.h"
  39.  
  40. /** @addtogroup STM32F0xx_HAL_Driver
  41.   * @{
  42.   */
  43.  
  44. /** @defgroup UARTEx UARTEx
  45.   * @brief UART Extended HAL module driver
  46.   * @{
  47.   */
  48.  
  49. #ifdef HAL_UART_MODULE_ENABLED
  50.  
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53.  
  54. /* Private macros ------------------------------------------------------------*/
  55. /* Private variables ---------------------------------------------------------*/
  56. /* Private function prototypes -----------------------------------------------*/
  57. /** @defgroup UARTEx_Private_Functions UARTEx Private Functions
  58.   * @{
  59.   */
  60. #if defined(USART_CR1_UESM)
  61. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
  62. #endif /* USART_CR1_UESM */
  63. /**
  64.   * @}
  65.   */
  66.  
  67. /* Exported functions --------------------------------------------------------*/
  68.  
  69. /** @defgroup UARTEx_Exported_Functions  UARTEx Exported Functions
  70.   * @{
  71.   */
  72.  
  73. /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions
  74.   * @brief    Extended Initialization and Configuration Functions
  75.   *
  76. @verbatim
  77. ===============================================================================
  78.             ##### Initialization and Configuration functions #####
  79.  ===============================================================================
  80.     [..]
  81.     This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  82.     in asynchronous mode.
  83.       (+) For the asynchronous mode the parameters below can be configured:
  84.         (++) Baud Rate
  85.         (++) Word Length
  86.         (++) Stop Bit
  87.         (++) Parity: If the parity is enabled, then the MSB bit of the data written
  88.              in the data register is transmitted but is changed by the parity bit.
  89.         (++) Hardware flow control
  90.         (++) Receiver/transmitter modes
  91.         (++) Over Sampling Method
  92.         (++) One-Bit Sampling Method
  93.       (+) For the asynchronous mode, the following advanced features can be configured as well:
  94.         (++) TX and/or RX pin level inversion
  95.         (++) data logical level inversion
  96.         (++) RX and TX pins swap
  97.         (++) RX overrun detection disabling
  98.         (++) DMA disabling on RX error
  99.         (++) MSB first on communication line
  100.         (++) auto Baud rate detection
  101.     [..]
  102.     The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration
  103.      procedures (details for the procedures are available in reference manual).
  104.  
  105. @endverbatim
  106.  
  107.   Depending on the frame length defined by the M1 and M0 bits (7-bit,
  108.   8-bit or 9-bit), the possible UART formats are listed in the
  109.   following table.
  110.  
  111.     Table 1. UART frame format.
  112.     +-----------------------------------------------------------------------+
  113.     |  M1 bit |  M0 bit |  PCE bit  |             UART frame                |
  114.     |---------|---------|-----------|---------------------------------------|
  115.     |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
  116.     |---------|---------|-----------|---------------------------------------|
  117.     |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
  118.     |---------|---------|-----------|---------------------------------------|
  119.     |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
  120.     |---------|---------|-----------|---------------------------------------|
  121.     |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
  122.     |---------|---------|-----------|---------------------------------------|
  123.     |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
  124.     |---------|---------|-----------|---------------------------------------|
  125.     |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
  126.     +-----------------------------------------------------------------------+
  127.  
  128.   * @{
  129.   */
  130.  
  131. /**
  132.   * @brief Initialize the RS485 Driver enable feature according to the specified
  133.   *         parameters in the UART_InitTypeDef and creates the associated handle.
  134.   * @param huart            UART handle.
  135.   * @param Polarity         Select the driver enable polarity.
  136.   *          This parameter can be one of the following values:
  137.   *          @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
  138.   *          @arg @ref UART_DE_POLARITY_LOW  DE signal is active low
  139.   * @param AssertionTime    Driver Enable assertion time:
  140.   *       5-bit value defining the time between the activation of the DE (Driver Enable)
  141.   *       signal and the beginning of the start bit. It is expressed in sample time
  142.   *       units (1/8 or 1/16 bit time, depending on the oversampling rate)
  143.   * @param DeassertionTime  Driver Enable deassertion time:
  144.   *       5-bit value defining the time between the end of the last stop bit, in a
  145.   *       transmitted message, and the de-activation of the DE (Driver Enable) signal.
  146.   *       It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
  147.   *       oversampling rate).
  148.   * @retval HAL status
  149.   */
  150. HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
  151.                                    uint32_t DeassertionTime)
  152. {
  153.   uint32_t temp;
  154.  
  155.   /* Check the UART handle allocation */
  156.   if (huart == NULL)
  157.   {
  158.     return HAL_ERROR;
  159.   }
  160.   /* Check the Driver Enable UART instance */
  161.   assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
  162.  
  163.   /* Check the Driver Enable polarity */
  164.   assert_param(IS_UART_DE_POLARITY(Polarity));
  165.  
  166.   /* Check the Driver Enable assertion time */
  167.   assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
  168.  
  169.   /* Check the Driver Enable deassertion time */
  170.   assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
  171.  
  172.   if (huart->gState == HAL_UART_STATE_RESET)
  173.   {
  174.     /* Allocate lock resource and initialize it */
  175.     huart->Lock = HAL_UNLOCKED;
  176.  
  177. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  178.     UART_InitCallbacksToDefault(huart);
  179.  
  180.     if (huart->MspInitCallback == NULL)
  181.     {
  182.       huart->MspInitCallback = HAL_UART_MspInit;
  183.     }
  184.  
  185.     /* Init the low level hardware */
  186.     huart->MspInitCallback(huart);
  187. #else
  188.     /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  189.     HAL_UART_MspInit(huart);
  190. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  191.   }
  192.  
  193.   huart->gState = HAL_UART_STATE_BUSY;
  194.  
  195.   /* Disable the Peripheral */
  196.   __HAL_UART_DISABLE(huart);
  197.  
  198.   /* Set the UART Communication parameters */
  199.   if (UART_SetConfig(huart) == HAL_ERROR)
  200.   {
  201.     return HAL_ERROR;
  202.   }
  203.  
  204.   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  205.   {
  206.     UART_AdvFeatureConfig(huart);
  207.   }
  208.  
  209.   /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
  210.   SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
  211.  
  212.   /* Set the Driver Enable polarity */
  213.   MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
  214.  
  215.   /* Set the Driver Enable assertion and deassertion times */
  216.   temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
  217.   temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
  218.   MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
  219.  
  220.   /* Enable the Peripheral */
  221.   __HAL_UART_ENABLE(huart);
  222.  
  223.   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  224.   return (UART_CheckIdleState(huart));
  225. }
  226.  
  227. /**
  228.   * @}
  229.   */
  230.  
  231. /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions
  232.   *  @brief Extended functions
  233.   *
  234. @verbatim
  235.  ===============================================================================
  236.                       ##### IO operation functions #####
  237.  ===============================================================================
  238.     This subsection provides a set of Wakeup and FIFO mode related callback functions.
  239.  
  240. #if defined(USART_CR1_UESM)
  241.     (#) Wakeup from Stop mode Callback:
  242.         (+) HAL_UARTEx_WakeupCallback()
  243.  
  244. #endif
  245. @endverbatim
  246.   * @{
  247.   */
  248.  
  249. #if defined(USART_CR1_UESM)
  250. /**
  251.   * @brief UART wakeup from Stop mode callback.
  252.   * @param huart UART handle.
  253.   * @retval None
  254.   */
  255. __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
  256. {
  257.   /* Prevent unused argument(s) compilation warning */
  258.   UNUSED(huart);
  259.  
  260.   /* NOTE : This function should not be modified, when the callback is needed,
  261.             the HAL_UARTEx_WakeupCallback can be implemented in the user file.
  262.    */
  263. }
  264.  
  265. #endif /* USART_CR1_UESM */
  266.  
  267. /**
  268.   * @}
  269.   */
  270.  
  271. /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
  272.   * @brief    Extended Peripheral Control functions
  273.   *
  274. @verbatim
  275.  ===============================================================================
  276.                       ##### Peripheral Control functions #####
  277.  ===============================================================================
  278.     [..] This section provides the following functions:
  279.      (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
  280.          detection length to more than 4 bits for multiprocessor address mark wake up.
  281. #if defined(USART_CR1_UESM)
  282.      (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode
  283.          trigger: address match, Start Bit detection or RXNE bit status.
  284.      (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
  285.      (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
  286. #endif
  287.  
  288. @endverbatim
  289.   * @{
  290.   */
  291.  
  292. /**
  293.   * @brief By default in multiprocessor mode, when the wake up method is set
  294.   *        to address mark, the UART handles only 4-bit long addresses detection;
  295.   *        this API allows to enable longer addresses detection (6-, 7- or 8-bit
  296.   *        long).
  297.   * @note  Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
  298.   *        7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
  299.   * @param huart         UART handle.
  300.   * @param AddressLength This parameter can be one of the following values:
  301.   *          @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
  302.   *          @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
  303.   * @retval HAL status
  304.   */
  305. HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
  306. {
  307.   /* Check the UART handle allocation */
  308.   if (huart == NULL)
  309.   {
  310.     return HAL_ERROR;
  311.   }
  312.  
  313.   /* Check the address length parameter */
  314.   assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
  315.  
  316.   huart->gState = HAL_UART_STATE_BUSY;
  317.  
  318.   /* Disable the Peripheral */
  319.   __HAL_UART_DISABLE(huart);
  320.  
  321.   /* Set the address length */
  322.   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
  323.  
  324.   /* Enable the Peripheral */
  325.   __HAL_UART_ENABLE(huart);
  326.  
  327.   /* TEACK and/or REACK to check before moving huart->gState to Ready */
  328.   return (UART_CheckIdleState(huart));
  329. }
  330.  
  331. #if defined(USART_CR1_UESM)
  332. /**
  333.   * @brief Set Wakeup from Stop mode interrupt flag selection.
  334.   * @note It is the application responsibility to enable the interrupt used as
  335.   *       usart_wkup interrupt source before entering low-power mode.
  336.   * @param huart           UART handle.
  337.   * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
  338.   *          This parameter can be one of the following values:
  339.   *          @arg @ref UART_WAKEUP_ON_ADDRESS
  340.   *          @arg @ref UART_WAKEUP_ON_STARTBIT
  341.   *          @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
  342.   * @retval HAL status
  343.   */
  344. HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  345. {
  346.   HAL_StatusTypeDef status = HAL_OK;
  347.   uint32_t tickstart;
  348.  
  349.   /* check the wake-up from stop mode UART instance */
  350.   assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
  351.   /* check the wake-up selection parameter */
  352.   assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
  353.  
  354.   /* Process Locked */
  355.   __HAL_LOCK(huart);
  356.  
  357.   huart->gState = HAL_UART_STATE_BUSY;
  358.  
  359.   /* Disable the Peripheral */
  360.   __HAL_UART_DISABLE(huart);
  361.  
  362.   /* Set the wake-up selection scheme */
  363.   MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
  364.  
  365.   if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
  366.   {
  367.     UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
  368.   }
  369.  
  370.   /* Enable the Peripheral */
  371.   __HAL_UART_ENABLE(huart);
  372.  
  373.   /* Init tickstart for timeout managment*/
  374.   tickstart = HAL_GetTick();
  375.  
  376.   /* Wait until REACK flag is set */
  377.   if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  378.   {
  379.     status = HAL_TIMEOUT;
  380.   }
  381.   else
  382.   {
  383.     /* Initialize the UART State */
  384.     huart->gState = HAL_UART_STATE_READY;
  385.   }
  386.  
  387.   /* Process Unlocked */
  388.   __HAL_UNLOCK(huart);
  389.  
  390.   return status;
  391. }
  392.  
  393. /**
  394.   * @brief Enable UART Stop Mode.
  395.   * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
  396.   * @param huart UART handle.
  397.   * @retval HAL status
  398.   */
  399. HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
  400. {
  401.   /* Process Locked */
  402.   __HAL_LOCK(huart);
  403.  
  404.   /* Set UESM bit */
  405.   SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
  406.  
  407.   /* Process Unlocked */
  408.   __HAL_UNLOCK(huart);
  409.  
  410.   return HAL_OK;
  411. }
  412.  
  413. /**
  414.   * @brief Disable UART Stop Mode.
  415.   * @param huart UART handle.
  416.   * @retval HAL status
  417.   */
  418. HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
  419. {
  420.   /* Process Locked */
  421.   __HAL_LOCK(huart);
  422.  
  423.   /* Clear UESM bit */
  424.   CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
  425.  
  426.   /* Process Unlocked */
  427.   __HAL_UNLOCK(huart);
  428.  
  429.   return HAL_OK;
  430. }
  431.  
  432. #endif /* USART_CR1_UESM */
  433. /**
  434.   * @}
  435.   */
  436.  
  437. /**
  438.   * @}
  439.   */
  440.  
  441. /** @addtogroup UARTEx_Private_Functions
  442.   * @{
  443.   */
  444. #if defined(USART_CR1_UESM)
  445.  
  446. /**
  447.   * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
  448.   * @param huart           UART handle.
  449.   * @param WakeUpSelection UART wake up from stop mode parameters.
  450.   * @retval None
  451.   */
  452. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  453. {
  454.   assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
  455.  
  456.   /* Set the USART address length */
  457.   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
  458.  
  459.   /* Set the USART address node */
  460.   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
  461. }
  462. #endif /* USART_CR1_UESM */
  463.  
  464. /**
  465.   * @}
  466.   */
  467.  
  468. #endif /* HAL_UART_MODULE_ENABLED */
  469.  
  470. /**
  471.   * @}
  472.   */
  473.  
  474. /**
  475.   * @}
  476.   */
  477.  
  478. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  479.