Compare commits

..

No commits in common. "master" and "mcpreg" have entirely different histories.

26 changed files with 330 additions and 1154 deletions

View file

@ -7,27 +7,26 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/setup-python@v4 - uses: actions/setup-python@v1
with: with:
python-version: '3.x' python-version: '3.x'
- uses: actions/checkout@v3 - uses: actions/checkout@v2
- uses: actions/checkout@v3 - uses: actions/checkout@v2
with: with:
repository: adafruit/ci-arduino repository: adafruit/ci-arduino
path: ci path: ci
- name: Install the prerequisites - name: pre-install
run: bash ci/actions_install.sh run: bash ci/actions_install.sh
- name: Check for correct code formatting with clang-format - name: test platforms
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . run: python3 ci/build_platform.py main_platforms
- name: Check for correct documentation with doxygen - name: clang
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r .
- name: doxygen
env: env:
GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }}
PRETTYNAME : "Adafruit Bus IO Library" PRETTYNAME : "Adafruit Bus IO Library"
run: bash ci/doxy_gen_and_deploy.sh run: bash ci/doxy_gen_and_deploy.sh
- name: Test the code on supported platforms
run: python3 ci/build_platform.py main_platforms zero feather32u4

View file

@ -1,8 +1,5 @@
#include <Adafruit_BusIO_Register.h> #include <Adafruit_BusIO_Register.h>
#if !defined(SPI_INTERFACES_COUNT) || \
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
/*! /*!
* @brief Create a register we access over an I2C Device (which defines the * @brief Create a register we access over an I2C Device (which defines the
* bus and address) * bus and address)
@ -21,7 +18,7 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice,
uint8_t byteorder, uint8_t byteorder,
uint8_t address_width) { uint8_t address_width) {
_i2cdevice = i2cdevice; _i2cdevice = i2cdevice;
_spidevice = nullptr; _spidevice = NULL;
_addrwidth = address_width; _addrwidth = address_width;
_address = reg_addr; _address = reg_addr;
_byteorder = byteorder; _byteorder = byteorder;
@ -50,7 +47,7 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice,
uint8_t address_width) { uint8_t address_width) {
_spidevice = spidevice; _spidevice = spidevice;
_spiregtype = type; _spiregtype = type;
_i2cdevice = nullptr; _i2cdevice = NULL;
_addrwidth = address_width; _addrwidth = address_width;
_address = reg_addr; _address = reg_addr;
_byteorder = byteorder; _byteorder = byteorder;
@ -59,12 +56,12 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice,
/*! /*!
* @brief Create a register we access over an I2C or SPI Device. This is a * @brief Create a register we access over an I2C or SPI Device. This is a
* handy function because we can pass in nullptr for the unused interface, * handy function because we can pass in NULL for the unused interface, allowing
* allowing libraries to mass-define all the registers * libraries to mass-define all the registers
* @param i2cdevice The I2CDevice to use for underlying I2C access, if * @param i2cdevice The I2CDevice to use for underlying I2C access, if NULL
* nullptr we use SPI * we use SPI
* @param spidevice The SPIDevice to use for underlying SPI access, if * @param spidevice The SPIDevice to use for underlying SPI access, if NULL
* nullptr we use I2C * we use I2C
* @param reg_addr The address pointer value for the I2C/SMBus/SPI register, * @param reg_addr The address pointer value for the I2C/SMBus/SPI register,
* can be 8 or 16 bits * can be 8 or 16 bits
* @param type The method we use to read/write data to SPI (which is not * @param type The method we use to read/write data to SPI (which is not
@ -88,26 +85,6 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(
_width = width; _width = width;
} }
/*!
* @brief Create a register we access over a GenericDevice
* @param genericdevice Generic device to use
* @param reg_addr Register address we will read/write
* @param width Width of the register in bytes (1-4)
* @param byteorder Byte order of register data (LSBFIRST or MSBFIRST)
* @param address_width Width of the register address in bytes (1 or 2)
*/
Adafruit_BusIO_Register::Adafruit_BusIO_Register(
Adafruit_GenericDevice *genericdevice, uint16_t reg_addr, uint8_t width,
uint8_t byteorder, uint8_t address_width) {
_i2cdevice = nullptr;
_spidevice = nullptr;
_genericdevice = genericdevice;
_addrwidth = address_width;
_address = reg_addr;
_byteorder = byteorder;
_width = width;
}
/*! /*!
* @brief Write a buffer of data to the register location * @brief Write a buffer of data to the register location
* @param buffer Pointer to data to write * @param buffer Pointer to data to write
@ -116,14 +93,17 @@ Adafruit_BusIO_Register::Adafruit_BusIO_Register(
* uncheckable) * uncheckable)
*/ */
bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) { bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF), uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
(uint8_t)(_address >> 8)}; (uint8_t)(_address >> 8)};
if (_i2cdevice) { if (_i2cdevice) {
return _i2cdevice->write(buffer, len, true, addrbuffer, _addrwidth); return _i2cdevice->write(buffer, len, true, addrbuffer, _addrwidth);
} }
if (_spidevice) { if (_spidevice) {
if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) { if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
// very special case! // very special case!
// pass the special opcode address which we set as the high byte of the // pass the special opcode address which we set as the high byte of the
// regaddr // regaddr
addrbuffer[0] = addrbuffer[0] =
@ -133,6 +113,7 @@ bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
// the address appears to be a byte longer // the address appears to be a byte longer
return _spidevice->write(buffer, len, addrbuffer, _addrwidth + 1); return _spidevice->write(buffer, len, addrbuffer, _addrwidth + 1);
} }
if (_spiregtype == ADDRBIT8_HIGH_TOREAD) { if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
addrbuffer[0] &= ~0x80; addrbuffer[0] &= ~0x80;
} }
@ -145,9 +126,6 @@ bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
} }
return _spidevice->write(buffer, len, addrbuffer, _addrwidth); return _spidevice->write(buffer, len, addrbuffer, _addrwidth);
} }
if (_genericdevice) {
return _genericdevice->writeRegister(addrbuffer, _addrwidth, buffer, len);
}
return false; return false;
} }
@ -211,20 +189,23 @@ uint32_t Adafruit_BusIO_Register::read(void) {
uint32_t Adafruit_BusIO_Register::readCached(void) { return _cached; } uint32_t Adafruit_BusIO_Register::readCached(void) { return _cached; }
/*! /*!
@brief Read a number of bytes from a register into a buffer * @brief Read a buffer of data from the register location
@param buffer Buffer to read data into * @param buffer Pointer to data to read into
@param len Number of bytes to read into the buffer * @param len Number of bytes to read
@return true on successful read, otherwise false * @return True on successful write (only really useful for I2C as SPI is
*/ * uncheckable)
*/
bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) { bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF), uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
(uint8_t)(_address >> 8)}; (uint8_t)(_address >> 8)};
if (_i2cdevice) { if (_i2cdevice) {
return _i2cdevice->write_then_read(addrbuffer, _addrwidth, buffer, len); return _i2cdevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
} }
if (_spidevice) { if (_spidevice) {
if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) { if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
// very special case! // very special case!
// pass the special opcode address which we set as the high byte of the // pass the special opcode address which we set as the high byte of the
// regaddr // regaddr
addrbuffer[0] = addrbuffer[0] =
@ -246,9 +227,6 @@ bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
} }
return _spidevice->write_then_read(addrbuffer, _addrwidth, buffer, len); return _spidevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
} }
if (_genericdevice) {
return _genericdevice->readRegister(addrbuffer, _addrwidth, buffer, len);
}
return false; return false;
} }
@ -380,5 +358,3 @@ void Adafruit_BusIO_Register::setAddress(uint16_t address) {
void Adafruit_BusIO_Register::setAddressWidth(uint16_t address_width) { void Adafruit_BusIO_Register::setAddressWidth(uint16_t address_width) {
_addrwidth = address_width; _addrwidth = address_width;
} }
#endif // SPI exists

View file

@ -1,14 +1,9 @@
#ifndef Adafruit_BusIO_Register_h
#define Adafruit_BusIO_Register_h
#include <Arduino.h>
#if !defined(SPI_INTERFACES_COUNT) || \
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
#include <Adafruit_GenericDevice.h>
#include <Adafruit_I2CDevice.h> #include <Adafruit_I2CDevice.h>
#include <Adafruit_SPIDevice.h> #include <Adafruit_SPIDevice.h>
#include <Arduino.h>
#ifndef Adafruit_BusIO_Register_h
#define Adafruit_BusIO_Register_h
typedef enum _Adafruit_BusIO_SPIRegType { typedef enum _Adafruit_BusIO_SPIRegType {
ADDRBIT8_HIGH_TOREAD = 0, ADDRBIT8_HIGH_TOREAD = 0,
@ -58,11 +53,6 @@ public:
uint8_t width = 1, uint8_t byteorder = LSBFIRST, uint8_t width = 1, uint8_t byteorder = LSBFIRST,
uint8_t address_width = 1); uint8_t address_width = 1);
Adafruit_BusIO_Register(Adafruit_GenericDevice *genericdevice,
uint16_t reg_addr, uint8_t width = 1,
uint8_t byteorder = LSBFIRST,
uint8_t address_width = 1);
bool read(uint8_t *buffer, uint8_t len); bool read(uint8_t *buffer, uint8_t len);
bool read(uint8_t *value); bool read(uint8_t *value);
bool read(uint16_t *value); bool read(uint16_t *value);
@ -83,11 +73,10 @@ public:
private: private:
Adafruit_I2CDevice *_i2cdevice; Adafruit_I2CDevice *_i2cdevice;
Adafruit_SPIDevice *_spidevice; Adafruit_SPIDevice *_spidevice;
Adafruit_GenericDevice *_genericdevice;
Adafruit_BusIO_SPIRegType _spiregtype; Adafruit_BusIO_SPIRegType _spiregtype;
uint16_t _address; uint16_t _address;
uint8_t _width, _addrwidth, _byteorder; uint8_t _width, _addrwidth, _byteorder;
uint8_t _buffer[4]; // we won't support anything larger than uint32 for uint8_t _buffer[4]; // we wont support anything larger than uint32 for
// non-buffered read // non-buffered read
uint32_t _cached = 0; uint32_t _cached = 0;
}; };
@ -108,5 +97,4 @@ private:
uint8_t _bits, _shift; uint8_t _bits, _shift;
}; };
#endif // SPI exists
#endif // BusIO_Register_h #endif // BusIO_Register_h

View file

@ -1,90 +0,0 @@
/*
Written with help by Claude!
https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
chats are not shareable :(
*/
#include "Adafruit_GenericDevice.h"
/*!
* @brief Create a Generic device with the provided read/write functions
* @param obj Pointer to object instance
* @param read_func Function pointer for reading raw data
* @param write_func Function pointer for writing raw data
* @param readreg_func Function pointer for reading registers (optional)
* @param writereg_func Function pointer for writing registers (optional) */
Adafruit_GenericDevice::Adafruit_GenericDevice(
void *obj, busio_genericdevice_read_t read_func,
busio_genericdevice_write_t write_func,
busio_genericdevice_readreg_t readreg_func,
busio_genericdevice_writereg_t writereg_func) {
_obj = obj;
_read_func = read_func;
_write_func = write_func;
_readreg_func = readreg_func;
_writereg_func = writereg_func;
_begun = false;
}
/*! @brief Simple begin function (doesn't do much at this time)
@return true always
*/
bool Adafruit_GenericDevice::begin(void) {
_begun = true;
return true;
}
/*!
@brief Marks the GenericDevice as no longer in use.
@note: Since this is a GenericDevice, if you are using this with a Serial
object, this does NOT disable serial communication or release the RX/TX pins.
That must be done manually by calling Serial.end().
*/
void Adafruit_GenericDevice::end(void) { _begun = false; }
/*! @brief Write a buffer of data
@param buffer Pointer to buffer of data to write
@param len Number of bytes to write
@return true if write was successful, otherwise false */
bool Adafruit_GenericDevice::write(const uint8_t *buffer, size_t len) {
if (!_begun)
return false;
return _write_func(_obj, buffer, len);
}
/*! @brief Read data into a buffer
@param buffer Pointer to buffer to read data into
@param len Number of bytes to read
@return true if read was successful, otherwise false */
bool Adafruit_GenericDevice::read(uint8_t *buffer, size_t len) {
if (!_begun)
return false;
return _read_func(_obj, buffer, len);
}
/*! @brief Read from a register location
@param addr_buf Buffer containing register address
@param addrsiz Size of register address in bytes
@param buf Buffer to store read data
@param bufsiz Size of data to read in bytes
@return true if read was successful, otherwise false */
bool Adafruit_GenericDevice::readRegister(uint8_t *addr_buf, uint8_t addrsiz,
uint8_t *buf, uint16_t bufsiz) {
if (!_begun || !_readreg_func)
return false;
return _readreg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
}
/*! @brief Write to a register location
@param addr_buf Buffer containing register address
@param addrsiz Size of register address in bytes
@param buf Buffer containing data to write
@param bufsiz Size of data to write in bytes
@return true if write was successful, otherwise false */
bool Adafruit_GenericDevice::writeRegister(uint8_t *addr_buf, uint8_t addrsiz,
const uint8_t *buf,
uint16_t bufsiz) {
if (!_begun || !_writereg_func)
return false;
return _writereg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
}

View file

@ -1,56 +0,0 @@
#ifndef ADAFRUIT_GENERICDEVICE_H
#define ADAFRUIT_GENERICDEVICE_H
#include <Arduino.h>
typedef bool (*busio_genericdevice_read_t)(void *obj, uint8_t *buffer,
size_t len);
typedef bool (*busio_genericdevice_write_t)(void *obj, const uint8_t *buffer,
size_t len);
typedef bool (*busio_genericdevice_readreg_t)(void *obj, uint8_t *addr_buf,
uint8_t addrsiz, uint8_t *data,
uint16_t datalen);
typedef bool (*busio_genericdevice_writereg_t)(void *obj, uint8_t *addr_buf,
uint8_t addrsiz,
const uint8_t *data,
uint16_t datalen);
/*!
* @brief Class for communicating with a device via generic read/write functions
*/
class Adafruit_GenericDevice {
public:
Adafruit_GenericDevice(
void *obj, busio_genericdevice_read_t read_func,
busio_genericdevice_write_t write_func,
busio_genericdevice_readreg_t readreg_func = nullptr,
busio_genericdevice_writereg_t writereg_func = nullptr);
bool begin(void);
void end(void);
bool read(uint8_t *buffer, size_t len);
bool write(const uint8_t *buffer, size_t len);
bool readRegister(uint8_t *addr_buf, uint8_t addrsiz, uint8_t *buf,
uint16_t bufsiz);
bool writeRegister(uint8_t *addr_buf, uint8_t addrsiz, const uint8_t *buf,
uint16_t bufsiz);
protected:
/*! @brief Function pointer for reading raw data from the device */
busio_genericdevice_read_t _read_func;
/*! @brief Function pointer for writing raw data to the device */
busio_genericdevice_write_t _write_func;
/*! @brief Function pointer for reading a 'register' from the device */
busio_genericdevice_readreg_t _readreg_func;
/*! @brief Function pointer for writing a 'register' to the device */
busio_genericdevice_writereg_t _writereg_func;
bool _begun; ///< whether we have initialized yet (in case the function needs
///< to do something)
private:
void *_obj; ///< Pointer to object instance
};
#endif // ADAFRUIT_GENERICDEVICE_H

View file

@ -1,6 +1,7 @@
#include "Adafruit_I2CDevice.h" #include <Adafruit_I2CDevice.h>
#include <Arduino.h>
// #define DEBUG_SERIAL Serial //#define DEBUG_SERIAL Serial
/*! /*!
* @brief Create an I2C device at a given address * @brief Create an I2C device at a given address
@ -13,8 +14,6 @@ Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire) {
_begun = false; _begun = false;
#ifdef ARDUINO_ARCH_SAMD #ifdef ARDUINO_ARCH_SAMD
_maxBufferSize = 250; // as defined in Wire.h's RingBuffer _maxBufferSize = 250; // as defined in Wire.h's RingBuffer
#elif defined(ESP32)
_maxBufferSize = I2C_BUFFER_LENGTH;
#else #else
_maxBufferSize = 32; _maxBufferSize = 32;
#endif #endif
@ -23,8 +22,8 @@ Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire) {
/*! /*!
* @brief Initializes and does basic address detection * @brief Initializes and does basic address detection
* @param addr_detect Whether we should attempt to detect the I2C address * @param addr_detect Whether we should attempt to detect the I2C address
* with a scan. 99% of sensors/devices don't mind, but once in a while they * with a scan. 99% of sensors/devices don't mind but once in a while, they spaz
* don't respond well to a scan! * on a scan!
* @return True if I2C initialized and a device with the addr found * @return True if I2C initialized and a device with the addr found
*/ */
bool Adafruit_I2CDevice::begin(bool addr_detect) { bool Adafruit_I2CDevice::begin(bool addr_detect) {
@ -37,23 +36,6 @@ bool Adafruit_I2CDevice::begin(bool addr_detect) {
return true; return true;
} }
/*!
* @brief De-initialize device, turn off the Wire interface
*/
void Adafruit_I2CDevice::end(void) {
// Not all port implement Wire::end(), such as
// - ESP8266
// - AVR core without WIRE_HAS_END
// - ESP32: end() is implemented since 2.0.1 which is latest at the moment.
// Temporarily disable for now to give time for user to update.
#if !(defined(ESP8266) || \
(defined(ARDUINO_ARCH_AVR) && !defined(WIRE_HAS_END)) || \
defined(ARDUINO_ARCH_ESP32))
_wire->end();
_begun = false;
#endif
}
/*! /*!
* @brief Scans I2C for the address - note will give a false-positive * @brief Scans I2C for the address - note will give a false-positive
* if there's no pullups on I2C * if there's no pullups on I2C
@ -67,22 +49,9 @@ bool Adafruit_I2CDevice::detected(void) {
// A basic scanner, see if it ACK's // A basic scanner, see if it ACK's
_wire->beginTransmission(_addr); _wire->beginTransmission(_addr);
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.print(F("Address 0x"));
DEBUG_SERIAL.print(_addr, HEX);
#endif
#ifdef ARDUINO_ARCH_MBED
_wire->write(0); // forces a write request instead of a read
#endif
if (_wire->endTransmission() == 0) { if (_wire->endTransmission() == 0) {
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println(F(" Detected"));
#endif
return true; return true;
} }
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println(F(" Not detected"));
#endif
return false; return false;
} }
@ -115,7 +84,7 @@ bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,
_wire->beginTransmission(_addr); _wire->beginTransmission(_addr);
// Write the prefix data (usually an address) // Write the prefix data (usually an address)
if ((prefix_len != 0) && (prefix_buffer != nullptr)) { if ((prefix_len != 0) && (prefix_buffer != NULL)) {
if (_wire->write(prefix_buffer, prefix_len) != prefix_len) { if (_wire->write(prefix_buffer, prefix_len) != prefix_len) {
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
DEBUG_SERIAL.println(F("\tI2CDevice failed to write")); DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
@ -137,7 +106,7 @@ bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,
DEBUG_SERIAL.print(F("\tI2CWRITE @ 0x")); DEBUG_SERIAL.print(F("\tI2CWRITE @ 0x"));
DEBUG_SERIAL.print(_addr, HEX); DEBUG_SERIAL.print(_addr, HEX);
DEBUG_SERIAL.print(F(" :: ")); DEBUG_SERIAL.print(F(" :: "));
if ((prefix_len != 0) && (prefix_buffer != nullptr)) { if ((prefix_len != 0) && (prefix_buffer != NULL)) {
for (uint16_t i = 0; i < prefix_len; i++) { for (uint16_t i = 0; i < prefix_len; i++) {
DEBUG_SERIAL.print(F("0x")); DEBUG_SERIAL.print(F("0x"));
DEBUG_SERIAL.print(prefix_buffer[i], HEX); DEBUG_SERIAL.print(prefix_buffer[i], HEX);
@ -152,21 +121,21 @@ bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,
DEBUG_SERIAL.println(); DEBUG_SERIAL.println();
} }
} }
DEBUG_SERIAL.println();
#endif
if (stop) { #ifdef DEBUG_SERIAL
DEBUG_SERIAL.print("\tSTOP"); // DEBUG_SERIAL.print("Stop: "); DEBUG_SERIAL.println(stop);
}
#endif #endif
if (_wire->endTransmission(stop) == 0) { if (_wire->endTransmission(stop) == 0) {
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
DEBUG_SERIAL.println();
// DEBUG_SERIAL.println("Sent!"); // DEBUG_SERIAL.println("Sent!");
#endif #endif
return true; return true;
} else { } else {
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("\tFailed to send!"); DEBUG_SERIAL.println("Failed to send!");
#endif #endif
return false; return false;
} }
@ -181,27 +150,17 @@ bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,
* @return True if read was successful, otherwise false. * @return True if read was successful, otherwise false.
*/ */
bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) { bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) {
size_t pos = 0; if (len > maxBufferSize()) {
while (pos < len) { // currently not guaranteed to work if more than 32 bytes!
size_t read_len = // we will need to find out if some platforms have larger
((len - pos) > maxBufferSize()) ? maxBufferSize() : (len - pos); // I2C buffer sizes :/
bool read_stop = (pos < (len - read_len)) ? false : stop; #ifdef DEBUG_SERIAL
if (!_read(buffer + pos, read_len, read_stop)) DEBUG_SERIAL.println(F("\tI2CDevice could not read such a large buffer"));
return false;
pos += read_len;
}
return true;
}
bool Adafruit_I2CDevice::_read(uint8_t *buffer, size_t len, bool stop) {
#if defined(TinyWireM_h)
size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len);
#elif defined(ARDUINO_ARCH_MEGAAVR)
size_t recv = _wire->requestFrom(_addr, len, stop);
#else
size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len, (uint8_t)stop);
#endif #endif
return false;
}
size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len, (uint8_t)stop);
if (recv != len) { if (recv != len) {
// Not enough data available to fulfill our obligation! // Not enough data available to fulfill our obligation!
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
@ -268,53 +227,10 @@ uint8_t Adafruit_I2CDevice::address(void) { return _addr; }
* Not necessarily that the speed was achieved! * Not necessarily that the speed was achieved!
*/ */
bool Adafruit_I2CDevice::setSpeed(uint32_t desiredclk) { bool Adafruit_I2CDevice::setSpeed(uint32_t desiredclk) {
#if defined(__AVR_ATmega328__) || \ #if (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER)
defined(__AVR_ATmega328P__) // fix arduino core set clock
// calculate TWBR correctly
if ((F_CPU / 18) < desiredclk) {
#ifdef DEBUG_SERIAL
Serial.println(F("I2C.setSpeed too high."));
#endif
return false;
}
uint32_t atwbr = ((F_CPU / desiredclk) - 16) / 2;
if (atwbr > 16320) {
#ifdef DEBUG_SERIAL
Serial.println(F("I2C.setSpeed too low."));
#endif
return false;
}
if (atwbr <= 255) {
atwbr /= 1;
TWSR = 0x0;
} else if (atwbr <= 1020) {
atwbr /= 4;
TWSR = 0x1;
} else if (atwbr <= 4080) {
atwbr /= 16;
TWSR = 0x2;
} else { // if (atwbr <= 16320)
atwbr /= 64;
TWSR = 0x3;
}
TWBR = atwbr;
#ifdef DEBUG_SERIAL
Serial.print(F("TWSR prescaler = "));
Serial.println(pow(4, TWSR));
Serial.print(F("TWBR = "));
Serial.println(atwbr);
#endif
return true;
#elif (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER) && \
!defined(TinyWireM_h)
_wire->setClock(desiredclk); _wire->setClock(desiredclk);
return true; return true;
#else #else
(void)desiredclk;
return false; return false;
#endif #endif
} }

View file

@ -1,21 +1,19 @@
#include <Wire.h>
#ifndef Adafruit_I2CDevice_h #ifndef Adafruit_I2CDevice_h
#define Adafruit_I2CDevice_h #define Adafruit_I2CDevice_h
#include <Arduino.h>
#include <Wire.h>
///< The class which defines how we will talk to this device over I2C ///< The class which defines how we will talk to this device over I2C
class Adafruit_I2CDevice { class Adafruit_I2CDevice {
public: public:
Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire = &Wire); Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire = &Wire);
uint8_t address(void); uint8_t address(void);
bool begin(bool addr_detect = true); bool begin(bool addr_detect = true);
void end(void);
bool detected(void); bool detected(void);
bool read(uint8_t *buffer, size_t len, bool stop = true); bool read(uint8_t *buffer, size_t len, bool stop = true);
bool write(const uint8_t *buffer, size_t len, bool stop = true, bool write(const uint8_t *buffer, size_t len, bool stop = true,
const uint8_t *prefix_buffer = nullptr, size_t prefix_len = 0); const uint8_t *prefix_buffer = NULL, size_t prefix_len = 0);
bool write_then_read(const uint8_t *write_buffer, size_t write_len, bool write_then_read(const uint8_t *write_buffer, size_t write_len,
uint8_t *read_buffer, size_t read_len, uint8_t *read_buffer, size_t read_len,
bool stop = false); bool stop = false);
@ -30,7 +28,6 @@ private:
TwoWire *_wire; TwoWire *_wire;
bool _begun; bool _begun;
size_t _maxBufferSize; size_t _maxBufferSize;
bool _read(uint8_t *buffer, size_t len, bool stop);
}; };
#endif // Adafruit_I2CDevice_h #endif // Adafruit_I2CDevice_h

View file

@ -1,9 +1,7 @@
#include "Adafruit_BusIO_Register.h"
#ifndef _ADAFRUIT_I2C_REGISTER_H_ #ifndef _ADAFRUIT_I2C_REGISTER_H_
#define _ADAFRUIT_I2C_REGISTER_H_ #define _ADAFRUIT_I2C_REGISTER_H_
#include <Adafruit_BusIO_Register.h>
#include <Arduino.h>
typedef Adafruit_BusIO_Register Adafruit_I2CRegister; typedef Adafruit_BusIO_Register Adafruit_I2CRegister;
typedef Adafruit_BusIO_RegisterBits Adafruit_I2CRegisterBits; typedef Adafruit_BusIO_RegisterBits Adafruit_I2CRegisterBits;

View file

@ -1,9 +1,10 @@
#include "Adafruit_SPIDevice.h" #include <Adafruit_SPIDevice.h>
#include <Arduino.h>
// #define DEBUG_SERIAL Serial //#define DEBUG_SERIAL Serial
/*! /*!
* @brief Create an SPI device with the given CS pin and settings * @brief Create an SPI device with the given CS pin and settins
* @param cspin The arduino pin number to use for chip select * @param cspin The arduino pin number to use for chip select
* @param freq The SPI clock frequency to use, defaults to 1MHz * @param freq The SPI clock frequency to use, defaults to 1MHz
* @param dataOrder The SPI data order to use for bits within each byte, * @param dataOrder The SPI data order to use for bits within each byte,
@ -12,9 +13,8 @@
* @param theSPI The SPI bus to use, defaults to &theSPI * @param theSPI The SPI bus to use, defaults to &theSPI
*/ */
Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, uint32_t freq, Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, uint32_t freq,
BusIOBitOrder dataOrder, BitOrder dataOrder, uint8_t dataMode,
uint8_t dataMode, SPIClass *theSPI) { SPIClass *theSPI) {
#ifdef BUSIO_HAS_HW_SPI
_cs = cspin; _cs = cspin;
_sck = _mosi = _miso = -1; _sck = _mosi = _miso = -1;
_spi = theSPI; _spi = theSPI;
@ -23,18 +23,10 @@ Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, uint32_t freq,
_freq = freq; _freq = freq;
_dataOrder = dataOrder; _dataOrder = dataOrder;
_dataMode = dataMode; _dataMode = dataMode;
#else
// unused, but needed to suppress compiler warns
(void)cspin;
(void)freq;
(void)dataOrder;
(void)dataMode;
(void)theSPI;
#endif
} }
/*! /*!
* @brief Create an SPI device with the given CS pin and settings * @brief Create an SPI device with the given CS pin and settins
* @param cspin The arduino pin number to use for chip select * @param cspin The arduino pin number to use for chip select
* @param sckpin The arduino pin number to use for SCK * @param sckpin The arduino pin number to use for SCK
* @param misopin The arduino pin number to use for MISO, set to -1 if not * @param misopin The arduino pin number to use for MISO, set to -1 if not
@ -48,7 +40,7 @@ Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, uint32_t freq,
*/ */
Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, int8_t sckpin, Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, int8_t sckpin,
int8_t misopin, int8_t mosipin, int8_t misopin, int8_t mosipin,
uint32_t freq, BusIOBitOrder dataOrder, uint32_t freq, BitOrder dataOrder,
uint8_t dataMode) { uint8_t dataMode) {
_cs = cspin; _cs = cspin;
_sck = sckpin; _sck = sckpin;
@ -74,14 +66,18 @@ Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, int8_t sckpin,
_dataOrder = dataOrder; _dataOrder = dataOrder;
_dataMode = dataMode; _dataMode = dataMode;
_begun = false; _begun = false;
_spiSetting = new SPISettings(freq, dataOrder, dataMode);
_spi = NULL;
} }
/*! /*!
* @brief Release memory allocated in constructors * @brief Release memory allocated in constructors
*/ */
Adafruit_SPIDevice::~Adafruit_SPIDevice() { Adafruit_SPIDevice::~Adafruit_SPIDevice() {
if (_spiSetting) if (_spiSetting) {
delete _spiSetting; delete _spiSetting;
_spiSetting = nullptr;
}
} }
/*! /*!
@ -90,15 +86,11 @@ Adafruit_SPIDevice::~Adafruit_SPIDevice() {
* init * init
*/ */
bool Adafruit_SPIDevice::begin(void) { bool Adafruit_SPIDevice::begin(void) {
if (_cs != -1) { pinMode(_cs, OUTPUT);
pinMode(_cs, OUTPUT); digitalWrite(_cs, HIGH);
digitalWrite(_cs, HIGH);
}
if (_spi) { // hardware SPI if (_spi) { // hardware SPI
#ifdef BUSIO_HAS_HW_SPI
_spi->begin(); _spi->begin();
#endif
} else { } else {
pinMode(_sck, OUTPUT); pinMode(_sck, OUTPUT);
@ -123,19 +115,16 @@ bool Adafruit_SPIDevice::begin(void) {
} }
/*! /*!
* @brief Transfer (send/receive) a buffer over hard/soft SPI, without * @brief Transfer (send/receive) one byte over hard/soft SPI
* transaction management
* @param buffer The buffer to send and receive at the same time * @param buffer The buffer to send and receive at the same time
* @param len The number of bytes to transfer * @param len The number of bytes to transfer
*/ */
void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) { void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
//
// HARDWARE SPI
//
if (_spi) { if (_spi) {
#ifdef BUSIO_HAS_HW_SPI // hardware SPI is easy
#if defined(SPARK) #if defined(SPARK)
_spi->transfer(buffer, buffer, len, nullptr); _spi->transfer(buffer, buffer, len, NULL);
#elif defined(STM32) #elif defined(STM32)
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
_spi->transfer(buffer[i]); _spi->transfer(buffer[i]);
@ -144,12 +133,8 @@ void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
_spi->transfer(buffer, len); _spi->transfer(buffer, len);
#endif #endif
return; return;
#endif
} }
//
// SOFTWARE SPI
//
uint8_t startbit; uint8_t startbit;
if (_dataOrder == SPI_BITORDER_LSBFIRST) { if (_dataOrder == SPI_BITORDER_LSBFIRST) {
startbit = 0x1; startbit = 0x1;
@ -160,7 +145,9 @@ void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
bool towrite, lastmosi = !(buffer[0] & startbit); bool towrite, lastmosi = !(buffer[0] & startbit);
uint8_t bitdelay_us = (1000000 / _freq) / 2; uint8_t bitdelay_us = (1000000 / _freq) / 2;
// for softSPI we'll do it by hand
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
// software SPI
uint8_t reply = 0; uint8_t reply = 0;
uint8_t send = buffer[i]; uint8_t send = buffer[i];
@ -183,9 +170,9 @@ void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
if ((_mosi != -1) && (lastmosi != towrite)) { if ((_mosi != -1) && (lastmosi != towrite)) {
#ifdef BUSIO_USE_FAST_PINIO #ifdef BUSIO_USE_FAST_PINIO
if (towrite) if (towrite)
*mosiPort = *mosiPort | mosiPinMask; *mosiPort |= mosiPinMask;
else else
*mosiPort = *mosiPort & ~mosiPinMask; *mosiPort &= ~mosiPinMask;
#else #else
digitalWrite(_mosi, towrite); digitalWrite(_mosi, towrite);
#endif #endif
@ -193,7 +180,7 @@ void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
} }
#ifdef BUSIO_USE_FAST_PINIO #ifdef BUSIO_USE_FAST_PINIO
*clkPort = *clkPort | clkPinMask; // Clock high *clkPort |= clkPinMask; // Clock high
#else #else
digitalWrite(_sck, HIGH); digitalWrite(_sck, HIGH);
#endif #endif
@ -213,14 +200,14 @@ void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
} }
#ifdef BUSIO_USE_FAST_PINIO #ifdef BUSIO_USE_FAST_PINIO
*clkPort = *clkPort & ~clkPinMask; // Clock low *clkPort &= ~clkPinMask; // Clock low
#else #else
digitalWrite(_sck, LOW); digitalWrite(_sck, LOW);
#endif #endif
} else { // if (_dataMode == SPI_MODE1 || _dataMode == SPI_MODE3) } else { // if (_dataMode == SPI_MODE1 || _dataMode == SPI_MODE3)
#ifdef BUSIO_USE_FAST_PINIO #ifdef BUSIO_USE_FAST_PINIO
*clkPort = *clkPort | clkPinMask; // Clock high *clkPort |= clkPinMask; // Clock high
#else #else
digitalWrite(_sck, HIGH); digitalWrite(_sck, HIGH);
#endif #endif
@ -232,16 +219,16 @@ void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
if (_mosi != -1) { if (_mosi != -1) {
#ifdef BUSIO_USE_FAST_PINIO #ifdef BUSIO_USE_FAST_PINIO
if (send & b) if (send & b)
*mosiPort = *mosiPort | mosiPinMask; *mosiPort |= mosiPinMask;
else else
*mosiPort = *mosiPort & ~mosiPinMask; *mosiPort &= ~mosiPinMask;
#else #else
digitalWrite(_mosi, send & b); digitalWrite(_mosi, send & b);
#endif #endif
} }
#ifdef BUSIO_USE_FAST_PINIO #ifdef BUSIO_USE_FAST_PINIO
*clkPort = *clkPort & ~clkPinMask; // Clock low *clkPort &= ~clkPinMask; // Clock low
#else #else
digitalWrite(_sck, LOW); digitalWrite(_sck, LOW);
#endif #endif
@ -265,8 +252,7 @@ void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
} }
/*! /*!
* @brief Transfer (send/receive) one byte over hard/soft SPI, without * @brief Transfer (send/receive) one byte over hard/soft SPI
* transaction management
* @param send The byte to send * @param send The byte to send
* @return The byte received while transmitting * @return The byte received while transmitting
*/ */
@ -282,9 +268,7 @@ uint8_t Adafruit_SPIDevice::transfer(uint8_t send) {
*/ */
void Adafruit_SPIDevice::beginTransaction(void) { void Adafruit_SPIDevice::beginTransaction(void) {
if (_spi) { if (_spi) {
#ifdef BUSIO_HAS_HW_SPI
_spi->beginTransaction(*_spiSetting); _spi->beginTransaction(*_spiSetting);
#endif
} }
} }
@ -293,45 +277,12 @@ void Adafruit_SPIDevice::beginTransaction(void) {
*/ */
void Adafruit_SPIDevice::endTransaction(void) { void Adafruit_SPIDevice::endTransaction(void) {
if (_spi) { if (_spi) {
#ifdef BUSIO_HAS_HW_SPI
_spi->endTransaction(); _spi->endTransaction();
#endif
} }
} }
/*! /*!
* @brief Assert/Deassert the CS pin if it is defined * @brief Write a buffer or two to the SPI device.
* @param value The state the CS is set to
*/
void Adafruit_SPIDevice::setChipSelect(int value) {
if (_cs != -1) {
digitalWrite(_cs, value);
}
}
/*!
* @brief Write a buffer or two to the SPI device, with transaction
* management.
* @brief Manually begin a transaction (calls beginTransaction if hardware
* SPI) with asserting the CS pin
*/
void Adafruit_SPIDevice::beginTransactionWithAssertingCS() {
beginTransaction();
setChipSelect(LOW);
}
/*!
* @brief Manually end a transaction (calls endTransaction if hardware SPI)
* with deasserting the CS pin
*/
void Adafruit_SPIDevice::endTransactionWithDeassertingCS() {
setChipSelect(HIGH);
endTransaction();
}
/*!
* @brief Write a buffer or two to the SPI device, with transaction
* management.
* @param buffer Pointer to buffer of data to write * @param buffer Pointer to buffer of data to write
* @param len Number of bytes from buffer to write * @param len Number of bytes from buffer to write
* @param prefix_buffer Pointer to optional array of data to write before * @param prefix_buffer Pointer to optional array of data to write before
@ -340,35 +291,29 @@ void Adafruit_SPIDevice::endTransactionWithDeassertingCS() {
* @return Always returns true because there's no way to test success of SPI * @return Always returns true because there's no way to test success of SPI
* writes * writes
*/ */
bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len, bool Adafruit_SPIDevice::write(uint8_t *buffer, size_t len,
const uint8_t *prefix_buffer, uint8_t *prefix_buffer, size_t prefix_len) {
size_t prefix_len) {
beginTransactionWithAssertingCS();
// do the writing
#if defined(ARDUINO_ARCH_ESP32)
if (_spi) { if (_spi) {
if (prefix_len > 0) { _spi->beginTransaction(*_spiSetting);
_spi->transferBytes((uint8_t *)prefix_buffer, nullptr, prefix_len); }
}
if (len > 0) { digitalWrite(_cs, LOW);
_spi->transferBytes((uint8_t *)buffer, nullptr, len); // do the writing
} for (size_t i = 0; i < prefix_len; i++) {
} else transfer(prefix_buffer[i]);
#endif }
{ for (size_t i = 0; i < len; i++) {
for (size_t i = 0; i < prefix_len; i++) { transfer(buffer[i]);
transfer(prefix_buffer[i]); }
} digitalWrite(_cs, HIGH);
for (size_t i = 0; i < len; i++) {
transfer(buffer[i]); if (_spi) {
} _spi->endTransaction();
} }
endTransactionWithDeassertingCS();
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
DEBUG_SERIAL.print(F("\tSPIDevice Wrote: ")); DEBUG_SERIAL.print(F("\tSPIDevice Wrote: "));
if ((prefix_len != 0) && (prefix_buffer != nullptr)) { if ((prefix_len != 0) && (prefix_buffer != NULL)) {
for (uint16_t i = 0; i < prefix_len; i++) { for (uint16_t i = 0; i < prefix_len; i++) {
DEBUG_SERIAL.print(F("0x")); DEBUG_SERIAL.print(F("0x"));
DEBUG_SERIAL.print(prefix_buffer[i], HEX); DEBUG_SERIAL.print(prefix_buffer[i], HEX);
@ -390,8 +335,7 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
} }
/*! /*!
* @brief Read from SPI into a buffer from the SPI device, with transaction * @brief Read from SPI into a buffer from the SPI device.
* management.
* @param buffer Pointer to buffer of data to read into * @param buffer Pointer to buffer of data to read into
* @param len Number of bytes from buffer to read. * @param len Number of bytes from buffer to read.
* @param sendvalue The 8-bits of data to write when doing the data read, * @param sendvalue The 8-bits of data to write when doing the data read,
@ -401,10 +345,16 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
*/ */
bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) { bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
memset(buffer, sendvalue, len); // clear out existing buffer memset(buffer, sendvalue, len); // clear out existing buffer
if (_spi) {
beginTransactionWithAssertingCS(); _spi->beginTransaction(*_spiSetting);
}
digitalWrite(_cs, LOW);
transfer(buffer, len); transfer(buffer, len);
endTransactionWithDeassertingCS(); digitalWrite(_cs, HIGH);
if (_spi) {
_spi->endTransaction();
}
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
DEBUG_SERIAL.print(F("\tSPIDevice Read: ")); DEBUG_SERIAL.print(F("\tSPIDevice Read: "));
@ -423,9 +373,9 @@ bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
} }
/*! /*!
* @brief Write some data, then read some data from SPI into another buffer, * @brief Write some data, then read some data from SPI into another buffer.
* with transaction management. The buffers can point to same/overlapping * The buffers can point to same/overlapping locations. This does not
* locations. This does not transmit-receive at the same time! * transmit-receive at the same time!
* @param write_buffer Pointer to buffer of data to write from * @param write_buffer Pointer to buffer of data to write from
* @param write_len Number of bytes from buffer to write. * @param write_len Number of bytes from buffer to write.
* @param read_buffer Pointer to buffer of data to read into. * @param read_buffer Pointer to buffer of data to read into.
@ -435,22 +385,17 @@ bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
* @return Always returns true because there's no way to test success of SPI * @return Always returns true because there's no way to test success of SPI
* writes * writes
*/ */
bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer, bool Adafruit_SPIDevice::write_then_read(uint8_t *write_buffer,
size_t write_len, uint8_t *read_buffer, size_t write_len, uint8_t *read_buffer,
size_t read_len, uint8_t sendvalue) { size_t read_len, uint8_t sendvalue) {
beginTransactionWithAssertingCS();
// do the writing
#if defined(ARDUINO_ARCH_ESP32)
if (_spi) { if (_spi) {
if (write_len > 0) { _spi->beginTransaction(*_spiSetting);
_spi->transferBytes((uint8_t *)write_buffer, nullptr, write_len); }
}
} else digitalWrite(_cs, LOW);
#endif // do the writing
{ for (size_t i = 0; i < write_len; i++) {
for (size_t i = 0; i < write_len; i++) { transfer(write_buffer[i]);
transfer(write_buffer[i]);
}
} }
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
@ -484,25 +429,11 @@ bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
DEBUG_SERIAL.println(); DEBUG_SERIAL.println();
#endif #endif
endTransactionWithDeassertingCS(); digitalWrite(_cs, HIGH);
return true; if (_spi) {
} _spi->endTransaction();
}
/*!
* @brief Write some data and read some data at the same time from SPI
* into the same buffer, with transaction management. This is basicaly a wrapper
* for transfer() with CS-pin and transaction management. This /does/
* transmit-receive at the same time!
* @param buffer Pointer to buffer of data to write/read to/from
* @param len Number of bytes from buffer to write/read.
* @return Always returns true because there's no way to test success of SPI
* writes
*/
bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) {
beginTransactionWithAssertingCS();
transfer(buffer, len);
endTransactionWithDeassertingCS();
return true; return true;
} }

View file

@ -1,19 +1,8 @@
#include <SPI.h>
#ifndef Adafruit_SPIDevice_h #ifndef Adafruit_SPIDevice_h
#define Adafruit_SPIDevice_h #define Adafruit_SPIDevice_h
#include <Arduino.h>
#if !defined(SPI_INTERFACES_COUNT) || \
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
// HW SPI available
#include <SPI.h>
#define BUSIO_HAS_HW_SPI
#else
// SW SPI ONLY
enum { SPI_MODE0, SPI_MODE1, SPI_MODE2, _SPI_MODE4 };
typedef uint8_t SPIClass;
#endif
// some modern SPI definitions don't have BitOrder enum // some modern SPI definitions don't have BitOrder enum
#if (defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)) || \ #if (defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)) || \
defined(ESP8266) || defined(TEENSYDUINO) || defined(SPARK) || \ defined(ESP8266) || defined(TEENSYDUINO) || defined(SPARK) || \
@ -22,51 +11,28 @@ typedef uint8_t SPIClass;
defined(ARDUINO_AVR_ATmega4808) || defined(ARDUINO_AVR_ATmega3209) || \ defined(ARDUINO_AVR_ATmega4808) || defined(ARDUINO_AVR_ATmega3209) || \
defined(ARDUINO_AVR_ATmega3208) || defined(ARDUINO_AVR_ATmega1609) || \ defined(ARDUINO_AVR_ATmega3208) || defined(ARDUINO_AVR_ATmega1609) || \
defined(ARDUINO_AVR_ATmega1608) || defined(ARDUINO_AVR_ATmega809) || \ defined(ARDUINO_AVR_ATmega1608) || defined(ARDUINO_AVR_ATmega809) || \
defined(ARDUINO_AVR_ATmega808) || defined(ARDUINO_ARCH_ARC32) || \ defined(ARDUINO_AVR_ATmega808)
defined(ARDUINO_ARCH_XMC)
typedef enum _BitOrder { typedef enum _BitOrder {
SPI_BITORDER_MSBFIRST = MSBFIRST, SPI_BITORDER_MSBFIRST = MSBFIRST,
SPI_BITORDER_LSBFIRST = LSBFIRST, SPI_BITORDER_LSBFIRST = LSBFIRST,
} BusIOBitOrder; } BitOrder;
#elif defined(ESP32) || defined(__ASR6501__) || defined(__ASR6502__) #elif defined(ESP32) || defined(__ASR6501__)
// some modern SPI definitions don't have BitOrder enum and have different SPI // some modern SPI definitions don't have BitOrder enum and have different SPI
// mode defines // mode defines
typedef enum _BitOrder { typedef enum _BitOrder {
SPI_BITORDER_MSBFIRST = SPI_MSBFIRST, SPI_BITORDER_MSBFIRST = SPI_MSBFIRST,
SPI_BITORDER_LSBFIRST = SPI_LSBFIRST, SPI_BITORDER_LSBFIRST = SPI_LSBFIRST,
} BusIOBitOrder; } BitOrder;
#else #else
// Some platforms have a BitOrder enum but its named MSBFIRST/LSBFIRST // Some platforms have a BitOrder enum but its named MSBFIRST/LSBFIRST
#define SPI_BITORDER_MSBFIRST MSBFIRST #define SPI_BITORDER_MSBFIRST MSBFIRST
#define SPI_BITORDER_LSBFIRST LSBFIRST #define SPI_BITORDER_LSBFIRST LSBFIRST
typedef BitOrder BusIOBitOrder;
#endif #endif
#if defined(__IMXRT1062__) // Teensy 4.x #if defined(__AVR__) || defined(TEENSYDUINO)
// *Warning* I disabled the usage of FAST_PINIO as the set/clear operations
// used in the cpp file are not atomic and can effect multiple IO pins
// and if an interrupt happens in between the time the code reads the register
// and writes out the updated value, that changes one or more other IO pins
// on that same IO port, those change will be clobbered when the updated
// values are written back. A fast version can be implemented that uses the
// ports set and clear registers which are atomic.
// typedef volatile uint32_t BusIO_PortReg;
// typedef uint32_t BusIO_PortMask;
// #define BUSIO_USE_FAST_PINIO
#elif defined(__MBED__) || defined(__ZEPHYR__)
// Boards based on RTOS cores like mbed or Zephyr are not going to expose the
// low level registers needed for fast pin manipulation
#undef BUSIO_USE_FAST_PINIO
#elif defined(ARDUINO_ARCH_XMC)
#undef BUSIO_USE_FAST_PINIO
#elif defined(__AVR__) || defined(TEENSYDUINO)
typedef volatile uint8_t BusIO_PortReg; typedef volatile uint8_t BusIO_PortReg;
typedef uint8_t BusIO_PortMask; typedef uint8_t BusIO_PortMask;
#define BUSIO_USE_FAST_PINIO #define BUSIO_USE_FAST_PINIO
@ -78,12 +44,10 @@ typedef uint32_t BusIO_PortMask;
#define BUSIO_USE_FAST_PINIO #define BUSIO_USE_FAST_PINIO
#elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) && \ #elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) && \
!defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_SILABS) && \ !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_RP2040)
!defined(ARDUINO_UNOR4_MINIMA) && !defined(ARDUINO_UNOR4_WIFI) && \
!defined(PORTDUINO)
typedef volatile uint32_t BusIO_PortReg; typedef volatile uint32_t BusIO_PortReg;
typedef uint32_t BusIO_PortMask; typedef uint32_t BusIO_PortMask;
#if !defined(__ASR6501__) && !defined(__ASR6502__) #if not defined(__ASR6501__)
#define BUSIO_USE_FAST_PINIO #define BUSIO_USE_FAST_PINIO
#endif #endif
@ -94,49 +58,35 @@ typedef uint32_t BusIO_PortMask;
/**! The class which defines how we will talk to this device over SPI **/ /**! The class which defines how we will talk to this device over SPI **/
class Adafruit_SPIDevice { class Adafruit_SPIDevice {
public: public:
#ifdef BUSIO_HAS_HW_SPI
Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000, Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000,
BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST, BitOrder dataOrder = SPI_BITORDER_MSBFIRST,
uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = &SPI); uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = &SPI);
#else
Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000,
BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = nullptr);
#endif
Adafruit_SPIDevice(int8_t cspin, int8_t sck, int8_t miso, int8_t mosi, Adafruit_SPIDevice(int8_t cspin, int8_t sck, int8_t miso, int8_t mosi,
uint32_t freq = 1000000, uint32_t freq = 1000000,
BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST, BitOrder dataOrder = SPI_BITORDER_MSBFIRST,
uint8_t dataMode = SPI_MODE0); uint8_t dataMode = SPI_MODE0);
~Adafruit_SPIDevice(); ~Adafruit_SPIDevice();
bool begin(void); bool begin(void);
bool read(uint8_t *buffer, size_t len, uint8_t sendvalue = 0xFF); bool read(uint8_t *buffer, size_t len, uint8_t sendvalue = 0xFF);
bool write(const uint8_t *buffer, size_t len, bool write(uint8_t *buffer, size_t len, uint8_t *prefix_buffer = NULL,
const uint8_t *prefix_buffer = nullptr, size_t prefix_len = 0); size_t prefix_len = 0);
bool write_then_read(const uint8_t *write_buffer, size_t write_len, bool write_then_read(uint8_t *write_buffer, size_t write_len,
uint8_t *read_buffer, size_t read_len, uint8_t *read_buffer, size_t read_len,
uint8_t sendvalue = 0xFF); uint8_t sendvalue = 0xFF);
bool write_and_read(uint8_t *buffer, size_t len);
uint8_t transfer(uint8_t send); uint8_t transfer(uint8_t send);
void transfer(uint8_t *buffer, size_t len); void transfer(uint8_t *buffer, size_t len);
void beginTransaction(void); void beginTransaction(void);
void endTransaction(void); void endTransaction(void);
void beginTransactionWithAssertingCS();
void endTransactionWithDeassertingCS();
private: private:
#ifdef BUSIO_HAS_HW_SPI SPIClass *_spi;
SPIClass *_spi = nullptr; SPISettings *_spiSetting;
SPISettings *_spiSetting = nullptr;
#else
uint8_t *_spi = nullptr;
uint8_t *_spiSetting = nullptr;
#endif
uint32_t _freq; uint32_t _freq;
BusIOBitOrder _dataOrder; BitOrder _dataOrder;
uint8_t _dataMode; uint8_t _dataMode;
void setChipSelect(int value);
int8_t _cs, _sck, _mosi, _miso; int8_t _cs, _sck, _mosi, _miso;
#ifdef BUSIO_USE_FAST_PINIO #ifdef BUSIO_USE_FAST_PINIO

View file

@ -1,11 +0,0 @@
# Adafruit Bus IO Library
# https://github.com/adafruit/Adafruit_BusIO
# MIT License
cmake_minimum_required(VERSION 3.5)
idf_component_register(SRCS "Adafruit_I2CDevice.cpp" "Adafruit_BusIO_Register.cpp" "Adafruit_SPIDevice.cpp" "Adafruit_GenericDevice.cpp"
INCLUDE_DIRS "."
REQUIRES arduino-esp32)
project(Adafruit_BusIO)

View file

@ -1,7 +1,7 @@
# Adafruit Bus IO Library [![Build Status](https://github.com/adafruit/Adafruit_BusIO/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_BusIO/actions) # Adafruit Bus IO Library [![Build Status](https://github.com/adafruit/Adafruit_BusIO/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_BusIO/actions)
This is a helper library to abstract away I2C, SPI, and 'generic transport' (e.g. UART) transactions and registers This is a helper libary to abstract away I2C & SPI transactions and registers
Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!

View file

@ -1 +0,0 @@
COMPONENT_ADD_INCLUDEDIRS = .

View file

@ -1,219 +0,0 @@
/*
Advanced example of using bstracted transport for reading and writing
register data from a UART-based device such as a TMC2209
Written with help by Claude!
https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
chats are not shareable :(
*/
#include "Adafruit_BusIO_Register.h"
#include "Adafruit_GenericDevice.h"
// Debugging macros
#define DEBUG_SERIAL Serial
#ifdef DEBUG_SERIAL
#define DEBUG_PRINT(x) DEBUG_SERIAL.print(x)
#define DEBUG_PRINTLN(x) DEBUG_SERIAL.println(x)
#define DEBUG_PRINT_HEX(x) \
do { \
if (x < 0x10) \
DEBUG_SERIAL.print('0'); \
DEBUG_SERIAL.print(x, HEX); \
DEBUG_SERIAL.print(' '); \
} while (0)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINT_HEX(x)
#endif
#define TMC2209_IOIN 0x06
class TMC2209_UART {
private:
Stream *_uart_stream;
uint8_t _addr;
static bool uart_read(void *thiz, uint8_t *buffer, size_t len) {
TMC2209_UART *dev = (TMC2209_UART *)thiz;
uint16_t timeout = 100;
while (dev->_uart_stream->available() < len && timeout--) {
delay(1);
}
if (timeout == 0) {
DEBUG_PRINTLN("Read timeout!");
return false;
}
DEBUG_PRINT("Reading: ");
for (size_t i = 0; i < len; i++) {
buffer[i] = dev->_uart_stream->read();
DEBUG_PRINT_HEX(buffer[i]);
}
DEBUG_PRINTLN("");
return true;
}
static bool uart_write(void *thiz, const uint8_t *buffer, size_t len) {
TMC2209_UART *dev = (TMC2209_UART *)thiz;
DEBUG_PRINT("Writing: ");
for (size_t i = 0; i < len; i++) {
DEBUG_PRINT_HEX(buffer[i]);
}
DEBUG_PRINTLN("");
dev->_uart_stream->write(buffer, len);
return true;
}
static bool uart_readreg(void *thiz, uint8_t *addr_buf, uint8_t addrsiz,
uint8_t *data, uint16_t datalen) {
TMC2209_UART *dev = (TMC2209_UART *)thiz;
while (dev->_uart_stream->available())
dev->_uart_stream->read();
uint8_t packet[4] = {0x05, uint8_t(dev->_addr << 1), addr_buf[0], 0x00};
packet[3] = calcCRC(packet, 3);
if (!uart_write(thiz, packet, 4))
return false;
// Read back echo
uint8_t echo[4];
if (!uart_read(thiz, echo, 4))
return false;
// Verify echo
for (uint8_t i = 0; i < 4; i++) {
if (echo[i] != packet[i]) {
DEBUG_PRINTLN("Echo mismatch");
return false;
}
}
uint8_t response[8]; // sync + 0xFF + reg + 4 data bytes + CRC
if (!uart_read(thiz, response, 8))
return false;
// Verify response
if (response[0] != 0x05) {
DEBUG_PRINTLN("Invalid sync byte");
return false;
}
if (response[1] != 0xFF) {
DEBUG_PRINTLN("Invalid reply address");
return false;
}
if (response[2] != addr_buf[0]) {
DEBUG_PRINTLN("Register mismatch");
return false;
}
uint8_t crc = calcCRC(response, 7);
if (crc != response[7]) {
DEBUG_PRINTLN("CRC mismatch");
return false;
}
memcpy(data, &response[3], 4);
return true;
}
static bool uart_writereg(void *thiz, uint8_t *addr_buf, uint8_t addrsiz,
const uint8_t *data, uint16_t datalen) {
TMC2209_UART *dev = (TMC2209_UART *)thiz;
while (dev->_uart_stream->available())
dev->_uart_stream->read();
uint8_t packet[8] = {0x05,
uint8_t(dev->_addr << 1),
uint8_t(addr_buf[0] | 0x80),
data[0],
data[1],
data[2],
data[3],
0x00};
packet[7] = calcCRC(packet, 7);
if (!uart_write(thiz, packet, 8))
return false;
uint8_t echo[8];
if (!uart_read(thiz, echo, 8))
return false;
for (uint8_t i = 0; i < 8; i++) {
if (echo[i] != packet[i]) {
DEBUG_PRINTLN("Write echo mismatch");
return false;
}
}
return true;
}
static uint8_t calcCRC(uint8_t *data, uint8_t length) {
uint8_t crc = 0;
for (uint8_t i = 0; i < length; i++) {
uint8_t currentByte = data[i];
for (uint8_t j = 0; j < 8; j++) {
if ((crc >> 7) ^ (currentByte & 0x01)) {
crc = (crc << 1) ^ 0x07;
} else {
crc = crc << 1;
}
currentByte = currentByte >> 1;
}
}
return crc;
}
public:
TMC2209_UART(Stream *serial, uint8_t addr)
: _uart_stream(serial), _addr(addr) {}
Adafruit_GenericDevice *createDevice() {
return new Adafruit_GenericDevice(this, uart_read, uart_write, uart_readreg,
uart_writereg);
}
};
void setup() {
Serial.begin(115200);
while (!Serial)
;
delay(100);
Serial.println("TMC2209 Generic Device register read/write test!");
Serial1.begin(115200);
TMC2209_UART uart(&Serial1, 0);
Adafruit_GenericDevice *device = uart.createDevice();
device->begin();
// Create register object for IOIN
Adafruit_BusIO_Register ioin_reg(device,
TMC2209_IOIN, // device and register address
4, // width = 4 bytes
MSBFIRST, // byte order
1); // address width = 1 byte
Serial.print("IOIN = 0x");
Serial.println(ioin_reg.read(), HEX);
// Create RegisterBits for VERSION field (bits 31:24)
Adafruit_BusIO_RegisterBits version_bits(
&ioin_reg, 8, 24); // 8 bits wide, starting at bit 24
Serial.println("Reading VERSION...");
uint8_t version = version_bits.read();
Serial.print("VERSION = 0x");
Serial.println(version, HEX);
}
void loop() { delay(1000); }

View file

@ -1,98 +0,0 @@
/*
Abstracted transport for reading and writing data from a UART-based
device such as a TMC2209
Written with help by Claude!
https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
chats are not shareable :(
*/
#include "Adafruit_GenericDevice.h"
/**
* Basic UART device class that demonstrates using GenericDevice with a Stream
* interface. This example shows how to wrap a Stream (like HardwareSerial or
* SoftwareSerial) with read/write callbacks that can be used by BusIO's
* register functions.
*/
class UARTDevice {
public:
UARTDevice(Stream *serial) : _serial(serial) {}
// Static callback for writing data to UART
// Called by GenericDevice when data needs to be sent
static bool uart_write(void *thiz, const uint8_t *buffer, size_t len) {
UARTDevice *dev = (UARTDevice *)thiz;
dev->_serial->write(buffer, len);
return true;
}
// Static callback for reading data from UART
// Includes timeout and will return false if not enough data available
static bool uart_read(void *thiz, uint8_t *buffer, size_t len) {
UARTDevice *dev = (UARTDevice *)thiz;
uint16_t timeout = 100;
while (dev->_serial->available() < len && timeout--) {
delay(1);
}
if (timeout == 0) {
return false;
}
for (size_t i = 0; i < len; i++) {
buffer[i] = dev->_serial->read();
}
return true;
}
// Create a GenericDevice instance using our callbacks
Adafruit_GenericDevice *createDevice() {
return new Adafruit_GenericDevice(this, uart_read, uart_write);
}
private:
Stream *_serial; // Underlying Stream instance (HardwareSerial, etc)
};
void setup() {
Serial.begin(115200);
while (!Serial)
;
delay(100);
Serial.println("Generic Device test!");
// Initialize UART for device communication
Serial1.begin(115200);
// Create UART wrapper and BusIO device
UARTDevice uart(&Serial1);
Adafruit_GenericDevice *device = uart.createDevice();
device->begin();
// Test write/read cycle
uint8_t write_buf[4] = {0x5, 0x0, 0x0, 0x48};
uint8_t read_buf[8];
Serial.println("Writing data...");
if (!device->write(write_buf, 4)) {
Serial.println("Write failed!");
return;
}
Serial.println("Reading response...");
if (!device->read(read_buf, 8)) {
Serial.println("Read failed!");
return;
}
// Print response bytes
Serial.print("Got response: ");
for (int i = 0; i < 8; i++) {
Serial.print("0x");
Serial.print(read_buf[i], HEX);
Serial.print(" ");
}
Serial.println();
}
void loop() { delay(1000); }

View file

@ -3,20 +3,19 @@
Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(0x10); Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(0x10);
void setup() { void setup() {
while (!Serial) { while (!Serial) { delay(10); }
delay(10);
}
Serial.begin(115200); Serial.begin(115200);
Serial.println("I2C address detection test"); Serial.println("I2C address detection test");
if (!i2c_dev.begin()) { if (!i2c_dev.begin()) {
Serial.print("Did not find device at 0x"); Serial.print("Did not find device at 0x");
Serial.println(i2c_dev.address(), HEX); Serial.println(i2c_dev.address(), HEX);
while (1) while (1);
;
} }
Serial.print("Device found on address 0x"); Serial.print("Device found on address 0x");
Serial.println(i2c_dev.address(), HEX); Serial.println(i2c_dev.address(), HEX);
} }
void loop() {} void loop() {
}

View file

@ -3,18 +3,16 @@
#define I2C_ADDRESS 0x60 #define I2C_ADDRESS 0x60
Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS); Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS);
void setup() { void setup() {
while (!Serial) { while (!Serial) { delay(10); }
delay(10);
}
Serial.begin(115200); Serial.begin(115200);
Serial.println("I2C device read and write test"); Serial.println("I2C device read and write test");
if (!i2c_dev.begin()) { if (!i2c_dev.begin()) {
Serial.print("Did not find device at 0x"); Serial.print("Did not find device at 0x");
Serial.println(i2c_dev.address(), HEX); Serial.println(i2c_dev.address(), HEX);
while (1) while (1);
;
} }
Serial.print("Device found on address 0x"); Serial.print("Device found on address 0x");
Serial.println(i2c_dev.address(), HEX); Serial.println(i2c_dev.address(), HEX);
@ -23,23 +21,21 @@ void setup() {
// Try to read 32 bytes // Try to read 32 bytes
i2c_dev.read(buffer, 32); i2c_dev.read(buffer, 32);
Serial.print("Read: "); Serial.print("Read: ");
for (uint8_t i = 0; i < 32; i++) { for (uint8_t i=0; i<32; i++) {
Serial.print("0x"); Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
Serial.print(buffer[i], HEX);
Serial.print(", ");
} }
Serial.println(); Serial.println();
// read a register by writing first, then reading // read a register by writing first, then reading
buffer[0] = 0x0C; // we'll reuse the same buffer buffer[0] = 0x0C; // we'll reuse the same buffer
i2c_dev.write_then_read(buffer, 1, buffer, 2, false); i2c_dev.write_then_read(buffer, 1, buffer, 2, false);
Serial.print("Write then Read: "); Serial.print("Write then Read: ");
for (uint8_t i = 0; i < 2; i++) { for (uint8_t i=0; i<2; i++) {
Serial.print("0x"); Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
Serial.print(buffer[i], HEX);
Serial.print(", ");
} }
Serial.println(); Serial.println();
} }
void loop() {} void loop() {
}

View file

@ -1,43 +1,38 @@
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h> #include <Adafruit_I2CDevice.h>
#include <Adafruit_BusIO_Register.h>
#define I2C_ADDRESS 0x60 #define I2C_ADDRESS 0x60
Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS); Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS);
void setup() { void setup() {
while (!Serial) { while (!Serial) { delay(10); }
delay(10);
}
Serial.begin(115200); Serial.begin(115200);
Serial.println("I2C device register test"); Serial.println("I2C device register test");
if (!i2c_dev.begin()) { if (!i2c_dev.begin()) {
Serial.print("Did not find device at 0x"); Serial.print("Did not find device at 0x");
Serial.println(i2c_dev.address(), HEX); Serial.println(i2c_dev.address(), HEX);
while (1) while (1);
;
} }
Serial.print("Device found on address 0x"); Serial.print("Device found on address 0x");
Serial.println(i2c_dev.address(), HEX); Serial.println(i2c_dev.address(), HEX);
Adafruit_BusIO_Register id_reg = Adafruit_BusIO_Register id_reg = Adafruit_BusIO_Register(&i2c_dev, 0x0C, 2, LSBFIRST);
Adafruit_BusIO_Register(&i2c_dev, 0x0C, 2, LSBFIRST);
uint16_t id; uint16_t id;
id_reg.read(&id); id_reg.read(&id);
Serial.print("ID register = 0x"); Serial.print("ID register = 0x"); Serial.println(id, HEX);
Serial.println(id, HEX);
Adafruit_BusIO_Register thresh_reg = Adafruit_BusIO_Register thresh_reg = Adafruit_BusIO_Register(&i2c_dev, 0x01, 2, LSBFIRST);
Adafruit_BusIO_Register(&i2c_dev, 0x01, 2, LSBFIRST);
uint16_t thresh; uint16_t thresh;
thresh_reg.read(&thresh); thresh_reg.read(&thresh);
Serial.print("Initial threshold register = 0x"); Serial.print("Initial threshold register = 0x"); Serial.println(thresh, HEX);
Serial.println(thresh, HEX);
thresh_reg.write(~thresh); thresh_reg.write(~thresh);
Serial.print("Post threshold register = 0x"); Serial.print("Post threshold register = 0x"); Serial.println(thresh_reg.read(), HEX);
Serial.println(thresh_reg.read(), HEX);
} }
void loop() {} void loop() {
}

View file

@ -9,9 +9,7 @@ Adafruit_SPIDevice *spi_dev = NULL; // new Adafruit_SPIDevice(SPIDEVICE_CS);
Adafruit_I2CDevice *i2c_dev = new Adafruit_I2CDevice(I2C_ADDRESS); Adafruit_I2CDevice *i2c_dev = new Adafruit_I2CDevice(I2C_ADDRESS);
void setup() { void setup() {
while (!Serial) { while (!Serial) { delay(10); }
delay(10);
}
Serial.begin(115200); Serial.begin(115200);
Serial.println("I2C or SPI device register test"); Serial.println("I2C or SPI device register test");
@ -29,12 +27,12 @@ void setup() {
} }
} }
Adafruit_BusIO_Register id_reg = Adafruit_BusIO_Register id_reg = Adafruit_BusIO_Register(i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, 0x0F);
Adafruit_BusIO_Register(i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, 0x0F); uint8_t id;
uint8_t id = 0;
id_reg.read(&id); id_reg.read(&id);
Serial.print("ID register = 0x"); Serial.print("ID register = 0x"); Serial.println(id, HEX);
Serial.println(id, HEX);
} }
void loop() {} void loop() {
}

View file

@ -1,34 +1,28 @@
#include <Adafruit_SPIDevice.h> #include <Adafruit_SPIDevice.h>
#define SPIDEVICE_CS 10 #define SPIDEVICE_CS 10
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS, 100000, SPI_BITORDER_MSBFIRST, SPI_MODE1);
Adafruit_SPIDevice(SPIDEVICE_CS, 100000, SPI_BITORDER_MSBFIRST, SPI_MODE1); //Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS, 13, 12, 11, 100000, SPI_BITORDER_MSBFIRST, SPI_MODE1);
// Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS, 13, 12, 11,
// 100000, SPI_BITORDER_MSBFIRST, SPI_MODE1);
void setup() { void setup() {
while (!Serial) { while (!Serial) { delay(10); }
delay(10);
}
Serial.begin(115200); Serial.begin(115200);
Serial.println("SPI device mode test"); Serial.println("SPI device mode test");
if (!spi_dev.begin()) { if (!spi_dev.begin()) {
Serial.println("Could not initialize SPI device"); Serial.println("Could not initialize SPI device");
while (1) while (1);
;
} }
} }
void loop() { void loop() {
Serial.println("\n\nTransfer test"); Serial.println("\n\nTransfer test");
for (uint16_t x = 0; x <= 0xFF; x++) { for (uint16_t x=0; x<=0xFF; x++) {
uint8_t i = x; uint8_t i = x;
Serial.print("0x"); Serial.print("0x"); Serial.print(i, HEX);
Serial.print(i, HEX);
spi_dev.read(&i, 1, i); spi_dev.read(&i, 1, i);
Serial.print("/"); Serial.print("/"); Serial.print(i, HEX);
Serial.print(i, HEX);
Serial.print(", "); Serial.print(", ");
delay(25); delay(25);
} }

View file

@ -3,17 +3,15 @@
#define SPIDEVICE_CS 10 #define SPIDEVICE_CS 10
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS); Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS);
void setup() { void setup() {
while (!Serial) { while (!Serial) { delay(10); }
delay(10);
}
Serial.begin(115200); Serial.begin(115200);
Serial.println("SPI device read and write test"); Serial.println("SPI device read and write test");
if (!spi_dev.begin()) { if (!spi_dev.begin()) {
Serial.println("Could not initialize SPI device"); Serial.println("Could not initialize SPI device");
while (1) while (1);
;
} }
uint8_t buffer[32]; uint8_t buffer[32];
@ -21,23 +19,21 @@ void setup() {
// Try to read 32 bytes // Try to read 32 bytes
spi_dev.read(buffer, 32); spi_dev.read(buffer, 32);
Serial.print("Read: "); Serial.print("Read: ");
for (uint8_t i = 0; i < 32; i++) { for (uint8_t i=0; i<32; i++) {
Serial.print("0x"); Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
Serial.print(buffer[i], HEX);
Serial.print(", ");
} }
Serial.println(); Serial.println();
// read a register by writing first, then reading // read a register by writing first, then reading
buffer[0] = 0x8F; // we'll reuse the same buffer buffer[0] = 0x8F; // we'll reuse the same buffer
spi_dev.write_then_read(buffer, 1, buffer, 2, false); spi_dev.write_then_read(buffer, 1, buffer, 2, false);
Serial.print("Write then Read: "); Serial.print("Write then Read: ");
for (uint8_t i = 0; i < 2; i++) { for (uint8_t i=0; i<2; i++) {
Serial.print("0x"); Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
Serial.print(buffer[i], HEX);
Serial.print(", ");
} }
Serial.println(); Serial.println();
} }
void loop() {} void loop() {
}

View file

@ -1,233 +1,163 @@
/*************************************************** /***************************************************
This is an example for how to use Adafruit_BusIO_RegisterBits from This is an example for how to use Adafruit_BusIO_RegisterBits from Adafruit_BusIO library.
Adafruit_BusIO library.
Designed specifically to work with the Adafruit RTD Sensor Designed specifically to work with the Adafruit RTD Sensor
----> https://www.adafruit.com/products/3328 ----> https://www.adafruit.com/products/3328
uisng a MAX31865 RTD-to-Digital Converter uisng a MAX31865 RTD-to-Digital Converter
----> https://datasheets.maximintegrated.com/en/ds/MAX31865.pdf ----> https://datasheets.maximintegrated.com/en/ds/MAX31865.pdf
This sensor uses SPI to communicate, 4 pins are required to This sensor uses SPI to communicate, 4 pins are required to
interface. interface.
A fifth pin helps to detect when a new conversion is ready. A fifth pin helps to detect when a new conversion is ready.
Adafruit invests time and resources providing this open source code, Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing please support Adafruit and open-source hardware by purchasing
products from Adafruit! products from Adafruit!
Example written (2020/3) by Andreas Hardtung/AnHard. Example written (2020/3) by Andreas Hardtung/AnHard.
BSD license, all text above must be included in any redistribution BSD license, all text above must be included in any redistribution
****************************************************/ ****************************************************/
#include <Adafruit_BusIO_Register.h> #include <Adafruit_BusIO_Register.h>
#include <Adafruit_SPIDevice.h> #include <Adafruit_SPIDevice.h>
#define MAX31865_SPI_SPEED (5000000) #define MAX31865_SPI_SPEED (5000000)
#define MAX31865_SPI_BITORDER (SPI_BITORDER_MSBFIRST) #define MAX31865_SPI_BITORDER (SPI_BITORDER_MSBFIRST)
#define MAX31865_SPI_MODE (SPI_MODE1) #define MAX31865_SPI_MODE (SPI_MODE1)
#define MAX31865_SPI_CS (10) #define MAX31865_SPI_CS (10)
#define MAX31865_READY_PIN (2) #define MAX31865_READY_PIN (2)
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(
MAX31865_SPI_CS, MAX31865_SPI_SPEED, MAX31865_SPI_BITORDER,
MAX31865_SPI_MODE, &SPI); // Hardware SPI
// Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice( MAX31865_SPI_CS, 13, 12, 11,
// MAX31865_SPI_SPEED, MAX31865_SPI_BITORDER, MAX31865_SPI_MODE); // Software
// SPI
// MAX31865 chip related Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice( MAX31865_SPI_CS, MAX31865_SPI_SPEED, MAX31865_SPI_BITORDER, MAX31865_SPI_MODE, &SPI); // Hardware SPI
// ********************************************************************************************* // Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice( MAX31865_SPI_CS, 13, 12, 11, MAX31865_SPI_SPEED, MAX31865_SPI_BITORDER, MAX31865_SPI_MODE); // Software SPI
Adafruit_BusIO_Register config_reg =
Adafruit_BusIO_Register(&spi_dev, 0x00, ADDRBIT8_HIGH_TOWRITE, 1, MSBFIRST);
Adafruit_BusIO_RegisterBits bias_bit =
Adafruit_BusIO_RegisterBits(&config_reg, 1, 7);
Adafruit_BusIO_RegisterBits auto_bit =
Adafruit_BusIO_RegisterBits(&config_reg, 1, 6);
Adafruit_BusIO_RegisterBits oneS_bit =
Adafruit_BusIO_RegisterBits(&config_reg, 1, 5);
Adafruit_BusIO_RegisterBits wire_bit =
Adafruit_BusIO_RegisterBits(&config_reg, 1, 4);
Adafruit_BusIO_RegisterBits faultT_bits =
Adafruit_BusIO_RegisterBits(&config_reg, 2, 2);
Adafruit_BusIO_RegisterBits faultR_bit =
Adafruit_BusIO_RegisterBits(&config_reg, 1, 1);
Adafruit_BusIO_RegisterBits fi50hz_bit =
Adafruit_BusIO_RegisterBits(&config_reg, 1, 0);
Adafruit_BusIO_Register rRatio_reg = // MAX31865 chip related *********************************************************************************************
Adafruit_BusIO_Register(&spi_dev, 0x01, ADDRBIT8_HIGH_TOWRITE, 2, MSBFIRST); Adafruit_BusIO_Register config_reg = Adafruit_BusIO_Register(&spi_dev, 0x00, ADDRBIT8_HIGH_TOWRITE, 1, MSBFIRST);
Adafruit_BusIO_RegisterBits rRatio_bits = Adafruit_BusIO_RegisterBits bias_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 7);
Adafruit_BusIO_RegisterBits(&rRatio_reg, 15, 1); Adafruit_BusIO_RegisterBits auto_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 6);
Adafruit_BusIO_RegisterBits fault_bit = Adafruit_BusIO_RegisterBits oneS_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 5);
Adafruit_BusIO_RegisterBits(&rRatio_reg, 1, 0); Adafruit_BusIO_RegisterBits wire_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 4);
Adafruit_BusIO_RegisterBits faultT_bits = Adafruit_BusIO_RegisterBits(&config_reg, 2, 2);
Adafruit_BusIO_RegisterBits faultR_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 1);
Adafruit_BusIO_RegisterBits fi50hz_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 0);
Adafruit_BusIO_Register maxRratio_reg = Adafruit_BusIO_Register rRatio_reg = Adafruit_BusIO_Register(&spi_dev, 0x01, ADDRBIT8_HIGH_TOWRITE, 2, MSBFIRST);
Adafruit_BusIO_Register(&spi_dev, 0x03, ADDRBIT8_HIGH_TOWRITE, 2, MSBFIRST); Adafruit_BusIO_RegisterBits rRatio_bits = Adafruit_BusIO_RegisterBits(&rRatio_reg, 15, 1);
Adafruit_BusIO_RegisterBits maxRratio_bits = Adafruit_BusIO_RegisterBits fault_bit = Adafruit_BusIO_RegisterBits(&rRatio_reg, 1, 0);
Adafruit_BusIO_RegisterBits(&maxRratio_reg, 15, 1);
Adafruit_BusIO_Register minRratio_reg = Adafruit_BusIO_Register maxRratio_reg = Adafruit_BusIO_Register(&spi_dev, 0x03, ADDRBIT8_HIGH_TOWRITE, 2, MSBFIRST);
Adafruit_BusIO_Register(&spi_dev, 0x05, ADDRBIT8_HIGH_TOWRITE, 2, MSBFIRST); Adafruit_BusIO_RegisterBits maxRratio_bits = Adafruit_BusIO_RegisterBits(&maxRratio_reg, 15, 1);
Adafruit_BusIO_RegisterBits minRratio_bits =
Adafruit_BusIO_RegisterBits(&minRratio_reg, 15, 1);
Adafruit_BusIO_Register fault_reg = Adafruit_BusIO_Register minRratio_reg = Adafruit_BusIO_Register(&spi_dev, 0x05, ADDRBIT8_HIGH_TOWRITE, 2, MSBFIRST);
Adafruit_BusIO_Register(&spi_dev, 0x07, ADDRBIT8_HIGH_TOWRITE, 1, MSBFIRST); Adafruit_BusIO_RegisterBits minRratio_bits = Adafruit_BusIO_RegisterBits(&minRratio_reg, 15, 1);
Adafruit_BusIO_RegisterBits range_high_fault_bit =
Adafruit_BusIO_RegisterBits(&fault_reg, 1, 7); Adafruit_BusIO_Register fault_reg = Adafruit_BusIO_Register(&spi_dev, 0x07, ADDRBIT8_HIGH_TOWRITE, 1, MSBFIRST);
Adafruit_BusIO_RegisterBits range_low_fault_bit = Adafruit_BusIO_RegisterBits range_high_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 7);
Adafruit_BusIO_RegisterBits(&fault_reg, 1, 6); Adafruit_BusIO_RegisterBits range_low_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 6);
Adafruit_BusIO_RegisterBits refin_high_fault_bit = Adafruit_BusIO_RegisterBits refin_high_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 5);
Adafruit_BusIO_RegisterBits(&fault_reg, 1, 5); Adafruit_BusIO_RegisterBits refin_low_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 4);
Adafruit_BusIO_RegisterBits refin_low_fault_bit = Adafruit_BusIO_RegisterBits rtdin_low_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 3);
Adafruit_BusIO_RegisterBits(&fault_reg, 1, 4); Adafruit_BusIO_RegisterBits voltage_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 2);
Adafruit_BusIO_RegisterBits rtdin_low_fault_bit =
Adafruit_BusIO_RegisterBits(&fault_reg, 1, 3);
Adafruit_BusIO_RegisterBits voltage_fault_bit =
Adafruit_BusIO_RegisterBits(&fault_reg, 1, 2);
// Print the details of the configuration register. // Print the details of the configuration register.
void printConfig(void) { void printConfig( void ) {
Serial.print("BIAS: "); Serial.print("BIAS: "); if (bias_bit.read() ) Serial.print("ON"); else Serial.print("OFF");
if (bias_bit.read()) Serial.print(", AUTO: "); if (auto_bit.read() ) Serial.print("ON"); else Serial.print("OFF");
Serial.print("ON"); Serial.print(", ONES: "); if (oneS_bit.read() ) Serial.print("ON"); else Serial.print("OFF");
else Serial.print(", WIRE: "); if (wire_bit.read() ) Serial.print("3"); else Serial.print("2/4");
Serial.print("OFF"); Serial.print(", FAULTCLEAR: "); if (faultR_bit.read() ) Serial.print("ON"); else Serial.print("OFF");
Serial.print(", AUTO: "); Serial.print(", "); if (fi50hz_bit.read() ) Serial.print("50HZ"); else Serial.print("60HZ");
if (auto_bit.read())
Serial.print("ON");
else
Serial.print("OFF");
Serial.print(", ONES: ");
if (oneS_bit.read())
Serial.print("ON");
else
Serial.print("OFF");
Serial.print(", WIRE: ");
if (wire_bit.read())
Serial.print("3");
else
Serial.print("2/4");
Serial.print(", FAULTCLEAR: ");
if (faultR_bit.read())
Serial.print("ON");
else
Serial.print("OFF");
Serial.print(", ");
if (fi50hz_bit.read())
Serial.print("50HZ");
else
Serial.print("60HZ");
Serial.println(); Serial.println();
} }
// Check and print faults. Then clear them. // Check and print faults. Then clear them.
void checkFaults(void) { void checkFaults( void ) {
if (fault_bit.read()) { if (fault_bit.read()) {
Serial.print("MAX: "); Serial.print("MAX: "); Serial.println(maxRratio_bits.read());
Serial.println(maxRratio_bits.read()); Serial.print("VAL: "); Serial.println( rRatio_bits.read());
Serial.print("VAL: "); Serial.print("MIN: "); Serial.println(minRratio_bits.read());
Serial.println(rRatio_bits.read());
Serial.print("MIN: ");
Serial.println(minRratio_bits.read());
if (range_high_fault_bit.read()) if (range_high_fault_bit.read() ) Serial.println("Range high fault");
Serial.println("Range high fault"); if ( range_low_fault_bit.read() ) Serial.println("Range low fault");
if (range_low_fault_bit.read()) if (refin_high_fault_bit.read() ) Serial.println("REFIN high fault");
Serial.println("Range low fault"); if ( refin_low_fault_bit.read() ) Serial.println("REFIN low fault");
if (refin_high_fault_bit.read()) if ( rtdin_low_fault_bit.read() ) Serial.println("RTDIN low fault");
Serial.println("REFIN high fault"); if ( voltage_fault_bit.read() ) Serial.println("Voltage fault");
if (refin_low_fault_bit.read())
Serial.println("REFIN low fault");
if (rtdin_low_fault_bit.read())
Serial.println("RTDIN low fault");
if (voltage_fault_bit.read())
Serial.println("Voltage fault");
faultR_bit.write(1); // clear fault faultR_bit.write(1); // clear fault
} }
} }
void setup() { void setup() {
#if (MAX31865_1_READY_PIN != -1) #if (MAX31865_1_READY_PIN != -1)
pinMode(MAX31865_READY_PIN, INPUT_PULLUP); pinMode(MAX31865_READY_PIN ,INPUT_PULLUP);
#endif #endif
while (!Serial) { while (!Serial) { delay(10); }
delay(10);
}
Serial.begin(115200); Serial.begin(115200);
Serial.println("SPI Adafruit_BusIO_RegisterBits test on MAX31865"); Serial.println("SPI Adafruit_BusIO_RegisterBits test on MAX31865");
if (!spi_dev.begin()) { if (!spi_dev.begin()) {
Serial.println("Could not initialize SPI device"); Serial.println("Could not initialize SPI device");
while (1) while (1);
;
} }
// Set up for automode 50Hz. We don't care about selfheating. We want the // Set up for automode 50Hz. We don't care about selfheating. We want the highest possible sampling rate.
// highest possible sampling rate.
auto_bit.write(0); // Don't switch filtermode while auto_mode is on. auto_bit.write(0); // Don't switch filtermode while auto_mode is on.
fi50hz_bit.write(1); // Set filter to 50Hz mode. fi50hz_bit.write(1); // Set filter to 50Hz mode.
faultR_bit.write(1); // Clear faults. faultR_bit.write(1); // Clear faults.
bias_bit.write(1); // In automode we want to have the bias current always on. bias_bit.write(1); // In automode we want to have the bias current always on.
delay(5); // Wait until bias current settles down. delay(5); // Wait until bias current settles down.
// 10.5 time constants of the input RC network is required. // 10.5 time constants of the input RC network is required.
// 10ms worst case for 10kω reference resistor and a 0.1µF capacitor // 10ms worst case for 10kω reference resistor and a 0.1µF capacitor across the RTD inputs.
// across the RTD inputs. Adafruit Module has 0.1µF and only // Adafruit Module has 0.1µF and only 430/4300ω So here 0.43/4.3ms
// 430/4300ω So here 0.43/4.3ms auto_bit.write(1); // Now we can set automode. Automatically starting first conversion.
auto_bit.write(
1); // Now we can set automode. Automatically starting first conversion.
// Test the READY_PIN // Test the READY_PIN
#if (defined(MAX31865_READY_PIN) && (MAX31865_READY_PIN != -1)) #if (defined( MAX31865_READY_PIN ) && (MAX31865_READY_PIN != -1))
int i = 0; int i = 0;
while (digitalRead(MAX31865_READY_PIN) && i++ <= 100) { while (digitalRead(MAX31865_READY_PIN) && i++ <= 100) { delay(1); }
delay(1); if (i >= 100) {
} Serial.print("ERROR: Max31865 Pin detection does not work. PIN:");
if (i >= 100) { Serial.println(MAX31865_READY_PIN);
Serial.print("ERROR: Max31865 Pin detection does not work. PIN:"); }
Serial.println(MAX31865_READY_PIN); #else
} delay(100);
#else #endif
delay(100);
#endif
// Set ratio range. // Set ratio range.
// Setting the temperatures would need some more calculation - not related to // Setting the temperatures would need some more calculation - not related to Adafruit_BusIO_RegisterBits.
// Adafruit_BusIO_RegisterBits.
uint16_t ratio = rRatio_bits.read(); uint16_t ratio = rRatio_bits.read();
maxRratio_bits.write((ratio < 0x8fffu - 1000u) ? ratio + 1000u : 0x8fffu); maxRratio_bits.write( (ratio < 0x8fffu-1000u) ? ratio + 1000u : 0x8fffu );
minRratio_bits.write((ratio > 1000u) ? ratio - 1000u : 0u); minRratio_bits.write( (ratio > 1000u) ? ratio - 1000u : 0u );
printConfig(); printConfig();
checkFaults(); checkFaults();
} }
void loop() { void loop() {
#if (defined(MAX31865_READY_PIN) && (MAX31865_1_READY_PIN != -1)) #if (defined( MAX31865_READY_PIN ) && (MAX31865_1_READY_PIN != -1))
// Is conversion ready? // Is converstion ready?
if (!digitalRead(MAX31865_READY_PIN)) if (!digitalRead(MAX31865_READY_PIN))
#else #else
// Warant conversion is ready. // Warant conversion is ready.
delay(21); // 21ms for 50Hz-mode. 19ms in 60Hz-mode. delay(21); // 21ms for 50Hz-mode. 19ms in 60Hz-mode.
#endif #endif
{ {
// Read ratio, calculate temperature, scale, filter and print. // Read ratio, calculate temperature, scale, filter and print.
Serial.println(rRatio2C(rRatio_bits.read()) * 100.0f, Serial.println( rRatio2C( rRatio_bits.read() ) * 100.0f, 0); // Temperature scaled by 100
0); // Temperature scaled by 100 // Check, print, clear faults.
// Check, print, clear faults. checkFaults();
checkFaults(); }
}
// Do something else. // Do something else.
// delay(15000); //delay(15000);
} }
// Module/Sensor related. Here Adafruit PT100 module with a 2_Wire PT100 Class C
// ***************************** // Module/Sensor related. Here Adafruit PT100 module with a 2_Wire PT100 Class C *****************************
float rRatio2C(uint16_t ratio) { float rRatio2C(uint16_t ratio) {
// A simple linear conversion. // A simple linear conversion.
const float R0 = 100.0f; const float R0 = 100.0f;
@ -235,34 +165,28 @@ float rRatio2C(uint16_t ratio) {
const float alphaPT = 0.003850f; const float alphaPT = 0.003850f;
const float ADCmax = (1u << 15) - 1.0f; const float ADCmax = (1u << 15) - 1.0f;
const float rscale = Rref / ADCmax; const float rscale = Rref / ADCmax;
// Measured temperature in boiling water 101.08°C with factor a = 1 and b = 0. // Measured temperature in boiling water 101.08°C with factor a = 1 and b = 0. Rref and MAX at about 22±2°C.
// Rref and MAX at about 22±2°C. Measured temperature in ice/water bath 0.76°C // Measured temperature in ice/water bath 0.76°C with factor a = 1 and b = 0. Rref and MAX at about 22±2°C.
// with factor a = 1 and b = 0. Rref and MAX at about 22±2°C. //const float a = 1.0f / (alphaPT * R0);
// const float a = 1.0f / (alphaPT * R0); const float a = (100.0f/101.08f) / (alphaPT * R0);
const float a = (100.0f / 101.08f) / (alphaPT * R0); //const float b = 0.0f; // 101.08
// const float b = 0.0f; // 101.08 const float b = -0.76f; // 100.32 > 101.08
const float b = -0.76f; // 100.32 > 101.08
return filterRing(((ratio * rscale) - R0) * a + b); return filterRing( ((ratio * rscale) - R0) * a + b );
} }
// General purpose // General purpose *********************************************************************************************
// *********************************************************************************************
#define RINGLENGTH 250 #define RINGLENGTH 250
float filterRing(float newVal) { float filterRing( float newVal ) {
static float ring[RINGLENGTH] = {0.0}; static float ring[RINGLENGTH] = { 0.0 };
static uint8_t ringIndex = 0; static uint8_t ringIndex = 0;
static bool ringFull = false; static bool ringFull = false;
if (ringIndex == RINGLENGTH) { if ( ringIndex == RINGLENGTH ) { ringFull = true; ringIndex = 0; }
ringFull = true;
ringIndex = 0;
}
ring[ringIndex] = newVal; ring[ringIndex] = newVal;
uint8_t loopEnd = (ringFull) ? RINGLENGTH : ringIndex + 1; uint8_t loopEnd = (ringFull) ? RINGLENGTH : ringIndex + 1;
float ringSum = 0.0f; float ringSum = 0.0f;
for (uint8_t i = 0; i < loopEnd; i++) for (uint8_t i = 0; i < loopEnd; i++) ringSum += ring[i];
ringSum += ring[i];
ringIndex++; ringIndex++;
return ringSum / loopEnd; return ringSum / loopEnd;
} }

View file

@ -5,36 +5,30 @@
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS); Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS);
void setup() { void setup() {
while (!Serial) { while (!Serial) { delay(10); }
delay(10);
}
Serial.begin(115200); Serial.begin(115200);
Serial.println("SPI device register test"); Serial.println("SPI device register test");
if (!spi_dev.begin()) { if (!spi_dev.begin()) {
Serial.println("Could not initialize SPI device"); Serial.println("Could not initialize SPI device");
while (1) while (1);
;
} }
Adafruit_BusIO_Register id_reg = Adafruit_BusIO_Register id_reg = Adafruit_BusIO_Register(&spi_dev, 0x0F, ADDRBIT8_HIGH_TOREAD);
Adafruit_BusIO_Register(&spi_dev, 0x0F, ADDRBIT8_HIGH_TOREAD); uint8_t id;
uint8_t id = 0;
id_reg.read(&id); id_reg.read(&id);
Serial.print("ID register = 0x"); Serial.print("ID register = 0x"); Serial.println(id, HEX);
Serial.println(id, HEX);
Adafruit_BusIO_Register thresh_reg = Adafruit_BusIO_Register( Adafruit_BusIO_Register thresh_reg = Adafruit_BusIO_Register(&spi_dev, 0x0C, ADDRBIT8_HIGH_TOREAD, 2, LSBFIRST);
&spi_dev, 0x0C, ADDRBIT8_HIGH_TOREAD, 2, LSBFIRST); uint16_t thresh;
uint16_t thresh = 0;
thresh_reg.read(&thresh); thresh_reg.read(&thresh);
Serial.print("Initial threshold register = 0x"); Serial.print("Initial threshold register = 0x"); Serial.println(thresh, HEX);
Serial.println(thresh, HEX);
thresh_reg.write(~thresh); thresh_reg.write(~thresh);
Serial.print("Post threshold register = 0x"); Serial.print("Post threshold register = 0x"); Serial.println(thresh_reg.read(), HEX);
Serial.println(thresh_reg.read(), HEX);
} }
void loop() {} void loop() {
}

View file

@ -1,5 +1,5 @@
name=Adafruit BusIO name=Adafruit BusIO
version=1.17.2 version=1.7.5
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=This is a library for abstracting away UART, I2C and SPI interfacing sentence=This is a library for abstracting away UART, I2C and SPI interfacing