Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed by
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 | ||
) |
Member Function Documentation
void address | ( | int | address ) |
Sets the I2C slave address.
- Parameters:
-
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.
void frequency | ( | int | hz ) |
Set the frequency of the I2C interface.
- Parameters:
-
hz The bus frequency in hertz
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:
-
data pointer to the byte array to read data in to length maximum 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 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:
-
data pointer to the byte array to be transmitted length the number of bytes to transmite
- Returns:
- 0 on success, non-0 otherwise
Generated on Tue Jul 12 2022 23:37:52 by
