25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

147 satır
4.5 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_pit.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.pit"
  12. #endif
  13. /*******************************************************************************
  14. * Prototypes
  15. ******************************************************************************/
  16. /*!
  17. * @brief Gets the instance from the base address to be used to gate or ungate the module clock
  18. *
  19. * @param base PIT peripheral base address
  20. *
  21. * @return The PIT instance
  22. */
  23. static uint32_t PIT_GetInstance(PIT_Type *base);
  24. /*******************************************************************************
  25. * Variables
  26. ******************************************************************************/
  27. /*! @brief Pointers to PIT bases for each instance. */
  28. static PIT_Type *const s_pitBases[] = PIT_BASE_PTRS;
  29. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  30. /*! @brief Pointers to PIT clocks for each instance. */
  31. static const clock_ip_name_t s_pitClocks[] = PIT_CLOCKS;
  32. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  33. /*******************************************************************************
  34. * Code
  35. ******************************************************************************/
  36. static uint32_t PIT_GetInstance(PIT_Type *base)
  37. {
  38. uint32_t instance;
  39. /* Find the instance index from base address mappings. */
  40. for (instance = 0; instance < ARRAY_SIZE(s_pitBases); instance++)
  41. {
  42. if (s_pitBases[instance] == base)
  43. {
  44. break;
  45. }
  46. }
  47. assert(instance < ARRAY_SIZE(s_pitBases));
  48. return instance;
  49. }
  50. /*!
  51. * brief Ungates the PIT clock, enables the PIT module, and configures the peripheral for basic operations.
  52. *
  53. * note This API should be called at the beginning of the application using the PIT driver.
  54. *
  55. * param base PIT peripheral base address
  56. * param config Pointer to the user's PIT config structure
  57. */
  58. void PIT_Init(PIT_Type *base, const pit_config_t *config)
  59. {
  60. assert(NULL != config);
  61. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  62. /* Ungate the PIT clock*/
  63. CLOCK_EnableClock(s_pitClocks[PIT_GetInstance(base)]);
  64. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  65. #if defined(FSL_FEATURE_PIT_HAS_MDIS) && FSL_FEATURE_PIT_HAS_MDIS
  66. /* Enable PIT timers */
  67. base->MCR &= ~PIT_MCR_MDIS_MASK;
  68. #endif
  69. #if defined(FSL_FEATURE_PIT_TIMER_COUNT) && (FSL_FEATURE_PIT_TIMER_COUNT)
  70. /* Clear all status bits for all channels to make sure the status of all TCTRL registers is clean. */
  71. for (uint8_t i = 0U; i < (uint32_t)FSL_FEATURE_PIT_TIMER_COUNT; i++)
  72. {
  73. base->CHANNEL[i].TCTRL &= ~(PIT_TCTRL_TEN_MASK | PIT_TCTRL_TIE_MASK | PIT_TCTRL_CHN_MASK);
  74. }
  75. #endif /* FSL_FEATURE_PIT_TIMER_COUNT */
  76. /* Config timer operation when in debug mode */
  77. if (true == config->enableRunInDebug)
  78. {
  79. base->MCR &= ~PIT_MCR_FRZ_MASK;
  80. }
  81. else
  82. {
  83. base->MCR |= PIT_MCR_FRZ_MASK;
  84. }
  85. }
  86. /*!
  87. * brief Gates the PIT clock and disables the PIT module.
  88. *
  89. * param base PIT peripheral base address
  90. */
  91. void PIT_Deinit(PIT_Type *base)
  92. {
  93. #if defined(FSL_FEATURE_PIT_HAS_MDIS) && FSL_FEATURE_PIT_HAS_MDIS
  94. /* Disable PIT timers */
  95. base->MCR |= PIT_MCR_MDIS_MASK;
  96. #endif
  97. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  98. /* Gate the PIT clock*/
  99. CLOCK_DisableClock(s_pitClocks[PIT_GetInstance(base)]);
  100. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  101. }
  102. #if defined(FSL_FEATURE_PIT_HAS_LIFETIME_TIMER) && FSL_FEATURE_PIT_HAS_LIFETIME_TIMER
  103. /*!
  104. * brief Reads the current lifetime counter value.
  105. *
  106. * The lifetime timer is a 64-bit timer which chains timer 0 and timer 1 together.
  107. * Timer 0 and 1 are chained by calling the PIT_SetTimerChainMode before using this timer.
  108. * The period of lifetime timer is equal to the "period of timer 0 * period of timer 1".
  109. * For the 64-bit value, the higher 32-bit has the value of timer 1, and the lower 32-bit
  110. * has the value of timer 0.
  111. *
  112. * param base PIT peripheral base address
  113. *
  114. * return Current lifetime timer value
  115. */
  116. uint64_t PIT_GetLifetimeTimerCount(PIT_Type *base)
  117. {
  118. uint32_t valueH = 0U;
  119. uint32_t valueL = 0U;
  120. /* LTMR64H should be read before LTMR64L */
  121. valueH = base->LTMR64H;
  122. valueL = base->LTMR64L;
  123. return (((uint64_t)valueH << 32U) + (uint64_t)(valueL));
  124. }
  125. #endif /* FSL_FEATURE_PIT_HAS_LIFETIME_TIMER */