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.
 
 
 

262 lines
9.9 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_ll_lpuart.c
  4. * @author MCD Application Team
  5. * @brief LPUART 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 "stm32l0xx_ll_lpuart.h"
  22. #include "stm32l0xx_ll_rcc.h"
  23. #include "stm32l0xx_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 STM32L0xx_LL_Driver
  30. * @{
  31. */
  32. #if defined (LPUART1)
  33. /** @addtogroup LPUART_LL
  34. * @{
  35. */
  36. /* Private types -------------------------------------------------------------*/
  37. /* Private variables ---------------------------------------------------------*/
  38. /* Private constants ---------------------------------------------------------*/
  39. /** @addtogroup LPUART_LL_Private_Constants
  40. * @{
  41. */
  42. /**
  43. * @}
  44. */
  45. /* Private macros ------------------------------------------------------------*/
  46. /** @addtogroup LPUART_LL_Private_Macros
  47. * @{
  48. */
  49. /* Check of parameters for configuration of LPUART registers */
  50. /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register */
  51. /* value : */
  52. /* - fck must be in the range [3 x baudrate, 4096 x baudrate] */
  53. /* - LPUART_BRR register value should be >= 0x300 */
  54. /* - LPUART_BRR register value should be <= 0xFFFFF (20 bits) */
  55. /* Baudrate specified by the user should belong to [8, 10600000].*/
  56. #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 10600000U) && ((__BAUDRATE__) >= 8U))
  57. /* __VALUE__ BRR content must be greater than or equal to 0x300. */
  58. #define IS_LL_LPUART_BRR_MIN(__VALUE__) ((__VALUE__) >= 0x300U)
  59. /* __VALUE__ BRR content must be lower than or equal to 0xFFFFF. */
  60. #define IS_LL_LPUART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x000FFFFFU)
  61. #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \
  62. || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \
  63. || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \
  64. || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX))
  65. #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \
  66. || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \
  67. || ((__VALUE__) == LL_LPUART_PARITY_ODD))
  68. #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \
  69. || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \
  70. || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B))
  71. #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \
  72. || ((__VALUE__) == LL_LPUART_STOPBITS_2))
  73. #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \
  74. || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \
  75. || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \
  76. || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS))
  77. /**
  78. * @}
  79. */
  80. /* Private function prototypes -----------------------------------------------*/
  81. /* Exported functions --------------------------------------------------------*/
  82. /** @addtogroup LPUART_LL_Exported_Functions
  83. * @{
  84. */
  85. /** @addtogroup LPUART_LL_EF_Init
  86. * @{
  87. */
  88. /**
  89. * @brief De-initialize LPUART registers (Registers restored to their default values).
  90. * @param LPUARTx LPUART Instance
  91. * @retval An ErrorStatus enumeration value:
  92. * - SUCCESS: LPUART registers are de-initialized
  93. * - ERROR: not applicable
  94. */
  95. ErrorStatus LL_LPUART_DeInit(USART_TypeDef *LPUARTx)
  96. {
  97. ErrorStatus status = SUCCESS;
  98. /* Check the parameters */
  99. assert_param(IS_LPUART_INSTANCE(LPUARTx));
  100. if (LPUARTx == LPUART1)
  101. {
  102. /* Force reset of LPUART peripheral */
  103. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPUART1);
  104. /* Release reset of LPUART peripheral */
  105. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPUART1);
  106. }
  107. else
  108. {
  109. status = ERROR;
  110. }
  111. return (status);
  112. }
  113. /**
  114. * @brief Initialize LPUART registers according to the specified
  115. * parameters in LPUART_InitStruct.
  116. * @note As some bits in LPUART configuration registers can only be written when the LPUART is disabled (USART_CR1_UE bit =0),
  117. * LPUART Peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  118. * @note Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0).
  119. * @param LPUARTx LPUART Instance
  120. * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
  121. * that contains the configuration information for the specified LPUART peripheral.
  122. * @retval An ErrorStatus enumeration value:
  123. * - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content
  124. * - ERROR: Problem occurred during LPUART Registers initialization
  125. */
  126. ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, LL_LPUART_InitTypeDef *LPUART_InitStruct)
  127. {
  128. ErrorStatus status = ERROR;
  129. uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
  130. /* Check the parameters */
  131. assert_param(IS_LPUART_INSTANCE(LPUARTx));
  132. assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct->BaudRate));
  133. assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct->DataWidth));
  134. assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct->StopBits));
  135. assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct->Parity));
  136. assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct->TransferDirection));
  137. assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct->HardwareFlowControl));
  138. /* LPUART needs to be in disabled state, in order to be able to configure some bits in
  139. CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */
  140. if (LL_LPUART_IsEnabled(LPUARTx) == 0U)
  141. {
  142. /*---------------------------- LPUART CR1 Configuration -----------------------
  143. * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters:
  144. * - DataWidth: USART_CR1_M bits according to LPUART_InitStruct->DataWidth value
  145. * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value
  146. * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value
  147. */
  148. MODIFY_REG(LPUARTx->CR1,
  149. (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
  150. (LPUART_InitStruct->DataWidth | LPUART_InitStruct->Parity | LPUART_InitStruct->TransferDirection));
  151. /*---------------------------- LPUART CR2 Configuration -----------------------
  152. * Configure LPUARTx CR2 (Stop bits) with parameters:
  153. * - Stop Bits: USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value.
  154. */
  155. LL_LPUART_SetStopBitsLength(LPUARTx, LPUART_InitStruct->StopBits);
  156. /*---------------------------- LPUART CR3 Configuration -----------------------
  157. * Configure LPUARTx CR3 (Hardware Flow Control) with parameters:
  158. * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to LPUART_InitStruct->HardwareFlowControl value.
  159. */
  160. LL_LPUART_SetHWFlowCtrl(LPUARTx, LPUART_InitStruct->HardwareFlowControl);
  161. /*---------------------------- LPUART BRR Configuration -----------------------
  162. * Retrieve Clock frequency used for LPUART Peripheral
  163. */
  164. periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
  165. /* Configure the LPUART Baud Rate :
  166. - valid baud rate value (different from 0) is required
  167. - Peripheral clock as returned by RCC service, should be valid (different from 0).
  168. */
  169. if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
  170. && (LPUART_InitStruct->BaudRate != 0U))
  171. {
  172. status = SUCCESS;
  173. LL_LPUART_SetBaudRate(LPUARTx,
  174. periphclk,
  175. LPUART_InitStruct->BaudRate);
  176. /* Check BRR is greater than or equal to 0x300 */
  177. assert_param(IS_LL_LPUART_BRR_MIN(LPUARTx->BRR));
  178. /* Check BRR is lower than or equal to 0xFFFFF */
  179. assert_param(IS_LL_LPUART_BRR_MAX(LPUARTx->BRR));
  180. }
  181. }
  182. return (status);
  183. }
  184. /**
  185. * @brief Set each @ref LL_LPUART_InitTypeDef field to default value.
  186. * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
  187. * whose fields will be set to default values.
  188. * @retval None
  189. */
  190. void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct)
  191. {
  192. /* Set LPUART_InitStruct fields to default values */
  193. LPUART_InitStruct->BaudRate = 9600U;
  194. LPUART_InitStruct->DataWidth = LL_LPUART_DATAWIDTH_8B;
  195. LPUART_InitStruct->StopBits = LL_LPUART_STOPBITS_1;
  196. LPUART_InitStruct->Parity = LL_LPUART_PARITY_NONE ;
  197. LPUART_InitStruct->TransferDirection = LL_LPUART_DIRECTION_TX_RX;
  198. LPUART_InitStruct->HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
  199. }
  200. /**
  201. * @}
  202. */
  203. /**
  204. * @}
  205. */
  206. /**
  207. * @}
  208. */
  209. #endif /* defined (LPUART1) */
  210. /**
  211. * @}
  212. */
  213. #endif /* USE_FULL_LL_DRIVER */
  214. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/