Skip to content

nRF52 I2CSlave Implementation #12161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 72 additions & 16 deletions drivers/I2CSlave.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,95 @@ namespace mbed {
*
* @note Synchronization level: Not protected
*
* Example Simple I2C responder:
* Example Simple I2C slave and master (requires two Mbed-boards):
* @code
* #include <mbed.h>
* #include <mbed_wait_api.h>
* #include <string.h>
*
* const int SLAVE_ADDRESS = 0xA0;
* const char message[] = "Slave!";
* #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() {
* slave.address(SLAVE_ADDRESS);
*
* char buf[BUFFER_SIZE] = "ABCDE";
*
* slave.address(SLAVE_ADDR);
* while (1) {
* int operation = slave.receive();
* switch (operation) {
* int i = slave.receive();
* switch (i) {
* 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");
* }
* // Write back the buffer from the master
* slave.write(buf, BUFFER_SIZE);
* printf("Written to master (addressed): %s\n", buf);
* break;
*
* case I2CSlave::WriteGeneral:
* int byte_read = slave.read();
* printf("Read General: %c (%d)\n", byte_read, byte_read);
* slave.read(buf, BUFFER_SIZE);
* printf("Read from master (general): %s\n", buf);
* break;
*
* case I2CSlave::WriteAddressed:
* int byte_read = slave.read();
* printf("Read Addressed: %c (%d)\n", byte_read, byte_read);
* 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
*
* @endcode
*/
class I2CSlave {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3908,20 +3908,20 @@
// <e> NRFX_TWIS_ENABLED - nrfx_twis - TWIS peripheral driver
//==========================================================
#ifndef NRFX_TWIS_ENABLED
#define NRFX_TWIS_ENABLED 0
#define NRFX_TWIS_ENABLED 1
#endif
// <q> NRFX_TWIS0_ENABLED - Enable TWIS0 instance


#ifndef NRFX_TWIS0_ENABLED
#define NRFX_TWIS0_ENABLED 0
#define NRFX_TWIS0_ENABLED 1
#endif

// <q> NRFX_TWIS1_ENABLED - Enable TWIS1 instance


#ifndef NRFX_TWIS1_ENABLED
#define NRFX_TWIS1_ENABLED 0
#define NRFX_TWIS1_ENABLED 1
#endif

// <q> NRFX_TWIS_ASSUME_INIT_AFTER_RESET_ONLY - Assume that any instance would be initialized only once
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3908,20 +3908,20 @@
// <e> NRFX_TWIS_ENABLED - nrfx_twis - TWIS peripheral driver
//==========================================================
#ifndef NRFX_TWIS_ENABLED
#define NRFX_TWIS_ENABLED 0
#define NRFX_TWIS_ENABLED 1
#endif
// <q> NRFX_TWIS0_ENABLED - Enable TWIS0 instance


#ifndef NRFX_TWIS0_ENABLED
#define NRFX_TWIS0_ENABLED 0
#define NRFX_TWIS0_ENABLED 1
#endif

// <q> NRFX_TWIS1_ENABLED - Enable TWIS1 instance


#ifndef NRFX_TWIS1_ENABLED
#define NRFX_TWIS1_ENABLED 0
#define NRFX_TWIS1_ENABLED 1
#endif

// <q> NRFX_TWIS_ASSUME_INIT_AFTER_RESET_ONLY - Assume that any instance would be initialized only once
Expand Down
Loading