1
1
#include < Adafruit_BusIO_Register.h>
2
2
3
+ #if !defined(SPI_INTERFACES_COUNT) || \
4
+ (defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0 ))
5
+
3
6
/* !
4
7
* @brief Create a register we access over an I2C Device (which defines the
5
8
* bus and address)
@@ -18,7 +21,7 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice,
18
21
uint8_t byteorder,
19
22
uint8_t address_width) {
20
23
_i2cdevice = i2cdevice;
21
- _spidevice = NULL ;
24
+ _spidevice = nullptr ;
22
25
_addrwidth = address_width;
23
26
_address = reg_addr;
24
27
_byteorder = byteorder;
@@ -47,7 +50,7 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice,
47
50
uint8_t address_width) {
48
51
_spidevice = spidevice;
49
52
_spiregtype = type;
50
- _i2cdevice = NULL ;
53
+ _i2cdevice = nullptr ;
51
54
_addrwidth = address_width;
52
55
_address = reg_addr;
53
56
_byteorder = byteorder;
@@ -56,12 +59,12 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice,
56
59
57
60
/* !
58
61
* @brief Create a register we access over an I2C or SPI Device. This is a
59
- * handy function because we can pass in NULL for the unused interface, allowing
60
- * libraries to mass-define all the registers
61
- * @param i2cdevice The I2CDevice to use for underlying I2C access, if NULL
62
- * we use SPI
63
- * @param spidevice The SPIDevice to use for underlying SPI access, if NULL
64
- * we use I2C
62
+ * handy function because we can pass in nullptr for the unused interface,
63
+ * allowing libraries to mass-define all the registers
64
+ * @param i2cdevice The I2CDevice to use for underlying I2C access, if
65
+ * nullptr we use SPI
66
+ * @param spidevice The SPIDevice to use for underlying SPI access, if
67
+ * nullptr we use I2C
65
68
* @param reg_addr The address pointer value for the I2C/SMBus/SPI register,
66
69
* can be 8 or 16 bits
67
70
* @param type The method we use to read/write data to SPI (which is not
@@ -101,6 +104,19 @@ bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
101
104
return _i2cdevice->write (buffer, len, true , addrbuffer, _addrwidth);
102
105
}
103
106
if (_spidevice) {
107
+ if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
108
+ // very special case!
109
+
110
+ // pass the special opcode address which we set as the high byte of the
111
+ // regaddr
112
+ addrbuffer[0 ] =
113
+ (uint8_t )(_address >> 8 ) & ~0x01 ; // set bottom bit low to write
114
+ // the 'actual' reg addr is the second byte then
115
+ addrbuffer[1 ] = (uint8_t )(_address & 0xFF );
116
+ // the address appears to be a byte longer
117
+ return _spidevice->write (buffer, len, addrbuffer, _addrwidth + 1 );
118
+ }
119
+
104
120
if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
105
121
addrbuffer[0 ] &= ~0x80 ;
106
122
}
@@ -190,6 +206,19 @@ bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
190
206
return _i2cdevice->write_then_read (addrbuffer, _addrwidth, buffer, len);
191
207
}
192
208
if (_spidevice) {
209
+ if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
210
+ // very special case!
211
+
212
+ // pass the special opcode address which we set as the high byte of the
213
+ // regaddr
214
+ addrbuffer[0 ] =
215
+ (uint8_t )(_address >> 8 ) | 0x01 ; // set bottom bit high to read
216
+ // the 'actual' reg addr is the second byte then
217
+ addrbuffer[1 ] = (uint8_t )(_address & 0xFF );
218
+ // the address appears to be a byte longer
219
+ return _spidevice->write_then_read (addrbuffer, _addrwidth + 1 , buffer,
220
+ len);
221
+ }
193
222
if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
194
223
addrbuffer[0 ] |= 0x80 ;
195
224
}
@@ -310,3 +339,27 @@ bool Adafruit_BusIO_RegisterBits::write(uint32_t data) {
310
339
* @returns The data width used when initializing the register
311
340
*/
312
341
uint8_t Adafruit_BusIO_Register::width (void ) { return _width; }
342
+
343
+ /* !
344
+ * @brief Set the default width of data
345
+ * @param width the default width of data read from register
346
+ */
347
+ void Adafruit_BusIO_Register::setWidth (uint8_t width) { _width = width; }
348
+
349
+ /* !
350
+ * @brief Set register address
351
+ * @param address the address from register
352
+ */
353
+ void Adafruit_BusIO_Register::setAddress (uint16_t address) {
354
+ _address = address;
355
+ }
356
+
357
+ /* !
358
+ * @brief Set the width of register address
359
+ * @param address_width the width for register address
360
+ */
361
+ void Adafruit_BusIO_Register::setAddressWidth (uint16_t address_width) {
362
+ _addrwidth = address_width;
363
+ }
364
+
365
+ #endif // SPI exists
0 commit comments