Fork of the official mbed C/C++ SDK provides the software platform and libraries to build your applications. The fork has the documentation converted to Doxygen format

Dependents:   NervousPuppySprintOne NervousPuppySprint2602 Robot WarehouseBot1 ... more

Fork of mbed by mbed official

Embed: (wiki syntax)

« Back to documentation index

I2CSlave Class Reference

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

#include <I2CSlave.h>

Inherits mbed::Base.

Public Member Functions

 I2CSlave (PinName sda, PinName scl, const char *name=NULL)
 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.
void register_object (const char *name)
 Registers this object with the given name, so that it can be looked up with lookup.
const char * name ()
 Returns the name of the object.
virtual bool rpc (const char *method, const char *arguments, char *result)
 Call the given method with the given arguments, and write the result into the string pointed to by result.
virtual struct rpc_method * get_rpc_methods ()
 Returns a pointer to an array describing the rpc methods supported by this object, terminated by either RPC_METHOD_END or RPC_METHOD_SUPER(Superclass).

Static Public Member Functions

static bool rpc (const char *name, const char *method, const char *arguments, char *result)
 Use the lookup function to lookup an object and, if successful, call its rpc method.
static Baselookup (const char *name, unsigned int len)
 Lookup and return the object that has the given name.
template<class C >
static void add_rpc_class ()
 Add the class to the list of classes which can have static methods called via rpc (the static methods which can be called are defined by that class' get_rpc_class() static method).

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 53 of file I2CSlave.h.


Constructor & Destructor Documentation

I2CSlave ( PinName  sda,
PinName  scl,
const char *  name = NULL 
)

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

Parameters:
sdaI2C data line pin
sclI2C clock line pin

Member Function Documentation

static void add_rpc_class (  ) [static, inherited]

Add the class to the list of classes which can have static methods called via rpc (the static methods which can be called are defined by that class' get_rpc_class() static method).

Definition at line 143 of file Base.h.

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.
void frequency ( int  hz )

Set the frequency of the I2C interface.

Parameters:
hzThe bus frequency in hertz
virtual struct rpc_method* get_rpc_methods (  ) [read, virtual, inherited]

Returns a pointer to an array describing the rpc methods supported by this object, terminated by either RPC_METHOD_END or RPC_METHOD_SUPER(Superclass).

Example

 class Example : public Base {
     int foo(int a, int b) { return a + b; }
     virtual const struct rpc_method *get_rpc_methods() {
         static const rpc_method rpc_methods[] = {
             { "foo", generic_caller<int, Example, int, int, &Example::foo> },
             RPC_METHOD_SUPER(Base)
         };
         return rpc_methods;
     }
 };

Reimplemented in AnalogIn, AnalogOut, BusIn, BusInOut, BusOut, DigitalIn, DigitalInOut, DigitalOut, PwmOut, Serial, SPI, and Timer.

static Base* lookup ( const char *  name,
unsigned int  len 
) [static, inherited]

Lookup and return the object that has the given name.

Parameters:
namethe name to lookup.
lenthe length of name.
const char* name (  ) [inherited]

Returns the name of the object.

Returns:
The name of the object, or NULL if it has no name.
int read ( void   )

Read a single byte from an I2C master.

Returns:
the byte read
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
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
void register_object ( const char *  name ) [inherited]

Registers this object with the given name, so that it can be looked up with lookup.

If this object has already been registered, then this just changes the name.

Parameters:
nameThe name to give the object. If NULL we do nothing.
virtual bool rpc ( const char *  method,
const char *  arguments,
char *  result 
) [virtual, inherited]

Call the given method with the given arguments, and write the result into the string pointed to by result.

The default implementation calls rpc_methods to determine the supported methods.

Parameters:
methodThe name of the method to call.
argumentsA list of arguments separated by spaces.
resultA pointer to a string to write the result into. May be NULL, in which case nothing is written.
Returns:
true if method corresponds to a valid rpc method, or false otherwise.
static bool rpc ( const char *  name,
const char *  method,
const char *  arguments,
char *  result 
) [static, inherited]

Use the lookup function to lookup an object and, if successful, call its rpc method.

Returns:
false if name does not correspond to an object, otherwise the return value of the call to the object's rpc method.
void stop ( void   )

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

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
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