]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F1/stm32f1xx_hal_uart.h
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F1 / stm32f1xx_hal_uart.h
1 /**
2   ******************************************************************************
3   * @file    stm32f1xx_hal_uart.h
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    15-December-2014
7   * @brief   Header file of UART HAL module.
8   ******************************************************************************
9   * @attention
10   *
11   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
12   *
13   * Redistribution and use in source and binary forms, with or without modification,
14   * are permitted provided that the following conditions are met:
15   *   1. Redistributions of source code must retain the above copyright notice,
16   *      this list of conditions and the following disclaimer.
17   *   2. Redistributions in binary form must reproduce the above copyright notice,
18   *      this list of conditions and the following disclaimer in the documentation
19   *      and/or other materials provided with the distribution.
20   *   3. Neither the name of STMicroelectronics nor the names of its contributors
21   *      may be used to endorse or promote products derived from this software
22   *      without specific prior written permission.
23   *
24   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34   *
35   ******************************************************************************
36   */ 
37
38 /* Define to prevent recursive inclusion -------------------------------------*/
39 #ifndef __STM32F1xx_HAL_UART_H
40 #define __STM32F1xx_HAL_UART_H
41
42 #ifdef __cplusplus
43  extern "C" {
44 #endif
45
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32f1xx_hal_def.h"
48
49 /** @addtogroup STM32F1xx_HAL_Driver
50   * @{
51   */
52
53 /** @addtogroup UART
54   * @{
55   */ 
56
57 /* Exported types ------------------------------------------------------------*/ 
58 /** @defgroup UART_Exported_Types UART Exported Types
59   * @{
60   */ 
61
62
63 /** 
64   * @brief UART Init Structure definition
65   */ 
66 typedef struct
67 {
68   uint32_t BaudRate;                  /*!< This member configures the UART communication baud rate.
69                                            The baud rate is computed using the following formula:
70                                            - IntegerDivider = ((PCLKx) / (16 * (huart->Init.BaudRate)))
71                                            - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 16) + 0.5 */
72
73   uint32_t WordLength;                /*!< Specifies the number of data bits transmitted or received in a frame.
74                                            This parameter can be a value of @ref UART_Word_Length */
75
76   uint32_t StopBits;                  /*!< Specifies the number of stop bits transmitted.
77                                            This parameter can be a value of @ref UART_Stop_Bits */
78
79   uint32_t Parity;                    /*!< Specifies the parity mode.
80                                            This parameter can be a value of @ref UART_Parity
81                                            @note When parity is enabled, the computed parity is inserted
82                                                  at the MSB position of the transmitted data (9th bit when
83                                                  the word length is set to 9 data bits; 8th bit when the
84                                                  word length is set to 8 data bits). */
85  
86   uint32_t Mode;                      /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
87                                            This parameter can be a value of @ref UART_Mode */
88
89   uint32_t HwFlowCtl;                 /*!< Specifies wether the hardware flow control mode is enabled
90                                            or disabled.
91                                            This parameter can be a value of @ref UART_Hardware_Flow_Control */
92   
93   uint32_t OverSampling;              /*!< Specifies whether the Over sampling 8 is enabled or disabled, to achieve higher speed (up to fPCLK/8).
94                                            This parameter can be a value of @ref UART_Over_Sampling. This feature is not available 
95                                            on STM32F1xx family, so OverSampling parameter should always be set to 16. */ 
96 }UART_InitTypeDef;
97
98 /** 
99   * @brief HAL UART State structures definition  
100   */ 
101 typedef enum
102 {
103   HAL_UART_STATE_RESET             = 0x00,    /*!< Peripheral is not initialized                      */
104   HAL_UART_STATE_READY             = 0x01,    /*!< Peripheral Initialized and ready for use           */
105   HAL_UART_STATE_BUSY              = 0x02,    /*!< an internal process is ongoing                     */
106   HAL_UART_STATE_BUSY_TX           = 0x12,    /*!< Data Transmission process is ongoing               */
107   HAL_UART_STATE_BUSY_RX           = 0x22,    /*!< Data Reception process is ongoing                  */
108   HAL_UART_STATE_BUSY_TX_RX        = 0x32,    /*!< Data Transmission and Reception process is ongoing */
109   HAL_UART_STATE_TIMEOUT           = 0x03,    /*!< Timeout state                                      */
110   HAL_UART_STATE_ERROR             = 0x04     /*!< Error                                              */
111 }HAL_UART_StateTypeDef;
112
113
114 /** 
115   * @brief  UART handle Structure definition  
116   */  
117 typedef struct
118 {
119   USART_TypeDef                 *Instance;        /*!< UART registers base address        */
120
121   UART_InitTypeDef              Init;             /*!< UART communication parameters      */
122
123   uint8_t                       *pTxBuffPtr;      /*!< Pointer to UART Tx transfer Buffer */
124
125   uint16_t                      TxXferSize;       /*!< UART Tx Transfer size              */
126
127   uint16_t                      TxXferCount;      /*!< UART Tx Transfer Counter           */
128
129   uint8_t                       *pRxBuffPtr;      /*!< Pointer to UART Rx transfer Buffer */
130
131   uint16_t                      RxXferSize;       /*!< UART Rx Transfer size              */
132
133   uint16_t                      RxXferCount;      /*!< UART Rx Transfer Counter           */  
134
135   DMA_HandleTypeDef             *hdmatx;          /*!< UART Tx DMA Handle parameters      */
136
137   DMA_HandleTypeDef             *hdmarx;          /*!< UART Rx DMA Handle parameters      */
138
139   HAL_LockTypeDef               Lock;             /*!< Locking object                     */
140
141   __IO HAL_UART_StateTypeDef    State;            /*!< UART communication state           */
142   
143   __IO uint32_t                 ErrorCode;        /*!< UART Error code                    */
144
145 }UART_HandleTypeDef;
146
147 /**
148   * @}
149   */
150
151 /* Exported constants --------------------------------------------------------*/
152 /** @defgroup UART_Exported_Constants UART Exported constants
153   * @{
154   */
155
156 /** @defgroup UART_Error_Codes   UART Error Codes
157   * @{
158   */
159
160 #define HAL_UART_ERROR_NONE      ((uint32_t)0x00)    /*!< No error            */
161 #define HAL_UART_ERROR_PE        ((uint32_t)0x01)    /*!< Parity error        */
162 #define HAL_UART_ERROR_NE        ((uint32_t)0x02)    /*!< Noise error         */
163 #define HAL_UART_ERROR_FE        ((uint32_t)0x04)    /*!< frame error         */
164 #define HAL_UART_ERROR_ORE       ((uint32_t)0x08)    /*!< Overrun error       */
165 #define HAL_UART_ERROR_DMA       ((uint32_t)0x10)    /*!< DMA transfer error  */
166   
167 /**
168   * @}
169   */
170
171
172
173
174 /** @defgroup UART_Word_Length   UART Word Length
175   * @{
176   */
177 #define UART_WORDLENGTH_8B                  ((uint32_t)0x00000000)
178 #define UART_WORDLENGTH_9B                  ((uint32_t)USART_CR1_M)
179 /**
180   * @}
181   */
182
183 /** @defgroup UART_Stop_Bits   UART Number of Stop Bits
184   * @{
185   */
186 #define UART_STOPBITS_1                     ((uint32_t)0x00000000)
187 #define UART_STOPBITS_2                     ((uint32_t)USART_CR2_STOP_1)
188 /**
189   * @}
190   */ 
191
192 /** @defgroup UART_Parity  UART Parity
193   * @{
194   */ 
195 #define UART_PARITY_NONE                    ((uint32_t)0x00000000)
196 #define UART_PARITY_EVEN                    ((uint32_t)USART_CR1_PCE)
197 #define UART_PARITY_ODD                     ((uint32_t)(USART_CR1_PCE | USART_CR1_PS)) 
198 /**
199   * @}
200   */ 
201
202 /** @defgroup UART_Hardware_Flow_Control UART Hardware Flow Control
203   * @{
204   */ 
205 #define UART_HWCONTROL_NONE                  ((uint32_t)0x00000000)
206 #define UART_HWCONTROL_RTS                   ((uint32_t)USART_CR3_RTSE)
207 #define UART_HWCONTROL_CTS                   ((uint32_t)USART_CR3_CTSE)
208 #define UART_HWCONTROL_RTS_CTS               ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
209 /**
210   * @}
211   */
212
213 /** @defgroup UART_Mode UART Transfer Mode
214   * @{
215   */ 
216 #define UART_MODE_RX                        ((uint32_t)USART_CR1_RE)
217 #define UART_MODE_TX                        ((uint32_t)USART_CR1_TE)
218 #define UART_MODE_TX_RX                     ((uint32_t)(USART_CR1_TE |USART_CR1_RE))
219
220 /**
221   * @}
222   */
223     
224  /** @defgroup UART_State  UART State
225   * @{
226   */ 
227 #define UART_STATE_DISABLE                  ((uint32_t)0x00000000)
228 #define UART_STATE_ENABLE                   ((uint32_t)USART_CR1_UE)
229 /**
230   * @}
231   */
232
233 /** @defgroup UART_Over_Sampling UART Over Sampling
234   * @{
235   */
236 #define UART_OVERSAMPLING_16                    ((uint32_t)0x00000000)
237 /**
238   * @}
239   */
240
241 /** @defgroup UART_LIN_Break_Detection_Length  UART LIN Break Detection Length
242   * @{
243   */  
244 #define UART_LINBREAKDETECTLENGTH_10B      ((uint32_t)0x00000000)
245 #define UART_LINBREAKDETECTLENGTH_11B      ((uint32_t)USART_CR2_LBDL)
246 /**
247   * @}
248   */
249
250 /** @defgroup UART_WakeUp_functions UART Wakeup Functions
251   * @{
252   */
253 #define UART_WAKEUPMETHOD_IDLELINE                ((uint32_t)0x00000000)
254 #define UART_WAKEUPMETHOD_ADDRESSMARK             ((uint32_t)USART_CR1_WAKE)
255 /**
256   * @}
257   */
258
259 /** @defgroup UART_Flags   UART FLags
260   *        Elements values convention: 0xXXXX
261   *           - 0xXXXX  : Flag mask in the SR register
262   * @{
263   */
264 #define UART_FLAG_CTS                       ((uint32_t)USART_SR_CTS)
265 #define UART_FLAG_LBD                       ((uint32_t)USART_SR_LBD)
266 #define UART_FLAG_TXE                       ((uint32_t)USART_SR_TXE)
267 #define UART_FLAG_TC                        ((uint32_t)USART_SR_TC)
268 #define UART_FLAG_RXNE                      ((uint32_t)USART_SR_RXNE)
269 #define UART_FLAG_IDLE                      ((uint32_t)USART_SR_IDLE)
270 #define UART_FLAG_ORE                       ((uint32_t)USART_SR_ORE)
271 #define UART_FLAG_NE                        ((uint32_t)USART_SR_NE)
272 #define UART_FLAG_FE                        ((uint32_t)USART_SR_FE)
273 #define UART_FLAG_PE                        ((uint32_t)USART_SR_PE)
274 /**
275   * @}
276   */
277
278 /** @defgroup UART_Interrupt_definition  UART Interrupt Definitions
279   *        Elements values convention: 0xY000XXXX
280   *           - XXXX  : Interrupt mask (16 bits) in the Y register
281   *           - Y  : Interrupt source register (2bits)
282   *                 - 0001: CR1 register
283   *                 - 0010: CR2 register
284   *                 - 0011: CR3 register
285   *
286   * @{
287   */ 
288
289 #define UART_IT_PE                       ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_PEIE))
290 #define UART_IT_TXE                      ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_TXEIE))
291 #define UART_IT_TC                       ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_TCIE))
292 #define UART_IT_RXNE                     ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_RXNEIE))
293 #define UART_IT_IDLE                     ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_IDLEIE))
294
295 #define UART_IT_LBD                      ((uint32_t)(UART_CR2_REG_INDEX << 28 | USART_CR2_LBDIE))
296
297 #define UART_IT_CTS                      ((uint32_t)(UART_CR3_REG_INDEX << 28 | USART_CR3_CTSIE))
298 #define UART_IT_ERR                      ((uint32_t)(UART_CR3_REG_INDEX << 28 | USART_CR3_EIE))
299
300 /**
301   * @}
302   */
303
304 /**
305   * @}
306   */
307
308     
309 /* Exported macro ------------------------------------------------------------*/
310 /** @defgroup UART_Exported_Macros UART Exported Macros
311   * @{
312   */
313
314
315 /** @brief Reset UART handle state
316   * @param  __HANDLE__: specifies the UART Handle.
317   *         UART Handle selects the USARTx or UARTy peripheral 
318   *         (USART,UART availability and x,y values depending on device).
319   * @retval None
320   */
321 #define __HAL_UART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_UART_STATE_RESET)
322
323 /** @brief  Flush the UART DR register 
324   * @param  __HANDLE__: specifies the UART Handle.
325   *         UART Handle selects the USARTx or UARTy peripheral 
326   *         (USART,UART availability and x,y values depending on device).
327   */
328 #define __HAL_UART_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
329
330 /** @brief  Check whether the specified UART flag is set or not.
331   * @param  __HANDLE__: specifies the UART Handle.
332   *         UART Handle selects the USARTx or UARTy peripheral 
333   *         (USART,UART availability and x,y values depending on device).
334   * @param  __FLAG__: specifies the flag to check.
335   *        This parameter can be one of the following values:
336   *            @arg UART_FLAG_CTS:  CTS Change flag (not available for UART4 and UART5)
337   *            @arg UART_FLAG_LBD:  LIN Break detection flag
338   *            @arg UART_FLAG_TXE:  Transmit data register empty flag
339   *            @arg UART_FLAG_TC:   Transmission Complete flag
340   *            @arg UART_FLAG_RXNE: Receive data register not empty flag
341   *            @arg UART_FLAG_IDLE: Idle Line detection flag
342   *            @arg UART_FLAG_ORE:  OverRun Error flag
343   *            @arg UART_FLAG_NE:   Noise Error flag
344   *            @arg UART_FLAG_FE:   Framing Error flag
345   *            @arg UART_FLAG_PE:   Parity Error flag
346   * @retval The new state of __FLAG__ (TRUE or FALSE).
347   */
348 #define __HAL_UART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))   
349
350 /** @brief  Clear the specified UART pending flag.
351   * @param  __HANDLE__: specifies the UART Handle.
352   *         UART Handle selects the USARTx or UARTy peripheral 
353   *         (USART,UART availability and x,y values depending on device).
354   * @param  __FLAG__: specifies the flag to check.
355   *          This parameter can be any combination of the following values:
356   *            @arg UART_FLAG_CTS:  CTS Change flag (not available for UART4 and UART5).
357   *            @arg UART_FLAG_LBD:  LIN Break detection flag.
358   *            @arg UART_FLAG_TC:   Transmission Complete flag.
359   *            @arg UART_FLAG_RXNE: Receive data register not empty flag.
360   *   
361   * @note   PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun 
362   *          error) and IDLE (Idle line detected) flags are cleared by software 
363   *          sequence: a read operation to USART_SR register followed by a read
364   *          operation to USART_DR register.
365   * @note   RXNE flag can be also cleared by a read to the USART_DR register.
366   * @note   TC flag can be also cleared by software sequence: a read operation to 
367   *          USART_SR register followed by a write operation to USART_DR register.
368   * @note   TXE flag is cleared only by a write to the USART_DR register.
369   *   
370   * @retval None
371   */
372 #define __HAL_UART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
373
374 /** @brief  Clear the UART PE pending flag.
375   * @param  __HANDLE__: specifies the UART Handle.
376   *         UART Handle selects the USARTx or UARTy peripheral 
377   *         (USART,UART availability and x,y values depending on device).
378   * @retval None
379   */
380 #define __HAL_UART_CLEAR_PEFLAG(__HANDLE__) \
381 do{                                         \
382   __IO uint32_t tmpreg;                     \
383   tmpreg = (__HANDLE__)->Instance->SR;      \
384   tmpreg = (__HANDLE__)->Instance->DR;      \
385   UNUSED(tmpreg);                           \
386 }while(0)
387
388
389
390 /** @brief  Clear the UART FE pending flag.
391   * @param  __HANDLE__: specifies the UART Handle.
392   *         UART Handle selects the USARTx or UARTy peripheral 
393   *         (USART,UART availability and x,y values depending on device).
394   * @retval None
395   */
396 #define __HAL_UART_CLEAR_FEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
397
398 /** @brief  Clear the UART NE pending flag.
399   * @param  __HANDLE__: specifies the UART Handle.
400   *         UART Handle selects the USARTx or UARTy peripheral 
401   *         (USART,UART availability and x,y values depending on device).
402   * @retval None
403   */
404 #define __HAL_UART_CLEAR_NEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
405
406 /** @brief  Clear the UART ORE pending flag.
407   * @param  __HANDLE__: specifies the UART Handle.
408   *         UART Handle selects the USARTx or UARTy peripheral 
409   *         (USART,UART availability and x,y values depending on device).
410   * @retval None
411   */
412 #define __HAL_UART_CLEAR_OREFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
413
414 /** @brief  Clear the UART IDLE pending flag.
415   * @param  __HANDLE__: specifies the UART Handle.
416   *         UART Handle selects the USARTx or UARTy peripheral 
417   *         (USART,UART availability and x,y values depending on device).
418   * @retval None
419   */
420 #define __HAL_UART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
421                                                  
422 /** @brief  Enable the specified UART interrupt.
423   * @param  __HANDLE__: specifies the UART Handle.
424   *         UART Handle selects the USARTx or UARTy peripheral 
425   *         (USART,UART availability and x,y values depending on device).
426   * @param  __INTERRUPT__: specifies the UART interrupt source to enable.
427   *          This parameter can be one of the following values:
428   *            @arg UART_IT_CTS:  CTS change interrupt
429   *            @arg UART_IT_LBD:  LIN Break detection interrupt
430   *            @arg UART_IT_TXE:  Transmit Data Register empty interrupt
431   *            @arg UART_IT_TC:   Transmission complete interrupt
432   *            @arg UART_IT_RXNE: Receive Data register not empty interrupt
433   *            @arg UART_IT_IDLE: Idle line detection interrupt
434   *            @arg UART_IT_PE:   Parity Error interrupt
435   *            @arg UART_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
436   * @retval None
437   */
438 #define __HAL_UART_ENABLE_IT(__HANDLE__, __INTERRUPT__)   ((((__INTERRUPT__) >> 28) == UART_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & UART_IT_MASK)): \
439                                                            (((__INTERRUPT__) >> 28) == UART_CR2_REG_INDEX)? ((__HANDLE__)->Instance->CR2 |=  ((__INTERRUPT__) & UART_IT_MASK)): \
440                                                            ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & UART_IT_MASK)))
441
442
443 /** @brief  Disable the specified UART interrupt.
444   * @param  __HANDLE__: specifies the UART Handle.
445   *         UART Handle selects the USARTx or UARTy peripheral 
446   *         (USART,UART availability and x,y values depending on device).
447   * @param  __INTERRUPT__: specifies the UART interrupt source to disable.
448   *          This parameter can be one of the following values:
449   *            @arg UART_IT_CTS:  CTS change interrupt
450   *            @arg UART_IT_LBD:  LIN Break detection interrupt
451   *            @arg UART_IT_TXE:  Transmit Data Register empty interrupt
452   *            @arg UART_IT_TC:   Transmission complete interrupt
453   *            @arg UART_IT_RXNE: Receive Data register not empty interrupt
454   *            @arg UART_IT_IDLE: Idle line detection interrupt
455   *            @arg UART_IT_PE:   Parity Error interrupt
456   *            @arg UART_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
457   * @retval None
458   */
459 #define __HAL_UART_DISABLE_IT(__HANDLE__, __INTERRUPT__)  ((((__INTERRUPT__) >> 28) == UART_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
460                                                            (((__INTERRUPT__) >> 28) == UART_CR2_REG_INDEX)? ((__HANDLE__)->Instance->CR2 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
461                                                            ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & UART_IT_MASK)))
462     
463 /** @brief  Check whether the specified UART interrupt has occurred or not.
464   * @param  __HANDLE__: specifies the UART Handle.
465   *         UART Handle selects the USARTx or UARTy peripheral 
466   *         (USART,UART availability and x,y values depending on device).
467   * @param  __IT__: specifies the UART interrupt source to check.
468   *          This parameter can be one of the following values:
469   *            @arg UART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
470   *            @arg UART_IT_LBD: LIN Break detection interrupt
471   *            @arg UART_IT_TXE: Transmit Data Register empty interrupt
472   *            @arg UART_IT_TC:  Transmission complete interrupt
473   *            @arg UART_IT_RXNE: Receive Data register not empty interrupt
474   *            @arg UART_IT_IDLE: Idle line detection interrupt
475   *            @arg UART_IT_ERR: Error interrupt
476   * @retval The new state of __IT__ (TRUE or FALSE).
477   */
478 #define __HAL_UART_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28) == UART_CR1_REG_INDEX)? (__HANDLE__)->Instance->CR1:(((((uint32_t)(__IT__)) >> 28) == UART_CR2_REG_INDEX)? \
479                                                       (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & (((uint32_t)(__IT__)) & UART_IT_MASK))
480
481 /** @brief  Enable CTS flow control 
482   *         This macro allows to enable CTS hardware flow control for a given UART instance, 
483   *         without need to call HAL_UART_Init() function.
484   *         As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
485   * @note   As macro is expected to be used for modifying CTS Hw flow control feature activation, without need
486   *         for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
487   *           - UART instance should have already been initialised (through call of HAL_UART_Init() )
488   *           - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
489   *             and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).                                                                                                                  
490   * @param  __HANDLE__: specifies the UART Handle.
491   *         This parameter can be any USARTx (supporting the HW Flow control feature).
492   *         It is used to select the USART peripheral (USART availability and x value depending on device).
493   * @retval None
494   */
495 #define __HAL_UART_HWCONTROL_CTS_ENABLE(__HANDLE__)        \
496   do{                                                      \
497     SET_BIT((__HANDLE__)->Instance->CR3, USART_CR3_CTSE);  \
498     (__HANDLE__)->Init.HwFlowCtl |= USART_CR3_CTSE;        \
499   } while(0)
500
501 /** @brief  Disable CTS flow control 
502   *         This macro allows to disable CTS hardware flow control for a given UART instance, 
503   *         without need to call HAL_UART_Init() function.
504   *         As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
505   * @note   As macro is expected to be used for modifying CTS Hw flow control feature activation, without need
506   *         for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
507   *           - UART instance should have already been initialised (through call of HAL_UART_Init() )
508   *           - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
509   *             and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)). 
510   * @param  __HANDLE__: specifies the UART Handle.
511   *         This parameter can be any USARTx (supporting the HW Flow control feature).
512   *         It is used to select the USART peripheral (USART availability and x value depending on device).
513   * @retval None
514   */
515 #define __HAL_UART_HWCONTROL_CTS_DISABLE(__HANDLE__)        \
516   do{                                                       \
517     CLEAR_BIT((__HANDLE__)->Instance->CR3, USART_CR3_CTSE); \
518     (__HANDLE__)->Init.HwFlowCtl &= ~(USART_CR3_CTSE);      \
519   } while(0)
520
521 /** @brief  Enable RTS flow control 
522   *         This macro allows to enable RTS hardware flow control for a given UART instance, 
523   *         without need to call HAL_UART_Init() function.
524   *         As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
525   * @note   As macro is expected to be used for modifying RTS Hw flow control feature activation, without need
526   *         for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
527   *           - UART instance should have already been initialised (through call of HAL_UART_Init() )
528   *           - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
529   *             and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)). 
530   * @param  __HANDLE__: specifies the UART Handle.
531   *         This parameter can be any USARTx (supporting the HW Flow control feature).
532   *         It is used to select the USART peripheral (USART availability and x value depending on device).
533   * @retval None
534   */
535 #define __HAL_UART_HWCONTROL_RTS_ENABLE(__HANDLE__)       \
536   do{                                                     \
537     SET_BIT((__HANDLE__)->Instance->CR3, USART_CR3_RTSE); \
538     (__HANDLE__)->Init.HwFlowCtl |= USART_CR3_RTSE;       \
539   } while(0)
540
541 /** @brief  Disable RTS flow control 
542   *         This macro allows to disable RTS hardware flow control for a given UART instance, 
543   *         without need to call HAL_UART_Init() function.
544   *         As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
545   * @note   As macro is expected to be used for modifying RTS Hw flow control feature activation, without need
546   *         for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
547   *           - UART instance should have already been initialised (through call of HAL_UART_Init() )
548   *           - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
549   *             and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)). 
550   * @param  __HANDLE__: specifies the UART Handle.
551   *         This parameter can be any USARTx (supporting the HW Flow control feature).
552   *         It is used to select the USART peripheral (USART availability and x value depending on device).
553   * @retval None
554   */
555 #define __HAL_UART_HWCONTROL_RTS_DISABLE(__HANDLE__)       \
556   do{                                                      \
557     CLEAR_BIT((__HANDLE__)->Instance->CR3, USART_CR3_RTSE);\
558     (__HANDLE__)->Init.HwFlowCtl &= ~(USART_CR3_RTSE);     \
559   } while(0)
560
561
562 /** @brief  Enable UART
563   * @param  __HANDLE__: specifies the UART Handle.
564   *         UART Handle selects the USARTx or UARTy peripheral 
565   *         (USART,UART availability and x,y values depending on device).
566   * @retval None
567   */ 
568 #define __HAL_UART_ENABLE(__HANDLE__)               ((__HANDLE__)->Instance->CR1 |=  USART_CR1_UE)
569
570 /** @brief  Disable UART
571   *         UART Handle selects the USARTx or UARTy peripheral 
572   *         (USART,UART availability and x,y values depending on device).
573   * @retval None
574   */
575 #define __HAL_UART_DISABLE(__HANDLE__)              ((__HANDLE__)->Instance->CR1 &=  ~USART_CR1_UE)
576
577 /**
578   * @}
579   */
580
581
582 /* Private macros --------------------------------------------------------*/
583 /** @defgroup UART_Private_Macros   UART Private Macros
584   * @{
585   */
586
587 #define UART_CR1_REG_INDEX               1    
588 #define UART_CR2_REG_INDEX               2    
589 #define UART_CR3_REG_INDEX               3    
590
591 #define UART_DIV_SAMPLING16(_PCLK_, _BAUD_)         (((_PCLK_)*25)/(4*(_BAUD_)))
592 #define UART_DIVMANT_SAMPLING16(_PCLK_, _BAUD_)     (UART_DIV_SAMPLING16((_PCLK_), (_BAUD_))/100)
593 #define UART_DIVFRAQ_SAMPLING16(_PCLK_, _BAUD_)     (((UART_DIV_SAMPLING16((_PCLK_), (_BAUD_)) - (UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) * 100)) * 16 + 50) / 100)
594 #define UART_BRR_SAMPLING16(_PCLK_, _BAUD_)         ((UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) << 4)|(UART_DIVFRAQ_SAMPLING16((_PCLK_), (_BAUD_)) & 0x0F))
595
596 #define IS_UART_WORD_LENGTH(LENGTH)       (((LENGTH) == UART_WORDLENGTH_8B) || \
597                                            ((LENGTH) == UART_WORDLENGTH_9B))
598 #define IS_UART_LIN_WORD_LENGTH(LENGTH)   ((LENGTH) == UART_WORDLENGTH_8B)
599
600 #define IS_UART_STOPBITS(STOPBITS)     (((STOPBITS) == UART_STOPBITS_1) || \
601                                         ((STOPBITS) == UART_STOPBITS_2))
602
603 #define IS_UART_PARITY(PARITY)         (((PARITY) == UART_PARITY_NONE) || \
604                                         ((PARITY) == UART_PARITY_EVEN) || \
605                                         ((PARITY) == UART_PARITY_ODD))
606
607 #define IS_UART_HARDWARE_FLOW_CONTROL(CONTROL)\
608                                        (((CONTROL) == UART_HWCONTROL_NONE) || \
609                                         ((CONTROL) == UART_HWCONTROL_RTS) || \
610                                         ((CONTROL) == UART_HWCONTROL_CTS) || \
611                                         ((CONTROL) == UART_HWCONTROL_RTS_CTS))
612
613 #define IS_UART_MODE(MODE)             ((((MODE) & (~((uint32_t)UART_MODE_TX_RX))) == 0x00) && \
614                                         ((MODE) != (uint32_t)0x00000000))
615
616 #define IS_UART_STATE(STATE)           (((STATE) == UART_STATE_DISABLE) || \
617                                         ((STATE) == UART_STATE_ENABLE))
618
619 #define IS_UART_OVERSAMPLING(SAMPLING)      ((SAMPLING) == UART_OVERSAMPLING_16)
620 #define IS_UART_LIN_OVERSAMPLING(SAMPLING)  ((SAMPLING) == UART_OVERSAMPLING_16)
621
622 #define IS_UART_LIN_BREAK_DETECT_LENGTH(LENGTH) (((LENGTH) == UART_LINBREAKDETECTLENGTH_10B) || \
623                                                  ((LENGTH) == UART_LINBREAKDETECTLENGTH_11B))
624
625 #define IS_UART_WAKEUPMETHOD(WAKEUP)   (((WAKEUP) == UART_WAKEUPMETHOD_IDLELINE) || \
626                                         ((WAKEUP) == UART_WAKEUPMETHOD_ADDRESSMARK))
627
628                                 
629 /** Check UART Baud rate
630   *         __BAUDRATE__: Baudrate specified by the user
631   *         The maximum Baud Rate is derived from the maximum clock on APB (i.e. 72 MHz) 
632   *         divided by the smallest oversampling used on the USART (i.e. 16) 
633   * Retrun : TRUE or FALSE
634   */
635 #define IS_UART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) < 4500001)
636
637 /** Check UART Node Address
638   *         __ADDRESS__: UART Node address specified by the user
639   *         UART Node address is used in Multi processor communication for wakeup 
640   *         with address mark detection. 
641   *         This parameter must be a number between Min_Data = 0 and Max_Data = 15 
642   * Return : TRUE or FALSE
643   */
644 #define IS_UART_ADDRESS(__ADDRESS__) ((__ADDRESS__) <= 0xF)
645
646 /** UART interruptions flag mask
647   */ 
648 #define UART_IT_MASK  ((uint32_t) USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RXNEIE | \
649                                   USART_CR1_IDLEIE | USART_CR2_LBDIE | USART_CR3_CTSIE | USART_CR3_EIE )
650
651 /**
652   * @}
653   */
654
655 /* Exported functions --------------------------------------------------------*/
656
657 /** @addtogroup UART_Exported_Functions UART Exported Functions
658   * @{
659   */
660   
661 /** @addtogroup UART_Exported_Functions_Group1 Initialization and de-initialization functions 
662   * @{
663   */
664
665 /* Initialization and de-initialization functions  ****************************/
666 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart);
667 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart);
668 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength);
669 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod);
670 HAL_StatusTypeDef HAL_UART_DeInit (UART_HandleTypeDef *huart);
671 void HAL_UART_MspInit(UART_HandleTypeDef *huart);
672 void HAL_UART_MspDeInit(UART_HandleTypeDef *huart);
673
674 /**
675   * @}
676   */
677
678 /** @addtogroup UART_Exported_Functions_Group2 IO operation functions 
679   * @{
680   */
681
682 /* IO operation functions *****************************************************/
683 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
684 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
685 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
686 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
687 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
688 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
689 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart);
690 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart);
691 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart);
692 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart);
693 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
694 void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart);
695 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
696 void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart);
697 void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart);
698
699 /**
700   * @}
701   */
702
703 /** @addtogroup UART_Exported_Functions_Group3 Peripheral Control functions
704   * @{
705   */
706
707 /* Peripheral Control functions  ************************************************/
708 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart);
709 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart);
710 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart);
711 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart);
712 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart);
713
714 /**
715   * @}
716   */
717
718 /** @addtogroup UART_Exported_Functions_Group4 Peripheral State and Errors functions 
719   * @{
720   */
721
722 /* Peripheral State and Errors functions  **************************************************/
723 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart);
724 uint32_t              HAL_UART_GetError(UART_HandleTypeDef *huart);
725
726 /**
727   * @}
728   */
729
730 /**
731   * @}
732   */
733
734 /**
735   * @}
736   */ 
737
738 /**
739   * @}
740   */
741
742 #ifdef __cplusplus
743 }
744 #endif
745
746 #endif /* __STM32F1xx_HAL_UART_H */
747
748 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/