Skip to content

option to pass I2C object #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions I2CEEBlockDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down
22 changes: 20 additions & 2 deletions I2CEEBlockDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,24 @@ class I2CEEBlockDevice : public BlockDevice {
I2CEEBlockDevice(
PinName sda, PinName scl, uint8_t address,
bd_size_t size, bd_size_t block=32,
int bus_speed=400000);
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
*
Expand Down Expand Up @@ -141,7 +158,8 @@ class I2CEEBlockDevice : public BlockDevice {
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;
Expand Down