stm32l4xx_ll_lptim.c 7.8 KB

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