Skip to content

Commit d4bf576

Browse files
committed
port to RTduino
1 parent e89ae64 commit d4bf576

7 files changed

+201
-324
lines changed

Adafruit_BusIO_Register.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
#include <Adafruit_BusIO_Register.h>
1+
/*
2+
* Copyright (c) 2021-2022, RTduino Development Team
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-02-19 Meco Man port to RTduino
9+
*/
210

3-
#if !defined(SPI_INTERFACES_COUNT) || \
4-
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
11+
#include <Adafruit_BusIO_Register.h>
512

13+
#ifdef RTDUINO_USING_WIRE
614
/*!
715
* @brief Create a register we access over an I2C Device (which defines the
816
* bus and address)
@@ -21,13 +29,18 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice,
2129
uint8_t byteorder,
2230
uint8_t address_width) {
2331
_i2cdevice = i2cdevice;
24-
_spidevice = nullptr;
32+
#ifdef RTDUINO_USING_SPI
33+
_spidevice = NULL;
34+
#endif /* RTDUINO_USING_SPI */
2535
_addrwidth = address_width;
2636
_address = reg_addr;
2737
_byteorder = byteorder;
2838
_width = width;
39+
_cached = 0;
2940
}
41+
#endif /* RTDUINO_USING_WIRE */
3042

43+
#ifdef RTDUINO_USING_SPI
3144
/*!
3245
* @brief Create a register we access over an SPI Device (which defines the
3346
* bus and CS pin)
@@ -50,11 +63,12 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice,
5063
uint8_t address_width) {
5164
_spidevice = spidevice;
5265
_spiregtype = type;
53-
_i2cdevice = nullptr;
66+
_i2cdevice = NULL;
5467
_addrwidth = address_width;
5568
_address = reg_addr;
5669
_byteorder = byteorder;
5770
_width = width;
71+
_cached = 0;
5872
}
5973

6074
/*!
@@ -86,7 +100,9 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(
86100
_address = reg_addr;
87101
_byteorder = byteorder;
88102
_width = width;
103+
_cached = 0;
89104
}
105+
#endif /* RTDUINO_USING_SPI */
90106

91107
/*!
92108
* @brief Write a buffer of data to the register location
@@ -99,10 +115,12 @@ bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
99115

100116
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
101117
(uint8_t)(_address >> 8)};
102-
118+
#ifdef RTDUINO_USING_WIRE
103119
if (_i2cdevice) {
104120
return _i2cdevice->write(buffer, len, true, addrbuffer, _addrwidth);
105121
}
122+
#endif /* RTDUINO_USING_WIRE */
123+
#ifdef RTDUINO_USING_SPI
106124
if (_spidevice) {
107125
if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
108126
// very special case!
@@ -129,6 +147,7 @@ bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
129147
}
130148
return _spidevice->write(buffer, len, addrbuffer, _addrwidth);
131149
}
150+
#endif /* RTDUINO_USING_SPI */
132151
return false;
133152
}
134153

@@ -168,7 +187,7 @@ bool Adafruit_BusIO_Register::write(uint32_t value, uint8_t numbytes) {
168187
*/
169188
uint32_t Adafruit_BusIO_Register::read(void) {
170189
if (!read(_buffer, _width)) {
171-
return -1;
190+
return (uint32_t)-1;
172191
}
173192

174193
uint32_t value = 0;
@@ -201,10 +220,12 @@ uint32_t Adafruit_BusIO_Register::readCached(void) { return _cached; }
201220
bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
202221
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
203222
(uint8_t)(_address >> 8)};
204-
223+
#ifdef RTDUINO_USING_WIRE
205224
if (_i2cdevice) {
206225
return _i2cdevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
207226
}
227+
#endif /* RTDUINO_USING_WIRE */
228+
#ifdef RTDUINO_USING_SPI
208229
if (_spidevice) {
209230
if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
210231
// very special case!
@@ -230,6 +251,7 @@ bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
230251
}
231252
return _spidevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
232253
}
254+
#endif /* RTDUINO_USING_SPI */
233255
return false;
234256
}
235257

@@ -361,5 +383,3 @@ void Adafruit_BusIO_Register::setAddress(uint16_t address) {
361383
void Adafruit_BusIO_Register::setAddressWidth(uint16_t address_width) {
362384
_addrwidth = address_width;
363385
}
364-
365-
#endif // SPI exists

Adafruit_BusIO_Register.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
/*
2+
* Copyright (c) 2021-2022, RTduino Development Team
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-02-19 Meco Man port to RTduino
9+
*/
10+
111
#ifndef Adafruit_BusIO_Register_h
212
#define Adafruit_BusIO_Register_h
313

414
#include <Arduino.h>
515

6-
#if !defined(SPI_INTERFACES_COUNT) || \
7-
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
8-
16+
#ifdef RTDUINO_USING_WIRE
917
#include <Adafruit_I2CDevice.h>
18+
#endif /* RTDUINO_USING_WIRE */
19+
#ifdef RTDUINO_USING_SPI
1020
#include <Adafruit_SPIDevice.h>
21+
#endif /* RTDUINO_USING_SPI */
1122

1223
typedef enum _Adafruit_BusIO_SPIRegType {
1324
ADDRBIT8_HIGH_TOREAD = 0,
@@ -42,10 +53,12 @@ typedef enum _Adafruit_BusIO_SPIRegType {
4253
*/
4354
class Adafruit_BusIO_Register {
4455
public:
56+
#ifdef RTDUINO_USING_WIRE
4557
Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice, uint16_t reg_addr,
4658
uint8_t width = 1, uint8_t byteorder = LSBFIRST,
4759
uint8_t address_width = 1);
48-
60+
#endif /* RTDUINO_USING_WIRE */
61+
#ifdef RTDUINO_USING_SPI
4962
Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice, uint16_t reg_addr,
5063
Adafruit_BusIO_SPIRegType type, uint8_t width = 1,
5164
uint8_t byteorder = LSBFIRST,
@@ -56,7 +69,7 @@ class Adafruit_BusIO_Register {
5669
Adafruit_BusIO_SPIRegType type, uint16_t reg_addr,
5770
uint8_t width = 1, uint8_t byteorder = LSBFIRST,
5871
uint8_t address_width = 1);
59-
72+
#endif /* RTDUINO_USING_SPI */
6073
bool read(uint8_t *buffer, uint8_t len);
6174
bool read(uint8_t *value);
6275
bool read(uint16_t *value);
@@ -75,14 +88,17 @@ class Adafruit_BusIO_Register {
7588
void println(Stream *s = &Serial);
7689

7790
private:
91+
#ifdef RTDUINO_USING_WIRE
7892
Adafruit_I2CDevice *_i2cdevice;
93+
#endif /* RTDUINO_USING_WIRE */
94+
#ifdef RTDUINO_USING_SPI
7995
Adafruit_SPIDevice *_spidevice;
8096
Adafruit_BusIO_SPIRegType _spiregtype;
97+
#endif /* RTDUINO_USING_SPI */
8198
uint16_t _address;
8299
uint8_t _width, _addrwidth, _byteorder;
83-
uint8_t _buffer[4]; // we won't support anything larger than uint32 for
84-
// non-buffered read
85-
uint32_t _cached = 0;
100+
uint8_t _buffer[4]; // we won't support anything larger than uint32 for non-buffered read
101+
uint32_t _cached;
86102
};
87103

88104
/*!
@@ -101,5 +117,4 @@ class Adafruit_BusIO_RegisterBits {
101117
uint8_t _bits, _shift;
102118
};
103119

104-
#endif // SPI exists
105120
#endif // BusIO_Register_h

0 commit comments

Comments
 (0)