训练营PLSR题目
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.
 
 
 
 
 
 

501 line
18 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 "stm32f4xx_ll_usart.h"
  21. #include "stm32f4xx_ll_rcc.h"
  22. #include "stm32f4xx_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 STM32F4xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (USART1) || defined (USART2) || defined (USART3) || defined (USART6) || defined (UART4) || defined (UART5) || defined (UART7) || defined (UART8) || defined (UART9) || defined (UART10)
  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__) <= 12500000U)
  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(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(USART6)
  127. else if (USARTx == USART6)
  128. {
  129. /* Force reset of USART clock */
  130. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART6);
  131. /* Release reset of USART clock */
  132. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART6);
  133. }
  134. #endif /* USART6 */
  135. #if defined(UART4)
  136. else if (USARTx == UART4)
  137. {
  138. /* Force reset of UART clock */
  139. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART4);
  140. /* Release reset of UART clock */
  141. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART4);
  142. }
  143. #endif /* UART4 */
  144. #if defined(UART5)
  145. else if (USARTx == UART5)
  146. {
  147. /* Force reset of UART clock */
  148. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART5);
  149. /* Release reset of UART clock */
  150. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART5);
  151. }
  152. #endif /* UART5 */
  153. #if defined(UART7)
  154. else if (USARTx == UART7)
  155. {
  156. /* Force reset of UART clock */
  157. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART7);
  158. /* Release reset of UART clock */
  159. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART7);
  160. }
  161. #endif /* UART7 */
  162. #if defined(UART8)
  163. else if (USARTx == UART8)
  164. {
  165. /* Force reset of UART clock */
  166. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART8);
  167. /* Release reset of UART clock */
  168. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART8);
  169. }
  170. #endif /* UART8 */
  171. #if defined(UART9)
  172. else if (USARTx == UART9)
  173. {
  174. /* Force reset of UART clock */
  175. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_UART9);
  176. /* Release reset of UART clock */
  177. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_UART9);
  178. }
  179. #endif /* UART9 */
  180. #if defined(UART10)
  181. else if (USARTx == UART10)
  182. {
  183. /* Force reset of UART clock */
  184. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_UART10);
  185. /* Release reset of UART clock */
  186. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_UART10);
  187. }
  188. #endif /* UART10 */
  189. else
  190. {
  191. status = ERROR;
  192. }
  193. return (status);
  194. }
  195. /**
  196. * @brief Initialize USART registers according to the specified
  197. * parameters in USART_InitStruct.
  198. * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
  199. * USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  200. * @note Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
  201. * @param USARTx USART Instance
  202. * @param USART_InitStruct pointer to a LL_USART_InitTypeDef structure
  203. * that contains the configuration information for the specified USART peripheral.
  204. * @retval An ErrorStatus enumeration value:
  205. * - SUCCESS: USART registers are initialized according to USART_InitStruct content
  206. * - ERROR: Problem occurred during USART Registers initialization
  207. */
  208. ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, LL_USART_InitTypeDef *USART_InitStruct)
  209. {
  210. ErrorStatus status = ERROR;
  211. uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
  212. LL_RCC_ClocksTypeDef rcc_clocks;
  213. /* Check the parameters */
  214. assert_param(IS_UART_INSTANCE(USARTx));
  215. assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct->BaudRate));
  216. assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct->DataWidth));
  217. assert_param(IS_LL_USART_STOPBITS(USART_InitStruct->StopBits));
  218. assert_param(IS_LL_USART_PARITY(USART_InitStruct->Parity));
  219. assert_param(IS_LL_USART_DIRECTION(USART_InitStruct->TransferDirection));
  220. assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct->HardwareFlowControl));
  221. assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct->OverSampling));
  222. /* USART needs to be in disabled state, in order to be able to configure some bits in
  223. CRx registers */
  224. if (LL_USART_IsEnabled(USARTx) == 0U)
  225. {
  226. /*---------------------------- USART CR1 Configuration -----------------------
  227. * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
  228. * - DataWidth: USART_CR1_M bits according to USART_InitStruct->DataWidth value
  229. * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
  230. * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
  231. * - Oversampling: USART_CR1_OVER8 bit according to USART_InitStruct->OverSampling value.
  232. */
  233. MODIFY_REG(USARTx->CR1,
  234. (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
  235. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
  236. (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
  237. USART_InitStruct->TransferDirection | USART_InitStruct->OverSampling));
  238. /*---------------------------- USART CR2 Configuration -----------------------
  239. * Configure USARTx CR2 (Stop bits) with parameters:
  240. * - Stop Bits: USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
  241. * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
  242. */
  243. LL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
  244. /*---------------------------- USART CR3 Configuration -----------------------
  245. * Configure USARTx CR3 (Hardware Flow Control) with parameters:
  246. * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to USART_InitStruct->HardwareFlowControl value.
  247. */
  248. LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
  249. /*---------------------------- USART BRR Configuration -----------------------
  250. * Retrieve Clock frequency used for USART Peripheral
  251. */
  252. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  253. if (USARTx == USART1)
  254. {
  255. periphclk = rcc_clocks.PCLK2_Frequency;
  256. }
  257. else if (USARTx == USART2)
  258. {
  259. periphclk = rcc_clocks.PCLK1_Frequency;
  260. }
  261. #if defined(USART3)
  262. else if (USARTx == USART3)
  263. {
  264. periphclk = rcc_clocks.PCLK1_Frequency;
  265. }
  266. #endif /* USART3 */
  267. #if defined(USART6)
  268. else if (USARTx == USART6)
  269. {
  270. periphclk = rcc_clocks.PCLK2_Frequency;
  271. }
  272. #endif /* USART6 */
  273. #if defined(UART4)
  274. else if (USARTx == UART4)
  275. {
  276. periphclk = rcc_clocks.PCLK1_Frequency;
  277. }
  278. #endif /* UART4 */
  279. #if defined(UART5)
  280. else if (USARTx == UART5)
  281. {
  282. periphclk = rcc_clocks.PCLK1_Frequency;
  283. }
  284. #endif /* UART5 */
  285. #if defined(UART7)
  286. else if (USARTx == UART7)
  287. {
  288. periphclk = rcc_clocks.PCLK1_Frequency;
  289. }
  290. #endif /* UART7 */
  291. #if defined(UART8)
  292. else if (USARTx == UART8)
  293. {
  294. periphclk = rcc_clocks.PCLK1_Frequency;
  295. }
  296. #endif /* UART8 */
  297. #if defined(UART9)
  298. else if (USARTx == UART9)
  299. {
  300. periphclk = rcc_clocks.PCLK2_Frequency;
  301. }
  302. #endif /* UART9 */
  303. #if defined(UART10)
  304. else if (USARTx == UART10)
  305. {
  306. periphclk = rcc_clocks.PCLK2_Frequency;
  307. }
  308. #endif /* UART10 */
  309. else
  310. {
  311. /* Nothing to do, as error code is already assigned to ERROR value */
  312. }
  313. /* Configure the USART Baud Rate :
  314. - valid baud rate value (different from 0) is required
  315. - Peripheral clock as returned by RCC service, should be valid (different from 0).
  316. */
  317. if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
  318. && (USART_InitStruct->BaudRate != 0U))
  319. {
  320. status = SUCCESS;
  321. LL_USART_SetBaudRate(USARTx,
  322. periphclk,
  323. USART_InitStruct->OverSampling,
  324. USART_InitStruct->BaudRate);
  325. /* Check BRR is greater than or equal to 16d */
  326. assert_param(IS_LL_USART_BRR_MIN(USARTx->BRR));
  327. }
  328. }
  329. /* Endif (=> USART not in Disabled state => return ERROR) */
  330. return (status);
  331. }
  332. /**
  333. * @brief Set each @ref LL_USART_InitTypeDef field to default value.
  334. * @param USART_InitStruct Pointer to a @ref LL_USART_InitTypeDef structure
  335. * whose fields will be set to default values.
  336. * @retval None
  337. */
  338. void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
  339. {
  340. /* Set USART_InitStruct fields to default values */
  341. USART_InitStruct->BaudRate = 9600U;
  342. USART_InitStruct->DataWidth = LL_USART_DATAWIDTH_8B;
  343. USART_InitStruct->StopBits = LL_USART_STOPBITS_1;
  344. USART_InitStruct->Parity = LL_USART_PARITY_NONE ;
  345. USART_InitStruct->TransferDirection = LL_USART_DIRECTION_TX_RX;
  346. USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  347. USART_InitStruct->OverSampling = LL_USART_OVERSAMPLING_16;
  348. }
  349. /**
  350. * @brief Initialize USART Clock related settings according to the
  351. * specified parameters in the USART_ClockInitStruct.
  352. * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
  353. * USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  354. * @param USARTx USART Instance
  355. * @param USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
  356. * that contains the Clock configuration information for the specified USART peripheral.
  357. * @retval An ErrorStatus enumeration value:
  358. * - SUCCESS: USART registers related to Clock settings are initialized according to USART_ClockInitStruct content
  359. * - ERROR: Problem occurred during USART Registers initialization
  360. */
  361. ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  362. {
  363. ErrorStatus status = SUCCESS;
  364. /* Check USART Instance and Clock signal output parameters */
  365. assert_param(IS_UART_INSTANCE(USARTx));
  366. assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
  367. /* USART needs to be in disabled state, in order to be able to configure some bits in
  368. CRx registers */
  369. if (LL_USART_IsEnabled(USARTx) == 0U)
  370. {
  371. /*---------------------------- USART CR2 Configuration -----------------------*/
  372. /* If Clock signal has to be output */
  373. if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
  374. {
  375. /* Deactivate Clock signal delivery :
  376. * - Disable Clock Output: USART_CR2_CLKEN cleared
  377. */
  378. LL_USART_DisableSCLKOutput(USARTx);
  379. }
  380. else
  381. {
  382. /* Ensure USART instance is USART capable */
  383. assert_param(IS_USART_INSTANCE(USARTx));
  384. /* Check clock related parameters */
  385. assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
  386. assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
  387. assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
  388. /*---------------------------- USART CR2 Configuration -----------------------
  389. * Configure USARTx CR2 (Clock signal related bits) with parameters:
  390. * - Enable Clock Output: USART_CR2_CLKEN set
  391. * - Clock Polarity: USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
  392. * - Clock Phase: USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
  393. * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
  394. */
  395. MODIFY_REG(USARTx->CR2,
  396. USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
  397. USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
  398. USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
  399. }
  400. }
  401. /* Else (USART not in Disabled state => return ERROR */
  402. else
  403. {
  404. status = ERROR;
  405. }
  406. return (status);
  407. }
  408. /**
  409. * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
  410. * @param USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
  411. * whose fields will be set to default values.
  412. * @retval None
  413. */
  414. void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  415. {
  416. /* Set LL_USART_ClockInitStruct fields with default values */
  417. USART_ClockInitStruct->ClockOutput = LL_USART_CLOCK_DISABLE;
  418. USART_ClockInitStruct->ClockPolarity = LL_USART_POLARITY_LOW; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  419. USART_ClockInitStruct->ClockPhase = LL_USART_PHASE_1EDGE; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  420. USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  421. }
  422. /**
  423. * @}
  424. */
  425. /**
  426. * @}
  427. */
  428. /**
  429. * @}
  430. */
  431. #endif /* USART1 || USART2 || USART3 || USART6 || UART4 || UART5 || UART7 || UART8 || UART9 || UART10 */
  432. /**
  433. * @}
  434. */
  435. #endif /* USE_FULL_LL_DRIVER */