Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

766 lines
36 KiB

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