fsl_pdb.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2015, 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_pdb.h"
  31. /*******************************************************************************
  32. * Prototypes
  33. ******************************************************************************/
  34. /*!
  35. * @brief Get instance number for PDB module.
  36. *
  37. * @param base PDB peripheral base address
  38. */
  39. static uint32_t PDB_GetInstance(PDB_Type *base);
  40. /*******************************************************************************
  41. * Variables
  42. ******************************************************************************/
  43. /*! @brief Pointers to PDB bases for each instance. */
  44. static PDB_Type *const s_pdbBases[] = PDB_BASE_PTRS;
  45. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  46. /*! @brief Pointers to PDB clocks for each instance. */
  47. static const clock_ip_name_t s_pdbClocks[] = PDB_CLOCKS;
  48. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  49. /*******************************************************************************
  50. * Codes
  51. ******************************************************************************/
  52. static uint32_t PDB_GetInstance(PDB_Type *base)
  53. {
  54. uint32_t instance;
  55. /* Find the instance index from base address mappings. */
  56. for (instance = 0; instance < ARRAY_SIZE(s_pdbBases); instance++)
  57. {
  58. if (s_pdbBases[instance] == base)
  59. {
  60. break;
  61. }
  62. }
  63. assert(instance < ARRAY_SIZE(s_pdbBases));
  64. return instance;
  65. }
  66. void PDB_Init(PDB_Type *base, const pdb_config_t *config)
  67. {
  68. assert(NULL != config);
  69. uint32_t tmp32;
  70. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  71. /* Enable the clock. */
  72. CLOCK_EnableClock(s_pdbClocks[PDB_GetInstance(base)]);
  73. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  74. /* Configure. */
  75. /* PDBx_SC. */
  76. tmp32 = base->SC &
  77. ~(PDB_SC_LDMOD_MASK | PDB_SC_PRESCALER_MASK | PDB_SC_TRGSEL_MASK | PDB_SC_MULT_MASK | PDB_SC_CONT_MASK);
  78. tmp32 |= PDB_SC_LDMOD(config->loadValueMode) | PDB_SC_PRESCALER(config->prescalerDivider) |
  79. PDB_SC_TRGSEL(config->triggerInputSource) | PDB_SC_MULT(config->dividerMultiplicationFactor);
  80. if (config->enableContinuousMode)
  81. {
  82. tmp32 |= PDB_SC_CONT_MASK;
  83. }
  84. base->SC = tmp32;
  85. PDB_Enable(base, true); /* Enable the PDB module. */
  86. }
  87. void PDB_Deinit(PDB_Type *base)
  88. {
  89. PDB_Enable(base, false); /* Disable the PDB module. */
  90. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  91. /* Disable the clock. */
  92. CLOCK_DisableClock(s_pdbClocks[PDB_GetInstance(base)]);
  93. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  94. }
  95. void PDB_GetDefaultConfig(pdb_config_t *config)
  96. {
  97. assert(NULL != config);
  98. config->loadValueMode = kPDB_LoadValueImmediately;
  99. config->prescalerDivider = kPDB_PrescalerDivider1;
  100. config->dividerMultiplicationFactor = kPDB_DividerMultiplicationFactor1;
  101. config->triggerInputSource = kPDB_TriggerSoftware;
  102. config->enableContinuousMode = false;
  103. }
  104. #if defined(FSL_FEATURE_PDB_HAS_DAC) && FSL_FEATURE_PDB_HAS_DAC
  105. void PDB_SetDACTriggerConfig(PDB_Type *base, uint32_t channel, pdb_dac_trigger_config_t *config)
  106. {
  107. assert(channel < PDB_INTC_COUNT);
  108. assert(NULL != config);
  109. uint32_t tmp32 = 0U;
  110. /* PDBx_DACINTC. */
  111. if (config->enableExternalTriggerInput)
  112. {
  113. tmp32 |= PDB_INTC_EXT_MASK;
  114. }
  115. if (config->enableIntervalTrigger)
  116. {
  117. tmp32 |= PDB_INTC_TOE_MASK;
  118. }
  119. base->DAC[channel].INTC = tmp32;
  120. }
  121. #endif /* FSL_FEATURE_PDB_HAS_DAC */