stm32l4xx_ll_lpuart.c 12 KB

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