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.
 
 
 

284 lines
12 KiB

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