]> git.leonardobizzoni.com Git - pioneer-stm32/blob
415fb56bbeabcc682fd0450a0e3ce5e033885b5e
[pioneer-stm32] /
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_uart_ex.c\r
4   * @author  MCD Application Team\r
5   * @brief   Extended UART HAL module driver.\r
6   *          This file provides firmware functions to manage the following extended\r
7   *          functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).\r
8   *           + Initialization and de-initialization functions\r
9   *           + Peripheral Control functions\r
10   *\r
11   *\r
12   @verbatim\r
13   ==============================================================================\r
14                ##### UART peripheral extended features  #####\r
15   ==============================================================================\r
16 \r
17     (#) Declare a UART_HandleTypeDef handle structure.\r
18 \r
19     (#) For the UART RS485 Driver Enable mode, initialize the UART registers\r
20         by calling the HAL_RS485Ex_Init() API.\r
21 \r
22   @endverbatim\r
23   ******************************************************************************\r
24   * @attention\r
25   *\r
26   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.\r
27   * All rights reserved.</center></h2>\r
28   *\r
29   * This software component is licensed by ST under BSD 3-Clause license,\r
30   * the "License"; You may not use this file except in compliance with the\r
31   * License. You may obtain a copy of the License at:\r
32   *                        opensource.org/licenses/BSD-3-Clause\r
33   *\r
34   ******************************************************************************\r
35   */\r
36 \r
37 /* Includes ------------------------------------------------------------------*/\r
38 #include "stm32f7xx_hal.h"\r
39 \r
40 /** @addtogroup STM32F7xx_HAL_Driver\r
41   * @{\r
42   */\r
43 \r
44 /** @defgroup UARTEx UARTEx\r
45   * @brief UART Extended HAL module driver\r
46   * @{\r
47   */\r
48 \r
49 #ifdef HAL_UART_MODULE_ENABLED\r
50 \r
51 /* Private typedef -----------------------------------------------------------*/\r
52 /* Private define ------------------------------------------------------------*/\r
53 \r
54 /* Private macros ------------------------------------------------------------*/\r
55 /* Private variables ---------------------------------------------------------*/\r
56 /* Private function prototypes -----------------------------------------------*/\r
57 /** @defgroup UARTEx_Private_Functions UARTEx Private Functions\r
58   * @{\r
59   */\r
60 /**\r
61   * @}\r
62   */\r
63 \r
64 /* Exported functions --------------------------------------------------------*/\r
65 \r
66 /** @defgroup UARTEx_Exported_Functions  UARTEx Exported Functions\r
67   * @{\r
68   */\r
69 \r
70 /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions\r
71   * @brief    Extended Initialization and Configuration Functions\r
72   *\r
73 @verbatim\r
74 ===============================================================================\r
75             ##### Initialization and Configuration functions #####\r
76  ===============================================================================\r
77     [..]\r
78     This subsection provides a set of functions allowing to initialize the USARTx or the UARTy\r
79     in asynchronous mode.\r
80       (+) For the asynchronous mode the parameters below can be configured:\r
81         (++) Baud Rate\r
82         (++) Word Length\r
83         (++) Stop Bit\r
84         (++) Parity: If the parity is enabled, then the MSB bit of the data written\r
85              in the data register is transmitted but is changed by the parity bit.\r
86         (++) Hardware flow control\r
87         (++) Receiver/transmitter modes\r
88         (++) Over Sampling Method\r
89         (++) One-Bit Sampling Method\r
90       (+) For the asynchronous mode, the following advanced features can be configured as well:\r
91         (++) TX and/or RX pin level inversion\r
92         (++) data logical level inversion\r
93         (++) RX and TX pins swap\r
94         (++) RX overrun detection disabling\r
95         (++) DMA disabling on RX error\r
96         (++) MSB first on communication line\r
97         (++) auto Baud rate detection\r
98     [..]\r
99     The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration\r
100      procedures (details for the procedures are available in reference manual).\r
101 \r
102 @endverbatim\r
103 \r
104   Depending on the frame length defined by the M1 and M0 bits (7-bit,\r
105   8-bit or 9-bit), the possible UART formats are listed in the\r
106   following table.\r
107 \r
108     Table 1. UART frame format.\r
109     +-----------------------------------------------------------------------+\r
110     |  M1 bit |  M0 bit |  PCE bit  |             UART frame                |\r
111     |---------|---------|-----------|---------------------------------------|\r
112     |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |\r
113     |---------|---------|-----------|---------------------------------------|\r
114     |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |\r
115     |---------|---------|-----------|---------------------------------------|\r
116     |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |\r
117     |---------|---------|-----------|---------------------------------------|\r
118     |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |\r
119     |---------|---------|-----------|---------------------------------------|\r
120     |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |\r
121     |---------|---------|-----------|---------------------------------------|\r
122     |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |\r
123     +-----------------------------------------------------------------------+\r
124 \r
125   * @{\r
126   */\r
127 \r
128 /**\r
129   * @brief Initialize the RS485 Driver enable feature according to the specified\r
130   *         parameters in the UART_InitTypeDef and creates the associated handle.\r
131   * @param huart            UART handle.\r
132   * @param Polarity         Select the driver enable polarity.\r
133   *          This parameter can be one of the following values:\r
134   *          @arg @ref UART_DE_POLARITY_HIGH DE signal is active high\r
135   *          @arg @ref UART_DE_POLARITY_LOW  DE signal is active low\r
136   * @param AssertionTime    Driver Enable assertion time:\r
137   *       5-bit value defining the time between the activation of the DE (Driver Enable)\r
138   *       signal and the beginning of the start bit. It is expressed in sample time\r
139   *       units (1/8 or 1/16 bit time, depending on the oversampling rate)\r
140   * @param DeassertionTime  Driver Enable deassertion time:\r
141   *       5-bit value defining the time between the end of the last stop bit, in a\r
142   *       transmitted message, and the de-activation of the DE (Driver Enable) signal.\r
143   *       It is expressed in sample time units (1/8 or 1/16 bit time, depending on the\r
144   *       oversampling rate).\r
145   * @retval HAL status\r
146   */\r
147 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)\r
148 {\r
149   uint32_t temp;\r
150 \r
151   /* Check the UART handle allocation */\r
152   if (huart == NULL)\r
153   {\r
154     return HAL_ERROR;\r
155   }\r
156   /* Check the Driver Enable UART instance */\r
157   assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));\r
158 \r
159   /* Check the Driver Enable polarity */\r
160   assert_param(IS_UART_DE_POLARITY(Polarity));\r
161 \r
162   /* Check the Driver Enable assertion time */\r
163   assert_param(IS_UART_ASSERTIONTIME(AssertionTime));\r
164 \r
165   /* Check the Driver Enable deassertion time */\r
166   assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));\r
167 \r
168   if (huart->gState == HAL_UART_STATE_RESET)\r
169   {\r
170     /* Allocate lock resource and initialize it */\r
171     huart->Lock = HAL_UNLOCKED;\r
172 \r
173 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)\r
174     UART_InitCallbacksToDefault(huart);\r
175 \r
176     if (huart->MspInitCallback == NULL)\r
177     {\r
178       huart->MspInitCallback = HAL_UART_MspInit;\r
179     }\r
180 \r
181     /* Init the low level hardware */\r
182     huart->MspInitCallback(huart);\r
183 #else\r
184     /* Init the low level hardware : GPIO, CLOCK, CORTEX */\r
185     HAL_UART_MspInit(huart);\r
186 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */\r
187   }\r
188 \r
189   huart->gState = HAL_UART_STATE_BUSY;\r
190 \r
191   /* Disable the Peripheral */\r
192   __HAL_UART_DISABLE(huart);\r
193 \r
194   /* Set the UART Communication parameters */\r
195   if (UART_SetConfig(huart) == HAL_ERROR)\r
196   {\r
197     return HAL_ERROR;\r
198   }\r
199 \r
200   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)\r
201   {\r
202     UART_AdvFeatureConfig(huart);\r
203   }\r
204 \r
205   /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */\r
206   SET_BIT(huart->Instance->CR3, USART_CR3_DEM);\r
207 \r
208   /* Set the Driver Enable polarity */\r
209   MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);\r
210 \r
211   /* Set the Driver Enable assertion and deassertion times */\r
212   temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);\r
213   temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);\r
214   MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);\r
215 \r
216   /* Enable the Peripheral */\r
217   __HAL_UART_ENABLE(huart);\r
218 \r
219   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */\r
220   return (UART_CheckIdleState(huart));\r
221 }\r
222 \r
223 /**\r
224   * @}\r
225   */\r
226 \r
227 /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions\r
228   *  @brief Extended functions\r
229   *\r
230 @verbatim\r
231  ===============================================================================\r
232                       ##### IO operation functions #####\r
233  ===============================================================================\r
234     This subsection provides a set of Wakeup and FIFO mode related callback functions.\r
235 \r
236 @endverbatim\r
237   * @{\r
238   */\r
239 \r
240 \r
241 /**\r
242   * @}\r
243   */\r
244 \r
245 /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions\r
246   * @brief    Extended Peripheral Control functions\r
247  *\r
248 @verbatim\r
249  ===============================================================================\r
250                       ##### Peripheral Control functions #####\r
251  ===============================================================================\r
252     [..] This section provides the following functions:\r
253      (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address\r
254          detection length to more than 4 bits for multiprocessor address mark wake up.\r
255 \r
256 @endverbatim\r
257   * @{\r
258   */\r
259 \r
260 \r
261 \r
262 \r
263 /**\r
264   * @brief By default in multiprocessor mode, when the wake up method is set\r
265   *        to address mark, the UART handles only 4-bit long addresses detection;\r
266   *        this API allows to enable longer addresses detection (6-, 7- or 8-bit\r
267   *        long).\r
268   * @note  Addresses detection lengths are: 6-bit address detection in 7-bit data mode,\r
269   *        7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.\r
270   * @param huart         UART handle.\r
271   * @param AddressLength This parameter can be one of the following values:\r
272   *          @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address\r
273   *          @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address\r
274   * @retval HAL status\r
275   */\r
276 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)\r
277 {\r
278   /* Check the UART handle allocation */\r
279   if (huart == NULL)\r
280   {\r
281     return HAL_ERROR;\r
282   }\r
283 \r
284   /* Check the address length parameter */\r
285   assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));\r
286 \r
287   huart->gState = HAL_UART_STATE_BUSY;\r
288 \r
289   /* Disable the Peripheral */\r
290   __HAL_UART_DISABLE(huart);\r
291 \r
292   /* Set the address length */\r
293   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);\r
294 \r
295   /* Enable the Peripheral */\r
296   __HAL_UART_ENABLE(huart);\r
297 \r
298   /* TEACK and/or REACK to check before moving huart->gState to Ready */\r
299   return (UART_CheckIdleState(huart));\r
300 }\r
301 \r
302 \r
303 \r
304 /**\r
305   * @}\r
306   */\r
307 \r
308 /**\r
309   * @}\r
310   */\r
311 \r
312 /** @addtogroup UARTEx_Private_Functions\r
313   * @{\r
314   */\r
315 \r
316 /**\r
317   * @}\r
318   */\r
319 \r
320 #endif /* HAL_UART_MODULE_ENABLED */\r
321 \r
322 /**\r
323   * @}\r
324   */\r
325 \r
326 /**\r
327   * @}\r
328   */\r
329 \r
330 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r