fsl_utick.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, 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_utick.h"
  35. #include "fsl_power.h"
  36. /*******************************************************************************
  37. * Definitions
  38. ******************************************************************************/
  39. /* Component ID definition, used by tools. */
  40. #ifndef FSL_COMPONENT_ID
  41. #define FSL_COMPONENT_ID "platform.drivers.utick"
  42. #endif
  43. /* Typedef for interrupt handler. */
  44. typedef void (*utick_isr_t)(UTICK_Type *base, utick_callback_t cb);
  45. /*******************************************************************************
  46. * Prototypes
  47. ******************************************************************************/
  48. /*!
  49. * @brief Gets the instance from the base address
  50. *
  51. * @param base UTICK peripheral base address
  52. *
  53. * @return The UTICK instance
  54. */
  55. static uint32_t UTICK_GetInstance(UTICK_Type *base);
  56. /*******************************************************************************
  57. * Variables
  58. ******************************************************************************/
  59. /* Array of UTICK handle. */
  60. static utick_callback_t s_utickHandle[FSL_FEATURE_SOC_UTICK_COUNT];
  61. /* Array of UTICK peripheral base address. */
  62. static UTICK_Type *const s_utickBases[] = UTICK_BASE_PTRS;
  63. /* Array of UTICK IRQ number. */
  64. static const IRQn_Type s_utickIRQ[] = UTICK_IRQS;
  65. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  66. /* Array of UTICK clock name. */
  67. static const clock_ip_name_t s_utickClock[] = UTICK_CLOCKS;
  68. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  69. /* UTICK ISR for transactional APIs. */
  70. static utick_isr_t s_utickIsr;
  71. /*******************************************************************************
  72. * Code
  73. ******************************************************************************/
  74. static uint32_t UTICK_GetInstance(UTICK_Type *base)
  75. {
  76. uint32_t instance;
  77. /* Find the instance index from base address mappings. */
  78. for (instance = 0; instance < ARRAY_SIZE(s_utickBases); instance++)
  79. {
  80. if (s_utickBases[instance] == base)
  81. {
  82. break;
  83. }
  84. }
  85. assert(instance < ARRAY_SIZE(s_utickBases));
  86. return instance;
  87. }
  88. void UTICK_SetTick(UTICK_Type *base, utick_mode_t mode, uint32_t count, utick_callback_t cb)
  89. {
  90. uint32_t instance;
  91. /* Get instance from peripheral base address. */
  92. instance = UTICK_GetInstance(base);
  93. /* Save the handle in global variables to support the double weak mechanism. */
  94. s_utickHandle[instance] = cb;
  95. EnableDeepSleepIRQ(s_utickIRQ[instance]);
  96. base->CTRL = count | UTICK_CTRL_REPEAT(mode);
  97. }
  98. void UTICK_Init(UTICK_Type *base)
  99. {
  100. /* Enable utick clock */
  101. CLOCK_EnableClock(s_utickClock[UTICK_GetInstance(base)]);
  102. /* Power up Watchdog oscillator*/
  103. POWER_DisablePD(kPDRUNCFG_PD_WDT_OSC);
  104. s_utickIsr = UTICK_HandleIRQ;
  105. }
  106. void UTICK_Deinit(UTICK_Type *base)
  107. {
  108. /* Turn off utick */
  109. base->CTRL = 0;
  110. /* Disable utick clock */
  111. CLOCK_DisableClock(s_utickClock[UTICK_GetInstance(base)]);
  112. }
  113. uint32_t UTICK_GetStatusFlags(UTICK_Type *base)
  114. {
  115. return (base->STAT);
  116. }
  117. void UTICK_ClearStatusFlags(UTICK_Type *base)
  118. {
  119. base->STAT = UTICK_STAT_INTR_MASK;
  120. }
  121. void UTICK_HandleIRQ(UTICK_Type *base, utick_callback_t cb)
  122. {
  123. UTICK_ClearStatusFlags(base);
  124. if (cb)
  125. {
  126. cb();
  127. }
  128. }
  129. #if defined(UTICK0)
  130. void UTICK0_DriverIRQHandler(void)
  131. {
  132. s_utickIsr(UTICK0, s_utickHandle[0]);
  133. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  134. exception return operation might vector to incorrect interrupt */
  135. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  136. __DSB();
  137. #endif
  138. }
  139. #endif
  140. #if defined(UTICK1)
  141. void UTICK1_DriverIRQHandler(void)
  142. {
  143. s_utickIsr(UTICK1, s_utickHandle[1]);
  144. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  145. exception return operation might vector to incorrect interrupt */
  146. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  147. __DSB();
  148. #endif
  149. }
  150. #endif
  151. #if defined(UTICK2)
  152. void UTICK2_DriverIRQHandler(void)
  153. {
  154. s_utickIsr(UTICK2, s_utickHandle[2]);
  155. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  156. exception return operation might vector to incorrect interrupt */
  157. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  158. __DSB();
  159. #endif
  160. }
  161. #endif