stm32f1xx_ll_usart.c 18 KB

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