stm32l4xx_ll_lpuart.c 13 KB

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