mbed library sources

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

I2CSlave Class Reference

I2CSlave Class Reference

An I2C Slave, used for communicating with an I2C Master device. More...

#include <I2CSlave.h>

Public Member Functions

 I2CSlave (PinName sda, PinName scl)
 Create an I2C Slave interface, connected to the specified pins.
void frequency (int hz)
 Set the frequency of the I2C interface.
int receive (void)
 Checks to see if this I2C Slave has been addressed.
int read (char *data, int length)
 Read from an I2C master.
int read (void)
 Read a single byte from an I2C master.
int write (const char *data, int length)
 Write to an I2C master.
int write (int data)
 Write a single byte to an I2C master.
void address (int address)
 Sets the I2C slave address.
void stop (void)
 Reset the I2C slave back into the known ready receiving state.

Detailed Description

An I2C Slave, used for communicating with an I2C Master device.

Example:

 // Simple I2C responder
 #include <mbed.h>

 I2CSlave slave(p9, p10);

 int main() {
     char buf[10];
     char msg[] = "Slave!";

     slave.address(0xA0);
     while (1) {
         int i = slave.receive();
         switch (i) {
             case I2CSlave::ReadAddressed:
                 slave.write(msg, strlen(msg) + 1); // Includes null char
                 break;
             case I2CSlave::WriteGeneral:
                 slave.read(buf, 10);
                 printf("Read G: %s\n", buf);
                 break;
             case I2CSlave::WriteAddressed:
                 slave.read(buf, 10);
                 printf("Read A: %s\n", buf);
                 break;
         }
         for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
     }
 }

Definition at line 61 of file I2CSlave.h.


Constructor & Destructor Documentation

I2CSlave ( PinName  sda,
PinName  scl 
)

Create an I2C Slave interface, connected to the specified pins.

Parameters:
sdaI2C data line pin
sclI2C clock line pin

Definition at line 22 of file I2CSlave.cpp.


Member Function Documentation

void address ( int  address )

Sets the I2C slave address.

Parameters:
addressThe address to set for the slave (ignoring the least signifcant bit). If set to 0, the slave will only respond to the general call address.

Definition at line 32 of file I2CSlave.cpp.

void frequency ( int  hz )

Set the frequency of the I2C interface.

Parameters:
hzThe bus frequency in hertz

Definition at line 28 of file I2CSlave.cpp.

int read ( void   )

Read a single byte from an I2C master.

Returns:
the byte read

Definition at line 45 of file I2CSlave.cpp.

int read ( char *  data,
int  length 
)

Read from an I2C master.

Parameters:
datapointer to the byte array to read data in to
lengthmaximum number of bytes to read
Returns:
0 on success, non-0 otherwise

Definition at line 41 of file I2CSlave.cpp.

int receive ( void   )

Checks to see if this I2C Slave has been addressed.

Returns:
A status indicating if the device has been addressed, and how
  • NoData - the slave has not been addressed
  • ReadAddressed - the master has requested a read from this slave
  • WriteAddressed - the master is writing to this slave
  • WriteGeneral - the master is writing to all slave

Definition at line 37 of file I2CSlave.cpp.

void stop ( void   )

Reset the I2C slave back into the known ready receiving state.

Definition at line 57 of file I2CSlave.cpp.

int write ( int  data )

Write a single byte to an I2C master.

the byte to write

Returns:
'1' if an ACK was received, '0' otherwise

Definition at line 53 of file I2CSlave.cpp.

int write ( const char *  data,
int  length 
)

Write to an I2C master.

Parameters:
datapointer to the byte array to be transmitted
lengththe number of bytes to transmite
Returns:
0 on success, non-0 otherwise

Definition at line 49 of file I2CSlave.cpp.