fsl_gpt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2021 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_gpt.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.gpt"
  12. #endif
  13. /*******************************************************************************
  14. * Prototypes
  15. ******************************************************************************/
  16. /*******************************************************************************
  17. * Variables
  18. ******************************************************************************/
  19. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  20. /*! @brief Pointers to GPT bases for each instance. */
  21. static GPT_Type *const s_gptBases[] = GPT_BASE_PTRS;
  22. /*! @brief Pointers to GPT clocks for each instance. */
  23. static const clock_ip_name_t s_gptClocks[] = GPT_CLOCKS;
  24. /*******************************************************************************
  25. * Code
  26. ******************************************************************************/
  27. static uint32_t GPT_GetInstance(GPT_Type *base)
  28. {
  29. uint32_t instance;
  30. /* Find the instance index from base address mappings. */
  31. for (instance = 0U; instance < ARRAY_SIZE(s_gptBases); instance++)
  32. {
  33. if (s_gptBases[instance] == base)
  34. {
  35. break;
  36. }
  37. }
  38. assert(instance < ARRAY_SIZE(s_gptBases));
  39. return instance;
  40. }
  41. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  42. /*!
  43. * brief Initialize GPT to reset state and initialize running mode.
  44. *
  45. * param base GPT peripheral base address.
  46. * param initConfig GPT mode setting configuration.
  47. */
  48. void GPT_Init(GPT_Type *base, const gpt_config_t *initConfig)
  49. {
  50. assert(NULL != initConfig);
  51. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  52. /* Ungate the GPT clock*/
  53. (void)CLOCK_EnableClock(s_gptClocks[GPT_GetInstance(base)]);
  54. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  55. base->CR = 0U;
  56. GPT_SoftwareReset(base);
  57. base->CR =
  58. (initConfig->enableFreeRun ? GPT_CR_FRR_MASK : 0UL) | (initConfig->enableRunInWait ? GPT_CR_WAITEN_MASK : 0UL) |
  59. (initConfig->enableRunInStop ? GPT_CR_STOPEN_MASK : 0UL) |
  60. (initConfig->enableRunInDoze ? GPT_CR_DOZEEN_MASK : 0UL) |
  61. (initConfig->enableRunInDbg ? GPT_CR_DBGEN_MASK : 0UL) | (initConfig->enableMode ? GPT_CR_ENMOD_MASK : 0UL);
  62. GPT_SetClockSource(base, initConfig->clockSource);
  63. GPT_SetClockDivider(base, initConfig->divider);
  64. }
  65. /*!
  66. * brief Disables the module and gates the GPT clock.
  67. *
  68. * param base GPT peripheral base address.
  69. */
  70. void GPT_Deinit(GPT_Type *base)
  71. {
  72. /* Disable GPT timers */
  73. base->CR = 0U;
  74. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  75. /* Gate the GPT clock*/
  76. (void)CLOCK_DisableClock(s_gptClocks[GPT_GetInstance(base)]);
  77. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  78. }
  79. /*!
  80. * brief Fills in the GPT configuration structure with default settings.
  81. *
  82. * The default values are:
  83. * code
  84. * config->clockSource = kGPT_ClockSource_Periph;
  85. * config->divider = 1U;
  86. * config->enableRunInStop = true;
  87. * config->enableRunInWait = true;
  88. * config->enableRunInDoze = false;
  89. * config->enableRunInDbg = false;
  90. * config->enableFreeRun = false;
  91. * config->enableMode = true;
  92. * endcode
  93. * param config Pointer to the user configuration structure.
  94. */
  95. void GPT_GetDefaultConfig(gpt_config_t *config)
  96. {
  97. assert(NULL != config);
  98. /* Initializes the configure structure to zero. */
  99. (void)memset(config, 0, sizeof(*config));
  100. config->clockSource = kGPT_ClockSource_Periph;
  101. config->divider = 1U;
  102. config->enableRunInStop = true;
  103. config->enableRunInWait = true;
  104. config->enableRunInDoze = false;
  105. config->enableRunInDbg = false;
  106. config->enableFreeRun = false;
  107. config->enableMode = true;
  108. }