fsl_gpio.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_gpio.h"
  35. /*******************************************************************************
  36. * Variables
  37. ******************************************************************************/
  38. #if !(defined(FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT) && FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT)
  39. static PORT_Type *const s_portBases[] = PORT_BASE_PTRS;
  40. static GPIO_Type *const s_gpioBases[] = GPIO_BASE_PTRS;
  41. #endif
  42. #if defined(FSL_FEATURE_SOC_FGPIO_COUNT) && FSL_FEATURE_SOC_FGPIO_COUNT
  43. #if defined(FSL_FEATURE_PCC_HAS_FGPIO_CLOCK_GATE_CONTROL) && FSL_FEATURE_PCC_HAS_FGPIO_CLOCK_GATE_CONTROL
  44. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  45. /*! @brief Array to map FGPIO instance number to clock name. */
  46. static const clock_ip_name_t s_fgpioClockName[] = FGPIO_CLOCKS;
  47. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  48. #endif /* FSL_FEATURE_PCC_HAS_FGPIO_CLOCK_GATE_CONTROL */
  49. #endif /* FSL_FEATURE_SOC_FGPIO_COUNT */
  50. /*******************************************************************************
  51. * Prototypes
  52. ******************************************************************************/
  53. #if !(defined(FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT) && FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT)
  54. /*!
  55. * @brief Gets the GPIO instance according to the GPIO base
  56. *
  57. * @param base GPIO peripheral base pointer(PTA, PTB, PTC, etc.)
  58. * @retval GPIO instance
  59. */
  60. static uint32_t GPIO_GetInstance(GPIO_Type *base);
  61. #endif
  62. /*******************************************************************************
  63. * Code
  64. ******************************************************************************/
  65. #if !(defined(FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT) && FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT)
  66. static uint32_t GPIO_GetInstance(GPIO_Type *base)
  67. {
  68. uint32_t instance;
  69. /* Find the instance index from base address mappings. */
  70. for (instance = 0; instance < ARRAY_SIZE(s_gpioBases); instance++)
  71. {
  72. if (s_gpioBases[instance] == base)
  73. {
  74. break;
  75. }
  76. }
  77. assert(instance < ARRAY_SIZE(s_gpioBases));
  78. return instance;
  79. }
  80. #endif
  81. void GPIO_PinInit(GPIO_Type *base, uint32_t pin, const gpio_pin_config_t *config)
  82. {
  83. assert(config);
  84. if (config->pinDirection == kGPIO_DigitalInput)
  85. {
  86. base->PDDR &= ~(1U << pin);
  87. }
  88. else
  89. {
  90. GPIO_WritePinOutput(base, pin, config->outputLogic);
  91. base->PDDR |= (1U << pin);
  92. }
  93. }
  94. #if !(defined(FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT) && FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT)
  95. uint32_t GPIO_PortGetInterruptFlags(GPIO_Type *base)
  96. {
  97. uint8_t instance;
  98. PORT_Type *portBase;
  99. instance = GPIO_GetInstance(base);
  100. portBase = s_portBases[instance];
  101. return portBase->ISFR;
  102. }
  103. void GPIO_PortClearInterruptFlags(GPIO_Type *base, uint32_t mask)
  104. {
  105. uint8_t instance;
  106. PORT_Type *portBase;
  107. instance = GPIO_GetInstance(base);
  108. portBase = s_portBases[instance];
  109. portBase->ISFR = mask;
  110. }
  111. #endif
  112. #if defined(FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER) && FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER
  113. void GPIO_CheckAttributeBytes(GPIO_Type *base, gpio_checker_attribute_t attribute)
  114. {
  115. base->GACR = ((uint32_t)attribute << GPIO_GACR_ACB0_SHIFT) | ((uint32_t)attribute << GPIO_GACR_ACB1_SHIFT) |
  116. ((uint32_t)attribute << GPIO_GACR_ACB2_SHIFT) | ((uint32_t)attribute << GPIO_GACR_ACB3_SHIFT);
  117. }
  118. #endif
  119. #if defined(FSL_FEATURE_SOC_FGPIO_COUNT) && FSL_FEATURE_SOC_FGPIO_COUNT
  120. /*******************************************************************************
  121. * Variables
  122. ******************************************************************************/
  123. #if !(defined(FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT) && FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT)
  124. static FGPIO_Type *const s_fgpioBases[] = FGPIO_BASE_PTRS;
  125. #endif
  126. /*******************************************************************************
  127. * Prototypes
  128. ******************************************************************************/
  129. #if !(defined(FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT) && FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT)
  130. /*!
  131. * @brief Gets the FGPIO instance according to the GPIO base
  132. *
  133. * @param base FGPIO peripheral base pointer(PTA, PTB, PTC, etc.)
  134. * @retval FGPIO instance
  135. */
  136. static uint32_t FGPIO_GetInstance(FGPIO_Type *base);
  137. #endif
  138. /*******************************************************************************
  139. * Code
  140. ******************************************************************************/
  141. #if !(defined(FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT) && FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT)
  142. static uint32_t FGPIO_GetInstance(FGPIO_Type *base)
  143. {
  144. uint32_t instance;
  145. /* Find the instance index from base address mappings. */
  146. for (instance = 0; instance < ARRAY_SIZE(s_fgpioBases); instance++)
  147. {
  148. if (s_fgpioBases[instance] == base)
  149. {
  150. break;
  151. }
  152. }
  153. assert(instance < ARRAY_SIZE(s_fgpioBases));
  154. return instance;
  155. }
  156. #endif
  157. #if defined(FSL_FEATURE_PCC_HAS_FGPIO_CLOCK_GATE_CONTROL) && FSL_FEATURE_PCC_HAS_FGPIO_CLOCK_GATE_CONTROL
  158. void FGPIO_PortInit(FGPIO_Type *base)
  159. {
  160. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  161. /* Ungate FGPIO periphral clock */
  162. CLOCK_EnableClock(s_fgpioClockName[FGPIO_GetInstance(base)]);
  163. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  164. }
  165. #endif /* FSL_FEATURE_PCC_HAS_FGPIO_CLOCK_GATE_CONTROL */
  166. void FGPIO_PinInit(FGPIO_Type *base, uint32_t pin, const gpio_pin_config_t *config)
  167. {
  168. assert(config);
  169. if (config->pinDirection == kGPIO_DigitalInput)
  170. {
  171. base->PDDR &= ~(1U << pin);
  172. }
  173. else
  174. {
  175. FGPIO_WritePinOutput(base, pin, config->outputLogic);
  176. base->PDDR |= (1U << pin);
  177. }
  178. }
  179. #if !(defined(FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT) && FSL_FEATURE_GPIO_HAS_NO_PORTINTERRUPT)
  180. uint32_t FGPIO_PortGetInterruptFlags(FGPIO_Type *base)
  181. {
  182. uint8_t instance;
  183. instance = FGPIO_GetInstance(base);
  184. PORT_Type *portBase;
  185. portBase = s_portBases[instance];
  186. return portBase->ISFR;
  187. }
  188. void FGPIO_PortClearInterruptFlags(FGPIO_Type *base, uint32_t mask)
  189. {
  190. uint8_t instance;
  191. instance = FGPIO_GetInstance(base);
  192. PORT_Type *portBase;
  193. portBase = s_portBases[instance];
  194. portBase->ISFR = mask;
  195. }
  196. #endif
  197. #if defined(FSL_FEATURE_FGPIO_HAS_ATTRIBUTE_CHECKER) && FSL_FEATURE_FGPIO_HAS_ATTRIBUTE_CHECKER
  198. void FGPIO_CheckAttributeBytes(FGPIO_Type *base, gpio_checker_attribute_t attribute)
  199. {
  200. base->GACR = (attribute << FGPIO_GACR_ACB0_SHIFT) | (attribute << FGPIO_GACR_ACB1_SHIFT) |
  201. (attribute << FGPIO_GACR_ACB2_SHIFT) | (attribute << FGPIO_GACR_ACB3_SHIFT);
  202. }
  203. #endif
  204. #endif /* FSL_FEATURE_SOC_FGPIO_COUNT */