GPIO_LPC18xx.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* --------------------------------------------------------------------------
  2. * Copyright (c) 2013-2016 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * $Date: 02. March 2016
  19. * $Revision: V1.0
  20. *
  21. * Project: GPIO Driver for NXP LPC18xx
  22. * -------------------------------------------------------------------------- */
  23. #include "LPC18xx.h"
  24. #include "GPIO_LPC18xx.h"
  25. /**
  26. \fn void GPIO_PortClock (uint32_t clock)
  27. \brief Port Clock Control
  28. \param[in] clock Enable or disable clock
  29. */
  30. void GPIO_PortClock (uint32_t clock) {
  31. if (clock) {
  32. LPC_CCU1->CLK_M3_GPIO_CFG |= 3;
  33. while (!(LPC_CCU1->CLK_M3_GPIO_STAT & 1));
  34. }
  35. else {
  36. LPC_CCU1->CLK_M3_GPIO_CFG &= ~(3);
  37. while (LPC_CCU1->CLK_M3_GPIO_STAT & 1);
  38. }
  39. }
  40. /**
  41. \fn void GPIO_SetDir (uint32_t port_num,
  42. uint32_t pin_num,
  43. uint32_t dir)
  44. \brief Configure GPIO pin direction
  45. \param[in] port_num GPIO number (0..7)
  46. \param[in] pin_num Port pin number
  47. \param[in] dir GPIO_DIR_INPUT, GPIO_DIR_OUTPUT
  48. */
  49. void GPIO_SetDir (uint32_t port_num, uint32_t pin_num, uint32_t dir) {
  50. dir ? (LPC_GPIO_PORT->DIR[port_num] |= (1UL << pin_num)) : \
  51. (LPC_GPIO_PORT->DIR[port_num] &= ~(1UL << pin_num));
  52. }
  53. /**
  54. \fn void GPIO_PinWrite (uint32_t port_num,
  55. uint32_t pin_num,
  56. uint32_t val)
  57. \brief Write port pin
  58. \param[in] port_num GPIO number (0..7)
  59. \param[in] pin_num Port pin number
  60. \param[in] val Port pin value (0 or 1)
  61. */
  62. void GPIO_PinWrite (uint32_t port_num, uint32_t pin_num, uint32_t val) {
  63. val ? (LPC_GPIO_PORT->SET[port_num] = (1UL << pin_num)) : \
  64. (LPC_GPIO_PORT->CLR[port_num] = (1UL << pin_num));
  65. }
  66. /**
  67. \fn uint32_t GPIO_PinRead (uint32_t port_num, uint32_t pin_num)
  68. \brief Read port pin
  69. \param[in] port_num GPIO number (0..7)
  70. \param[in] pin_num Port pin number
  71. \return pin value (0 or 1)
  72. */
  73. uint32_t GPIO_PinRead (uint32_t port_num, uint32_t pin_num) {
  74. return ((LPC_GPIO_PORT->PIN[port_num] & (1UL << pin_num)) ? (1) : (0));
  75. }
  76. /**
  77. \fn void GPIO_PortWrite (uint32_t port_num,
  78. uint32_t mask,
  79. uint32_t val)
  80. \brief Write port pins
  81. \param[in] port_num GPIO number (0..7)
  82. \param[in] mask Selected pins
  83. \param[in] val Pin values
  84. */
  85. void GPIO_PortWrite (uint32_t port_num, uint32_t mask, uint32_t val) {
  86. LPC_GPIO_PORT->MASK[port_num] = ~mask;
  87. LPC_GPIO_PORT->MPIN[port_num] = val;
  88. }
  89. /**
  90. \fn uint32_t GPIO_PortRead (uint32_t port_num)
  91. \brief Read port pins
  92. \param[in] port_num GPIO number (0..7)
  93. \return port pin inputs
  94. */
  95. uint32_t GPIO_PortRead (uint32_t port_num) {
  96. return (LPC_GPIO_PORT->PIN[port_num]);
  97. }