hal_gpio.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2021, 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. P##port->PDIO[pin] = 1; \
  35. (void)HAL_GPIO_##name##_set; \
  36. } \
  37. \
  38. static inline void HAL_GPIO_##name##_clr(void) \
  39. { \
  40. P##port->PDIO[pin] = 0; \
  41. (void)HAL_GPIO_##name##_clr; \
  42. } \
  43. \
  44. static inline void HAL_GPIO_##name##_toggle(void) \
  45. { \
  46. P##port->DOUT ^= GPIO_DOUT_DOUT##pin##_Msk; \
  47. (void)HAL_GPIO_##name##_toggle; \
  48. } \
  49. \
  50. static inline void HAL_GPIO_##name##_write(int value) \
  51. { \
  52. P##port->PDIO[pin] = (value > 0); \
  53. (void)HAL_GPIO_##name##_write; \
  54. } \
  55. \
  56. static inline void HAL_GPIO_##name##_in(void) \
  57. { \
  58. P##port->MODE = (P##port->MODE & ~GPIO_MODE_MODE##pin##_Msk); \
  59. (void)HAL_GPIO_##name##_in; \
  60. } \
  61. \
  62. static inline void HAL_GPIO_##name##_out(void) \
  63. { \
  64. P##port->MODE = (P##port->MODE & ~GPIO_MODE_MODE##pin##_Msk) | \
  65. (1 << GPIO_MODE_MODE##pin##_Pos); \
  66. (void)HAL_GPIO_##name##_out; \
  67. } \
  68. \
  69. static inline void HAL_GPIO_##name##_odrain(void) \
  70. { \
  71. P##port->MODE = (P##port->MODE & ~GPIO_MODE_MODE##pin##_Msk) | \
  72. (2 << GPIO_MODE_MODE##pin##_Pos); \
  73. (void)HAL_GPIO_##name##_odrain; \
  74. } \
  75. \
  76. static inline void HAL_GPIO_##name##_pullup(void) \
  77. { \
  78. P##port->PUSEL = (P##port->PUSEL & ~GPIO_PUSEL_PUSEL##pin##_Msk) | \
  79. (1 << GPIO_PUSEL_PUSEL##pin##_Pos); \
  80. (void)HAL_GPIO_##name##_pullup; \
  81. } \
  82. \
  83. static inline void HAL_GPIO_##name##_pulldown(void) \
  84. { \
  85. P##port->PUSEL = (P##port->PUSEL & ~GPIO_PUSEL_PUSEL##pin##_Msk) | \
  86. (2 << GPIO_PUSEL_PUSEL##pin##_Pos); \
  87. (void)HAL_GPIO_##name##_pulldown; \
  88. } \
  89. \
  90. static inline void HAL_GPIO_##name##_pulldis(void) \
  91. { \
  92. P##port->PUSEL = (P##port->PUSEL & ~GPIO_PUSEL_PUSEL##pin##_Msk); \
  93. (void)HAL_GPIO_##name##_pulldis; \
  94. } \
  95. \
  96. static inline int HAL_GPIO_##name##_read(void) \
  97. { \
  98. return P##port->PDIO[pin]; \
  99. (void)HAL_GPIO_##name##_read; \
  100. } \
  101. \
  102. static inline void HAL_GPIO_##name##_mfp(int value) \
  103. { \
  104. uint32_t mfp = (pin < 8) ? SYS->GP##port##_MFPL : SYS->GP##port##_MFPH; \
  105. uint32_t offs = ((pin < 8) ? pin : (pin-8)) * 4; \
  106. mfp = (mfp & ~(0xf << offs)) | (value << offs); \
  107. if (pin < 8) \
  108. SYS->GP##port##_MFPL = mfp; \
  109. else \
  110. SYS->GP##port##_MFPH = mfp; \
  111. (void)HAL_GPIO_##name##_mfp; \
  112. } \
  113. \
  114. static inline void HAL_GPIO_##name##_mfos(int value) \
  115. { \
  116. if (value) \
  117. SYS->GP##port##_MFOS |= SYS_GPA_MFOS_MFOS##pin##_Msk; \
  118. else \
  119. SYS->GP##port##_MFOS &= ~SYS_GPA_MFOS_MFOS##pin##_Msk; \
  120. (void)HAL_GPIO_##name##_mfos; \
  121. } \
  122. \
  123. static inline void HAL_GPIO_##name##_smten(int value) \
  124. { \
  125. if (value) \
  126. P##port->SMTEN |= GPIO_SMTEN_SMTEN##pin##_Msk; \
  127. else \
  128. P##port->SMTEN &= ~GPIO_SMTEN_SMTEN##pin##_Msk; \
  129. (void)HAL_GPIO_##name##_smten; \
  130. } \
  131. \
  132. static inline void HAL_GPIO_##name##_dinoff(int value) \
  133. { \
  134. if (value) \
  135. P##port->DINOFF |= GPIO_DINOFF_DINOFF##pin##_Msk; \
  136. else \
  137. P##port->DINOFF &= ~GPIO_DINOFF_DINOFF##pin##_Msk; \
  138. (void)HAL_GPIO_##name##_dinoff; \
  139. } \
  140. \
  141. static inline void HAL_GPIO_##name##_dben(int value) \
  142. { \
  143. if (value) \
  144. P##port->DBEN |= GPIO_DBEN_DBEN##pin##_Msk; \
  145. else \
  146. P##port->DBEN &= ~GPIO_DBEN_DBEN##pin##_Msk; \
  147. (void)HAL_GPIO_##name##_dben; \
  148. } \
  149. \
  150. static inline void HAL_GPIO_##name##_slew(int value) \
  151. { \
  152. P##port->SLEWCTL = (P##port->SLEWCTL & ~GPIO_SLEWCTL_HSREN##pin##_Msk) | \
  153. (value << GPIO_SLEWCTL_HSREN##pin##_Pos); \
  154. (void)HAL_GPIO_##name##_slew; \
  155. } \
  156. #endif // _HAL_GPIO_H_