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