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.

90 lines
2.1KB

  1. // Copyright 2011 Peter Kvitek
  2. //
  3. // Author: Peter Kvitek (pete@kvitek.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. // CD4051 mux/demux abstraction. 4051 must be connected to low or high
  19. // nibble of a port.
  20. //
  21. // Based on parallel_io.cc code by Olivier Gillet (ol.gillet@gmail.com)
  22. #ifndef AVRLIB_DEVICES_MUX4051_H_
  23. #define AVRLIB_DEVICES_MUX4051_H_
  24. #include <avr/io.h>
  25. #include "avrlib/gpio.h"
  26. namespace avrlib {
  27. enum Mux4051PortMode {
  28. MUX4051_NIBBLE_HIGH,
  29. MUX4051_NIBBLE_LOW,
  30. };
  31. template<Mux4051PortMode mode>
  32. struct ShiftMasks {
  33. enum Masks {
  34. maskD = 0,
  35. maskE = 0,
  36. shift = 0
  37. };
  38. };
  39. template<>
  40. struct ShiftMasks<MUX4051_NIBBLE_HIGH> {
  41. enum Masks {
  42. maskD = 0x70,
  43. maskE = 0x80,
  44. shift = 4,
  45. };
  46. };
  47. template<>
  48. struct ShiftMasks<MUX4051_NIBBLE_LOW> {
  49. enum Masks {
  50. maskD = 0x07,
  51. maskE = 0x08,
  52. shift = 0,
  53. };
  54. };
  55. template<typename Port, Mux4051PortMode mux_mode = MUX4051_NIBBLE_LOW>
  56. struct Mux4051Port {
  57. typedef ShiftMasks<mux_mode> Masks;
  58. static inline void Init() {
  59. *Port::Mode::ptr()|= Masks::maskD | Masks::maskE;
  60. }
  61. static inline void Disable() {
  62. *Port::Output::ptr()|= (0x08 << Masks::shift);
  63. }
  64. static inline void Enable() {
  65. *Port::Output::ptr()&=~(0x08 << Masks::shift);
  66. }
  67. static inline void Write(uint8_t value) {
  68. uint8_t tmp = *Port::Output::ptr() & ~Masks::maskD;
  69. *Port::Output::ptr() = tmp | (value << Masks::shift);
  70. }
  71. };
  72. } // namespace avrlib
  73. #endif // AVRLIB_DEVICES_MUX4051_H_