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.

113 lines
2.9KB

  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. // Important: All buffer sizes are expected to be less than 256! (fit in 8
  19. // bits), and must be powers of 2.
  20. #ifndef AVRLIBX_AVRLIBX_H_
  21. #define AVRLIBX_AVRLIBX_H_
  22. #include <inttypes.h>
  23. #ifndef NULL
  24. #define NULL 0
  25. #endif
  26. #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  27. TypeName(const TypeName&); \
  28. void operator=(const TypeName&)
  29. template<bool b>
  30. inline void StaticAssertImplementation() {
  31. char static_assert_size_mismatch[b] = { 0 };
  32. }
  33. #define STATIC_ASSERT(expression) StaticAssertImplementation<(expression)>()
  34. typedef union {
  35. uint16_t value;
  36. uint8_t bytes[2];
  37. } Word;
  38. typedef union {
  39. uint32_t value;
  40. uint16_t words[2];
  41. uint8_t bytes[4];
  42. } LongWord;
  43. struct uint24_t {
  44. uint16_t integral;
  45. uint8_t fractional;
  46. };
  47. struct uint24c_t {
  48. uint8_t carry;
  49. uint16_t integral;
  50. uint8_t fractional;
  51. };
  52. template<uint32_t a, uint32_t b, uint32_t c, uint32_t d>
  53. struct FourCC {
  54. static const uint32_t value = (((((d << 8) | c) << 8) | b) << 8) | a;
  55. };
  56. namespace avrlibx {
  57. template<uint8_t size>
  58. struct DataTypeForSize {
  59. typedef uint16_t Type;
  60. };
  61. template<> struct DataTypeForSize<1> { typedef uint8_t Type; };
  62. template<> struct DataTypeForSize<2> { typedef uint8_t Type; };
  63. template<> struct DataTypeForSize<3> { typedef uint8_t Type; };
  64. template<> struct DataTypeForSize<4> { typedef uint8_t Type; };
  65. template<> struct DataTypeForSize<5> { typedef uint8_t Type; };
  66. template<> struct DataTypeForSize<6> { typedef uint8_t Type; };
  67. template<> struct DataTypeForSize<7> { typedef uint8_t Type; };
  68. template<> struct DataTypeForSize<8> { typedef uint8_t Type; };
  69. enum DataOrder {
  70. MSB_FIRST = 0,
  71. LSB_FIRST = 1
  72. };
  73. enum DigitalValue {
  74. LOW = 0,
  75. HIGH = 1
  76. };
  77. // Some classes (SPI, shift register) have a notion of communication session -
  78. // Begin is called, several R/W are done, and then End is called to pull high
  79. // a chip select or latch line. This template ensures that any path leaving a
  80. // block of code will release the resource.
  81. template<typename T>
  82. class scoped_resource {
  83. public:
  84. scoped_resource() {
  85. T::Begin();
  86. }
  87. ~scoped_resource() {
  88. T::End();
  89. }
  90. };
  91. } // namespace avrlibx
  92. #endif // AVRLIBX_AVRLIBX_H_