hal_gpio.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2014-2016, 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_PORTA 0
  32. #define HAL_GPIO_PORTB 1
  33. #define HAL_GPIO_PORTC 2
  34. #define HAL_GPIO_PMUX_A 0
  35. #define HAL_GPIO_PMUX_B 1
  36. #define HAL_GPIO_PMUX_C 2
  37. #define HAL_GPIO_PMUX_D 3
  38. #define HAL_GPIO_PMUX_E 4
  39. #define HAL_GPIO_PMUX_F 5
  40. #define HAL_GPIO_PMUX_G 6
  41. #define HAL_GPIO_PMUX_H 7
  42. #define HAL_GPIO_PMUX_I 8
  43. #define HAL_GPIO_PIN(name, port, pin) \
  44. static inline void HAL_GPIO_##name##_set(void) \
  45. { \
  46. PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
  47. (void)HAL_GPIO_##name##_set; \
  48. } \
  49. \
  50. static inline void HAL_GPIO_##name##_clr(void) \
  51. { \
  52. PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
  53. (void)HAL_GPIO_##name##_clr; \
  54. } \
  55. \
  56. static inline void HAL_GPIO_##name##_toggle(void) \
  57. { \
  58. PORT->Group[HAL_GPIO_PORT##port].OUTTGL.reg = (1 << pin); \
  59. (void)HAL_GPIO_##name##_toggle; \
  60. } \
  61. \
  62. static inline void HAL_GPIO_##name##_write(int value) \
  63. { \
  64. if (value) \
  65. PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
  66. else \
  67. PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
  68. (void)HAL_GPIO_##name##_write; \
  69. } \
  70. \
  71. static inline void HAL_GPIO_##name##_in(void) \
  72. { \
  73. PORT->Group[HAL_GPIO_PORT##port].DIRCLR.reg = (1 << pin); \
  74. PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
  75. PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PULLEN; \
  76. (void)HAL_GPIO_##name##_in; \
  77. } \
  78. \
  79. static inline void HAL_GPIO_##name##_out(void) \
  80. { \
  81. PORT->Group[HAL_GPIO_PORT##port].DIRSET.reg = (1 << pin); \
  82. PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
  83. (void)HAL_GPIO_##name##_out; \
  84. } \
  85. \
  86. static inline void HAL_GPIO_##name##_pullup(void) \
  87. { \
  88. PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
  89. PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN; \
  90. (void)HAL_GPIO_##name##_pullup; \
  91. } \
  92. \
  93. static inline int HAL_GPIO_##name##_read(void) \
  94. { \
  95. return (PORT->Group[HAL_GPIO_PORT##port].IN.reg & (1 << pin)) != 0; \
  96. (void)HAL_GPIO_##name##_read; \
  97. } \
  98. \
  99. static inline int HAL_GPIO_##name##_state(void) \
  100. { \
  101. return (PORT->Group[HAL_GPIO_PORT##port].DIR.reg & (1 << pin)) != 0; \
  102. (void)HAL_GPIO_##name##_state; \
  103. } \
  104. \
  105. static inline void HAL_GPIO_##name##_pmuxen(int mux) \
  106. { \
  107. PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PMUXEN; \
  108. if (pin & 1) \
  109. PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXO = mux; \
  110. else \
  111. PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXE = mux; \
  112. (void)HAL_GPIO_##name##_pmuxen; \
  113. } \
  114. \
  115. static inline void HAL_GPIO_##name##_pmuxdis(void) \
  116. { \
  117. PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PMUXEN; \
  118. (void)HAL_GPIO_##name##_pmuxdis; \
  119. } \
  120. #endif // _HAL_GPIO_H_