Skip to content

Commit ab11ae0

Browse files
authored
Merge branch 'adafruit:master' into master
2 parents 24c2d82 + e75ac29 commit ab11ae0

File tree

23 files changed

+832
-221
lines changed

23 files changed

+832
-221
lines changed

.github/workflows/githubci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ jobs:
77
runs-on: ubuntu-latest
88

99
steps:
10-
- uses: actions/setup-python@v1
10+
- uses: actions/setup-python@v4
1111
with:
1212
python-version: '3.x'
13-
- uses: actions/checkout@v2
14-
- uses: actions/checkout@v2
13+
- uses: actions/checkout@v3
14+
- uses: actions/checkout@v3
1515
with:
1616
repository: adafruit/ci-arduino
1717
path: ci

Adafruit_BusIO_Register.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(
8888
_width = width;
8989
}
9090

91+
/*!
92+
* @brief Create a register we access over a GenericDevice
93+
* @param genericdevice Generic device to use
94+
* @param reg_addr Register address we will read/write
95+
* @param width Width of the register in bytes (1-4)
96+
* @param byteorder Byte order of register data (LSBFIRST or MSBFIRST)
97+
* @param address_width Width of the register address in bytes (1 or 2)
98+
*/
99+
Adafruit_BusIO_Register::Adafruit_BusIO_Register(
100+
Adafruit_GenericDevice *genericdevice, uint16_t reg_addr, uint8_t width,
101+
uint8_t byteorder, uint8_t address_width) {
102+
_i2cdevice = nullptr;
103+
_spidevice = nullptr;
104+
_genericdevice = genericdevice;
105+
_addrwidth = address_width;
106+
_address = reg_addr;
107+
_byteorder = byteorder;
108+
_width = width;
109+
}
110+
91111
/*!
92112
* @brief Write a buffer of data to the register location
93113
* @param buffer Pointer to data to write
@@ -96,17 +116,14 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(
96116
* uncheckable)
97117
*/
98118
bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
99-
100119
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
101120
(uint8_t)(_address >> 8)};
102-
103121
if (_i2cdevice) {
104122
return _i2cdevice->write(buffer, len, true, addrbuffer, _addrwidth);
105123
}
106124
if (_spidevice) {
107125
if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
108126
// very special case!
109-
110127
// pass the special opcode address which we set as the high byte of the
111128
// regaddr
112129
addrbuffer[0] =
@@ -116,7 +133,6 @@ bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
116133
// the address appears to be a byte longer
117134
return _spidevice->write(buffer, len, addrbuffer, _addrwidth + 1);
118135
}
119-
120136
if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
121137
addrbuffer[0] &= ~0x80;
122138
}
@@ -129,6 +145,9 @@ bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
129145
}
130146
return _spidevice->write(buffer, len, addrbuffer, _addrwidth);
131147
}
148+
if (_genericdevice) {
149+
return _genericdevice->writeRegister(addrbuffer, _addrwidth, buffer, len);
150+
}
132151
return false;
133152
}
134153

@@ -192,23 +211,20 @@ uint32_t Adafruit_BusIO_Register::read(void) {
192211
uint32_t Adafruit_BusIO_Register::readCached(void) { return _cached; }
193212

194213
/*!
195-
* @brief Read a buffer of data from the register location
196-
* @param buffer Pointer to data to read into
197-
* @param len Number of bytes to read
198-
* @return True on successful write (only really useful for I2C as SPI is
199-
* uncheckable)
200-
*/
214+
@brief Read a number of bytes from a register into a buffer
215+
@param buffer Buffer to read data into
216+
@param len Number of bytes to read into the buffer
217+
@return true on successful read, otherwise false
218+
*/
201219
bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
202220
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
203221
(uint8_t)(_address >> 8)};
204-
205222
if (_i2cdevice) {
206223
return _i2cdevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
207224
}
208225
if (_spidevice) {
209226
if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
210227
// very special case!
211-
212228
// pass the special opcode address which we set as the high byte of the
213229
// regaddr
214230
addrbuffer[0] =
@@ -230,6 +246,9 @@ bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
230246
}
231247
return _spidevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
232248
}
249+
if (_genericdevice) {
250+
return _genericdevice->readRegister(addrbuffer, _addrwidth, buffer, len);
251+
}
233252
return false;
234253
}
235254

Adafruit_BusIO_Register.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#if !defined(SPI_INTERFACES_COUNT) || \
77
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
88

9+
#include <Adafruit_GenericDevice.h>
910
#include <Adafruit_I2CDevice.h>
1011
#include <Adafruit_SPIDevice.h>
1112

@@ -57,6 +58,11 @@ class Adafruit_BusIO_Register {
5758
uint8_t width = 1, uint8_t byteorder = LSBFIRST,
5859
uint8_t address_width = 1);
5960

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+
6066
bool read(uint8_t *buffer, uint8_t len);
6167
bool read(uint8_t *value);
6268
bool read(uint16_t *value);
@@ -82,6 +88,7 @@ class Adafruit_BusIO_Register {
8288
private:
8389
Adafruit_I2CDevice *_i2cdevice;
8490
Adafruit_SPIDevice *_spidevice;
91+
Adafruit_GenericDevice *_genericdevice;
8592
Adafruit_BusIO_SPIRegType _spiregtype;
8693
uint16_t _address;
8794
uint8_t _width, _addrwidth, _byteorder;

Adafruit_GenericDevice.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Written with help by Claude!
3+
https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
4+
chats are not shareable :(
5+
*/
6+
7+
#include "Adafruit_GenericDevice.h"
8+
9+
/*!
10+
* @brief Create a Generic device with the provided read/write functions
11+
* @param obj Pointer to object instance
12+
* @param read_func Function pointer for reading raw data
13+
* @param write_func Function pointer for writing raw data
14+
* @param readreg_func Function pointer for reading registers (optional)
15+
* @param writereg_func Function pointer for writing registers (optional) */
16+
Adafruit_GenericDevice::Adafruit_GenericDevice(
17+
void *obj, busio_genericdevice_read_t read_func,
18+
busio_genericdevice_write_t write_func,
19+
busio_genericdevice_readreg_t readreg_func,
20+
busio_genericdevice_writereg_t writereg_func) {
21+
_obj = obj;
22+
_read_func = read_func;
23+
_write_func = write_func;
24+
_readreg_func = readreg_func;
25+
_writereg_func = writereg_func;
26+
_begun = false;
27+
}
28+
29+
/*! @brief Simple begin function (doesn't do much at this time)
30+
@return true always
31+
*/
32+
bool Adafruit_GenericDevice::begin(void) {
33+
_begun = true;
34+
return true;
35+
}
36+
37+
/*!
38+
@brief Marks the GenericDevice as no longer in use.
39+
@note: Since this is a GenericDevice, if you are using this with a Serial
40+
object, this does NOT disable serial communication or release the RX/TX pins.
41+
That must be done manually by calling Serial.end().
42+
*/
43+
void Adafruit_GenericDevice::end(void) { _begun = false; }
44+
45+
/*! @brief Write a buffer of data
46+
@param buffer Pointer to buffer of data to write
47+
@param len Number of bytes to write
48+
@return true if write was successful, otherwise false */
49+
bool Adafruit_GenericDevice::write(const uint8_t *buffer, size_t len) {
50+
if (!_begun)
51+
return false;
52+
return _write_func(_obj, buffer, len);
53+
}
54+
55+
/*! @brief Read data into a buffer
56+
@param buffer Pointer to buffer to read data into
57+
@param len Number of bytes to read
58+
@return true if read was successful, otherwise false */
59+
bool Adafruit_GenericDevice::read(uint8_t *buffer, size_t len) {
60+
if (!_begun)
61+
return false;
62+
return _read_func(_obj, buffer, len);
63+
}
64+
65+
/*! @brief Read from a register location
66+
@param addr_buf Buffer containing register address
67+
@param addrsiz Size of register address in bytes
68+
@param buf Buffer to store read data
69+
@param bufsiz Size of data to read in bytes
70+
@return true if read was successful, otherwise false */
71+
bool Adafruit_GenericDevice::readRegister(uint8_t *addr_buf, uint8_t addrsiz,
72+
uint8_t *buf, uint16_t bufsiz) {
73+
if (!_begun || !_readreg_func)
74+
return false;
75+
return _readreg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
76+
}
77+
78+
/*! @brief Write to a register location
79+
@param addr_buf Buffer containing register address
80+
@param addrsiz Size of register address in bytes
81+
@param buf Buffer containing data to write
82+
@param bufsiz Size of data to write in bytes
83+
@return true if write was successful, otherwise false */
84+
bool Adafruit_GenericDevice::writeRegister(uint8_t *addr_buf, uint8_t addrsiz,
85+
const uint8_t *buf,
86+
uint16_t bufsiz) {
87+
if (!_begun || !_writereg_func)
88+
return false;
89+
return _writereg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
90+
}

Adafruit_GenericDevice.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef ADAFRUIT_GENERICDEVICE_H
2+
#define ADAFRUIT_GENERICDEVICE_H
3+
4+
#include <Arduino.h>
5+
6+
typedef bool (*busio_genericdevice_read_t)(void *obj, uint8_t *buffer,
7+
size_t len);
8+
typedef bool (*busio_genericdevice_write_t)(void *obj, const uint8_t *buffer,
9+
size_t len);
10+
typedef bool (*busio_genericdevice_readreg_t)(void *obj, uint8_t *addr_buf,
11+
uint8_t addrsiz, uint8_t *data,
12+
uint16_t datalen);
13+
typedef bool (*busio_genericdevice_writereg_t)(void *obj, uint8_t *addr_buf,
14+
uint8_t addrsiz,
15+
const uint8_t *data,
16+
uint16_t datalen);
17+
18+
/*!
19+
* @brief Class for communicating with a device via generic read/write functions
20+
*/
21+
class Adafruit_GenericDevice {
22+
public:
23+
Adafruit_GenericDevice(
24+
void *obj, busio_genericdevice_read_t read_func,
25+
busio_genericdevice_write_t write_func,
26+
busio_genericdevice_readreg_t readreg_func = nullptr,
27+
busio_genericdevice_writereg_t writereg_func = nullptr);
28+
29+
bool begin(void);
30+
void end(void);
31+
32+
bool read(uint8_t *buffer, size_t len);
33+
bool write(const uint8_t *buffer, size_t len);
34+
bool readRegister(uint8_t *addr_buf, uint8_t addrsiz, uint8_t *buf,
35+
uint16_t bufsiz);
36+
bool writeRegister(uint8_t *addr_buf, uint8_t addrsiz, const uint8_t *buf,
37+
uint16_t bufsiz);
38+
39+
protected:
40+
/*! @brief Function pointer for reading raw data from the device */
41+
busio_genericdevice_read_t _read_func;
42+
/*! @brief Function pointer for writing raw data to the device */
43+
busio_genericdevice_write_t _write_func;
44+
/*! @brief Function pointer for reading a 'register' from the device */
45+
busio_genericdevice_readreg_t _readreg_func;
46+
/*! @brief Function pointer for writing a 'register' to the device */
47+
busio_genericdevice_writereg_t _writereg_func;
48+
49+
bool _begun; ///< whether we have initialized yet (in case the function needs
50+
///< to do something)
51+
52+
private:
53+
void *_obj; ///< Pointer to object instance
54+
};
55+
56+
#endif // ADAFRUIT_GENERICDEVICE_H

Adafruit_I2CDevice.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "Adafruit_I2CDevice.h"
22

3-
//#define DEBUG_SERIAL Serial
3+
// #define DEBUG_SERIAL Serial
44

55
/*!
66
* @brief Create an I2C device at a given address
@@ -23,8 +23,8 @@ Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire) {
2323
/*!
2424
* @brief Initializes and does basic address detection
2525
* @param addr_detect Whether we should attempt to detect the I2C address
26-
* with a scan. 99% of sensors/devices don't mind but once in a while, they spaz
27-
* on a scan!
26+
* with a scan. 99% of sensors/devices don't mind, but once in a while they
27+
* don't respond well to a scan!
2828
* @return True if I2C initialized and a device with the addr found
2929
*/
3030
bool Adafruit_I2CDevice::begin(bool addr_detect) {
@@ -67,14 +67,21 @@ bool Adafruit_I2CDevice::detected(void) {
6767

6868
// A basic scanner, see if it ACK's
6969
_wire->beginTransmission(_addr);
70+
#ifdef DEBUG_SERIAL
71+
DEBUG_SERIAL.print(F("Address 0x"));
72+
DEBUG_SERIAL.print(_addr, HEX);
73+
#endif
74+
#ifdef ARDUINO_ARCH_MBED
75+
_wire->write(0); // forces a write request instead of a read
76+
#endif
7077
if (_wire->endTransmission() == 0) {
7178
#ifdef DEBUG_SERIAL
72-
DEBUG_SERIAL.println(F("Detected"));
79+
DEBUG_SERIAL.println(F(" Detected"));
7380
#endif
7481
return true;
7582
}
7683
#ifdef DEBUG_SERIAL
77-
DEBUG_SERIAL.println(F("Not detected"));
84+
DEBUG_SERIAL.println(F(" Not detected"));
7885
#endif
7986
return false;
8087
}

0 commit comments

Comments
 (0)