fsl_gpio.c 8.4 KB

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