Skip to content

Commit 098c72a

Browse files
authored
Merge pull request #12161 from AGlass0fMilk/nrf-i2c-slave
nRF52 I2CSlave Implementation
2 parents 64bc9d9 + 6baab6c commit 098c72a

File tree

6 files changed

+263
-51
lines changed

6 files changed

+263
-51
lines changed

drivers/I2CSlave.h

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,95 @@ namespace mbed {
3434
*
3535
* @note Synchronization level: Not protected
3636
*
37-
* Example Simple I2C responder:
37+
* Example Simple I2C slave and master (requires two Mbed-boards):
3838
* @code
3939
* #include <mbed.h>
40+
* #include <mbed_wait_api.h>
41+
* #include <string.h>
4042
*
41-
* const int SLAVE_ADDRESS = 0xA0;
42-
* const char message[] = "Slave!";
43+
* #define BUILD_I2C_SLAVE 1 // Build for slave or master of this example
44+
*
45+
* #define SLAVE_ADDR 0xA0
46+
* #define BUFFER_SIZE 6
47+
*
48+
* #if BUILD_I2C_SLAVE
49+
*
50+
* // Slave side of the example
51+
*
52+
* #if !DEVICE_I2CSLAVE
53+
* #error [NOT_SUPPORTED] I2C Slave is not supported
54+
* #endif
4355
*
4456
* I2CSlave slave(I2C_SDA, I2C_SCL);
4557
*
4658
* int main() {
47-
* slave.address(SLAVE_ADDRESS);
59+
*
60+
* char buf[BUFFER_SIZE] = "ABCDE";
61+
*
62+
* slave.address(SLAVE_ADDR);
4863
* while (1) {
49-
* int operation = slave.receive();
50-
* switch (operation) {
64+
* int i = slave.receive();
65+
* switch (i) {
5166
* case I2CSlave::ReadAddressed:
52-
* int status = slave.write(message, sizeof(message));
53-
* if (status == 0) {
54-
* printf("Written message: %s\n", message);
55-
* } else {
56-
* printf("Failed to write message.\n");
57-
* }
67+
* // Write back the buffer from the master
68+
* slave.write(buf, BUFFER_SIZE);
69+
* printf("Written to master (addressed): %s\n", buf);
5870
* break;
71+
*
5972
* case I2CSlave::WriteGeneral:
60-
* int byte_read = slave.read();
61-
* printf("Read General: %c (%d)\n", byte_read, byte_read);
73+
* slave.read(buf, BUFFER_SIZE);
74+
* printf("Read from master (general): %s\n", buf);
6275
* break;
76+
*
6377
* case I2CSlave::WriteAddressed:
64-
* int byte_read = slave.read();
65-
* printf("Read Addressed: %c (%d)\n", byte_read, byte_read);
78+
* slave.read(buf, BUFFER_SIZE);
79+
* printf("Read from master (addressed): %s\n", buf);
6680
* break;
6781
* }
6882
* }
6983
* }
84+
*
85+
* #else
86+
*
87+
* // Master side of the example
88+
*
89+
* I2C master(I2C_SDA, I2C_SCL);
90+
*
91+
* static const char* to_send[] = { "abcde", "12345", "EFGHI" };
92+
*
93+
* int main() {
94+
* char buf[BUFFER_SIZE];
95+
* int send_index = 0;
96+
*
97+
* while (1) {
98+
* strcpy(buf, to_send[send_index]);
99+
*
100+
* // Write the new message to the slave
101+
* if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE)) {
102+
* printf("Failed to write to slave!\n");
103+
* } else {
104+
* printf("Written to slave: %s\n", buf);
105+
* }
106+
*
107+
* // Read what the slave has (should be identical)
108+
* if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE)) {
109+
* printf("Failed to read from slave!\n");
110+
* } else {
111+
* printf("Read from slave: %s\n", buf);
112+
* }
113+
*
114+
* // Change the message we're writing to the slave
115+
* send_index++;
116+
* if (send_index > 2) {
117+
* send_index = 0;
118+
* }
119+
*
120+
* wait_us(500000); // Wait 0.5s
121+
* }
122+
* }
123+
*
124+
* #endif
125+
*
70126
* @endcode
71127
*/
72128
class I2CSlave {

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/config/sdk_config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3908,20 +3908,20 @@
39083908
// <e> NRFX_TWIS_ENABLED - nrfx_twis - TWIS peripheral driver
39093909
//==========================================================
39103910
#ifndef NRFX_TWIS_ENABLED
3911-
#define NRFX_TWIS_ENABLED 0
3911+
#define NRFX_TWIS_ENABLED 1
39123912
#endif
39133913
// <q> NRFX_TWIS0_ENABLED - Enable TWIS0 instance
39143914

39153915

39163916
#ifndef NRFX_TWIS0_ENABLED
3917-
#define NRFX_TWIS0_ENABLED 0
3917+
#define NRFX_TWIS0_ENABLED 1
39183918
#endif
39193919

39203920
// <q> NRFX_TWIS1_ENABLED - Enable TWIS1 instance
39213921

39223922

39233923
#ifndef NRFX_TWIS1_ENABLED
3924-
#define NRFX_TWIS1_ENABLED 0
3924+
#define NRFX_TWIS1_ENABLED 1
39253925
#endif
39263926

39273927
// <q> NRFX_TWIS_ASSUME_INIT_AFTER_RESET_ONLY - Assume that any instance would be initialized only once

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52840/config/sdk_config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3908,20 +3908,20 @@
39083908
// <e> NRFX_TWIS_ENABLED - nrfx_twis - TWIS peripheral driver
39093909
//==========================================================
39103910
#ifndef NRFX_TWIS_ENABLED
3911-
#define NRFX_TWIS_ENABLED 0
3911+
#define NRFX_TWIS_ENABLED 1
39123912
#endif
39133913
// <q> NRFX_TWIS0_ENABLED - Enable TWIS0 instance
39143914

39153915

39163916
#ifndef NRFX_TWIS0_ENABLED
3917-
#define NRFX_TWIS0_ENABLED 0
3917+
#define NRFX_TWIS0_ENABLED 1
39183918
#endif
39193919

39203920
// <q> NRFX_TWIS1_ENABLED - Enable TWIS1 instance
39213921

39223922

39233923
#ifndef NRFX_TWIS1_ENABLED
3924-
#define NRFX_TWIS1_ENABLED 0
3924+
#define NRFX_TWIS1_ENABLED 1
39253925
#endif
39263926

39273927
// <q> NRFX_TWIS_ASSUME_INIT_AFTER_RESET_ONLY - Assume that any instance would be initialized only once

0 commit comments

Comments
 (0)