Adafruit_BusIO_Register.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef Adafruit_BusIO_Register_h
  2. #define Adafruit_BusIO_Register_h
  3. #include <Arduino.h>
  4. #if !defined(SPI_INTERFACES_COUNT) || \
  5. (defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
  6. #include <Adafruit_GenericDevice.h>
  7. #include <Adafruit_I2CDevice.h>
  8. #include <Adafruit_SPIDevice.h>
  9. typedef enum _Adafruit_BusIO_SPIRegType {
  10. ADDRBIT8_HIGH_TOREAD = 0,
  11. /*!<
  12. * ADDRBIT8_HIGH_TOREAD
  13. * When reading a register you must actually send the value 0x80 + register
  14. * address to the device. e.g. To read the register 0x0B the register value
  15. * 0x8B is sent and to write 0x0B is sent.
  16. */
  17. AD8_HIGH_TOREAD_AD7_HIGH_TOINC = 1,
  18. /*!<
  19. * ADDRBIT8_HIGH_TOWRITE
  20. * When writing to a register you must actually send the value 0x80 +
  21. * the register address to the device. e.g. To write to the register 0x19 the
  22. * register value 0x99 is sent and to read 0x19 is sent.
  23. */
  24. ADDRBIT8_HIGH_TOWRITE = 2,
  25. /*!<
  26. * ADDRESSED_OPCODE_LOWBIT_TO_WRITE
  27. * Used by the MCP23S series, we send 0x40 |'rd with the opcode
  28. * Then set the lowest bit to write
  29. */
  30. ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE = 3,
  31. } Adafruit_BusIO_SPIRegType;
  32. /*!
  33. * @brief The class which defines a device register (a location to read/write
  34. * data from)
  35. */
  36. class Adafruit_BusIO_Register {
  37. public:
  38. Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice, uint16_t reg_addr,
  39. uint8_t width = 1, uint8_t byteorder = LSBFIRST,
  40. uint8_t address_width = 1);
  41. Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice, uint16_t reg_addr,
  42. Adafruit_BusIO_SPIRegType type, uint8_t width = 1,
  43. uint8_t byteorder = LSBFIRST,
  44. uint8_t address_width = 1);
  45. Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice,
  46. Adafruit_SPIDevice *spidevice,
  47. Adafruit_BusIO_SPIRegType type, uint16_t reg_addr,
  48. uint8_t width = 1, uint8_t byteorder = LSBFIRST,
  49. uint8_t address_width = 1);
  50. Adafruit_BusIO_Register(Adafruit_GenericDevice *genericdevice,
  51. uint16_t reg_addr, uint8_t width = 1,
  52. uint8_t byteorder = LSBFIRST,
  53. uint8_t address_width = 1);
  54. bool read(uint8_t *buffer, uint8_t len);
  55. bool read(uint8_t *value);
  56. bool read(uint16_t *value);
  57. uint32_t read(void);
  58. uint32_t readCached(void);
  59. bool write(uint8_t *buffer, uint8_t len);
  60. bool write(uint32_t value, uint8_t numbytes = 0);
  61. uint8_t width(void);
  62. void setWidth(uint8_t width);
  63. void setAddress(uint16_t address);
  64. void setAddressWidth(uint16_t address_width);
  65. #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
  66. void print(Stream *s = &Serial);
  67. void println(Stream *s = &Serial);
  68. #else
  69. void print(Stream *s);
  70. void println(Stream *s);
  71. #endif
  72. private:
  73. Adafruit_I2CDevice *_i2cdevice;
  74. Adafruit_SPIDevice *_spidevice;
  75. Adafruit_GenericDevice *_genericdevice;
  76. Adafruit_BusIO_SPIRegType _spiregtype;
  77. uint16_t _address;
  78. uint8_t _width, _addrwidth, _byteorder;
  79. uint8_t _buffer[4]; // we won't support anything larger than uint32 for
  80. // non-buffered read
  81. uint32_t _cached = 0;
  82. };
  83. /*!
  84. * @brief The class which defines a slice of bits from within a device register
  85. * (a location to read/write data from)
  86. */
  87. class Adafruit_BusIO_RegisterBits {
  88. public:
  89. Adafruit_BusIO_RegisterBits(Adafruit_BusIO_Register *reg, uint8_t bits,
  90. uint8_t shift);
  91. bool write(uint32_t value);
  92. uint32_t read(void);
  93. private:
  94. Adafruit_BusIO_Register *_register;
  95. uint8_t _bits, _shift;
  96. };
  97. #endif // SPI exists
  98. #endif // BusIO_Register_h