mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
I2CSlave Class Reference
[Drivers]
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) |
Check if this I2C Slave has been addressed. | |
int | read (char *data, int length) |
Read specified number of bytes 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) |
Set the I2C slave address. | |
void | stop (void) |
Reset the I2C slave back into the known ready receiving state. | |
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) |
Check if this I2C Slave has been addressed. | |
int | read (char *data, int length) |
Read specified number of bytes 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) |
Set 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.
- Note:
- Synchronization level: Not protected
Example Simple I2C responder:
#include <mbed.h> const int SLAVE_ADDRESS = 0xA0; const char message[] = "Slave!"; I2CSlave slave(I2C_SDA, I2C_SCL); int main() { slave.address(SLAVE_ADDRESS); while (1) { int operation = slave.receive(); switch (operation) { case I2CSlave::ReadAddressed: int status = slave.write(message, sizeof(message)); if (status == 0) { printf("Written message: %s\n", message); } else { printf("Failed to write message.\n"); } break; case I2CSlave::WriteGeneral: int byte_read = slave.read(); printf("Read General: %c (%d)\n", byte_read, byte_read); break; case I2CSlave::WriteAddressed: int byte_read = slave.read(); printf("Read Addressed: %c (%d)\n", byte_read, byte_read); break; } } }
Definition at line 69 of file cmsis/BUILD/mbed/drivers/I2CSlave.h.
Constructor & Destructor Documentation
I2CSlave | ( | PinName | sda, |
PinName | scl | ||
) |
Create an I2C Slave interface, connected to the specified pins.
Definition at line 23 of file I2CSlave.cpp.
I2CSlave | ( | PinName | sda, |
PinName | scl | ||
) |
Member Function Documentation
void address | ( | int | address ) |
Set the I2C slave address.
- Parameters:
-
address The 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.
Definition at line 35 of file I2CSlave.cpp.
void address | ( | int | address ) |
Set the I2C slave address.
- Parameters:
-
address The 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.
void frequency | ( | int | hz ) |
Set the frequency of the I2C interface.
- Parameters:
-
hz The bus frequency in Hertz.
void frequency | ( | int | hz ) |
Set the frequency of the I2C interface.
- Parameters:
-
hz The bus frequency in Hertz.
Definition at line 30 of file I2CSlave.cpp.
int read | ( | void | ) |
Read a single byte from an I2C master.
- Returns:
- The byte read.
Definition at line 51 of file I2CSlave.cpp.
int read | ( | char * | data, |
int | length | ||
) |
Read specified number of bytes from an I2C master.
- Parameters:
-
data Pointer to the buffer to read data into. length Number of bytes to read.
- Returns:
- Result of the operation.
- Return values:
-
0 If the number of bytes read is equal to length requested. nonzero On error or if the number of bytes read is less than requested.
int read | ( | void | ) |
Read a single byte from an I2C master.
- Returns:
- The byte read.
int read | ( | char * | data, |
int | length | ||
) |
Read specified number of bytes from an I2C master.
- Parameters:
-
data Pointer to the buffer to read data into. length Number of bytes to read.
- Returns:
- Result of the operation.
- Return values:
-
0 If the number of bytes read is equal to length requested. nonzero On error or if the number of bytes read is less than requested.
Definition at line 46 of file I2CSlave.cpp.
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:
-
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 41 of file I2CSlave.cpp.
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:
-
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.
Definition at line 66 of file I2CSlave.cpp.
void stop | ( | void | ) |
Reset the I2C slave back into the known ready receiving state.
int write | ( | const char * | data, |
int | length | ||
) |
Write to an I2C master.
- Parameters:
-
data Pointer to the buffer containing the data to be sent. length Number of bytes to send.
- Returns:
- Return values:
-
0 If written all bytes successfully. nonzero On error or if the number of bytes written is less than requested.
int write | ( | int | data ) |
Write a single byte to an I2C master.
- Parameters:
-
data Value to write.
- Returns:
- Result of the operation.
- Return values:
-
0 If a NACK is received. 1 If an ACK is received. 2 On timeout.
Definition at line 61 of file I2CSlave.cpp.
int write | ( | const char * | data, |
int | length | ||
) |
Write to an I2C master.
- Parameters:
-
data Pointer to the buffer containing the data to be sent. length Number of bytes to send.
- Returns:
- Return values:
-
0 If written all bytes successfully. nonzero On error or if the number of bytes written is less than requested.
Definition at line 56 of file I2CSlave.cpp.
int write | ( | int | data ) |
Write a single byte to an I2C master.
- Parameters:
-
data Value to write.
- Returns:
- Result of the operation.
- Return values:
-
0 If a NACK is received. 1 If an ACK is received. 2 On timeout.
Generated on Tue Jul 12 2022 20:41:17 by 1.7.2