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.
 
 
 

273 lines
11 KiB

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