Softi2c with pull up enabled

Embed: (wiki syntax)

« Back to documentation index

SoftI2C Class Reference

SoftI2C Class Reference

A software I2C class in case you run out of hardware I2C pins or cannot use those for another reason. More...

#include <SoftI2C.h>

Public Member Functions

 SoftI2C (PinName sda, PinName scl)
 Create an software I2C Master interface, connected to the specified pins.
void frequency (int hz)
 Set the frequency of the I2C interface.
int read (int address, char *data, int length, bool repeated=false)
 Read from an I2C slave.
int read (int ack)
 Read a single byte from the I2C bus.
int write (int address, const char *data, int length, bool repeated=false)
 Write to an I2C slave.
int write (int data)
 Write single byte out on the I2C bus.
void start (void)
 Create a (re-)start condition on the I2C bus.
void stop (void)
 Create a stop condition on the I2C bus.

Detailed Description

A software I2C class in case you run out of hardware I2C pins or cannot use those for another reason.

The class is a drop-in replacement of the classic mbed I2C class: include this file in your program, replace I2C with SoftI2C when creating the object and it should work properly without other modifications.

Definition at line 14 of file SoftI2C.h.


Constructor & Destructor Documentation

SoftI2C ( PinName  sda,
PinName  scl 
)

Create an software I2C Master interface, connected to the specified pins.

Parameters:
sdaI2C data pin
sclI2C clock pin

Definition at line 3 of file SoftI2C.cpp.


Member Function Documentation

void frequency ( int  hz )

Set the frequency of the I2C interface.

Note that the delay of the DigitalInOuts is not compensated: the real frequency will be lower than the one set. However since I2C is a synchronous protocol this shouldn't affect functionality

Parameters:
hzThe bus frequency in hertz

Definition at line 16 of file SoftI2C.cpp.

int read ( int  address,
char *  data,
int  length,
bool  repeated = false 
)

Read from an I2C slave.

Performs a complete read transaction. The bottom bit of the address is forced to 1 to indicate a read.

Parameters:
address8-bit I2C slave address [ addr | 1 ]
Pointerto the byte-array data to read to
lengthNumber of bytes to read
repeatedRepeated start, true - do not send stop at end
Returns:
0 on success (ack), non-0 on failure (nack)

Definition at line 20 of file SoftI2C.cpp.

int read ( int  ack )

Read a single byte from the I2C bus.

Parameters:
ackindicates if the byte is to be acknowledged (1 = acknowledge)
Returns:
the byte read

Definition at line 61 of file SoftI2C.cpp.

void start ( void   )

Create a (re-)start condition on the I2C bus.

Definition at line 138 of file SoftI2C.cpp.

void stop ( void   )

Create a stop condition on the I2C bus.

Definition at line 156 of file SoftI2C.cpp.

int write ( int  data )

Write single byte out on the I2C bus.

Parameters:
datadata to write on the bus
Returns:
'1' if an ACK is received, '0' otherwise

Definition at line 98 of file SoftI2C.cpp.

int write ( int  address,
const char *  data,
int  length,
bool  repeated = false 
)

Write to an I2C slave.

Performs a complete write transaction. The bottom bit of the address is forced to 0 to indicate a write.

Parameters:
address8-bit I2C slave address [ addr | 0 ]
Pointerto the byte-array data to send
lengthNumber of bytes to send
repeatedRepeated start, true - do not send stop at end
Returns:
0 on success (ack), non-0 on failure (nack)

Definition at line 40 of file SoftI2C.cpp.