You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.6KB

  1. // Copyright 2011 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #ifndef AVRLIBX_IO_GPIO_H_
  16. #define AVRLIBX_IO_GPIO_H_
  17. #include <avr/io.h>
  18. #include "avrlibx/avrlibx.h"
  19. namespace avrlibx {
  20. enum PortDirection {
  21. INPUT,
  22. OUTPUT
  23. };
  24. enum PortMode {
  25. PORT_MODE_TOTEM_POLE,
  26. PORT_MODE_BUS_KEEPER,
  27. PORT_MODE_PULL_UP,
  28. PORT_MODE_PULL_DOWN
  29. };
  30. enum SenseMode {
  31. SENSE_MODE_BOTH_EDGES,
  32. SENSE_MODE_RISING,
  33. SENSE_MODE_FALLING,
  34. SENSE_MODE_LOW_LEVEL
  35. };
  36. #define WRAP_PORT(letter) \
  37. struct Port ## letter { \
  38. static inline PORT_t& port_t() { return PORT ## letter; } \
  39. static inline void dir(uint8_t value) { PORT ## letter ## _DIR = value; } \
  40. static inline void dir_clr(uint8_t value) { PORT ## letter ## _DIRCLR = value; } \
  41. static inline void dir_set(uint8_t value) { PORT ## letter ## _DIRSET = value; } \
  42. static inline void out(uint8_t value) { PORT ## letter ## _OUT = value; } \
  43. static inline void out_set(uint8_t value) { PORT ## letter ## _OUTSET = value; } \
  44. static inline void out_clr(uint8_t value) { PORT ## letter ## _OUTCLR = value; } \
  45. static inline void out_tgl(uint8_t value) { PORT ## letter ## _OUTTGL = value; } \
  46. static inline uint8_t in() { return PORT ## letter ## _IN; } \
  47. static inline uint8_t dir() { return PORT ## letter ## _DIR; } \
  48. static inline uint8_t out() { return PORT ## letter ## _OUT; } \
  49. static inline uint8_t event_base() { return EVSYS_CHMUX_PORT ## letter ## _PIN0_gc; } \
  50. };
  51. WRAP_PORT(A)
  52. WRAP_PORT(B)
  53. WRAP_PORT(C)
  54. WRAP_PORT(D)
  55. WRAP_PORT(E)
  56. #ifdef PORTF
  57. WRAP_PORT(F)
  58. #endif
  59. template<typename Port, uint8_t bit>
  60. struct Pin {
  61. static inline void set_control(uint8_t value) { }
  62. };
  63. #define SPECIALIZE_PIN(id) \
  64. template<typename Port> \
  65. struct Pin<Port, id> { \
  66. static inline void set_control(uint8_t value) { \
  67. Port::port_t().PIN ## id ## CTRL = value; \
  68. } \
  69. };
  70. SPECIALIZE_PIN(0)
  71. SPECIALIZE_PIN(1)
  72. SPECIALIZE_PIN(2)
  73. SPECIALIZE_PIN(3)
  74. SPECIALIZE_PIN(4)
  75. SPECIALIZE_PIN(5)
  76. SPECIALIZE_PIN(6)
  77. SPECIALIZE_PIN(7)
  78. template<typename Port, uint8_t bit, bool slow = true>
  79. struct Gpio {
  80. static inline void set_direction(PortDirection direction) {
  81. if (direction == INPUT) {
  82. Port::dir_clr(_BV(bit));
  83. } else {
  84. Port::dir_set(_BV(bit));
  85. if (slow) {
  86. Pin<Port, bit>::set_control(PORT_SRLEN_bm);
  87. }
  88. }
  89. }
  90. static inline void set_mode(PortMode mode) {
  91. if (mode == PORT_MODE_TOTEM_POLE) {
  92. Pin<Port, bit>::set_control(PORT_OPC_TOTEM_gc);
  93. } else if (mode == PORT_MODE_BUS_KEEPER) {
  94. Pin<Port, bit>::set_control(PORT_OPC_BUSKEEPER_gc);
  95. } else if (mode == PORT_MODE_PULL_UP) {
  96. Pin<Port, bit>::set_control(PORT_OPC_PULLUP_gc);
  97. } else if (mode == PORT_MODE_PULL_DOWN) {
  98. Pin<Port, bit>::set_control(PORT_OPC_PULLDOWN_gc);
  99. }
  100. }
  101. static inline void set_sense(SenseMode mode) {
  102. if (mode == SENSE_MODE_BOTH_EDGES) {
  103. Pin<Port, bit>::set_control(PORT_ISC_BOTHEDGES_gc);
  104. } else if (mode == SENSE_MODE_RISING) {
  105. Pin<Port, bit>::set_control(PORT_ISC_RISING_gc);
  106. } else if (mode == SENSE_MODE_FALLING) {
  107. Pin<Port, bit>::set_control(PORT_ISC_FALLING_gc);
  108. } else if (mode == SENSE_MODE_LOW_LEVEL) {
  109. Pin<Port, bit>::set_control(PORT_ISC_LEVEL_gc);
  110. }
  111. }
  112. static inline uint8_t event() {
  113. return Port::event_base() + bit;
  114. }
  115. static inline void High() {
  116. Port::out_set(_BV(bit));
  117. }
  118. static inline void Low() {
  119. Port::out_clr(_BV(bit));
  120. }
  121. static inline void Toggle() {
  122. Port::out_tgl(_BV(bit));
  123. }
  124. static inline void set_value(uint8_t value) {
  125. if (value) {
  126. Port::out_set(_BV(bit));
  127. } else {
  128. Port::out_clr(_BV(bit));
  129. }
  130. }
  131. static inline uint8_t value() {
  132. return Port::in() & _BV(bit) ? HIGH : LOW;
  133. }
  134. static inline void Write(uint8_t value) { set_value(value); }
  135. static inline uint8_t Read() { return value(); }
  136. };
  137. struct DummyGpio {
  138. static inline void set_direction(PortDirection direction) { }
  139. static inline void set_mode(PortDirection direction) { }
  140. static inline void High() { }
  141. static inline void Low() { }
  142. static inline void Toggle() { }
  143. static inline void set_value(uint8_t value) { }
  144. static inline uint8_t value() { return 0; }
  145. static inline void Write(uint8_t value) { set_value(value); }
  146. static inline uint8_t Read() { return value(); }
  147. };
  148. template<typename Gpio>
  149. struct Inverter {
  150. static inline void set_direction(PortDirection direction) {
  151. Gpio::set_direction(direction);
  152. }
  153. static inline void set_mode(PortDirection direction) {
  154. Gpio::set_mode(direction);
  155. }
  156. static inline void High() { Gpio::Low(); }
  157. static inline void Low() { Gpio::High(); }
  158. static inline void Toggle() { Gpio::Toggle(); }
  159. static inline void set_value(uint8_t value) { Gpio::set_value(!value); }
  160. static inline uint8_t value() { return !Gpio::value(); }
  161. static inline void Write(uint8_t value) { set_value(value); }
  162. static inline uint8_t Read() { return value(); }
  163. };
  164. } // namespace avrlibx
  165. #endif // AVRLIBX_IO_GPIO_H_