hal_gpio.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. // Copyright (c) 2014-2016, Alex Taradov <alex@taradov.com>. All rights reserved.
  3. #ifndef _HAL_GPIO_H_
  4. #define _HAL_GPIO_H_
  5. /*- Definitions -------------------------------------------------------------*/
  6. #define HAL_GPIO_PORTA 0
  7. #define HAL_GPIO_PORTB 1
  8. #define HAL_GPIO_PORTC 2
  9. #define HAL_GPIO_PMUX_A 0
  10. #define HAL_GPIO_PMUX_B 1
  11. #define HAL_GPIO_PMUX_C 2
  12. #define HAL_GPIO_PMUX_D 3
  13. #define HAL_GPIO_PMUX_E 4
  14. #define HAL_GPIO_PMUX_F 5
  15. #define HAL_GPIO_PMUX_G 6
  16. #define HAL_GPIO_PMUX_H 7
  17. #define HAL_GPIO_PMUX_I 8
  18. #define HAL_GPIO_PIN(name, port, pin) \
  19. static inline void HAL_GPIO_##name##_set(void) \
  20. { \
  21. PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
  22. (void)HAL_GPIO_##name##_set; \
  23. } \
  24. \
  25. static inline void HAL_GPIO_##name##_clr(void) \
  26. { \
  27. PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
  28. (void)HAL_GPIO_##name##_clr; \
  29. } \
  30. \
  31. static inline void HAL_GPIO_##name##_toggle(void) \
  32. { \
  33. PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTTGL.reg = (1 << pin); \
  34. (void)HAL_GPIO_##name##_toggle; \
  35. } \
  36. \
  37. static inline void HAL_GPIO_##name##_write(int value) \
  38. { \
  39. if (value) \
  40. PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
  41. else \
  42. PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
  43. (void)HAL_GPIO_##name##_write; \
  44. } \
  45. \
  46. static inline void HAL_GPIO_##name##_in(void) \
  47. { \
  48. PORT_IOBUS->Group[HAL_GPIO_PORT##port].DIRCLR.reg = (1 << pin); \
  49. PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
  50. PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PULLEN;\
  51. PORT_IOBUS->Group[HAL_GPIO_PORT##port].CTRL.reg |= (1 << pin); \
  52. (void)HAL_GPIO_##name##_in; \
  53. } \
  54. \
  55. static inline void HAL_GPIO_##name##_out(void) \
  56. { \
  57. PORT_IOBUS->Group[HAL_GPIO_PORT##port].DIRSET.reg = (1 << pin); \
  58. PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
  59. (void)HAL_GPIO_##name##_out; \
  60. } \
  61. \
  62. static inline void HAL_GPIO_##name##_pullup(void) \
  63. { \
  64. PORT_IOBUS->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
  65. PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN; \
  66. (void)HAL_GPIO_##name##_pullup; \
  67. } \
  68. \
  69. static inline int HAL_GPIO_##name##_read(void) \
  70. { \
  71. return (PORT_IOBUS->Group[HAL_GPIO_PORT##port].IN.reg & (1 << pin)) != 0; \
  72. (void)HAL_GPIO_##name##_read; \
  73. } \
  74. \
  75. static inline int HAL_GPIO_##name##_state(void) \
  76. { \
  77. return (PORT_IOBUS->Group[HAL_GPIO_PORT##port].DIR.reg & (1 << pin)) != 0; \
  78. (void)HAL_GPIO_##name##_state; \
  79. } \
  80. \
  81. static inline void HAL_GPIO_##name##_pmuxen(int mux) \
  82. { \
  83. PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PMUXEN; \
  84. if (pin & 1) \
  85. PORT_IOBUS->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXO = mux; \
  86. else \
  87. PORT_IOBUS->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXE = mux; \
  88. (void)HAL_GPIO_##name##_pmuxen; \
  89. } \
  90. \
  91. static inline void HAL_GPIO_##name##_pmuxdis(void) \
  92. { \
  93. PORT_IOBUS->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PMUXEN; \
  94. (void)HAL_GPIO_##name##_pmuxdis; \
  95. } \
  96. #endif // _HAL_GPIO_H_