fsl_pdb.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_pdb.h"
  35. /*******************************************************************************
  36. * Prototypes
  37. ******************************************************************************/
  38. /*!
  39. * @brief Get instance number for PDB module.
  40. *
  41. * @param base PDB peripheral base address
  42. */
  43. static uint32_t PDB_GetInstance(PDB_Type *base);
  44. /*******************************************************************************
  45. * Variables
  46. ******************************************************************************/
  47. /*! @brief Pointers to PDB bases for each instance. */
  48. static PDB_Type *const s_pdbBases[] = PDB_BASE_PTRS;
  49. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  50. /*! @brief Pointers to PDB clocks for each instance. */
  51. static const clock_ip_name_t s_pdbClocks[] = PDB_CLOCKS;
  52. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  53. /*******************************************************************************
  54. * Codes
  55. ******************************************************************************/
  56. static uint32_t PDB_GetInstance(PDB_Type *base)
  57. {
  58. uint32_t instance;
  59. /* Find the instance index from base address mappings. */
  60. for (instance = 0; instance < ARRAY_SIZE(s_pdbBases); instance++)
  61. {
  62. if (s_pdbBases[instance] == base)
  63. {
  64. break;
  65. }
  66. }
  67. assert(instance < ARRAY_SIZE(s_pdbBases));
  68. return instance;
  69. }
  70. void PDB_Init(PDB_Type *base, const pdb_config_t *config)
  71. {
  72. assert(NULL != config);
  73. uint32_t tmp32;
  74. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  75. /* Enable the clock. */
  76. CLOCK_EnableClock(s_pdbClocks[PDB_GetInstance(base)]);
  77. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  78. /* Configure. */
  79. /* PDBx_SC. */
  80. tmp32 = base->SC &
  81. ~(PDB_SC_LDMOD_MASK | PDB_SC_PRESCALER_MASK | PDB_SC_TRGSEL_MASK | PDB_SC_MULT_MASK | PDB_SC_CONT_MASK);
  82. tmp32 |= PDB_SC_LDMOD(config->loadValueMode) | PDB_SC_PRESCALER(config->prescalerDivider) |
  83. PDB_SC_TRGSEL(config->triggerInputSource) | PDB_SC_MULT(config->dividerMultiplicationFactor);
  84. if (config->enableContinuousMode)
  85. {
  86. tmp32 |= PDB_SC_CONT_MASK;
  87. }
  88. base->SC = tmp32;
  89. PDB_Enable(base, true); /* Enable the PDB module. */
  90. }
  91. void PDB_Deinit(PDB_Type *base)
  92. {
  93. PDB_Enable(base, false); /* Disable the PDB module. */
  94. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  95. /* Disable the clock. */
  96. CLOCK_DisableClock(s_pdbClocks[PDB_GetInstance(base)]);
  97. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  98. }
  99. void PDB_GetDefaultConfig(pdb_config_t *config)
  100. {
  101. assert(NULL != config);
  102. config->loadValueMode = kPDB_LoadValueImmediately;
  103. config->prescalerDivider = kPDB_PrescalerDivider1;
  104. config->dividerMultiplicationFactor = kPDB_DividerMultiplicationFactor1;
  105. config->triggerInputSource = kPDB_TriggerSoftware;
  106. config->enableContinuousMode = false;
  107. }
  108. #if defined(FSL_FEATURE_PDB_HAS_DAC) && FSL_FEATURE_PDB_HAS_DAC
  109. void PDB_SetDACTriggerConfig(PDB_Type *base, uint32_t channel, pdb_dac_trigger_config_t *config)
  110. {
  111. assert(channel < PDB_INTC_COUNT);
  112. assert(NULL != config);
  113. uint32_t tmp32 = 0U;
  114. /* PDBx_DACINTC. */
  115. if (config->enableExternalTriggerInput)
  116. {
  117. tmp32 |= PDB_INTC_EXT_MASK;
  118. }
  119. if (config->enableIntervalTrigger)
  120. {
  121. tmp32 |= PDB_INTC_TOE_MASK;
  122. }
  123. base->DAC[channel].INTC = tmp32;
  124. }
  125. #endif /* FSL_FEATURE_PDB_HAS_DAC */