nu_gpio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**************************************************************************//**
  2. * @file gpio.c
  3. * @brief GPIO driver source file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #include "nuc980.h"
  9. #include "nu_gpio.h"
  10. /** @addtogroup Standard_Driver Standard Driver
  11. @{
  12. */
  13. /** @addtogroup GPIO_Driver GPIO Driver
  14. @{
  15. */
  16. /** @addtogroup GPIO_EXPORTED_FUNCTIONS GPIO Exported Functions
  17. @{
  18. */
  19. /**
  20. * @brief Set GPIO operation mode
  21. *
  22. * @param[in] port GPIO port. It could be It could be PA, PB, PC, PD, PE, PF, PG or PH.
  23. * @param[in] u32PinMask The single or multiple pins of specified GPIO port.
  24. * It could be BIT0 ~ BIT15 for PA, PB, PC, PD, PF and PH GPIO port.
  25. * It could be BIT0 ~ BIT13 for PE GPIO port.
  26. * It could be BIT0 ~ BIT11 for PG GPIO port.
  27. * @param[in] u32Mode Operation mode. It could be \n
  28. * GPIO_MODE_INPUT, GPIO_MODE_OUTPUT, GPIO_MODE_OPEN_DRAIN.
  29. *
  30. * @return None
  31. *
  32. * @details This function is used to set specified GPIO operation mode.
  33. */
  34. void GPIO_SetMode(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode)
  35. {
  36. uint32_t i;
  37. for (i = 0ul; i < GPIO_PIN_MAX; i++)
  38. {
  39. if ((u32PinMask & (1ul << i)) == (1ul << i))
  40. {
  41. port->MODE = (port->MODE & ~(0x3ul << (i << 1))) | (u32Mode << (i << 1));
  42. }
  43. }
  44. }
  45. /**
  46. * @brief Enable GPIO interrupt
  47. *
  48. * @param[in] port GPIO port. It could be It could be PA, PB, PC, PD, PE, PF, PG or PH.
  49. * @param[in] u32Pin The pin of specified GPIO port.
  50. * It could be 0 ~ 15 for PA, PB, PC, PD, PF and PH GPIO port.
  51. * It could be 0 ~ 13 for PE GPIO port.
  52. * It could be 0 ~ 11 for PG GPIO port.
  53. * @param[in] u32IntAttribs The interrupt attribute of specified GPIO pin. It could be \n
  54. * GPIO_INT_RISING, GPIO_INT_FALLING, GPIO_INT_BOTH_EDGE, GPIO_INT_HIGH, GPIO_INT_LOW.
  55. *
  56. * @return None
  57. *
  58. * @details This function is used to enable specified GPIO pin interrupt.
  59. */
  60. void GPIO_EnableInt(GPIO_T *port, uint32_t u32Pin, uint32_t u32IntAttribs)
  61. {
  62. port->INTTYPE |= (((u32IntAttribs >> 24) & 0xFFUL) << u32Pin);
  63. port->INTEN |= ((u32IntAttribs & 0xFFFFFFUL) << u32Pin);
  64. }
  65. /**
  66. * @brief Disable GPIO interrupt
  67. *
  68. * @param[in] port GPIO port. It could be It could be PA, PB, PC, PD, PE, PF, PG or PH.
  69. * @param[in] u32Pin The pin of specified GPIO port.
  70. * It could be 0 ~ 15 for PA, PB, PC, PD, PF and PH GPIO port.
  71. * It could be 0 ~ 13 for PE GPIO port.
  72. * It could be 0 ~ 11 for PG GPIO port.
  73. *
  74. * @return None
  75. *
  76. * @details This function is used to disable specified GPIO pin interrupt.
  77. */
  78. void GPIO_DisableInt(GPIO_T *port, uint32_t u32Pin)
  79. {
  80. port->INTTYPE &= ~(1UL << u32Pin);
  81. port->INTEN &= ~((0x00010001UL) << u32Pin);
  82. }
  83. /**
  84. * @brief Set GPIO slew rate control
  85. *
  86. * @param[in] port GPIO port. It could be \ref PA, \ref PB, ... or \ref PG
  87. * @param[in] u32PinMask The single or multiple pins of specified GPIO port.
  88. * @param[in] u32Mode Slew rate mode. \ref GPIO_SLEWCTL_NORMAL (maximum 40 MHz at 2.7V)
  89. * \ref GPIO_SLEWCTL_HIGH (maximum 80 MHz at 2.7V)
  90. * \ref GPIO_SLEWCTL_FAST (maximum 100 MHz at 2.7V)
  91. *
  92. * @return None
  93. *
  94. * @details This function is used to set specified GPIO operation mode.
  95. */
  96. void GPIO_SetSlewCtl(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode)
  97. {
  98. uint32_t i;
  99. for (i = 0ul; i < GPIO_PIN_MAX; i++)
  100. {
  101. if (u32PinMask & (1ul << i))
  102. {
  103. port->SLEWCTL = (port->SLEWCTL & ~(0x3ul << (i << 1))) | (u32Mode << (i << 1));
  104. }
  105. }
  106. }
  107. /**
  108. * @brief Set GPIO Pull-up and Pull-down control
  109. *
  110. * @param[in] port GPIO port. It could be \ref PA, \ref PB, ... or \ref PG
  111. * @param[in] u32PinMask The pin of specified GPIO port. It could be 0 ~ 15.
  112. * @param[in] u32Mode The pin mode of specified GPIO pin. It could be
  113. * \ref GPIO_PUSEL_DISABLE
  114. * \ref GPIO_PUSEL_PULL_UP
  115. * \ref GPIO_PUSEL_PULL_DOWN
  116. *
  117. * @return None
  118. *
  119. * @details Set the pin mode of specified GPIO pin.
  120. */
  121. void GPIO_SetPullCtl(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode)
  122. {
  123. uint32_t i;
  124. for (i = 0ul; i < GPIO_PIN_MAX; i++)
  125. {
  126. if (u32PinMask & (1ul << i))
  127. {
  128. port->PUSEL = (port->PUSEL & ~(0x3ul << (i << 1))) | (u32Mode << (i << 1));
  129. }
  130. }
  131. }
  132. /*@}*/ /* end of group GPIO_EXPORTED_FUNCTIONS */
  133. /*@}*/ /* end of group GPIO_Driver */
  134. /*@}*/ /* end of group Standard_Driver */