x

Dependents:   20180621_FT813

Embed: (wiki syntax)

« Back to documentation index

I2CEEBlockDevice Class Reference

I2CEEBlockDevice Class Reference

BlockDevice for I2C based flash device such as Microchip's 24LC or ATMEL's AT24C ranges. More...

#include <I2CEEBlockDevice.h>

Public Member Functions

 I2CEEBlockDevice (PinName sda, PinName scl, uint8_t address, bd_size_t size, bd_size_t block=32, int bus_speed=400000)
 Constructor to create an I2CEEBlockDevice on I2C pins.
virtual int init ()
 Initialize a block device.
virtual int deinit ()
 Deinitialize a block device.
virtual int read (void *buffer, bd_addr_t addr, bd_size_t size)
 Read blocks from a block device.
virtual int program (const void *buffer, bd_addr_t addr, bd_size_t size)
 Program blocks to a block device.
virtual int erase (bd_addr_t addr, bd_size_t size)
 Erase blocks on a block device.
virtual bd_size_t get_read_size () const
 Get the size of a readable block.
virtual bd_size_t get_program_size () const
 Get the size of a programable block.
virtual bd_size_t get_erase_size () const
 Get the size of a eraseable block.
virtual bd_size_t size () const
 Get the total size of the underlying device.

Detailed Description

BlockDevice for I2C based flash device such as Microchip's 24LC or ATMEL's AT24C ranges.

  // Here's an example using a 24LC256 on a GR PEACH
  #include "mbed.h"
  #include "I2CEEBlockDevice.h"
  
  // Create EEPROM device on I2C bus with 32kbytes of memory
  I2CEEBlockDevice i2cee(D14, D15, 0xa0, 32*1024);
  
  int main() {
      printf("i2cee test\n");
  
      // Initialize the device and print the memory layout
      i2cee.init();
      printf("i2cee size: %llu\n",         i2cee.size());
      printf("i2cee read size: %llu\n",    i2cee.get_read_size());
      printf("i2cee program size: %llu\n", i2cee.get_program_size());
      printf("i2cee erase size: %llu\n",   i2cee.get_erase_size());
  
      // Write "Hello World!" to the first block
      char *buffer = (char*)malloc(i2cee.get_erase_size());
      sprintf(buffer, "Hello World!\n");
      i2cee.erase(0, i2cee.get_erase_size());
      i2cee.program(buffer, 0, i2cee.get_erase_size());
  
      // Read back what was stored
      i2cee.read(buffer, 0, i2cee.get_erase_size());
      printf("%s", buffer);
  
      // Deinitialize the device
      i2cee.deinit();
  }

Definition at line 59 of file I2CEEBlockDevice.h.


Constructor & Destructor Documentation

I2CEEBlockDevice ( PinName  sda,
PinName  scl,
uint8_t  address,
bd_size_t  size,
bd_size_t  block = 32,
int  bus_speed = 400000 
)

Constructor to create an I2CEEBlockDevice on I2C pins.

Parameters:
sdaThe pin name for the sda line of the I2C bus.
sclThe pin name for the scl line of the I2C bus.
addrThe 8bit I2C address of the chip, common range 0xa0 - 0xae.
sizeThe size of the device in bytes
blockThe page size of the device in bytes, defaults to 32bytes
freqThe frequency of the I2C bus, defaults to 400K.

Definition at line 21 of file I2CEEBlockDevice.cpp.


Member Function Documentation

int deinit (  ) [virtual]

Deinitialize a block device.

Returns:
0 on success or a negative error code on failure

Definition at line 34 of file I2CEEBlockDevice.cpp.

int erase ( bd_addr_t  addr,
bd_size_t  size 
) [virtual]

Erase blocks on a block device.

The state of an erased block is undefined until it has been programmed

Parameters:
addrAddress of block to begin erasing
sizeSize to erase in bytes, must be a multiple of erase block size
Returns:
0 on success, negative error code on failure

Definition at line 94 of file I2CEEBlockDevice.cpp.

bd_size_t get_erase_size (  ) const [virtual]

Get the size of a eraseable block.

Returns:
Size of a eraseable block in bytes
Note:
Must be a multiple of the program size

Definition at line 126 of file I2CEEBlockDevice.cpp.

bd_size_t get_program_size (  ) const [virtual]

Get the size of a programable block.

Returns:
Size of a programable block in bytes
Note:
Must be a multiple of the read size

Definition at line 121 of file I2CEEBlockDevice.cpp.

bd_size_t get_read_size (  ) const [virtual]

Get the size of a readable block.

Returns:
Size of a readable block in bytes

Definition at line 116 of file I2CEEBlockDevice.cpp.

int init (  ) [virtual]

Initialize a block device.

Returns:
0 on success or a negative error code on failure

Definition at line 29 of file I2CEEBlockDevice.cpp.

int program ( const void *  buffer,
bd_addr_t  addr,
bd_size_t  size 
) [virtual]

Program blocks to a block device.

The blocks must have been erased prior to being programmed

Parameters:
bufferBuffer of data to write to blocks
addrAddress of block to begin writing to
sizeSize to write in bytes, must be a multiple of program block size
Returns:
0 on success, negative error code on failure

Definition at line 59 of file I2CEEBlockDevice.cpp.

int read ( void *  buffer,
bd_addr_t  addr,
bd_size_t  size 
) [virtual]

Read blocks from a block device.

Parameters:
bufferBuffer to write blocks to
addrAddress of block to begin reading from
sizeSize to read in bytes, must be a multiple of read block size
Returns:
0 on success, negative error code on failure

Definition at line 39 of file I2CEEBlockDevice.cpp.

bd_size_t size (  ) const [virtual]

Get the total size of the underlying device.

Returns:
Size of the underlying device in bytes

Definition at line 131 of file I2CEEBlockDevice.cpp.