fsl_gpio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2015, 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_gpio.h"
  31. /*******************************************************************************
  32. * Variables
  33. ******************************************************************************/
  34. static PORT_Type *const s_portBases[] = PORT_BASE_PTRS;
  35. static GPIO_Type *const s_gpioBases[] = GPIO_BASE_PTRS;
  36. /*******************************************************************************
  37. * Prototypes
  38. ******************************************************************************/
  39. /*!
  40. * @brief Gets the GPIO instance according to the GPIO base
  41. *
  42. * @param base GPIO peripheral base pointer(PTA, PTB, PTC, etc.)
  43. * @retval GPIO instance
  44. */
  45. static uint32_t GPIO_GetInstance(GPIO_Type *base);
  46. /*******************************************************************************
  47. * Code
  48. ******************************************************************************/
  49. static uint32_t GPIO_GetInstance(GPIO_Type *base)
  50. {
  51. uint32_t instance;
  52. /* Find the instance index from base address mappings. */
  53. for (instance = 0; instance < ARRAY_SIZE(s_gpioBases); instance++)
  54. {
  55. if (s_gpioBases[instance] == base)
  56. {
  57. break;
  58. }
  59. }
  60. assert(instance < ARRAY_SIZE(s_gpioBases));
  61. return instance;
  62. }
  63. void GPIO_PinInit(GPIO_Type *base, uint32_t pin, const gpio_pin_config_t *config)
  64. {
  65. assert(config);
  66. if (config->pinDirection == kGPIO_DigitalInput)
  67. {
  68. base->PDDR &= ~(1U << pin);
  69. }
  70. else
  71. {
  72. GPIO_WritePinOutput(base, pin, config->outputLogic);
  73. base->PDDR |= (1U << pin);
  74. }
  75. }
  76. uint32_t GPIO_GetPinsInterruptFlags(GPIO_Type *base)
  77. {
  78. uint8_t instance;
  79. PORT_Type *portBase;
  80. instance = GPIO_GetInstance(base);
  81. portBase = s_portBases[instance];
  82. return portBase->ISFR;
  83. }
  84. void GPIO_ClearPinsInterruptFlags(GPIO_Type *base, uint32_t mask)
  85. {
  86. uint8_t instance;
  87. PORT_Type *portBase;
  88. instance = GPIO_GetInstance(base);
  89. portBase = s_portBases[instance];
  90. portBase->ISFR = mask;
  91. }
  92. #if defined(FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER) && FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER
  93. void GPIO_CheckAttributeBytes(GPIO_Type *base, gpio_checker_attribute_t attribute)
  94. {
  95. base->GACR = ((uint32_t)attribute << GPIO_GACR_ACB0_SHIFT) | ((uint32_t)attribute << GPIO_GACR_ACB1_SHIFT) |
  96. ((uint32_t)attribute << GPIO_GACR_ACB2_SHIFT) | ((uint32_t)attribute << GPIO_GACR_ACB3_SHIFT);
  97. }
  98. #endif
  99. #if defined(FSL_FEATURE_SOC_FGPIO_COUNT) && FSL_FEATURE_SOC_FGPIO_COUNT
  100. /*******************************************************************************
  101. * Variables
  102. ******************************************************************************/
  103. static FGPIO_Type *const s_fgpioBases[] = FGPIO_BASE_PTRS;
  104. /*******************************************************************************
  105. * Prototypes
  106. ******************************************************************************/
  107. /*!
  108. * @brief Gets the FGPIO instance according to the GPIO base
  109. *
  110. * @param base FGPIO peripheral base pointer(PTA, PTB, PTC, etc.)
  111. * @retval FGPIO instance
  112. */
  113. static uint32_t FGPIO_GetInstance(FGPIO_Type *base);
  114. /*******************************************************************************
  115. * Code
  116. ******************************************************************************/
  117. static uint32_t FGPIO_GetInstance(FGPIO_Type *base)
  118. {
  119. uint32_t instance;
  120. /* Find the instance index from base address mappings. */
  121. for (instance = 0; instance < ARRAY_SIZE(s_fgpioBases); instance++)
  122. {
  123. if (s_fgpioBases[instance] == base)
  124. {
  125. break;
  126. }
  127. }
  128. assert(instance < ARRAY_SIZE(s_fgpioBases));
  129. return instance;
  130. }
  131. void FGPIO_PinInit(FGPIO_Type *base, uint32_t pin, const gpio_pin_config_t *config)
  132. {
  133. assert(config);
  134. if (config->pinDirection == kGPIO_DigitalInput)
  135. {
  136. base->PDDR &= ~(1U << pin);
  137. }
  138. else
  139. {
  140. FGPIO_WritePinOutput(base, pin, config->outputLogic);
  141. base->PDDR |= (1U << pin);
  142. }
  143. }
  144. uint32_t FGPIO_GetPinsInterruptFlags(FGPIO_Type *base)
  145. {
  146. uint8_t instance;
  147. instance = FGPIO_GetInstance(base);
  148. PORT_Type *portBase;
  149. portBase = s_portBases[instance];
  150. return portBase->ISFR;
  151. }
  152. void FGPIO_ClearPinsInterruptFlags(FGPIO_Type *base, uint32_t mask)
  153. {
  154. uint8_t instance;
  155. instance = FGPIO_GetInstance(base);
  156. PORT_Type *portBase;
  157. portBase = s_portBases[instance];
  158. portBase->ISFR = mask;
  159. }
  160. #if defined(FSL_FEATURE_FGPIO_HAS_ATTRIBUTE_CHECKER) && FSL_FEATURE_FGPIO_HAS_ATTRIBUTE_CHECKER
  161. void FGPIO_CheckAttributeBytes(FGPIO_Type *base, gpio_checker_attribute_t attribute)
  162. {
  163. base->GACR = (attribute << FGPIO_GACR_ACB0_SHIFT) | (attribute << FGPIO_GACR_ACB1_SHIFT) |
  164. (attribute << FGPIO_GACR_ACB2_SHIFT) | (attribute << FGPIO_GACR_ACB3_SHIFT);
  165. }
  166. #endif
  167. #endif /* FSL_FEATURE_SOC_FGPIO_COUNT */