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.9KB

  1. // Copyright 2009 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. // Important: All buffer sizes are expected to be less than 256! (fit in 8
  19. // bits), and must be powers of 2.
  20. #ifndef AVRLIB_AVRLIB_H_
  21. #define AVRLIB_AVRLIB_H_
  22. #include <avr/io.h>
  23. #include "avrlib/base.h"
  24. #include "avrlib/size_to_type.h"
  25. namespace avrlib {
  26. enum DataOrder {
  27. MSB_FIRST = 0,
  28. LSB_FIRST = 1
  29. };
  30. enum DigitalValue {
  31. LOW = 0,
  32. HIGH = 1
  33. };
  34. // <avr/io.h> is full of useful defines, but they cannot be used as template
  35. // arguments because they are of the form: (*(volatile uint8_t *)(0x80))
  36. // The following define wraps this reference into a class to make it easier to
  37. // pass it as a template argument.
  38. #define IORegister(reg) struct reg##Register { \
  39. static volatile uint8_t* ptr() { return &reg; } \
  40. reg##Register& operator=(const uint8_t& value) { *ptr() = value; } \
  41. uint8_t operator()(const uint8_t& value) { return *ptr(); } \
  42. };
  43. #define IORegister16(reg) struct reg##Register { \
  44. static volatile uint16_t* ptr() { return &reg; } \
  45. reg##Register& operator=(const uint16_t& value) { *ptr() = value; } \
  46. uint16_t operator()(const uint16_t& value) { return *ptr(); } \
  47. };
  48. #define SpecialFunctionRegister(reg) struct reg##Register { \
  49. static volatile uint8_t* ptr() { return &_SFR_BYTE(reg); } \
  50. reg##Register& operator=(const uint8_t& value) { *ptr() = value; } \
  51. uint8_t operator()(const uint8_t& value) { return *ptr(); } \
  52. };
  53. #define SpecialFunctionRegister16(reg) struct reg##Register { \
  54. static volatile uint16_t* ptr() { return &_SFR_WORD(reg); } \
  55. reg##Register& operator=(const uint16_t& value) { *ptr() = value; } \
  56. uint16_t operator()(const uint16_t& value) { return *ptr(); } \
  57. };
  58. // Represents a bit in an i/o port register.
  59. template<typename Register, uint8_t bit>
  60. struct BitInRegister {
  61. static void clear() {
  62. *Register::ptr() &= ~_BV(bit);
  63. }
  64. static void set() {
  65. *Register::ptr() |= _BV(bit);
  66. }
  67. static void toggle() {
  68. *Register::ptr() ^= _BV(bit);
  69. }
  70. static uint8_t value() {
  71. return *Register::ptr() & _BV(bit) ? 1 : 0;
  72. }
  73. };
  74. // These classes implement/define the basic input/output interface. The default
  75. // implementation is that of an infinite stream of incoming/outgoing 0s.
  76. struct Input {
  77. enum {
  78. buffer_size = 0, // Recommended buffer size, when using buffered input.
  79. data_size = 0, // 0 for disabled port, 1 for digital, 8 for byte.
  80. };
  81. typedef uint8_t Value;
  82. // Blocking!
  83. static inline Value Read() { while (!readable()); return ImmediateRead(); }
  84. // Number of bytes available for read.
  85. static inline uint8_t readable() { return 1; }
  86. // A byte, or -1 if reading failed.
  87. static inline int16_t NonBlockingRead() { return readable() ? Read() : -1; }
  88. // No check for ready state.
  89. static inline Value ImmediateRead() { return 0; }
  90. // Called in data reception interrupt.
  91. static inline void Received() { }
  92. };
  93. struct Output {
  94. enum {
  95. buffer_size = 0, // Recommended buffer size, when using buffered output.
  96. data_size = 0 // 0 for disabled port, 1 for digital, 8 for byte.
  97. };
  98. typedef uint8_t Value;
  99. // Blocking!
  100. static inline void Write(Value v) { while (!writable()); Overwrite(v); }
  101. // Number of bytes that can be fed.
  102. static inline uint8_t writable() { return 1; }
  103. // 1 if success.
  104. static inline uint8_t NonBlockingWrite(Value v) {
  105. if (!writable()) {
  106. return 0;
  107. }
  108. Overwrite(v);
  109. return 1;
  110. }
  111. // No check for ready state.
  112. static inline void Overwrite(Value) { return; }
  113. // Called in data emission interrupt.
  114. static inline Value Requested() { return 0; }
  115. };
  116. // An object capable both of input and output, composed from an Input and an
  117. // Output implementation.
  118. template<typename I, typename O>
  119. struct InputOutput {
  120. typedef I Input;
  121. typedef O Output;
  122. static inline void Write(typename O::Value v) { O::Write(v); }
  123. static inline uint8_t writable() { return O::writable(); }
  124. static inline uint8_t NonBlockingWrite(typename O::Value v ) {
  125. return O::NonBlockingWrite(v);
  126. }
  127. static inline void Overwrite(typename O::Value v) { O::Overwrite(v); }
  128. static inline typename O::Value Requested() { return O::Requested(); }
  129. static inline typename I::Value Read() { return I::Read(); }
  130. static inline uint8_t readable() { return I::readable(); }
  131. static inline int16_t NonBlockingRead() { return I::NonBlockingRead(); }
  132. static inline typename I::Value ImmediateRead() { return I::ImmediateRead(); }
  133. static inline void Received() { I::Received(); }
  134. };
  135. // Dummy class that can be passed whenever we expect Input/Output types, and
  136. // which do not perform any IO.
  137. typedef Input DisabledInput;
  138. typedef Output DisabledOutput;
  139. typedef InputOutput<DisabledInput, DisabledOutput> DisabledInputOutput;
  140. enum PortMode {
  141. DISABLED = 0,
  142. POLLED = 1,
  143. BUFFERED = 2
  144. };
  145. // Some classes (SPI, shift register) have a notion of communication session -
  146. // Begin is called, several R/W are done, and then End is called to pull high
  147. // a chip select or latch line. This template ensures that any path leaving a
  148. // block of code will release the resource.
  149. template<typename T>
  150. class scoped_resource {
  151. public:
  152. scoped_resource() {
  153. T::Begin();
  154. }
  155. ~scoped_resource() {
  156. T::End();
  157. }
  158. };
  159. } // namespace avrlib
  160. #endif // AVRLIB_AVRLIB_H_