Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

stm32f1xx_ll_usart.c 17KB

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