mcu_gpio.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * File: mcu_gpio.h
  3. * Date: 23.01.2011
  4. * Denis Zheleznyakov aka ZiB @ http://ziblog.ru
  5. */
  6. #ifndef MCU_GPIO_H_
  7. #define MCU_GPIO_H_
  8. // PIN_TEST A, 3, HIGH, INPUT_FLOATING_WITHOUT_ITERRUPT
  9. #define INPUT_FLOATING_WITHOUT_ITERRUPT INPUT_FLOATING_WITHOUT_ITERRUPT
  10. #define INPUT_FLOATING_WITH_ITERRUPT INPUT_FLOATING_WITH_ITERRUPT
  11. #define INPUT_PULL_UP_WITHOUT_ITERRUPT INPUT_PULL_UP_WITHOUT_ITERRUPT
  12. #define INPUT_PULL_UP_WITH_ITERRUPT INPUT_PULL_UP_WITH_ITERRUPT
  13. #define OUTPUT_OPEN_DRAIN_NOSPEED_LIMIT OUTPUT_OPEN_DRAIN_NOSPEED_LIMIT
  14. #define OUTPUT_OPEN_DRAIN_SPEED_LIMIT_10MHZ OUTPUT_OPEN_DRAIN_SPEED_LIMIT_10MHZ
  15. #define OUTPUT_PUSH_PULL_NOSPEED_LIMIT OUTPUT_PUSH_PULL_NOSPEED_LIMIT
  16. #define OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ
  17. //------------------------------------------------------------------------------
  18. #define GPIO_PIN_CONFIGURATION_OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ(PORT, PIN, LEVEL, MODE) \
  19. { \
  20. GPIO##PORT->DDR |= (1 << PIN); \
  21. GPIO##PORT->CR1 |= (1 << PIN); \
  22. GPIO##PORT->CR2 |= (1 << PIN); \
  23. }
  24. #define GPIO_PIN_CONFIGURATION_OUTPUT_PUSH_PULL_NOSPEED_LIMIT(PORT, PIN, LEVEL, MODE) \
  25. { \
  26. GPIO##PORT->DDR |= (1 << PIN); \
  27. GPIO##PORT->CR1 |= (1 << PIN); \
  28. GPIO##PORT->CR2 &= (uint8_t)(~(1 << PIN)); \
  29. }
  30. #define GPIO_PIN_CONFIGURATION_OUTPUT_OPEN_DRAIN_SPEED_LIMIT_10MHZ(PORT, PIN, LEVEL, MODE) \
  31. { \
  32. GPIO##PORT->DDR |= (1 << PIN); \
  33. GPIO##PORT->CR1 &= (uint8_t)(~(1 << PIN)); \
  34. GPIO##PORT->CR2 |= (1 << PIN); \
  35. }
  36. #define GPIO_PIN_CONFIGURATION_OUTPUT_OPEN_DRAIN_NOSPEED_LIMIT(PORT, PIN, LEVEL, MODE) \
  37. { \
  38. GPIO##PORT->DDR |= (1 << PIN); \
  39. GPIO##PORT->CR1 &= (uint8_t)(~(1 << PIN)); \
  40. GPIO##PORT->CR2 &= (uint8_t)(~(1 << PIN)); \
  41. }
  42. #define GPIO_PIN_CONFIGURATION_INPUT_PULL_UP_WITH_ITERRUPT(PORT, PIN, LEVEL, MODE) \
  43. { \
  44. GPIO##PORT->DDR &= (uint8_t)(~(1 << PIN)); \
  45. GPIO##PORT->CR1 |= (1 << PIN); \
  46. GPIO##PORT->CR2 |= (1 << PIN); \
  47. }
  48. #define GPIO_PIN_CONFIGURATION_INPUT_PULL_UP_WITHOUT_ITERRUPT(PORT, PIN, LEVEL, MODE) \
  49. { \
  50. GPIO##PORT->DDR &= (uint8_t)(~(1 << PIN)); \
  51. GPIO##PORT->CR1 |= (1 << PIN); \
  52. GPIO##PORT->CR2 &= (uint8_t)(~(1 << PIN)); \
  53. }
  54. #define GPIO_PIN_CONFIGURATION_INPUT_FLOATING_WITH_ITERRUPT(PORT, PIN, LEVEL, MODE) \
  55. { \
  56. GPIO##PORT->DDR &= (uint8_t)(~(1 << PIN)); \
  57. GPIO##PORT->CR1 &= (uint8_t)(~(1 << PIN)); \
  58. GPIO##PORT->CR2 |= (1 << PIN); \
  59. }
  60. #define GPIO_PIN_CONFIGURATION_INPUT_FLOATING_WITHOUT_ITERRUPT(PORT, PIN, LEVEL, MODE) \
  61. { \
  62. GPIO##PORT->DDR &= (uint8_t)(~(1 << PIN)); \
  63. GPIO##PORT->CR1 &= (uint8_t)(~(1 << PIN)); \
  64. GPIO##PORT->CR2 &= (uint8_t)(~(1 << PIN)); \
  65. }
  66. //------------------------------------------------------------------------------
  67. #define GPIO_PIN_CONFIGURATION(PORT, PIN, LEVEL, MODE) \
  68. { \
  69. GPIO_PIN_CONFIGURATION_##MODE(PORT, PIN, LEVEL, MODE); \
  70. }
  71. //------------------------------------------------------------------------------
  72. #define GPIO_PIN_OFF_HIGH(PORT, PIN, LEVEL, MODE) \
  73. { GPIO##PORT->ODR &= (uint8_t)((uint8_t)(~(1 << PIN))); }
  74. #define GPIO_PIN_OFF_LOW(PORT, PIN, LEVEL, MODE) \
  75. { GPIO##PORT->ODR |= (1 << PIN); }
  76. #define GPIO_PIN_OFF(PORT, PIN, LEVEL, MODE) \
  77. { GPIO_PIN_OFF_##LEVEL(PORT, PIN, LEVEL, MODE) }
  78. //------------------------------------------------------------------------------
  79. #define GPIO_PIN_ON_HIGH(PORT, PIN, LEVEL, MODE) \
  80. { GPIO##PORT->ODR |= (1 << PIN); }
  81. #define GPIO_PIN_ON_LOW(PORT, PIN, LEVEL, MODE) \
  82. { GPIO##PORT->ODR &= (uint8_t)(~(1 << PIN)); }
  83. #define GPIO_PIN_ON(PORT, PIN, LEVEL, MODE) \
  84. { GPIO_PIN_ON_##LEVEL(PORT, PIN, LEVEL, MODE) }
  85. //------------------------------------------------------------------------------
  86. #define GPIO_PIN_TOGGLE(PORT, PIN, LEVEL, MODE) \
  87. { GPIO##PORT->ODR ^= (uint8_t)(1 << PIN); }
  88. //------------------------------------------------------------------------------
  89. #define GPIO_PIN_SIGNAL_HIGH(PORT, PIN, LEVEL, MODE) \
  90. ( (GPIO##PORT->IDR & (1 << PIN)) == (1 << PIN) )
  91. #define GPIO_PIN_SIGNAL_LOW(PORT, PIN, LEVEL, MODE) \
  92. ( (GPIO##PORT->IDR & (1 << PIN)) != (1 << PIN) )
  93. #define GPIO_PIN_SIGNAL(PORT, PIN, LEVEL, MODE) \
  94. ( GPIO_PIN_SIGNAL_##LEVEL(PORT, PIN, LEVEL, MODE) )
  95. //------------------------------------------------------------------------------
  96. #define PIN_CONFIGURATION(PIN_DESCRIPTION) GPIO_PIN_CONFIGURATION(PIN_DESCRIPTION)
  97. #define PIN_ON(PIN_DESCRIPTION) GPIO_PIN_ON(PIN_DESCRIPTION)
  98. #define PIN_OFF(PIN_DESCRIPTION) GPIO_PIN_OFF(PIN_DESCRIPTION)
  99. #define PIN_TOGGLE(PIN_DESCRIPTION) GPIO_PIN_TOGGLE(PIN_DESCRIPTION)
  100. #define PIN_SIGNAL(PIN_DESCRIPTION) GPIO_PIN_SIGNAL(PIN_DESCRIPTION)
  101. #endif /* MCU_GPIO_H_ */