api / mbed / trunk / I2CSlave

I2CSlave

class I2CSlave : public Base

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
    }
}
I2CSlaveAn I2C Slave, used for communicating with an I2C Master device
Functions
I2CSlaveCreate an I2C Slave interface, connected to the specified pins.
frequencySet the frequency of the I2C interface
receiveChecks to see if this I2C Slave has been addressed.
readRead from an I2C master.
readRead a single byte from an I2C master.
writeWrite to an I2C master.
writeWrite a single byte to an I2C master.
addressSets the I2C slave address.
stopReset the I2C slave back into the known ready receiving state.

Functions

I2CSlave

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

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

Variables

sdaI2C data line pin
sclI2C clock line pin

frequency

void frequency(int hz)

Set the frequency of the I2C interface

Variables

hzThe bus frequency in hertz

receive

int receive(void)

Checks to see if this I2C Slave has been addressed.

Variables

returnsa 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

read

int read(char *data,
int length)

Read from an I2C master.

Variables

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

read

int read(void)

Read a single byte from an I2C master.

Variables

returnsthe byte read

write

int write(const char *data,
int length)

Write to an I2C master.

Variables

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

write

int write(int data)

Write a single byte to an I2C master.

Variables data - the byte to write returns - a ‘1’ if an ACK was received, a ‘0’ otherwise

address

void address(int address)

Sets the I2C slave address.

Variables address - the 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.

stop

void stop(void)

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

class I2CSlave : public Base
An I2C Slave, used for communicating with an I2C Master device
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 write(const char *data,
int length)
Write 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.