fsl_xbara.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2019 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_xbara.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.xbara"
  15. #endif
  16. /* Macros for entire XBARA_CTRL register. */
  17. #define XBARA_CTRLx(base, index) (((volatile uint16_t *)(&((base)->CTRL0)))[(index)])
  18. typedef union
  19. {
  20. uint8_t _u8[2];
  21. uint16_t _u16;
  22. } xbara_u8_u16_t;
  23. /*******************************************************************************
  24. * Prototypes
  25. ******************************************************************************/
  26. /*!
  27. * @brief Get the XBARA instance from peripheral base address.
  28. *
  29. * @param base XBARA peripheral base address.
  30. * @return XBARA instance.
  31. */
  32. static uint32_t XBARA_GetInstance(XBARA_Type *base);
  33. /*******************************************************************************
  34. * Variables
  35. ******************************************************************************/
  36. /* Array of XBARA peripheral base address. */
  37. static XBARA_Type *const s_xbaraBases[] = XBARA_BASE_PTRS;
  38. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  39. /* Array of XBARA clock name. */
  40. static const clock_ip_name_t s_xbaraClock[] = XBARA_CLOCKS;
  41. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  42. /*******************************************************************************
  43. * Code
  44. ******************************************************************************/
  45. static uint32_t XBARA_GetInstance(XBARA_Type *base)
  46. {
  47. uint32_t instance;
  48. /* Find the instance index from base address mappings. */
  49. for (instance = 0; instance < ARRAY_SIZE(s_xbaraBases); instance++)
  50. {
  51. if (s_xbaraBases[instance] == base)
  52. {
  53. break;
  54. }
  55. }
  56. assert(instance < ARRAY_SIZE(s_xbaraBases));
  57. return instance;
  58. }
  59. /*!
  60. * brief Initializes the XBARA module.
  61. *
  62. * This function un-gates the XBARA clock.
  63. *
  64. * param base XBARA peripheral address.
  65. */
  66. void XBARA_Init(XBARA_Type *base)
  67. {
  68. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  69. /* Enable XBARA module clock. */
  70. CLOCK_EnableClock(s_xbaraClock[XBARA_GetInstance(base)]);
  71. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  72. }
  73. /*!
  74. * brief Shuts down the XBARA module.
  75. *
  76. * This function disables XBARA clock.
  77. *
  78. * param base XBARA peripheral address.
  79. */
  80. void XBARA_Deinit(XBARA_Type *base)
  81. {
  82. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  83. /* Disable XBARA module clock. */
  84. CLOCK_DisableClock(s_xbaraClock[XBARA_GetInstance(base)]);
  85. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  86. }
  87. /*!
  88. * brief Sets a connection between the selected XBARA_IN[*] input and the XBARA_OUT[*] output signal.
  89. *
  90. * This function connects the XBARA input to the selected XBARA output.
  91. * If more than one XBARA module is available, only the inputs and outputs from the same module
  92. * can be connected.
  93. *
  94. * Example:
  95. code
  96. XBARA_SetSignalsConnection(XBARA, kXBARA_InputPIT_TRG0, kXBARA_OutputDMAMUX18);
  97. endcode
  98. *
  99. * param base XBARA peripheral address.
  100. * param input XBARA input signal.
  101. * param output XBARA output signal.
  102. */
  103. void XBARA_SetSignalsConnection(XBARA_Type *base, xbar_input_signal_t input, xbar_output_signal_t output)
  104. {
  105. xbara_u8_u16_t regVal;
  106. uint8_t byteInReg;
  107. uint8_t outputIndex = (uint8_t)output;
  108. byteInReg = outputIndex % 2U;
  109. regVal._u16 = XBARA_SELx(base, outputIndex);
  110. regVal._u8[byteInReg] = (uint8_t)input;
  111. XBARA_SELx(base, outputIndex) = regVal._u16;
  112. }
  113. /*!
  114. * brief Gets the active edge detection status.
  115. *
  116. * This function gets the active edge detect status of all XBARA_OUTs. If the
  117. * active edge occurs, the return value is asserted. When the interrupt or the DMA
  118. * functionality is enabled for the XBARA_OUTx, this field is 1 when the interrupt
  119. * or DMA request is asserted and 0 when the interrupt or DMA request has been
  120. * cleared.
  121. *
  122. * param base XBARA peripheral address.
  123. * return the mask of these status flag bits.
  124. */
  125. uint32_t XBARA_GetStatusFlags(XBARA_Type *base)
  126. {
  127. uint32_t status_flag;
  128. status_flag = ((uint32_t)base->CTRL0 & (XBARA_CTRL0_STS0_MASK | XBARA_CTRL0_STS1_MASK));
  129. status_flag |= (((uint32_t)base->CTRL1 & (XBARA_CTRL1_STS2_MASK | XBARA_CTRL1_STS3_MASK)) << 16U);
  130. return status_flag;
  131. }
  132. /*!
  133. * brief Clears the edge detection status flags of relative mask.
  134. *
  135. * param base XBARA peripheral address.
  136. * param mask the status flags to clear.
  137. */
  138. void XBARA_ClearStatusFlags(XBARA_Type *base, uint32_t mask)
  139. {
  140. uint16_t regVal;
  141. /* Assign regVal to CTRL0 register's value */
  142. regVal = (base->CTRL0);
  143. /* Perform this command to avoid writing 1 into interrupt flag bits */
  144. regVal &= (uint16_t)(~(XBARA_CTRL0_STS0_MASK | XBARA_CTRL0_STS1_MASK));
  145. /* Write 1 to interrupt flag bits corresponding to mask */
  146. regVal |= (uint16_t)(mask & (XBARA_CTRL0_STS0_MASK | XBARA_CTRL0_STS1_MASK));
  147. /* Write regVal value into CTRL0 register */
  148. base->CTRL0 = regVal;
  149. /* Assign regVal to CTRL1 register's value */
  150. regVal = (base->CTRL1);
  151. /* Perform this command to avoid writing 1 into interrupt flag bits */
  152. regVal &= (uint16_t)(~(XBARA_CTRL1_STS2_MASK | XBARA_CTRL1_STS3_MASK));
  153. /* Write 1 to interrupt flag bits corresponding to mask */
  154. regVal |= (uint16_t)((mask >> 16U) & (XBARA_CTRL1_STS2_MASK | XBARA_CTRL1_STS3_MASK));
  155. /* Write regVal value into CTRL1 register */
  156. base->CTRL1 = regVal;
  157. }
  158. /*!
  159. * brief Configures the XBARA control register.
  160. *
  161. * This function configures an XBARA control register. The active edge detection
  162. * and the DMA/IRQ function on the corresponding XBARA output can be set.
  163. *
  164. * Example:
  165. code
  166. xbara_control_config_t userConfig;
  167. userConfig.activeEdge = kXBARA_EdgeRising;
  168. userConfig.requestType = kXBARA_RequestInterruptEnalbe;
  169. XBARA_SetOutputSignalConfig(XBARA, kXBARA_OutputDMAMUX18, &userConfig);
  170. endcode
  171. *
  172. * param base XBARA peripheral address.
  173. * param output XBARA output number.
  174. * param controlConfig Pointer to structure that keeps configuration of control register.
  175. */
  176. void XBARA_SetOutputSignalConfig(XBARA_Type *base,
  177. xbar_output_signal_t output,
  178. const xbara_control_config_t *controlConfig)
  179. {
  180. uint8_t outputIndex = (uint8_t)output;
  181. uint8_t regIndex;
  182. uint8_t byteInReg;
  183. xbara_u8_u16_t regVal;
  184. assert(outputIndex < (uint32_t)FSL_FEATURE_XBARA_INTERRUPT_COUNT);
  185. regIndex = outputIndex / 2U;
  186. byteInReg = outputIndex % 2U;
  187. regVal._u16 = XBARA_CTRLx(base, regIndex);
  188. /* Don't clear the status flags. */
  189. regVal._u16 &= (uint16_t)(~(XBARA_CTRL0_STS0_MASK | XBARA_CTRL0_STS1_MASK));
  190. regVal._u8[byteInReg] = (uint8_t)(XBARA_CTRL0_EDGE0(controlConfig->activeEdge) |
  191. (uint16_t)(((uint32_t)controlConfig->requestType) << XBARA_CTRL0_DEN0_SHIFT));
  192. XBARA_CTRLx(base, regIndex) = regVal._u16;
  193. }