fsl_pit.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_pit.h"
  35. /*******************************************************************************
  36. * Prototypes
  37. ******************************************************************************/
  38. /*!
  39. * @brief Gets the instance from the base address to be used to gate or ungate the module clock
  40. *
  41. * @param base PIT peripheral base address
  42. *
  43. * @return The PIT instance
  44. */
  45. static uint32_t PIT_GetInstance(PIT_Type *base);
  46. /*******************************************************************************
  47. * Variables
  48. ******************************************************************************/
  49. /*! @brief Pointers to PIT bases for each instance. */
  50. static PIT_Type *const s_pitBases[] = PIT_BASE_PTRS;
  51. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  52. /*! @brief Pointers to PIT clocks for each instance. */
  53. static const clock_ip_name_t s_pitClocks[] = PIT_CLOCKS;
  54. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  55. /*******************************************************************************
  56. * Code
  57. ******************************************************************************/
  58. static uint32_t PIT_GetInstance(PIT_Type *base)
  59. {
  60. uint32_t instance;
  61. /* Find the instance index from base address mappings. */
  62. for (instance = 0; instance < ARRAY_SIZE(s_pitBases); instance++)
  63. {
  64. if (s_pitBases[instance] == base)
  65. {
  66. break;
  67. }
  68. }
  69. assert(instance < ARRAY_SIZE(s_pitBases));
  70. return instance;
  71. }
  72. void PIT_Init(PIT_Type *base, const pit_config_t *config)
  73. {
  74. assert(config);
  75. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  76. /* Ungate the PIT clock*/
  77. CLOCK_EnableClock(s_pitClocks[PIT_GetInstance(base)]);
  78. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  79. #if defined(FSL_FEATURE_PIT_HAS_MDIS) && FSL_FEATURE_PIT_HAS_MDIS
  80. /* Enable PIT timers */
  81. base->MCR &= ~PIT_MCR_MDIS_MASK;
  82. #endif
  83. /* Config timer operation when in debug mode */
  84. if (config->enableRunInDebug)
  85. {
  86. base->MCR &= ~PIT_MCR_FRZ_MASK;
  87. }
  88. else
  89. {
  90. base->MCR |= PIT_MCR_FRZ_MASK;
  91. }
  92. }
  93. void PIT_Deinit(PIT_Type *base)
  94. {
  95. #if defined(FSL_FEATURE_PIT_HAS_MDIS) && FSL_FEATURE_PIT_HAS_MDIS
  96. /* Disable PIT timers */
  97. base->MCR |= PIT_MCR_MDIS_MASK;
  98. #endif
  99. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  100. /* Gate the PIT clock*/
  101. CLOCK_DisableClock(s_pitClocks[PIT_GetInstance(base)]);
  102. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  103. }
  104. #if defined(FSL_FEATURE_PIT_HAS_LIFETIME_TIMER) && FSL_FEATURE_PIT_HAS_LIFETIME_TIMER
  105. uint64_t PIT_GetLifetimeTimerCount(PIT_Type *base)
  106. {
  107. uint32_t valueH = 0U;
  108. uint32_t valueL = 0U;
  109. /* LTMR64H should be read before LTMR64L */
  110. valueH = base->LTMR64H;
  111. valueL = base->LTMR64L;
  112. return (((uint64_t)valueH << 32U) + (uint64_t)(valueL));
  113. }
  114. #endif /* FSL_FEATURE_PIT_HAS_LIFETIME_TIMER */