initial commit
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* BusIOForStuduino
|
||||
*
|
||||
* Adafruit BusIO v1.0.0 (https://github.com/adafruit/Adafruit_BusIO) を
|
||||
* 株式会社アーテックが Studuino 向けに改変したものです。
|
||||
* 改変内容は同梱の README.md を参照してください。
|
||||
* オリジナルの著作権表示およびライセンスは LICENSE のとおりです。
|
||||
*/
|
||||
#include <BusIOForStuduino_I2CDevice.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
//#define DEBUG_SERIAL Serial
|
||||
|
||||
Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire) {
|
||||
_addr = addr;
|
||||
_wire = theWire;
|
||||
_begun = false;
|
||||
}
|
||||
|
||||
bool Adafruit_I2CDevice::begin(void) {
|
||||
_wire->begin();
|
||||
_begun = true;
|
||||
|
||||
return detected();
|
||||
}
|
||||
|
||||
bool Adafruit_I2CDevice::detected(void) {
|
||||
// Init I2C if not done yet
|
||||
if (!_begun && !begin()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// A basic scanner, see if it ACK's
|
||||
_wire->beginTransmission(_addr);
|
||||
if (_wire->endTransmission () == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Adafruit_I2CDevice::write(uint8_t *buffer, size_t len, bool stop, uint8_t *prefix_buffer, size_t prefix_len) {
|
||||
if ((len+prefix_len) > 32) {
|
||||
// currently not guaranteed to work if more than 32 bytes!
|
||||
// we will need to find out if some platforms have larger
|
||||
// I2C buffer sizes :/
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("\tI2CDevice could not write such a large buffer"));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
_wire->beginTransmission(_addr);
|
||||
|
||||
// Write the prefix data (usually an address)
|
||||
if ((prefix_len != 0) && (prefix_buffer != NULL)) {
|
||||
if (_wire->write(prefix_buffer, prefix_len) != prefix_len) {
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the data itself
|
||||
if (_wire->write(buffer, len) != len) {
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print(F("\tI2CDevice Wrote: "));
|
||||
if ((prefix_len != 0) && (prefix_buffer != NULL)) {
|
||||
for (uint16_t i=0; i<prefix_len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(prefix_buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
}
|
||||
}
|
||||
for (uint16_t i=0; i<len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
if (len % 32 == 31) {
|
||||
DEBUG_SERIAL.println();
|
||||
}
|
||||
}
|
||||
DEBUG_SERIAL.println();
|
||||
#endif
|
||||
|
||||
|
||||
return (_wire -> endTransmission(stop) == 0);
|
||||
}
|
||||
|
||||
bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) {
|
||||
if (len > 32) {
|
||||
// currently not guaranteed to work if more than 32 bytes!
|
||||
// we will need to find out if some platforms have larger
|
||||
// I2C buffer sizes :/
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("\tI2CDevice could not read such a large buffer"));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_wire->requestFrom((uint8_t)_addr, (uint8_t)len, (uint8_t)stop) != len) {
|
||||
// Not enough data available to fulfill our obligation!
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("\tI2CDevice did not receive enough data"));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint16_t i=0; i<len; i++) {
|
||||
buffer[i] = _wire->read();
|
||||
}
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print(F("\tI2CDevice Read: "));
|
||||
for (uint16_t i=0; i<len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
if (len % 32 == 31) {
|
||||
DEBUG_SERIAL.println();
|
||||
}
|
||||
}
|
||||
DEBUG_SERIAL.println();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Adafruit_I2CDevice::write_then_read(uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, bool stop) {
|
||||
if (! write(write_buffer, write_len, stop)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return read(read_buffer, read_len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t Adafruit_I2CDevice::address(void) {
|
||||
return _addr;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* BusIOForStuduino
|
||||
*
|
||||
* Adafruit BusIO v1.0.0 (https://github.com/adafruit/Adafruit_BusIO) を
|
||||
* 株式会社アーテックが Studuino 向けに改変したものです。
|
||||
* 改変内容は同梱の README.md を参照してください。
|
||||
* オリジナルの著作権表示およびライセンスは LICENSE のとおりです。
|
||||
*/
|
||||
#include <Wire.h>
|
||||
|
||||
#ifndef BusIOForStuduino_I2CDevice_h
|
||||
#define BusIOForStuduino_I2CDevice_h
|
||||
|
||||
|
||||
class Adafruit_I2CDevice {
|
||||
public:
|
||||
Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire=&Wire);
|
||||
uint8_t address(void);
|
||||
bool begin(void);
|
||||
bool detected(void);
|
||||
|
||||
bool read(uint8_t *buffer, size_t len, bool stop=true);
|
||||
bool write(uint8_t *buffer, size_t len, bool stop=true, uint8_t *prefix_buffer=NULL, size_t prefix_len=0);
|
||||
bool write_then_read(uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, bool stop=false);
|
||||
|
||||
|
||||
private:
|
||||
uint8_t _addr;
|
||||
TwoWire *_wire;
|
||||
bool _begun;
|
||||
|
||||
};
|
||||
|
||||
#endif // BusIOForStuduino_I2CDevice_h
|
||||
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* BusIOForStuduino
|
||||
*
|
||||
* Adafruit BusIO v1.0.0 (https://github.com/adafruit/Adafruit_BusIO) を
|
||||
* 株式会社アーテックが Studuino 向けに改変したものです。
|
||||
* 改変内容は同梱の README.md を参照してください。
|
||||
* オリジナルの著作権表示およびライセンスは LICENSE のとおりです。
|
||||
*/
|
||||
#include <BusIOForStuduino_Register.h>
|
||||
|
||||
Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice, uint16_t reg_addr,
|
||||
uint8_t width, uint8_t bitorder, uint8_t address_width) {
|
||||
_i2cdevice = i2cdevice;
|
||||
// _spidevice = NULL;
|
||||
_addrwidth = address_width;
|
||||
_address = reg_addr;
|
||||
_bitorder = bitorder;
|
||||
_width = width;
|
||||
}
|
||||
|
||||
// Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice, uint16_t reg_addr,
|
||||
// Adafruit_BusIO_SPIRegType type,
|
||||
// uint8_t width, uint8_t bitorder, uint8_t address_width) {
|
||||
// _spidevice = spidevice;
|
||||
// _spiregtype = type;
|
||||
// _i2cdevice = NULL;
|
||||
// _addrwidth = address_width;
|
||||
// _address = reg_addr;
|
||||
// _bitorder = bitorder;
|
||||
// _width = width;
|
||||
// }
|
||||
|
||||
// Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice, Adafruit_SPIDevice *spidevice,
|
||||
// Adafruit_BusIO_SPIRegType type, uint16_t reg_addr,
|
||||
// uint8_t width, uint8_t bitorder, uint8_t address_width) {
|
||||
// _spidevice = spidevice;
|
||||
// _i2cdevice = i2cdevice;
|
||||
// _spiregtype = type;
|
||||
// _addrwidth = address_width;
|
||||
// _address = reg_addr;
|
||||
// _bitorder = bitorder;
|
||||
// _width = width;
|
||||
// }
|
||||
|
||||
|
||||
bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
|
||||
|
||||
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF), (uint8_t)(_address>>8)};
|
||||
|
||||
if (_i2cdevice) {
|
||||
return _i2cdevice->write(buffer, len, true, addrbuffer, _addrwidth);
|
||||
}
|
||||
// if (_spidevice) {
|
||||
// if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
|
||||
// addrbuffer[0] &= ~0x80;
|
||||
// }
|
||||
// return _spidevice->write( buffer, len, addrbuffer, _addrwidth);
|
||||
// }
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Adafruit_BusIO_Register::write(uint32_t value, uint8_t numbytes) {
|
||||
if (numbytes == 0) {
|
||||
numbytes = _width;
|
||||
}
|
||||
if (numbytes > 4) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i=0; i<numbytes; i++) {
|
||||
if (_bitorder == LSBFIRST) {
|
||||
_buffer[i] = value & 0xFF;
|
||||
} else {
|
||||
_buffer[numbytes-i-1] = value & 0xFF;
|
||||
}
|
||||
value >>= 8;
|
||||
}
|
||||
return write(_buffer, numbytes);
|
||||
}
|
||||
|
||||
// This does not do any error checking! returns 0xFFFFFFFF on failure
|
||||
uint32_t Adafruit_BusIO_Register::read(void) {
|
||||
if (! read(_buffer, _width)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t value = 0;
|
||||
|
||||
for (int i=0; i < _width; i++) {
|
||||
value <<= 8;
|
||||
if (_bitorder == LSBFIRST) {
|
||||
value |= _buffer[_width-i-1];
|
||||
} else {
|
||||
value |= _buffer[i];
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
|
||||
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF), (uint8_t)(_address>>8)};
|
||||
|
||||
if (_i2cdevice) {
|
||||
return _i2cdevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
|
||||
}
|
||||
// if (_spidevice) {
|
||||
// if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
|
||||
// addrbuffer[0] |= 0x80;
|
||||
// }
|
||||
// return _spidevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
|
||||
// }
|
||||
return false;
|
||||
}
|
||||
|
||||
// bool Adafruit_BusIO_Register::read(uint16_t *value) {
|
||||
// if (! read(_buffer, 2)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// if (_bitorder == LSBFIRST) {
|
||||
// *value = _buffer[1];
|
||||
// *value <<= 8;
|
||||
// *value |= _buffer[0];
|
||||
// } else {
|
||||
// *value = _buffer[0];
|
||||
// *value <<= 8;
|
||||
// *value |= _buffer[1];
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// bool Adafruit_BusIO_Register::read(uint8_t *value) {
|
||||
// if (! read(_buffer, 1)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// *value = _buffer[0];
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// void Adafruit_BusIO_Register::print(Stream *s) {
|
||||
// uint32_t val = read();
|
||||
// s->print("0x"); s->print(val, HEX);
|
||||
// }
|
||||
|
||||
// void Adafruit_BusIO_Register::println(Stream *s) {
|
||||
// print(s);
|
||||
// s->println();
|
||||
// }
|
||||
|
||||
|
||||
Adafruit_BusIO_RegisterBits::Adafruit_BusIO_RegisterBits(Adafruit_BusIO_Register *reg, uint8_t bits, uint8_t shift) {
|
||||
_register = reg;
|
||||
_bits = bits;
|
||||
_shift = shift;
|
||||
}
|
||||
|
||||
// uint32_t Adafruit_BusIO_RegisterBits::read(void) {
|
||||
// uint32_t val = _register->read();
|
||||
// val >>= _shift;
|
||||
// return val & ((1 << (_bits+1)) - 1);
|
||||
// }
|
||||
|
||||
void Adafruit_BusIO_RegisterBits::write(uint32_t data) {
|
||||
uint32_t val = _register->read();
|
||||
|
||||
// mask off the data before writing
|
||||
uint32_t mask = (1 << (_bits+1)) - 1;
|
||||
data &= mask;
|
||||
|
||||
mask <<= _shift;
|
||||
val &= ~mask; // remove the current data at that spot
|
||||
val |= data << _shift; // and add in the new data
|
||||
|
||||
_register->write(val, _register->width());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* BusIOForStuduino
|
||||
*
|
||||
* Adafruit BusIO v1.0.0 (https://github.com/adafruit/Adafruit_BusIO) を
|
||||
* 株式会社アーテックが Studuino 向けに改変したものです。
|
||||
* 改変内容は同梱の README.md を参照してください。
|
||||
* オリジナルの著作権表示およびライセンスは LICENSE のとおりです。
|
||||
*/
|
||||
#include <BusIOForStuduino_I2CDevice.h>
|
||||
// #include <Adafruit_SPIDevice.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
#ifndef BusIOForStuduino_Register_h
|
||||
#define BusIOForStuduino_Register_h
|
||||
|
||||
// typedef enum _Adafruit_BusIO_SPIRegType {
|
||||
// ADDRBIT8_HIGH_TOREAD = 0,
|
||||
// } Adafruit_BusIO_SPIRegType;
|
||||
|
||||
class Adafruit_BusIO_Register {
|
||||
public:
|
||||
Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice, uint16_t reg_addr,
|
||||
uint8_t width=1, uint8_t bitorder=LSBFIRST,
|
||||
uint8_t address_width=1);
|
||||
// Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice, uint16_t reg_addr,
|
||||
// Adafruit_BusIO_SPIRegType type,
|
||||
// uint8_t width=1, uint8_t bitorder=LSBFIRST,
|
||||
// uint8_t address_width=1);
|
||||
|
||||
// Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice,
|
||||
// Adafruit_SPIDevice *spidevice,
|
||||
// Adafruit_BusIO_SPIRegType type,
|
||||
// uint16_t reg_addr,
|
||||
// uint8_t width=1, uint8_t bitorder=LSBFIRST,
|
||||
// uint8_t address_width=1);
|
||||
|
||||
bool read(uint8_t *buffer, uint8_t len);
|
||||
bool read(uint8_t *value);
|
||||
bool read(uint16_t *value);
|
||||
bool read(uint32_t *value);
|
||||
uint32_t read(void);
|
||||
bool write(uint8_t *buffer, uint8_t len);
|
||||
bool write(uint32_t value, uint8_t numbytes=0);
|
||||
|
||||
uint8_t width(void) { return _width; }
|
||||
|
||||
void print(Stream *s=&Serial);
|
||||
void println(Stream *s=&Serial);
|
||||
|
||||
private:
|
||||
Adafruit_I2CDevice *_i2cdevice;
|
||||
// Adafruit_SPIDevice *_spidevice;
|
||||
// Adafruit_BusIO_SPIRegType _spiregtype;
|
||||
uint16_t _address;
|
||||
uint8_t _width, _addrwidth, _bitorder;
|
||||
uint8_t _buffer[4]; // we wont support anything larger than uint32 for non-buffered read
|
||||
};
|
||||
|
||||
class Adafruit_BusIO_RegisterBits {
|
||||
public:
|
||||
Adafruit_BusIO_RegisterBits(Adafruit_BusIO_Register *reg, uint8_t bits, uint8_t shift);
|
||||
void write(uint32_t value);
|
||||
uint32_t read(void);
|
||||
private:
|
||||
Adafruit_BusIO_Register *_register;
|
||||
uint8_t _bits, _shift;
|
||||
};
|
||||
|
||||
|
||||
#endif //BusIOForStuduino_Register_h
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Adafruit Industries
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,48 @@
|
||||
# BusIOForStuduino
|
||||
|
||||
Adafruit BusIO ライブラリ v1.0.0 を、株式会社アーテックが Studuino 向けに
|
||||
改変したものです。I2C デバイスのレジスタアクセスを抽象化します。
|
||||
|
||||
オリジナル: https://github.com/adafruit/Adafruit_BusIO
|
||||
オリジナルのライセンス(MIT)は `LICENSE` を参照してください。
|
||||
|
||||
本ライブラリは `MSA301ForStuduino` から使用されます。単体で使うことは想定していません。
|
||||
|
||||
Adafruit が配布する公式版の `Adafruit_BusIO` と同時にインストールしても衝突しないよう、
|
||||
ライブラリ名・フォルダ名・ファイル名・インクルードガードを変更しています。
|
||||
|
||||
## オリジナルからの変更点
|
||||
|
||||
### 1. ライブラリ名・ファイル名の変更
|
||||
|
||||
公式版との衝突を避けるため、次のように改名しました。クラス名は変更していません。
|
||||
|
||||
| オリジナル | 本ライブラリ |
|
||||
|:--|:--|
|
||||
| `Adafruit_BusIO`(フォルダ) | `BusIOForStuduino` |
|
||||
| `Adafruit_I2CDevice.h` / `.cpp` | `BusIOForStuduino_I2CDevice.h` / `.cpp` |
|
||||
| `Adafruit_BusIO_Register.h` / `.cpp` | `BusIOForStuduino_Register.h` / `.cpp` |
|
||||
| `Adafruit_I2CDevice_h`(インクルードガード) | `BusIOForStuduino_I2CDevice_h` |
|
||||
| `BusIO_Register_h`(インクルードガード) | `BusIOForStuduino_Register_h` |
|
||||
| `name=Adafruit BusIO` | `name=BusIOForStuduino` |
|
||||
|
||||
### 2. SPI サポートの削除
|
||||
|
||||
`Adafruit_SPIDevice` および `Adafruit_BusIO_Register` の SPI 用コンストラクタ・
|
||||
SPI 分岐を削除(コメントアウト)しました。Studuino は I2C のみを使用します。
|
||||
これに伴い `Adafruit_I2CRegister.h` も削除しています。
|
||||
|
||||
### 3. 未使用オーバーロードの削除
|
||||
|
||||
`Adafruit_BusIO_Register` の `read(uint8_t*)` / `read(uint16_t*)` /
|
||||
`read(uint32_t*)` / `print()` / `println()`、および
|
||||
`Adafruit_BusIO_RegisterBits::read()` の実装を削除(コメントアウト)しています。
|
||||
|
||||
**注意:** これらは `.h` 側に宣言が残ったままです。使用するとリンクエラーになります。
|
||||
`MSA301ForStuduino` はいずれも使用していないため、現状は問題ありません。
|
||||
|
||||
### 4. 配布物からの除外
|
||||
|
||||
ビルドに不要なファイルを削除しました(`examples/`、`.travis.yml`)。
|
||||
`examples/` は上記 2. と 3. により、そのままではコンパイルが通らなくなっていたため
|
||||
削除しています。
|
||||
@@ -0,0 +1,9 @@
|
||||
name=BusIOForStuduino
|
||||
version=1.0.0
|
||||
author=Adafruit
|
||||
maintainer=Artec Co., Ltd. <planning@artec-kk.co.jp>
|
||||
sentence=I2C register access helper for Studuino.
|
||||
paragraph=Fork of the Adafruit BusIO library v1.0.0, modified by Artec Co., Ltd. for Studuino. SPI support has been removed. Used by MSA301ForStuduino. See README.md for the list of modifications.
|
||||
category=Signal Input/Output
|
||||
url=https://github.com/adafruit/Adafruit_BusIO
|
||||
architectures=*
|
||||
Reference in New Issue
Block a user