stm32f3xx_ll_usart.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_ll_usart.c
  4. * @author MCD Application Team
  5. * @brief USART LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f3xx_ll_usart.h"
  21. #include "stm32f3xx_ll_rcc.h"
  22. #include "stm32f3xx_ll_bus.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif /* USE_FULL_ASSERT */
  28. /** @addtogroup STM32F3xx_LL_Driver
  29. * @{
  30. */
  31. #if defined(USART1) || defined(USART2) || defined(USART3) || defined(UART4) || defined(UART5)
  32. /** @addtogroup USART_LL
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /** @addtogroup USART_LL_Private_Constants
  39. * @{
  40. */
  41. /* Definition of default baudrate value used for USART initialisation */
  42. #define USART_DEFAULT_BAUDRATE (9600U)
  43. /**
  44. * @}
  45. */
  46. /* Private macros ------------------------------------------------------------*/
  47. /** @addtogroup USART_LL_Private_Macros
  48. * @{
  49. */
  50. /* __BAUDRATE__ The maximum Baud Rate is derived from the maximum clock available
  51. * divided by the smallest oversampling used on the USART (i.e. 8) */
  52. #define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 9000000U)
  53. /* __VALUE__ In case of oversampling by 16 and 8, BRR content must be greater than or equal to 16d. */
  54. #define IS_LL_USART_BRR_MIN(__VALUE__) ((__VALUE__) >= 16U)
  55. #define IS_LL_USART_DIRECTION(__VALUE__) (((__VALUE__) == LL_USART_DIRECTION_NONE) \
  56. || ((__VALUE__) == LL_USART_DIRECTION_RX) \
  57. || ((__VALUE__) == LL_USART_DIRECTION_TX) \
  58. || ((__VALUE__) == LL_USART_DIRECTION_TX_RX))
  59. #define IS_LL_USART_PARITY(__VALUE__) (((__VALUE__) == LL_USART_PARITY_NONE) \
  60. || ((__VALUE__) == LL_USART_PARITY_EVEN) \
  61. || ((__VALUE__) == LL_USART_PARITY_ODD))
  62. #if defined(USART_7BITS_SUPPORT)
  63. #define IS_LL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_USART_DATAWIDTH_7B) \
  64. || ((__VALUE__) == LL_USART_DATAWIDTH_8B) \
  65. || ((__VALUE__) == LL_USART_DATAWIDTH_9B))
  66. #else
  67. #define IS_LL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_USART_DATAWIDTH_8B) \
  68. || ((__VALUE__) == LL_USART_DATAWIDTH_9B))
  69. #endif /* USART_7BITS_SUPPORT */
  70. #define IS_LL_USART_OVERSAMPLING(__VALUE__) (((__VALUE__) == LL_USART_OVERSAMPLING_16) \
  71. || ((__VALUE__) == LL_USART_OVERSAMPLING_8))
  72. #define IS_LL_USART_LASTBITCLKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_LASTCLKPULSE_NO_OUTPUT) \
  73. || ((__VALUE__) == LL_USART_LASTCLKPULSE_OUTPUT))
  74. #define IS_LL_USART_CLOCKPHASE(__VALUE__) (((__VALUE__) == LL_USART_PHASE_1EDGE) \
  75. || ((__VALUE__) == LL_USART_PHASE_2EDGE))
  76. #define IS_LL_USART_CLOCKPOLARITY(__VALUE__) (((__VALUE__) == LL_USART_POLARITY_LOW) \
  77. || ((__VALUE__) == LL_USART_POLARITY_HIGH))
  78. #define IS_LL_USART_CLOCKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_CLOCK_DISABLE) \
  79. || ((__VALUE__) == LL_USART_CLOCK_ENABLE))
  80. #define IS_LL_USART_STOPBITS(__VALUE__) (((__VALUE__) == LL_USART_STOPBITS_0_5) \
  81. || ((__VALUE__) == LL_USART_STOPBITS_1) \
  82. || ((__VALUE__) == LL_USART_STOPBITS_1_5) \
  83. || ((__VALUE__) == LL_USART_STOPBITS_2))
  84. #define IS_LL_USART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_USART_HWCONTROL_NONE) \
  85. || ((__VALUE__) == LL_USART_HWCONTROL_RTS) \
  86. || ((__VALUE__) == LL_USART_HWCONTROL_CTS) \
  87. || ((__VALUE__) == LL_USART_HWCONTROL_RTS_CTS))
  88. /**
  89. * @}
  90. */
  91. /* Private function prototypes -----------------------------------------------*/
  92. /* Exported functions --------------------------------------------------------*/
  93. /** @addtogroup USART_LL_Exported_Functions
  94. * @{
  95. */
  96. /** @addtogroup USART_LL_EF_Init
  97. * @{
  98. */
  99. /**
  100. * @brief De-initialize USART registers (Registers restored to their default values).
  101. * @param USARTx USART Instance
  102. * @retval An ErrorStatus enumeration value:
  103. * - SUCCESS: USART registers are de-initialized
  104. * - ERROR: USART registers are not de-initialized
  105. */
  106. ErrorStatus LL_USART_DeInit(const USART_TypeDef *USARTx)
  107. {
  108. ErrorStatus status = SUCCESS;
  109. /* Check the parameters */
  110. assert_param(IS_UART_INSTANCE(USARTx));
  111. if (USARTx == USART1)
  112. {
  113. /* Force reset of USART clock */
  114. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART1);
  115. /* Release reset of USART clock */
  116. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART1);
  117. }
  118. else if (USARTx == USART2)
  119. {
  120. /* Force reset of USART clock */
  121. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART2);
  122. /* Release reset of USART clock */
  123. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART2);
  124. }
  125. else if (USARTx == USART3)
  126. {
  127. /* Force reset of USART clock */
  128. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART3);
  129. /* Release reset of USART clock */
  130. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART3);
  131. }
  132. #if defined(UART4)
  133. else if (USARTx == UART4)
  134. {
  135. /* Force reset of UART clock */
  136. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART4);
  137. /* Release reset of UART clock */
  138. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART4);
  139. }
  140. #endif /* UART4 */
  141. #if defined(UART5)
  142. else if (USARTx == UART5)
  143. {
  144. /* Force reset of UART clock */
  145. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART5);
  146. /* Release reset of UART clock */
  147. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART5);
  148. }
  149. #endif /* UART5 */
  150. else
  151. {
  152. status = ERROR;
  153. }
  154. return (status);
  155. }
  156. /**
  157. * @brief Initialize USART registers according to the specified
  158. * parameters in USART_InitStruct.
  159. * @note As some bits in USART configuration registers can only be written when
  160. * the USART is disabled (USART_CR1_UE bit =0), USART Peripheral should be in disabled state prior calling
  161. * this function. Otherwise, ERROR result will be returned.
  162. * @note Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
  163. * @param USARTx USART Instance
  164. * @param USART_InitStruct pointer to a LL_USART_InitTypeDef structure
  165. * that contains the configuration information for the specified USART peripheral.
  166. * @retval An ErrorStatus enumeration value:
  167. * - SUCCESS: USART registers are initialized according to USART_InitStruct content
  168. * - ERROR: Problem occurred during USART Registers initialization
  169. */
  170. ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, const LL_USART_InitTypeDef *USART_InitStruct)
  171. {
  172. ErrorStatus status = ERROR;
  173. uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
  174. #if (!defined(RCC_CFGR3_USART2SW)||!defined (RCC_CFGR3_USART3SW))
  175. LL_RCC_ClocksTypeDef RCC_Clocks;
  176. #endif /* USART clock selection flags */
  177. /* Check the parameters */
  178. assert_param(IS_UART_INSTANCE(USARTx));
  179. assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct->BaudRate));
  180. assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct->DataWidth));
  181. assert_param(IS_LL_USART_STOPBITS(USART_InitStruct->StopBits));
  182. assert_param(IS_LL_USART_PARITY(USART_InitStruct->Parity));
  183. assert_param(IS_LL_USART_DIRECTION(USART_InitStruct->TransferDirection));
  184. assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct->HardwareFlowControl));
  185. assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct->OverSampling));
  186. /* USART needs to be in disabled state, in order to be able to configure some bits in
  187. CRx registers */
  188. if (LL_USART_IsEnabled(USARTx) == 0U)
  189. {
  190. /*---------------------------- USART CR1 Configuration ---------------------
  191. * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
  192. * - DataWidth: USART_CR1_M bits according to USART_InitStruct->DataWidth value
  193. * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
  194. * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
  195. * - Oversampling: USART_CR1_OVER8 bit according to USART_InitStruct->OverSampling value.
  196. */
  197. MODIFY_REG(USARTx->CR1,
  198. (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
  199. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
  200. (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
  201. USART_InitStruct->TransferDirection | USART_InitStruct->OverSampling));
  202. /*---------------------------- USART CR2 Configuration ---------------------
  203. * Configure USARTx CR2 (Stop bits) with parameters:
  204. * - Stop Bits: USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
  205. * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
  206. */
  207. LL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
  208. /*---------------------------- USART CR3 Configuration ---------------------
  209. * Configure USARTx CR3 (Hardware Flow Control) with parameters:
  210. * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to
  211. * USART_InitStruct->HardwareFlowControl value.
  212. */
  213. LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
  214. /*---------------------------- USART BRR Configuration ---------------------
  215. * Retrieve Clock frequency used for USART Peripheral
  216. */
  217. if (USARTx == USART1)
  218. {
  219. periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART1_CLKSOURCE);
  220. }
  221. else if (USARTx == USART2)
  222. {
  223. #if defined(RCC_CFGR3_USART2SW)
  224. periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART2_CLKSOURCE);
  225. #else
  226. /* USART2 clock is PCLK */
  227. LL_RCC_GetSystemClocksFreq(&RCC_Clocks);
  228. periphclk = RCC_Clocks.PCLK1_Frequency;
  229. #endif /* USART2 Clock selector flag */
  230. }
  231. else if (USARTx == USART3)
  232. {
  233. #if defined(RCC_CFGR3_USART3SW)
  234. periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART3_CLKSOURCE);
  235. #else
  236. /* USART3 clock is PCLK */
  237. LL_RCC_GetSystemClocksFreq(&RCC_Clocks);
  238. periphclk = RCC_Clocks.PCLK1_Frequency;
  239. #endif /* USART3 Clock selector flag */
  240. }
  241. #if defined(UART4)
  242. else if (USARTx == UART4)
  243. {
  244. periphclk = LL_RCC_GetUARTClockFreq(LL_RCC_UART4_CLKSOURCE);
  245. }
  246. #endif /* UART4 */
  247. #if defined(UART5)
  248. else if (USARTx == UART5)
  249. {
  250. periphclk = LL_RCC_GetUARTClockFreq(LL_RCC_UART5_CLKSOURCE);
  251. }
  252. #endif /* UART5 */
  253. else
  254. {
  255. /* Nothing to do, as error code is already assigned to ERROR value */
  256. }
  257. /* Configure the USART Baud Rate :
  258. - valid baud rate value (different from 0) is required
  259. - Peripheral clock as returned by RCC service, should be valid (different from 0).
  260. */
  261. if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
  262. && (USART_InitStruct->BaudRate != 0U))
  263. {
  264. status = SUCCESS;
  265. LL_USART_SetBaudRate(USARTx,
  266. periphclk,
  267. USART_InitStruct->OverSampling,
  268. USART_InitStruct->BaudRate);
  269. /* Check BRR is greater than or equal to 16d */
  270. assert_param(IS_LL_USART_BRR_MIN(USARTx->BRR));
  271. }
  272. }
  273. /* Endif (=> USART not in Disabled state => return ERROR) */
  274. return (status);
  275. }
  276. /**
  277. * @brief Set each @ref LL_USART_InitTypeDef field to default value.
  278. * @param USART_InitStruct pointer to a @ref LL_USART_InitTypeDef structure
  279. * whose fields will be set to default values.
  280. * @retval None
  281. */
  282. void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
  283. {
  284. /* Set USART_InitStruct fields to default values */
  285. USART_InitStruct->BaudRate = USART_DEFAULT_BAUDRATE;
  286. USART_InitStruct->DataWidth = LL_USART_DATAWIDTH_8B;
  287. USART_InitStruct->StopBits = LL_USART_STOPBITS_1;
  288. USART_InitStruct->Parity = LL_USART_PARITY_NONE ;
  289. USART_InitStruct->TransferDirection = LL_USART_DIRECTION_TX_RX;
  290. USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  291. USART_InitStruct->OverSampling = LL_USART_OVERSAMPLING_16;
  292. }
  293. /**
  294. * @brief Initialize USART Clock related settings according to the
  295. * specified parameters in the USART_ClockInitStruct.
  296. * @note As some bits in USART configuration registers can only be written when
  297. * the USART is disabled (USART_CR1_UE bit =0), USART Peripheral should be in disabled state prior calling
  298. * this function. Otherwise, ERROR result will be returned.
  299. * @param USARTx USART Instance
  300. * @param USART_ClockInitStruct pointer to a @ref LL_USART_ClockInitTypeDef structure
  301. * that contains the Clock configuration information for the specified USART peripheral.
  302. * @retval An ErrorStatus enumeration value:
  303. * - SUCCESS: USART registers related to Clock settings are initialized according
  304. * to USART_ClockInitStruct content
  305. * - ERROR: Problem occurred during USART Registers initialization
  306. */
  307. ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, const LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  308. {
  309. ErrorStatus status = SUCCESS;
  310. /* Check USART Instance and Clock signal output parameters */
  311. assert_param(IS_UART_INSTANCE(USARTx));
  312. assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
  313. /* USART needs to be in disabled state, in order to be able to configure some bits in
  314. CRx registers */
  315. if (LL_USART_IsEnabled(USARTx) == 0U)
  316. {
  317. /* If USART Clock signal is disabled */
  318. if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
  319. {
  320. /* Deactivate Clock signal delivery :
  321. * - Disable Clock Output: USART_CR2_CLKEN cleared
  322. */
  323. LL_USART_DisableSCLKOutput(USARTx);
  324. }
  325. else
  326. {
  327. /* Ensure USART instance is USART capable */
  328. assert_param(IS_USART_INSTANCE(USARTx));
  329. /* Check clock related parameters */
  330. assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
  331. assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
  332. assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
  333. /*---------------------------- USART CR2 Configuration -----------------------
  334. * Configure USARTx CR2 (Clock signal related bits) with parameters:
  335. * - Enable Clock Output: USART_CR2_CLKEN set
  336. * - Clock Polarity: USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
  337. * - Clock Phase: USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
  338. * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
  339. */
  340. MODIFY_REG(USARTx->CR2,
  341. USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
  342. USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
  343. USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
  344. }
  345. }
  346. /* Else (USART not in Disabled state => return ERROR */
  347. else
  348. {
  349. status = ERROR;
  350. }
  351. return (status);
  352. }
  353. /**
  354. * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
  355. * @param USART_ClockInitStruct pointer to a @ref LL_USART_ClockInitTypeDef structure
  356. * whose fields will be set to default values.
  357. * @retval None
  358. */
  359. void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  360. {
  361. /* Set LL_USART_ClockInitStruct fields with default values */
  362. USART_ClockInitStruct->ClockOutput = LL_USART_CLOCK_DISABLE;
  363. USART_ClockInitStruct->ClockPolarity = LL_USART_POLARITY_LOW; /* Not relevant when ClockOutput =
  364. LL_USART_CLOCK_DISABLE */
  365. USART_ClockInitStruct->ClockPhase = LL_USART_PHASE_1EDGE; /* Not relevant when ClockOutput =
  366. LL_USART_CLOCK_DISABLE */
  367. USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT; /* Not relevant when ClockOutput =
  368. LL_USART_CLOCK_DISABLE */
  369. }
  370. /**
  371. * @}
  372. */
  373. /**
  374. * @}
  375. */
  376. /**
  377. * @}
  378. */
  379. #endif /* USART1 || USART2 || USART3 || UART4 || UART5 */
  380. /**
  381. * @}
  382. */
  383. #endif /* USE_FULL_LL_DRIVER */