stm32l4xx_ll_lptim.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_lptim.c
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  7. * @brief LPTIM 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_lptim.h"
  40. #include "stm32l4xx_ll_bus.h"
  41. #ifdef USE_FULL_ASSERT
  42. #include "stm32_assert.h"
  43. #else
  44. #define assert_param(expr) ((void)0U)
  45. #endif
  46. /** @addtogroup STM32L4xx_LL_Driver
  47. * @{
  48. */
  49. #if defined (LPTIM1) || defined (LPTIM2)
  50. /** @addtogroup LPTIM_LL
  51. * @{
  52. */
  53. /* Private types -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private constants ---------------------------------------------------------*/
  56. /* Private macros ------------------------------------------------------------*/
  57. /** @addtogroup LPTIM_LL_Private_Macros
  58. * @{
  59. */
  60. #define IS_LPTIM_CLOCK_SOURCE(__VALUE__) (((__VALUE__) == LL_LPTIM_CLK_SOURCE_INTERNAL) \
  61. || ((__VALUE__) == LL_LPTIM_CLK_SOURCE_EXTERNAL))
  62. #define IS_LPTIM_CLOCK_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPTIM_PRESCALER_DIV1) \
  63. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV2) \
  64. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV4) \
  65. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV8) \
  66. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV16) \
  67. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV32) \
  68. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV64) \
  69. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV128))
  70. #define IS_LPTIM_WAVEFORM(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_PWM) \
  71. || ((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_SETONCE))
  72. #define IS_LPTIM_OUTPUT_POLARITY(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_REGULAR) \
  73. || ((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_INVERSE))
  74. /**
  75. * @}
  76. */
  77. /* Private function prototypes -----------------------------------------------*/
  78. /* Exported functions --------------------------------------------------------*/
  79. /** @addtogroup LPTIM_LL_Exported_Functions
  80. * @{
  81. */
  82. /** @addtogroup LPTIM_LL_EF_Init
  83. * @{
  84. */
  85. /**
  86. * @brief Set LPTIMx registers to their reset values.
  87. * @param LPTIMx LP Timer instance
  88. * @retval An ErrorStatus enumeration value:
  89. * - SUCCESS: LPTIMx registers are de-initialized
  90. * - ERROR: invalid LPTIMx instance
  91. */
  92. ErrorStatus LL_LPTIM_DeInit(LPTIM_TypeDef* LPTIMx)
  93. {
  94. ErrorStatus result = SUCCESS;
  95. /* Check the parameters */
  96. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  97. if (LPTIMx == LPTIM1)
  98. {
  99. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPTIM1);
  100. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPTIM1);
  101. }
  102. #if defined(LPTIM2)
  103. else if (LPTIMx == LPTIM2)
  104. {
  105. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_LPTIM2);
  106. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_LPTIM2);
  107. }
  108. #endif
  109. else
  110. {
  111. result = ERROR;
  112. }
  113. return result;
  114. }
  115. /**
  116. * @brief Set each fields of the LPTIM_InitStruct structure to its default
  117. * value.
  118. * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
  119. * @retval None
  120. */
  121. void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef* LPTIM_InitStruct)
  122. {
  123. /* Set the default configuration */
  124. LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
  125. LPTIM_InitStruct->Prescaler = LL_LPTIM_PRESCALER_DIV1;
  126. LPTIM_InitStruct->Waveform = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
  127. LPTIM_InitStruct->Polarity = LL_LPTIM_OUTPUT_POLARITY_REGULAR;
  128. }
  129. /**
  130. * @brief Configure the LPTIMx peripheral according to the specified parameters.
  131. * @note LL_LPTIM_Init can only be called when the LPTIM instance is disabled.
  132. * @note LPTIMx can be disabled using unitary function @ref LL_LPTIM_Disable().
  133. * @param LPTIMx LP Timer Instance
  134. * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
  135. * @retval An ErrorStatus enumeration value:
  136. * - SUCCESS: LPTIMx instance has been initialized
  137. * - ERROR: LPTIMx instance hasn't been initialized
  138. */
  139. ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef * LPTIMx, LL_LPTIM_InitTypeDef* LPTIM_InitStruct)
  140. {
  141. ErrorStatus result = SUCCESS;
  142. /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
  143. (ENABLE bit is reset to 0).
  144. */
  145. if (LL_LPTIM_IsEnabled(LPTIMx))
  146. {
  147. result = ERROR;
  148. }
  149. else
  150. {
  151. /* Check the parameters */
  152. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  153. assert_param(IS_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
  154. assert_param(IS_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
  155. assert_param(IS_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
  156. assert_param(IS_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->Polarity));
  157. /* Set CKSEL bitfield according to ClockSource value */
  158. /* Set PRESC bitfield according to Prescaler value */
  159. /* Set WAVE bitfield according to Waveform value */
  160. /* Set WAVEPOL bitfield according to Polarity value */
  161. MODIFY_REG(LPTIMx->CFGR,
  162. (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE| LPTIM_CFGR_WAVPOL),
  163. LPTIM_InitStruct->ClockSource | \
  164. LPTIM_InitStruct->Prescaler | \
  165. LPTIM_InitStruct->Waveform | \
  166. LPTIM_InitStruct->Polarity);
  167. }
  168. return result;
  169. }
  170. /**
  171. * @}
  172. */
  173. /**
  174. * @}
  175. */
  176. /**
  177. * @}
  178. */
  179. #endif /* defined (LPTIM1) || defined (LPTIM2) */
  180. /**
  181. * @}
  182. */
  183. #endif /* USE_FULL_LL_DRIVER */
  184. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/