From 87056697f404f1c4d336ea46ac80d33e364e5564 Mon Sep 17 00:00:00 2001
From: Pavel Slama
Date: Mon, 18 Jun 2018 20:35:54 +0200
Subject: [PATCH 1/3] option to pass I2C object
---
I2CEEBlockDevice.cpp | 17 ++++++++++++++++-
I2CEEBlockDevice.h | 22 ++++++++++++++++++++--
2 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/I2CEEBlockDevice.cpp b/I2CEEBlockDevice.cpp
index 3ae66fc3a3..134df11cfa 100644
--- a/I2CEEBlockDevice.cpp
+++ b/I2CEEBlockDevice.cpp
@@ -21,11 +21,26 @@
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_p(new I2C(sda, scl)), _i2c(*_i2c_p), _i2c_addr(addr),
+ _size(size), _block(block)
{
_i2c.frequency(freq);
}
+I2CEEBlockDevice::I2CEEBlockDevice(
+ I2C &i2c_obj, uint8_t addr,
+ bd_size_t size, bd_size_t block)
+ : _i2c_p(NULL), _i2c(i2c_obj), _i2c_addr(addr),
+ _size(size), _block(block)
+{
+}
+I2CEEBlockDevice::~I2CEEBlockDevice()
+{
+ if (_i2c_p != NULL){
+ delete _i2c_p;
+ }
+}
+
int I2CEEBlockDevice::init()
{
return _sync();
diff --git a/I2CEEBlockDevice.h b/I2CEEBlockDevice.h
index 5f31f7f790..110eed6482 100644
--- a/I2CEEBlockDevice.h
+++ b/I2CEEBlockDevice.h
@@ -70,7 +70,24 @@ public:
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
+ * @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
*
@@ -141,7 +158,8 @@ public:
virtual bd_size_t size() const;
private:
- I2C _i2c;
+ I2C *_i2c_p;
+ I2C &_i2c;
uint8_t _i2c_addr;
uint32_t _size;
uint32_t _block;
From ffcc8501049e7f9594954f58f5e6e95a18c28683 Mon Sep 17 00:00:00 2001
From: Pavel Slama
Date: Mon, 18 Jun 2018 21:47:40 +0200
Subject: [PATCH 2/3] requested changes
---
I2CEEBlockDevice.cpp | 42 ++++++++++++++++++++----------------------
I2CEEBlockDevice.h | 7 +++----
2 files changed, 23 insertions(+), 26 deletions(-)
diff --git a/I2CEEBlockDevice.cpp b/I2CEEBlockDevice.cpp
index 134df11cfa..59f9926fe2 100644
--- a/I2CEEBlockDevice.cpp
+++ b/I2CEEBlockDevice.cpp
@@ -21,24 +21,22 @@
I2CEEBlockDevice::I2CEEBlockDevice(
PinName sda, PinName scl, uint8_t addr,
bd_size_t size, bd_size_t block, int freq)
- : _i2c_p(new I2C(sda, scl)), _i2c(*_i2c_p), _i2c_addr(addr),
- _size(size), _block(block)
+ : _i2c_addr(addr), _size(size), _block(block)
{
- _i2c.frequency(freq);
+ _i2c = new I2C(sda, scl);
+ _i2c->frequency(freq);
}
I2CEEBlockDevice::I2CEEBlockDevice(
- I2C &i2c_obj, uint8_t addr,
+ I2C * i2c_obj, uint8_t addr,
bd_size_t size, bd_size_t block)
- : _i2c_p(NULL), _i2c(i2c_obj), _i2c_addr(addr),
- _size(size), _block(block)
+ : _i2c_addr(addr), _size(size), _block(block)
{
+ _i2c = i2c_obj;
}
I2CEEBlockDevice::~I2CEEBlockDevice()
{
- if (_i2c_p != NULL){
- delete _i2c_p;
- }
+ _i2c->~I2C();
}
int I2CEEBlockDevice::init()
@@ -56,15 +54,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(buffer), size) < 0) {
+ if (_i2c->read(_i2c_addr, static_cast(buffer), size) < 0) {
return BD_ERROR_DEVICE_ERROR;
}
@@ -81,17 +79,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(buffer)[i]);
+ _i2c->write(static_cast(buffer)[i]);
}
- _i2c.stop();
+ _i2c->stop();
int err = _sync();
if (err) {
@@ -118,7 +116,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;
}
diff --git a/I2CEEBlockDevice.h b/I2CEEBlockDevice.h
index 110eed6482..d85643d173 100644
--- a/I2CEEBlockDevice.h
+++ b/I2CEEBlockDevice.h
@@ -74,14 +74,14 @@ public:
/** Constructor to create an I2CEEBlockDevice on I2C pins
*
- * @param i2c The I2C instance
+ * @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,
+ I2C * i2c_obj, uint8_t address,
bd_size_t size, bd_size_t block=32);
/** Destructor of I2CEEBlockDevice
@@ -158,8 +158,7 @@ public:
virtual bd_size_t size() const;
private:
- I2C *_i2c_p;
- I2C &_i2c;
+ I2C * _i2c;
uint8_t _i2c_addr;
uint32_t _size;
uint32_t _block;
From b440d721140d07734648417d8f0ed47f7ed88e97 Mon Sep 17 00:00:00 2001
From: Christopher Haster
Date: Wed, 25 Jul 2018 11:40:00 -0500
Subject: [PATCH 3/3] Changed I2C default instance to use placement new
---
I2CEEBlockDevice.cpp | 6 ++++--
I2CEEBlockDevice.h | 1 +
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/I2CEEBlockDevice.cpp b/I2CEEBlockDevice.cpp
index 59f9926fe2..e1870ab529 100644
--- a/I2CEEBlockDevice.cpp
+++ b/I2CEEBlockDevice.cpp
@@ -23,7 +23,7 @@ I2CEEBlockDevice::I2CEEBlockDevice(
bd_size_t size, bd_size_t block, int freq)
: _i2c_addr(addr), _size(size), _block(block)
{
- _i2c = new I2C(sda, scl);
+ _i2c = new (_i2c_buffer) I2C(sda, scl);
_i2c->frequency(freq);
}
@@ -36,7 +36,9 @@ I2CEEBlockDevice::I2CEEBlockDevice(
}
I2CEEBlockDevice::~I2CEEBlockDevice()
{
- _i2c->~I2C();
+ if (_i2c == (I2C*)_i2c_buffer) {
+ _i2c->~I2C();
+ }
}
int I2CEEBlockDevice::init()
diff --git a/I2CEEBlockDevice.h b/I2CEEBlockDevice.h
index d85643d173..da4732d1e7 100644
--- a/I2CEEBlockDevice.h
+++ b/I2CEEBlockDevice.h
@@ -159,6 +159,7 @@ public:
private:
I2C * _i2c;
+ uint32_t _i2c_buffer[sizeof(I2C) / sizeof(uint32_t)];
uint8_t _i2c_addr;
uint32_t _size;
uint32_t _block;