Mbed OS Reference
Loading...
Searching...
No Matches
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. More...
 
 I2CSlave (const i2c_pinmap_t &static_pinmap)
 Create an I2C Slave interface, connected to the specified pins. More...
 
void frequency (int hz)
 Set the frequency of the I2C interface. More...
 
int receive (void)
 Check if this I2C Slave has been addressed. More...
 
int read (char *data, int length)
 Read bytes transmitted to this MCU from an I2C master. More...
 
int read (void)
 Read a single byte from an I2C master. More...
 
int write (const char *data, int length)
 Write to an I2C master which is addressing this slave device. More...
 
int write (int data)
 Write a single byte to an I2C master. More...
 
void address (int address)
 Set the I2C slave address. More...
 
void stop (void)
 Reset the I2C slave back into the known ready receiving state. More...
 

Detailed Description

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

Note
Synchronization level: Not protected

Example Simple I2C slave and master (requires two Mbed-boards):

#include <mbed.h>
#include <mbed_wait_api.h>
#include <string.h>
#define BUILD_I2C_SLAVE 1 // Build for slave or master of this example
#define SLAVE_ADDR 0xA0
#define BUFFER_SIZE 6
#if BUILD_I2C_SLAVE
// Slave side of the example
#if !DEVICE_I2CSLAVE
#error [NOT_SUPPORTED] I2C Slave is not supported
#endif
I2CSlave slave(I2C_SDA, I2C_SCL);
int main() {
char buf[BUFFER_SIZE] = "ABCDE";
slave.address(SLAVE_ADDR);
while (1) {
int i = slave.receive();
switch (i) {
case I2CSlave::ReadAddressed:
// Write back the buffer from the master
slave.write(buf, BUFFER_SIZE);
printf("Written to master (addressed): %s\n", buf);
break;
case I2CSlave::WriteGeneral:
slave.read(buf, BUFFER_SIZE);
printf("Read from master (general): %s\n", buf);
break;
case I2CSlave::WriteAddressed:
slave.read(buf, BUFFER_SIZE);
printf("Read from master (addressed): %s\n", buf);
break;
}
}
}
#else
// Master side of the example
I2C master(I2C_SDA, I2C_SCL);
static const char* to_send[] = { "abcde", "12345", "EFGHI" };
int main() {
char buf[BUFFER_SIZE];
int send_index = 0;
while (1) {
strcpy(buf, to_send[send_index]);
// Write the new message to the slave
if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE)) {
printf("Failed to write to slave!\n");
} else {
printf("Written to slave: %s\n", buf);
}
// Read what the slave has (should be identical)
if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE)) {
printf("Failed to read from slave!\n");
} else {
printf("Read from slave: %s\n", buf);
}
// Change the message we're writing to the slave
send_index++;
if (send_index > 2) {
send_index = 0;
}
wait_us(500000); // Wait 0.5s
}
}
#endif
An I2C Slave, used for communicating with an I2C Master device.
Definition: I2CSlave.h:128
void wait_us(int us)
Generic wait functions.

Definition at line 128 of file I2CSlave.h.

Constructor & Destructor Documentation

◆ I2CSlave() [1/2]

I2CSlave ( PinName  sda,
PinName  scl 
)

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

Parameters
sdaI2C data line pin.
sclI2C clock line pin.

◆ I2CSlave() [2/2]

I2CSlave ( const i2c_pinmap_t static_pinmap)

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

Parameters
static_pinmapreference to structure which holds static pinmap.

Member Function Documentation

◆ frequency()

void frequency ( int  hz)

Set the frequency of the I2C interface.

Parameters
hzThe bus frequency in Hertz.

◆ receive()

int receive ( void  )

Check if this I2C Slave has been addressed.

Returns
A status indicating if the device has been addressed and how.
Return values
NoDataThe slave has not been addressed.
ReadAddressedThe master has requested a read from this slave.
WriteAddressedThe master is writing to this slave.
WriteGeneralThe master is writing to all slave.

◆ read() [1/2]

int read ( char *  data,
int  length 
)

Read bytes transmitted to this MCU from an I2C master.

Call this function only once receive() returns WriteAddressed or WriteGeneral.

Parameters
dataPointer to the buffer to read data into.
lengthMaximum number of bytes to read.
Returns
The number of bytes read

◆ read() [2/2]

int read ( void  )

Read a single byte from an I2C master.

Returns
The byte read.

◆ write() [1/2]

int write ( const char *  data,
int  length 
)

Write to an I2C master which is addressing this slave device.

Call this function only once receive() returns ReadAddressed. This will send the given data bytes to the master and then send a NACK to end the read transaction.

Parameters
dataPointer to the buffer containing the data to be sent.
lengthNumber of bytes to send.
Returns
Return values
0If written all bytes successfully.
nonzeroOn error or if the number of bytes written is less than requested.

◆ write() [2/2]

int write ( int  data)

Write a single byte to an I2C master.

Parameters
dataValue to write.
Returns
Result of the operation.
Return values
0If a NACK is received.
1If an ACK is received.
2On timeout.

◆ address()

void address ( int  address)

Set the I2C slave address.

Parameters
addressThe 8-bit address to set for the slave (least significant bit is ignored).
Note
If address is 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.