Skip to content

Commit 8705669

Browse files
committed
option to pass I2C object
1 parent 286a260 commit 8705669

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

I2CEEBlockDevice.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,26 @@
2121
I2CEEBlockDevice::I2CEEBlockDevice(
2222
PinName sda, PinName scl, uint8_t addr,
2323
bd_size_t size, bd_size_t block, int freq)
24-
: _i2c(sda, scl), _i2c_addr(addr), _size(size), _block(block)
24+
: _i2c_p(new I2C(sda, scl)), _i2c(*_i2c_p), _i2c_addr(addr),
25+
_size(size), _block(block)
2526
{
2627
_i2c.frequency(freq);
2728
}
2829

30+
I2CEEBlockDevice::I2CEEBlockDevice(
31+
I2C &i2c_obj, uint8_t addr,
32+
bd_size_t size, bd_size_t block)
33+
: _i2c_p(NULL), _i2c(i2c_obj), _i2c_addr(addr),
34+
_size(size), _block(block)
35+
{
36+
}
37+
I2CEEBlockDevice::~I2CEEBlockDevice()
38+
{
39+
if (_i2c_p != NULL){
40+
delete _i2c_p;
41+
}
42+
}
43+
2944
int I2CEEBlockDevice::init()
3045
{
3146
return _sync();

I2CEEBlockDevice.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,24 @@ class I2CEEBlockDevice : public BlockDevice {
7070
I2CEEBlockDevice(
7171
PinName sda, PinName scl, uint8_t address,
7272
bd_size_t size, bd_size_t block=32,
73-
int bus_speed=400000);
73+
int bus_speed=400000);
74+
75+
/** Constructor to create an I2CEEBlockDevice on I2C pins
76+
*
77+
* @param i2c The I2C instance
78+
* @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
79+
* @param size The size of the device in bytes
80+
* @param block The page size of the device in bytes, defaults to 32bytes
81+
* @param freq The frequency of the I2C bus, defaults to 400K.
82+
*/
83+
I2CEEBlockDevice(
84+
I2C &i2c_obj, uint8_t address,
85+
bd_size_t size, bd_size_t block=32);
86+
87+
/** Destructor of I2CEEBlockDevice
88+
*/
89+
90+
virtual ~I2CEEBlockDevice();
7491

7592
/** Initialize a block device
7693
*
@@ -141,7 +158,8 @@ class I2CEEBlockDevice : public BlockDevice {
141158
virtual bd_size_t size() const;
142159

143160
private:
144-
I2C _i2c;
161+
I2C *_i2c_p;
162+
I2C &_i2c;
145163
uint8_t _i2c_addr;
146164
uint32_t _size;
147165
uint32_t _block;

0 commit comments

Comments
 (0)