fsl_gpio.c 5.0 KB

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