hal_gpio.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2017, Alex Taradov <alex@taradov.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef _HAL_GPIO_H_
  29. #define _HAL_GPIO_H_
  30. /*- Definitions -------------------------------------------------------------*/
  31. #define HAL_GPIO_PIN(name, port, pin) \
  32. static inline void HAL_GPIO_##name##_set(void) \
  33. { \
  34. PIO##port->PIO_SODR = (1 << pin); \
  35. (void)HAL_GPIO_##name##_set; \
  36. } \
  37. \
  38. static inline void HAL_GPIO_##name##_clr(void) \
  39. { \
  40. PIO##port->PIO_CODR = (1 << pin); \
  41. (void)HAL_GPIO_##name##_clr; \
  42. } \
  43. \
  44. static inline void HAL_GPIO_##name##_toggle(void) \
  45. { \
  46. if (PIO##port->PIO_ODSR & (1 << pin)) \
  47. PIO##port->PIO_CODR = (1 << pin); \
  48. else \
  49. PIO##port->PIO_SODR = (1 << pin); \
  50. (void)HAL_GPIO_##name##_toggle; \
  51. } \
  52. \
  53. static inline void HAL_GPIO_##name##_write(int value) \
  54. { \
  55. if (value) \
  56. PIO##port->PIO_SODR = (1 << pin); \
  57. else \
  58. PIO##port->PIO_CODR = (1 << pin); \
  59. (void)HAL_GPIO_##name##_write; \
  60. } \
  61. \
  62. static inline void HAL_GPIO_##name##_in(void) \
  63. { \
  64. PIO##port->PIO_PER = (1 << pin); \
  65. PIO##port->PIO_ODR = (1 << pin); \
  66. (void)HAL_GPIO_##name##_in; \
  67. } \
  68. \
  69. static inline void HAL_GPIO_##name##_out(void) \
  70. { \
  71. PIO##port->PIO_PER = (1 << pin); \
  72. PIO##port->PIO_OER = (1 << pin); \
  73. (void)HAL_GPIO_##name##_out; \
  74. } \
  75. \
  76. static inline void HAL_GPIO_##name##_pullup(void) \
  77. { \
  78. PIO##port->PIO_PUER = (1 << pin); \
  79. (void)HAL_GPIO_##name##_pullup; \
  80. } \
  81. \
  82. static inline int HAL_GPIO_##name##_read(void) \
  83. { \
  84. return (PIO##port->PIO_PDSR & (1 << pin)) != 0; \
  85. (void)HAL_GPIO_##name##_read; \
  86. } \
  87. \
  88. static inline int HAL_GPIO_##name##_state(void) \
  89. { \
  90. return (PIO##port->PIO_ODSR & (1 << pin)) != 0; \
  91. (void)HAL_GPIO_##name##_state; \
  92. } \
  93. \
  94. static inline void HAL_GPIO_##name##_abcd(int abcd) \
  95. { \
  96. if (abcd & 1) \
  97. PIO##port->PIO_ABCDSR[0] |= (1 << pin); \
  98. else \
  99. PIO##port->PIO_ABCDSR[0] &= ~(1 << pin); \
  100. if (abcd & 2) \
  101. PIO##port->PIO_ABCDSR[1] |= (1 << pin); \
  102. else \
  103. PIO##port->PIO_ABCDSR[1] &= ~(1 << pin); \
  104. PIO##port->PIO_PDR = (1 << pin); \
  105. (void)HAL_GPIO_##name##_abcd; \
  106. } \
  107. \
  108. static inline void HAL_GPIO_##name##_mden(void) \
  109. { \
  110. PIO##port->PIO_MDER = (1 << pin); \
  111. (void)HAL_GPIO_##name##_mden; \
  112. } \
  113. \
  114. static inline void HAL_GPIO_##name##_mddis(void) \
  115. { \
  116. PIO##port->PIO_MDDR = (1 << pin); \
  117. (void)HAL_GPIO_##name##_mddis; \
  118. } \
  119. #endif // _HAL_GPIO_H_