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.
 
 
 

464 lines
18 KiB

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