From 87056697f404f1c4d336ea46ac80d33e364e5564 Mon Sep 17 00:00:00 2001
From: Pavel Slama
Date: Mon, 18 Jun 2018 20:35:54 +0200
Subject: [PATCH] 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;