comparison l476rg/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart_ex.c @ 0:32a3b1785697

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