Merge pull request #4 from pilotak/master

option to pass I2C object
pull/10711/head
Christopher Haster 2018-07-26 09:47:15 -05:00 committed by GitHub
commit 383956367d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 17 deletions

View File

@ -21,9 +21,24 @@
I2CEEBlockDevice::I2CEEBlockDevice(
PinName sda, PinName scl, uint8_t addr,
bd_size_t size, bd_size_t block, int freq)
: _i2c(sda, scl), _i2c_addr(addr), _size(size), _block(block)
: _i2c_addr(addr), _size(size), _block(block)
{
_i2c.frequency(freq);
_i2c = new (_i2c_buffer) I2C(sda, scl);
_i2c->frequency(freq);
}
I2CEEBlockDevice::I2CEEBlockDevice(
I2C * i2c_obj, uint8_t addr,
bd_size_t size, bd_size_t block)
: _i2c_addr(addr), _size(size), _block(block)
{
_i2c = i2c_obj;
}
I2CEEBlockDevice::~I2CEEBlockDevice()
{
if (_i2c == (I2C*)_i2c_buffer) {
_i2c->~I2C();
}
}
int I2CEEBlockDevice::init()
@ -41,15 +56,15 @@ int I2CEEBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
// Check the address and size fit onto the chip.
MBED_ASSERT(is_valid_read(addr, size));
_i2c.start();
if (!_i2c.write(_i2c_addr | 0) ||
!_i2c.write((char)(addr >> 8)) ||
!_i2c.write((char)(addr & 0xff))) {
_i2c->start();
if (!_i2c->write(_i2c_addr | 0) ||
!_i2c->write((char)(addr >> 8)) ||
!_i2c->write((char)(addr & 0xff))) {
return BD_ERROR_DEVICE_ERROR;
}
_i2c.stop();
_i2c->stop();
if (_i2c.read(_i2c_addr, static_cast<char*>(buffer), size) < 0) {
if (_i2c->read(_i2c_addr, static_cast<char*>(buffer), size) < 0) {
return BD_ERROR_DEVICE_ERROR;
}
@ -66,17 +81,17 @@ int I2CEEBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size
uint32_t off = addr % _block;
uint32_t chunk = (off + size < _block) ? size : (_block - off);
_i2c.start();
if (!_i2c.write(_i2c_addr | 0) ||
!_i2c.write((char)(addr >> 8)) ||
!_i2c.write((char)(addr & 0xff))) {
_i2c->start();
if (!_i2c->write(_i2c_addr | 0) ||
!_i2c->write((char)(addr >> 8)) ||
!_i2c->write((char)(addr & 0xff))) {
return BD_ERROR_DEVICE_ERROR;
}
for (unsigned i = 0; i < chunk; i++) {
_i2c.write(static_cast<const char*>(buffer)[i]);
_i2c->write(static_cast<const char*>(buffer)[i]);
}
_i2c.stop();
_i2c->stop();
int err = _sync();
if (err) {
@ -103,7 +118,7 @@ int I2CEEBlockDevice::_sync()
// so loop trying to do a zero byte write until it is ACKed
// by the chip.
for (int i = 0; i < I2CEE_TIMEOUT; i++) {
if (_i2c.write(_i2c_addr | 0, 0, 0) < 1) {
if (_i2c->write(_i2c_addr | 0, 0, 0) < 1) {
return 0;
}

View File

@ -72,6 +72,23 @@ public:
bd_size_t size, bd_size_t block=32,
int bus_speed=400000);
/** Constructor to create an I2CEEBlockDevice on I2C pins
*
* @param i2c The I2C instance pointer
* @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
* @param size The size of the device in bytes
* @param block The page size of the device in bytes, defaults to 32bytes
* @param freq The frequency of the I2C bus, defaults to 400K.
*/
I2CEEBlockDevice(
I2C * i2c_obj, uint8_t address,
bd_size_t size, bd_size_t block=32);
/** Destructor of I2CEEBlockDevice
*/
virtual ~I2CEEBlockDevice();
/** Initialize a block device
*
* @return 0 on success or a negative error code on failure
@ -141,7 +158,8 @@ public:
virtual bd_size_t size() const;
private:
I2C _i2c;
I2C * _i2c;
uint32_t _i2c_buffer[sizeof(I2C) / sizeof(uint32_t)];
uint8_t _i2c_addr;
uint32_t _size;
uint32_t _block;