fsl_pdb.c 5.4 KB

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