fsl_pwm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_pwm.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /*******************************************************************************
  35. * Prototypes
  36. ******************************************************************************/
  37. /*!
  38. * @brief Get the instance from the base address
  39. *
  40. * @param base PWM peripheral base address
  41. *
  42. * @return The PWM module instance
  43. */
  44. static uint32_t pwm_get_instance(PWM_Type *base);
  45. /*******************************************************************************
  46. * Variables
  47. ******************************************************************************/
  48. /*! @brief Pointers to PWM bases for each instance. */
  49. static PWM_Type *const s_pwmBases[] = PWM_BASE_PTRS;
  50. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  51. /* Array of PWM clock name. */
  52. static const clock_ip_name_t s_pwmClock[] = PWM_CLOCKS;
  53. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  54. /*******************************************************************************
  55. * Code
  56. ******************************************************************************/
  57. static uint32_t pwm_get_instance(PWM_Type *base)
  58. {
  59. uint32_t instance;
  60. uint32_t pwmArrayCount = (sizeof(s_pwmBases) / sizeof(s_pwmBases[0]));
  61. /* Find the instance index from base address mappings. */
  62. for (instance = 0; instance < pwmArrayCount; instance++)
  63. {
  64. if (s_pwmBases[instance] == base)
  65. {
  66. break;
  67. }
  68. }
  69. assert(instance < pwmArrayCount);
  70. return instance;
  71. }
  72. status_t pwm_init(PWM_Type *base, const pwm_config_t *config)
  73. {
  74. assert(config);
  75. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  76. /* Ungate PWM clock */
  77. CLOCK_EnableClock(s_pwmClock[pwm_get_instance(base)]);
  78. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  79. /* Setup the PWM operation */
  80. base->PWMCR = (PWM_PWMCR_REPEAT(config->sampleRepeat) | PWM_PWMCR_PRESCALER(config->prescale) |
  81. PWM_PWMCR_CLKSRC(config->clockSource) | PWM_PWMCR_POUTC(config->outputConfig) |
  82. PWM_PWMCR_HCTR(config->halfWordSwap) | PWM_PWMCR_BCTR(config->byteSwap) |
  83. PWM_PWMCR_STOPEN(config->enableStopMode) | PWM_PWMCR_DBGEN(config->enableDebugMode) |
  84. PWM_PWMCR_WAITEN(config->enableWaitMode) | PWM_PWMCR_DOZEN(config->enableDozeMode) |
  85. PWM_PWMCR_FWM(config->fifoWater));
  86. return kStatus_Success;
  87. }
  88. void pwm_deinit(PWM_Type *base)
  89. {
  90. /* Set clock source to none to disable counter */
  91. base->PWMCR &= ~(PWM_PWMCR_CLKSRC_MASK);
  92. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  93. /* Gate the PWM clock */
  94. CLOCK_DisableClock(s_pwmClock[pwm_get_instance(base)]);
  95. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  96. }
  97. void pwm_get_default_config(pwm_config_t *config)
  98. {
  99. assert(config);
  100. /* Stop mode disabled */
  101. config->enableStopMode = false;
  102. /* Doze mode disabled */
  103. config->enableDozeMode = false;
  104. /* Wait mode disabled */
  105. config->enableWaitMode = false;
  106. /* Debug mode disabled */
  107. config->enableDebugMode = false;
  108. /* Choose low frequency clock to control counter operation */
  109. config->clockSource = KPWM_LOW_FREQUENCY_CLOCK;
  110. /* PWM clock devide by (config->prescale + 1) */
  111. config->prescale = 0U;
  112. /* Output pin is set at rollover and cleared at comparison */
  113. config->outputConfig = KPWM_SET_AT_ROLLOVER_AND_CLEAR_AT_COMPARISON;
  114. /* FIFO empty flag is set when there are more than or equal to 2 empty slots in FIFO */
  115. config->fifoWater = KPWM_FIFO_WATERMARK_2;
  116. /* Use each sample once */
  117. config->sampleRepeat = KPMW_EACH_SAMPLE_ONCE;
  118. /* byte ordering remains the same */
  119. config->byteSwap = KPWM_BYTE_NO_SWAP;
  120. /* Half word swapping does not take place */
  121. config->halfWordSwap = KPWM_HALF_WORD_NO_SWAP;
  122. }