Projet voilier 4IRA1 Arnaud Vergnet Marino Benassai Bastien Picco Yohan Simard
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.

stm32f1xx_ll_usart.c 17KB

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