fsl_pit.c 4.8 KB

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