Skip to content

Commit f5f96fd

Browse files
committed
updated libraries folder to contain all required libraries for teensy embedded code
1 parent 11e0d31 commit f5f96fd

File tree

156 files changed

+40891
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+40891
-0
lines changed
Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
#include <Adafruit_BusIO_Register.h>
2+
3+
#if !defined(SPI_INTERFACES_COUNT) || \
4+
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
5+
6+
/*!
7+
* @brief Create a register we access over an I2C Device (which defines the
8+
* bus and address)
9+
* @param i2cdevice The I2CDevice to use for underlying I2C access
10+
* @param reg_addr The address pointer value for the I2C/SMBus register, can
11+
* be 8 or 16 bits
12+
* @param width The width of the register data itself, defaults to 1 byte
13+
* @param byteorder The byte order of the register (used when width is > 1),
14+
* defaults to LSBFIRST
15+
* @param address_width The width of the register address itself, defaults
16+
* to 1 byte
17+
*/
18+
Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice,
19+
uint16_t reg_addr,
20+
uint8_t width,
21+
uint8_t byteorder,
22+
uint8_t address_width) {
23+
_i2cdevice = i2cdevice;
24+
_spidevice = nullptr;
25+
_addrwidth = address_width;
26+
_address = reg_addr;
27+
_byteorder = byteorder;
28+
_width = width;
29+
}
30+
31+
/*!
32+
* @brief Create a register we access over an SPI Device (which defines the
33+
* bus and CS pin)
34+
* @param spidevice The SPIDevice to use for underlying SPI access
35+
* @param reg_addr The address pointer value for the SPI register, can
36+
* be 8 or 16 bits
37+
* @param type The method we use to read/write data to SPI (which is not
38+
* as well defined as I2C)
39+
* @param width The width of the register data itself, defaults to 1 byte
40+
* @param byteorder The byte order of the register (used when width is > 1),
41+
* defaults to LSBFIRST
42+
* @param address_width The width of the register address itself, defaults
43+
* to 1 byte
44+
*/
45+
Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice,
46+
uint16_t reg_addr,
47+
Adafruit_BusIO_SPIRegType type,
48+
uint8_t width,
49+
uint8_t byteorder,
50+
uint8_t address_width) {
51+
_spidevice = spidevice;
52+
_spiregtype = type;
53+
_i2cdevice = nullptr;
54+
_addrwidth = address_width;
55+
_address = reg_addr;
56+
_byteorder = byteorder;
57+
_width = width;
58+
}
59+
60+
/*!
61+
* @brief Create a register we access over an I2C or SPI Device. This is a
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
68+
* @param reg_addr The address pointer value for the I2C/SMBus/SPI register,
69+
* can be 8 or 16 bits
70+
* @param type The method we use to read/write data to SPI (which is not
71+
* as well defined as I2C)
72+
* @param width The width of the register data itself, defaults to 1 byte
73+
* @param byteorder The byte order of the register (used when width is > 1),
74+
* defaults to LSBFIRST
75+
* @param address_width The width of the register address itself, defaults
76+
* to 1 byte
77+
*/
78+
Adafruit_BusIO_Register::Adafruit_BusIO_Register(
79+
Adafruit_I2CDevice *i2cdevice, Adafruit_SPIDevice *spidevice,
80+
Adafruit_BusIO_SPIRegType type, uint16_t reg_addr, uint8_t width,
81+
uint8_t byteorder, uint8_t address_width) {
82+
_spidevice = spidevice;
83+
_i2cdevice = i2cdevice;
84+
_spiregtype = type;
85+
_addrwidth = address_width;
86+
_address = reg_addr;
87+
_byteorder = byteorder;
88+
_width = width;
89+
}
90+
91+
/*!
92+
* @brief Write a buffer of data to the register location
93+
* @param buffer Pointer to data to write
94+
* @param len Number of bytes to write
95+
* @return True on successful write (only really useful for I2C as SPI is
96+
* uncheckable)
97+
*/
98+
bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
99+
100+
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
101+
(uint8_t)(_address >> 8)};
102+
103+
if (_i2cdevice) {
104+
return _i2cdevice->write(buffer, len, true, addrbuffer, _addrwidth);
105+
}
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+
120+
if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
121+
addrbuffer[0] &= ~0x80;
122+
}
123+
if (_spiregtype == ADDRBIT8_HIGH_TOWRITE) {
124+
addrbuffer[0] |= 0x80;
125+
}
126+
if (_spiregtype == AD8_HIGH_TOREAD_AD7_HIGH_TOINC) {
127+
addrbuffer[0] &= ~0x80;
128+
addrbuffer[0] |= 0x40;
129+
}
130+
return _spidevice->write(buffer, len, addrbuffer, _addrwidth);
131+
}
132+
return false;
133+
}
134+
135+
/*!
136+
* @brief Write up to 4 bytes of data to the register location
137+
* @param value Data to write
138+
* @param numbytes How many bytes from 'value' to write
139+
* @return True on successful write (only really useful for I2C as SPI is
140+
* uncheckable)
141+
*/
142+
bool Adafruit_BusIO_Register::write(uint32_t value, uint8_t numbytes) {
143+
if (numbytes == 0) {
144+
numbytes = _width;
145+
}
146+
if (numbytes > 4) {
147+
return false;
148+
}
149+
150+
// store a copy
151+
_cached = value;
152+
153+
for (int i = 0; i < numbytes; i++) {
154+
if (_byteorder == LSBFIRST) {
155+
_buffer[i] = value & 0xFF;
156+
} else {
157+
_buffer[numbytes - i - 1] = value & 0xFF;
158+
}
159+
value >>= 8;
160+
}
161+
return write(_buffer, numbytes);
162+
}
163+
164+
/*!
165+
* @brief Read data from the register location. This does not do any error
166+
* checking!
167+
* @return Returns 0xFFFFFFFF on failure, value otherwise
168+
*/
169+
uint32_t Adafruit_BusIO_Register::read(void) {
170+
if (!read(_buffer, _width)) {
171+
return -1;
172+
}
173+
174+
uint32_t value = 0;
175+
176+
for (int i = 0; i < _width; i++) {
177+
value <<= 8;
178+
if (_byteorder == LSBFIRST) {
179+
value |= _buffer[_width - i - 1];
180+
} else {
181+
value |= _buffer[i];
182+
}
183+
}
184+
185+
return value;
186+
}
187+
188+
/*!
189+
* @brief Read cached data from last time we wrote to this register
190+
* @return Returns 0xFFFFFFFF on failure, value otherwise
191+
*/
192+
uint32_t Adafruit_BusIO_Register::readCached(void) { return _cached; }
193+
194+
/*!
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+
*/
201+
bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
202+
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
203+
(uint8_t)(_address >> 8)};
204+
205+
if (_i2cdevice) {
206+
return _i2cdevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
207+
}
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+
}
222+
if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
223+
addrbuffer[0] |= 0x80;
224+
}
225+
if (_spiregtype == ADDRBIT8_HIGH_TOWRITE) {
226+
addrbuffer[0] &= ~0x80;
227+
}
228+
if (_spiregtype == AD8_HIGH_TOREAD_AD7_HIGH_TOINC) {
229+
addrbuffer[0] |= 0x80 | 0x40;
230+
}
231+
return _spidevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
232+
}
233+
return false;
234+
}
235+
236+
/*!
237+
* @brief Read 2 bytes of data from the register location
238+
* @param value Pointer to uint16_t variable to read into
239+
* @return True on successful write (only really useful for I2C as SPI is
240+
* uncheckable)
241+
*/
242+
bool Adafruit_BusIO_Register::read(uint16_t *value) {
243+
if (!read(_buffer, 2)) {
244+
return false;
245+
}
246+
247+
if (_byteorder == LSBFIRST) {
248+
*value = _buffer[1];
249+
*value <<= 8;
250+
*value |= _buffer[0];
251+
} else {
252+
*value = _buffer[0];
253+
*value <<= 8;
254+
*value |= _buffer[1];
255+
}
256+
return true;
257+
}
258+
259+
/*!
260+
* @brief Read 1 byte of data from the register location
261+
* @param value Pointer to uint8_t variable to read into
262+
* @return True on successful write (only really useful for I2C as SPI is
263+
* uncheckable)
264+
*/
265+
bool Adafruit_BusIO_Register::read(uint8_t *value) {
266+
if (!read(_buffer, 1)) {
267+
return false;
268+
}
269+
270+
*value = _buffer[0];
271+
return true;
272+
}
273+
274+
/*!
275+
* @brief Pretty printer for this register
276+
* @param s The Stream to print to, defaults to &Serial
277+
*/
278+
void Adafruit_BusIO_Register::print(Stream *s) {
279+
uint32_t val = read();
280+
s->print("0x");
281+
s->print(val, HEX);
282+
}
283+
284+
/*!
285+
* @brief Pretty printer for this register
286+
* @param s The Stream to print to, defaults to &Serial
287+
*/
288+
void Adafruit_BusIO_Register::println(Stream *s) {
289+
print(s);
290+
s->println();
291+
}
292+
293+
/*!
294+
* @brief Create a slice of the register that we can address without
295+
* touching other bits
296+
* @param reg The Adafruit_BusIO_Register which defines the bus/register
297+
* @param bits The number of bits wide we are slicing
298+
* @param shift The number of bits that our bit-slice is shifted from LSB
299+
*/
300+
Adafruit_BusIO_RegisterBits::Adafruit_BusIO_RegisterBits(
301+
Adafruit_BusIO_Register *reg, uint8_t bits, uint8_t shift) {
302+
_register = reg;
303+
_bits = bits;
304+
_shift = shift;
305+
}
306+
307+
/*!
308+
* @brief Read 4 bytes of data from the register
309+
* @return data The 4 bytes to read
310+
*/
311+
uint32_t Adafruit_BusIO_RegisterBits::read(void) {
312+
uint32_t val = _register->read();
313+
val >>= _shift;
314+
return val & ((1 << (_bits)) - 1);
315+
}
316+
317+
/*!
318+
* @brief Write 4 bytes of data to the register
319+
* @param data The 4 bytes to write
320+
* @return True on successful write (only really useful for I2C as SPI is
321+
* uncheckable)
322+
*/
323+
bool Adafruit_BusIO_RegisterBits::write(uint32_t data) {
324+
uint32_t val = _register->read();
325+
326+
// mask off the data before writing
327+
uint32_t mask = (1 << (_bits)) - 1;
328+
data &= mask;
329+
330+
mask <<= _shift;
331+
val &= ~mask; // remove the current data at that spot
332+
val |= data << _shift; // and add in the new data
333+
334+
return _register->write(val, _register->width());
335+
}
336+
337+
/*!
338+
* @brief The width of the register data, helpful for doing calculations
339+
* @returns The data width used when initializing the register
340+
*/
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

Comments
 (0)