fsl_gpio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, 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.igpio"
  38. #endif
  39. /*******************************************************************************
  40. * Variables
  41. ******************************************************************************/
  42. /* Array of GPIO peripheral base address. */
  43. static GPIO_Type *const s_gpioBases[] = GPIO_BASE_PTRS;
  44. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  45. /* Array of GPIO clock name. */
  46. static const clock_ip_name_t s_gpioClock[] = GPIO_CLOCKS;
  47. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  48. /*******************************************************************************
  49. * Prototypes
  50. ******************************************************************************/
  51. /*!
  52. * @brief Gets the GPIO instance according to the GPIO base
  53. *
  54. * @param base GPIO peripheral base pointer(PTA, PTB, PTC, etc.)
  55. * @retval GPIO instance
  56. */
  57. static uint32_t GPIO_GetInstance(GPIO_Type *base);
  58. /*******************************************************************************
  59. * Code
  60. ******************************************************************************/
  61. static uint32_t GPIO_GetInstance(GPIO_Type *base)
  62. {
  63. uint32_t instance;
  64. /* Find the instance index from base address mappings. */
  65. for (instance = 0; instance < ARRAY_SIZE(s_gpioBases); instance++)
  66. {
  67. if (s_gpioBases[instance] == base)
  68. {
  69. break;
  70. }
  71. }
  72. assert(instance < ARRAY_SIZE(s_gpioBases));
  73. return instance;
  74. }
  75. void GPIO_PinInit(GPIO_Type *base, uint32_t pin, const gpio_pin_config_t *Config)
  76. {
  77. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  78. /* Enable GPIO clock. */
  79. CLOCK_EnableClock(s_gpioClock[GPIO_GetInstance(base)]);
  80. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  81. /* Register reset to default value */
  82. base->IMR &= ~(1U << pin);
  83. /* Configure GPIO pin direction */
  84. if (Config->direction == kGPIO_DigitalInput)
  85. {
  86. base->GDIR &= ~(1U << pin);
  87. }
  88. else
  89. {
  90. GPIO_PinWrite(base, pin, Config->outputLogic);
  91. base->GDIR |= (1U << pin);
  92. }
  93. /* Configure GPIO pin interrupt mode */
  94. GPIO_SetPinInterruptConfig(base, pin, Config->interruptMode);
  95. }
  96. void GPIO_PinWrite(GPIO_Type *base, uint32_t pin, uint8_t output)
  97. {
  98. assert(pin < 32);
  99. if (output == 0U)
  100. {
  101. base->DR &= ~(1U << pin); /* Set pin output to low level.*/
  102. }
  103. else
  104. {
  105. base->DR |= (1U << pin); /* Set pin output to high level.*/
  106. }
  107. }
  108. void GPIO_PinSetInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode)
  109. {
  110. volatile uint32_t *icr;
  111. uint32_t icrShift;
  112. icrShift = pin;
  113. /* Register reset to default value */
  114. base->EDGE_SEL &= ~(1U << pin);
  115. if (pin < 16)
  116. {
  117. icr = &(base->ICR1);
  118. }
  119. else
  120. {
  121. icr = &(base->ICR2);
  122. icrShift -= 16;
  123. }
  124. switch (pinInterruptMode)
  125. {
  126. case (kGPIO_IntLowLevel):
  127. *icr &= ~(3U << (2 * icrShift));
  128. break;
  129. case (kGPIO_IntHighLevel):
  130. *icr = (*icr & (~(3U << (2 * icrShift)))) | (1U << (2 * icrShift));
  131. break;
  132. case (kGPIO_IntRisingEdge):
  133. *icr = (*icr & (~(3U << (2 * icrShift)))) | (2U << (2 * icrShift));
  134. break;
  135. case (kGPIO_IntFallingEdge):
  136. *icr |= (3U << (2 * icrShift));
  137. break;
  138. case (kGPIO_IntRisingOrFallingEdge):
  139. base->EDGE_SEL |= (1U << pin);
  140. break;
  141. default:
  142. break;
  143. }
  144. }