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.

73 lines
2.0KB

  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. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Templates for using a range of bits from a port for parallel IO.
  19. #ifndef AVRLIBX_IO_PARALLEL_H_
  20. #define AVRLIBX_IO_PARALLEL_H_
  21. #include <avr/io.h>
  22. #include "avrlibx/io/gpio.h"
  23. namespace avrlibx {
  24. template<typename Port, uint8_t first, uint8_t last>
  25. struct ParallelPort {
  26. enum Masks {
  27. mask = ((1L << (last - first + 1)) - 1) << first,
  28. shift = first
  29. };
  30. static inline void set_direction(PortDirection direction) {
  31. if (direction == INPUT) {
  32. Port::dir_clr(mask);
  33. } else {
  34. Port::dir_set(mask);
  35. }
  36. }
  37. static inline void set_mode(PortMode mode) {
  38. PORTCFG.MPCMASK = mask;
  39. Gpio<Port, 0>::set_mode(mode);
  40. }
  41. static inline void High() {
  42. Port::out_set(mask);
  43. }
  44. static inline void Low() {
  45. Port::out_clr(mask);
  46. }
  47. static inline void Toggle() {
  48. Port::out_tgl(mask);
  49. }
  50. static inline void set_value(uint8_t value) {
  51. uint8_t preserve = Port::out() & ~mask;
  52. Port::out(preserve | (value << shift));
  53. }
  54. static inline uint8_t value() {
  55. return (Port::in() & mask) >> shift;
  56. }
  57. static inline void Write(uint8_t value) { set_value(value); }
  58. static inline uint8_t Read() { return value(); }
  59. };
  60. } // namespace avrlibx
  61. #endif // AVRLIBX_IO_PARALLEL_H_