fsl_gpt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_gpt.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.gpt"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. /*******************************************************************************
  43. * Variables
  44. ******************************************************************************/
  45. /*! @brief Pointers to GPT bases for each instance. */
  46. static GPT_Type *const s_gptBases[] = GPT_BASE_PTRS;
  47. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  48. /*! @brief Pointers to GPT clocks for each instance. */
  49. static const clock_ip_name_t s_gptClocks[] = GPT_CLOCKS;
  50. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  51. /*******************************************************************************
  52. * Code
  53. ******************************************************************************/
  54. static uint32_t GPT_GetInstance(GPT_Type *base)
  55. {
  56. uint32_t instance;
  57. /* Find the instance index from base address mappings. */
  58. for (instance = 0U; instance < ARRAY_SIZE(s_gptBases); instance++)
  59. {
  60. if (s_gptBases[instance] == base)
  61. {
  62. break;
  63. }
  64. }
  65. assert(instance < ARRAY_SIZE(s_gptBases));
  66. return instance;
  67. }
  68. void GPT_Init(GPT_Type *base, const gpt_config_t *initConfig)
  69. {
  70. assert(initConfig);
  71. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  72. /* Ungate the GPT clock*/
  73. CLOCK_EnableClock(s_gptClocks[GPT_GetInstance(base)]);
  74. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  75. base->CR = 0U;
  76. GPT_SoftwareReset(base);
  77. base->CR =
  78. (initConfig->enableFreeRun ? GPT_CR_FRR_MASK : 0U) | (initConfig->enableRunInWait ? GPT_CR_WAITEN_MASK : 0U) |
  79. (initConfig->enableRunInStop ? GPT_CR_STOPEN_MASK : 0U) |
  80. (initConfig->enableRunInDoze ? GPT_CR_DOZEEN_MASK : 0U) |
  81. (initConfig->enableRunInDbg ? GPT_CR_DBGEN_MASK : 0U) | (initConfig->enableMode ? GPT_CR_ENMOD_MASK : 0U);
  82. GPT_SetClockSource(base, initConfig->clockSource);
  83. GPT_SetClockDivider(base, initConfig->divider);
  84. }
  85. void GPT_Deinit(GPT_Type *base)
  86. {
  87. /* Disable GPT timers */
  88. base->CR = 0U;
  89. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  90. /* Gate the GPT clock*/
  91. CLOCK_DisableClock(s_gptClocks[GPT_GetInstance(base)]);
  92. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  93. }
  94. void GPT_GetDefaultConfig(gpt_config_t *config)
  95. {
  96. assert(config);
  97. config->clockSource = kGPT_ClockSource_Periph;
  98. config->divider = 1U;
  99. config->enableRunInStop = true;
  100. config->enableRunInWait = true;
  101. config->enableRunInDoze = false;
  102. config->enableRunInDbg = false;
  103. config->enableFreeRun = false;
  104. config->enableMode = true;
  105. }