stm32f1xx_ll_usart.c 17 KB

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