fsl_gint.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_gint.h"
  31. /*******************************************************************************
  32. * Variables
  33. ******************************************************************************/
  34. /*! @brief Pointers to GINT bases for each instance. */
  35. static GINT_Type *const s_gintBases[FSL_FEATURE_SOC_GINT_COUNT] = GINT_BASE_PTRS;
  36. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  37. /*! @brief Clocks for each instance. */
  38. static const clock_ip_name_t s_gintClocks[FSL_FEATURE_SOC_GINT_COUNT] = GINT_CLOCKS;
  39. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  40. /*! @brief Resets for each instance. */
  41. static const reset_ip_name_t s_gintResets[FSL_FEATURE_SOC_GINT_COUNT] = GINT_RSTS;
  42. /* @brief Irq number for each instance */
  43. static const IRQn_Type s_gintIRQ[FSL_FEATURE_SOC_GINT_COUNT] = GINT_IRQS;
  44. /*! @brief Callback function array for GINT(s). */
  45. static gint_cb_t s_gintCallback[FSL_FEATURE_SOC_GINT_COUNT];
  46. /*******************************************************************************
  47. * Code
  48. ******************************************************************************/
  49. static uint32_t GINT_GetInstance(GINT_Type *base)
  50. {
  51. uint32_t instance;
  52. /* Find the instance index from base address mappings. */
  53. for (instance = 0; instance < ARRAY_SIZE(s_gintBases); instance++)
  54. {
  55. if (s_gintBases[instance] == base)
  56. {
  57. break;
  58. }
  59. }
  60. assert(instance < ARRAY_SIZE(s_gintBases));
  61. return instance;
  62. }
  63. void GINT_Init(GINT_Type *base)
  64. {
  65. uint32_t instance;
  66. instance = GINT_GetInstance(base);
  67. s_gintCallback[instance] = NULL;
  68. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  69. /* Enable the peripheral clock */
  70. CLOCK_EnableClock(s_gintClocks[instance]);
  71. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  72. /* Reset the peripheral */
  73. RESET_PeripheralReset(s_gintResets[instance]);
  74. }
  75. void GINT_SetCtrl(GINT_Type *base, gint_comb_t comb, gint_trig_t trig, gint_cb_t callback)
  76. {
  77. uint32_t instance;
  78. instance = GINT_GetInstance(base);
  79. base->CTRL = (GINT_CTRL_COMB(comb) | GINT_CTRL_TRIG(trig));
  80. /* Save callback pointer */
  81. s_gintCallback[instance] = callback;
  82. }
  83. void GINT_GetCtrl(GINT_Type *base, gint_comb_t *comb, gint_trig_t *trig, gint_cb_t *callback)
  84. {
  85. uint32_t instance;
  86. instance = GINT_GetInstance(base);
  87. *comb = (gint_comb_t)((base->CTRL & GINT_CTRL_COMB_MASK) >> GINT_CTRL_COMB_SHIFT);
  88. *trig = (gint_trig_t)((base->CTRL & GINT_CTRL_TRIG_MASK) >> GINT_CTRL_TRIG_SHIFT);
  89. *callback = s_gintCallback[instance];
  90. }
  91. void GINT_ConfigPins(GINT_Type *base, gint_port_t port, uint32_t polarityMask, uint32_t enableMask)
  92. {
  93. base->PORT_POL[port] = polarityMask;
  94. base->PORT_ENA[port] = enableMask;
  95. }
  96. void GINT_GetConfigPins(GINT_Type *base, gint_port_t port, uint32_t *polarityMask, uint32_t *enableMask)
  97. {
  98. *polarityMask = base->PORT_POL[port];
  99. *enableMask = base->PORT_ENA[port];
  100. }
  101. void GINT_EnableCallback(GINT_Type *base)
  102. {
  103. uint32_t instance;
  104. instance = GINT_GetInstance(base);
  105. /* If GINT is configured in "AND" mode a spurious interrupt is generated.
  106. Clear status and pending interrupt before enabling the irq in NVIC. */
  107. GINT_ClrStatus(base);
  108. NVIC_ClearPendingIRQ(s_gintIRQ[instance]);
  109. EnableIRQ(s_gintIRQ[instance]);
  110. }
  111. void GINT_DisableCallback(GINT_Type *base)
  112. {
  113. uint32_t instance;
  114. instance = GINT_GetInstance(base);
  115. DisableIRQ(s_gintIRQ[instance]);
  116. GINT_ClrStatus(base);
  117. NVIC_ClearPendingIRQ(s_gintIRQ[instance]);
  118. }
  119. void GINT_Deinit(GINT_Type *base)
  120. {
  121. uint32_t instance;
  122. instance = GINT_GetInstance(base);
  123. /* Cleanup */
  124. GINT_DisableCallback(base);
  125. s_gintCallback[instance] = NULL;
  126. /* Reset the peripheral */
  127. RESET_PeripheralReset(s_gintResets[instance]);
  128. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  129. /* Disable the peripheral clock */
  130. CLOCK_DisableClock(s_gintClocks[instance]);
  131. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  132. }
  133. /* IRQ handler functions overloading weak symbols in the startup */
  134. #if defined(GINT0)
  135. void GINT0_DriverIRQHandler(void)
  136. {
  137. /* Clear interrupt before callback */
  138. s_gintBases[0]->CTRL |= GINT_CTRL_INT_MASK;
  139. /* Call user function */
  140. if (s_gintCallback[0] != NULL)
  141. {
  142. s_gintCallback[0]();
  143. }
  144. }
  145. #endif
  146. #if defined(GINT1)
  147. void GINT1_DriverIRQHandler(void)
  148. {
  149. /* Clear interrupt before callback */
  150. s_gintBases[1]->CTRL |= GINT_CTRL_INT_MASK;
  151. /* Call user function */
  152. if (s_gintCallback[1] != NULL)
  153. {
  154. s_gintCallback[1]();
  155. }
  156. }
  157. #endif
  158. #if defined(GINT2)
  159. void GINT2_DriverIRQHandler(void)
  160. {
  161. /* Clear interrupt before callback */
  162. s_gintBases[2]->CTRL |= GINT_CTRL_INT_MASK;
  163. /* Call user function */
  164. if (s_gintCallback[2] != NULL)
  165. {
  166. s_gintCallback[2]();
  167. }
  168. }
  169. #endif
  170. #if defined(GINT3)
  171. void GINT3_DriverIRQHandler(void)
  172. {
  173. /* Clear interrupt before callback */
  174. s_gintBases[3]->CTRL |= GINT_CTRL_INT_MASK;
  175. /* Call user function */
  176. if (s_gintCallback[3] != NULL)
  177. {
  178. s_gintCallback[3]();
  179. }
  180. }
  181. #endif
  182. #if defined(GINT4)
  183. void GINT4_DriverIRQHandler(void)
  184. {
  185. /* Clear interrupt before callback */
  186. s_gintBases[4]->CTRL |= GINT_CTRL_INT_MASK;
  187. /* Call user function */
  188. if (s_gintCallback[4] != NULL)
  189. {
  190. s_gintCallback[4]();
  191. }
  192. }
  193. #endif
  194. #if defined(GINT5)
  195. void GINT5_DriverIRQHandler(void)
  196. {
  197. /* Clear interrupt before callback */
  198. s_gintBases[5]->CTRL |= GINT_CTRL_INT_MASK;
  199. /* Call user function */
  200. if (s_gintCallback[5] != NULL)
  201. {
  202. s_gintCallback[5]();
  203. }
  204. }
  205. #endif
  206. #if defined(GINT6)
  207. void GINT6_DriverIRQHandler(void)
  208. {
  209. /* Clear interrupt before callback */
  210. s_gintBases[6]->CTRL |= GINT_CTRL_INT_MASK;
  211. /* Call user function */
  212. if (s_gintCallback[6] != NULL)
  213. {
  214. s_gintCallback[6]();
  215. }
  216. }
  217. #endif
  218. #if defined(GINT7)
  219. void GINT7_DriverIRQHandler(void)
  220. {
  221. /* Clear interrupt before callback */
  222. s_gintBases[7]->CTRL |= GINT_CTRL_INT_MASK;
  223. /* Call user function */
  224. if (s_gintCallback[7] != NULL)
  225. {
  226. s_gintCallback[7]();
  227. }
  228. }
  229. #endif