Skip to content

Commit 5f0c879

Browse files
author
Erwan GOURIOU
committed
[dev_asynch_i2c] Test Asynch I2C on F411RE
Add single board I2C master/slave asynch test
1 parent 6011365 commit 5f0c879

File tree

5 files changed

+147
-6
lines changed

5 files changed

+147
-6
lines changed

hal/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@
807807
"inherits": ["Target"],
808808
"progen": {"target": "nucleo-f411re"},
809809
"detect_code": ["0740"],
810-
"device_has": ["ANALOGIN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
810+
"device_has": ["ANALOGIN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
811811
"release_versions": ["2", "5"]
812812
},
813813
"ELMO_F411RE": {

libraries/tests/mbed/i2c_master/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
I2C i2c(PTE0, PTE1);
1313
#elif defined(TARGET_nRF51822)
1414
I2C i2c(p22,p20);
15+
#elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F429ZI) || defined(TARGET_NUCLEO_F446RE)
16+
#define TEST_SDA_PIN PB_9
17+
#define TEST_SCL_PIN PB_8
18+
I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN); // I2C_1 (Arduino: D14/D15)
1519
#elif defined(TARGET_FF_ARDUINO) || defined(TARGET_MAXWSNENV)
1620
I2C i2c(I2C_SDA, I2C_SCL);
1721
#elif defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32GG_STK3700) || defined(TARGET_EFM32WG_STK3800)
@@ -42,10 +46,6 @@ I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN);
4246
#define TEST_SDA_PIN PA10
4347
#define TEST_SCL_PIN PA09
4448
I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN);
45-
#elif defined(TARGET_NUCLEO_F411RE)
46-
#define TEST_SDA_PIN PB_9
47-
#define TEST_SCL_PIN PB_8
48-
I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN); // I2C_1 (Arduino: D14/D15)
4949
#else
5050
I2C i2c(p28, p27);
5151
#endif
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include "mbed.h"
2+
#include "test_env.h"
3+
#include <stdio.h>
4+
5+
#define ADDR (0x90)
6+
#define FREQ 100000
7+
#define SIZE 10
8+
9+
// ********************************************************
10+
// This tests data transfer between two I2C interfaces on
11+
// the same chip, one configured as master, the other as
12+
// slave.
13+
//
14+
// Wiring:
15+
// D14 <-> D3
16+
// D15 <-> D6
17+
// ********************************************************
18+
19+
#if defined(TARGET_NUCLEO_F411RE)
20+
I2C master(PB_9, PB_8); // I2C_1 (Arduino: D14/D15)
21+
I2CSlave slave(PB_3, PB_10); // I2C_2 (Arduino: D3/D6)
22+
#endif
23+
24+
volatile int why;
25+
volatile bool master_complete = false;
26+
void cbmaster_done(int event) {
27+
printf("cbmaster_done\n");
28+
master_complete = true;
29+
why = event;
30+
}
31+
32+
int main()
33+
{
34+
event_callback_t callback;
35+
bool success = true;
36+
char buf_master[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
37+
char buf_slave[SIZE];
38+
char res_master[SIZE];
39+
char buf_master_tx[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
40+
char buf_master_rx[SIZE];
41+
char buf_slave_txrx[SIZE];
42+
43+
callback.attach(cbmaster_done);
44+
45+
master.frequency(FREQ);
46+
slave.frequency(FREQ);
47+
slave.address(ADDR);
48+
49+
50+
// First transfer: master to slave
51+
printf("\nFirst transfer: Master Tx, Repeated Start\n");
52+
master.transfer(ADDR, buf_master, SIZE, 0, 0, callback, I2C_EVENT_ALL, true);
53+
54+
while (!master_complete) {
55+
if(slave.receive() == I2CSlave::WriteAddressed) {
56+
slave.read(buf_slave, SIZE);
57+
for(int i = 0; i < SIZE; i++){
58+
buf_slave[i]++;
59+
}
60+
}
61+
}
62+
if (why != I2C_EVENT_TRANSFER_COMPLETE) {
63+
printf("Transfer result: 0x%x\n", why);
64+
notify_completion(false);
65+
}
66+
master_complete = false;
67+
why = 0;
68+
printf("Transfer result: OK\n");
69+
70+
// Second transfer: slave to master
71+
printf("\nSecond transfer: Master Rx\n");
72+
master.transfer(ADDR, 0, 0, res_master, SIZE, callback, I2C_EVENT_ALL, true);
73+
74+
while (!master_complete) {
75+
if(slave.receive() == I2CSlave::ReadAddressed) {
76+
slave.write(buf_slave, SIZE);
77+
}
78+
}
79+
if (why != I2C_EVENT_TRANSFER_COMPLETE) {
80+
printf("Transfer result: 0x%x\n", why);
81+
notify_completion(false);
82+
}
83+
master_complete = false;
84+
why = 0;
85+
printf("Transfer result: OK\n");
86+
87+
// Check first exchange success
88+
for(int i = 0; i < SIZE; i++) {
89+
if (res_master[i] != (buf_master[i] + 1)) {
90+
printf("Buffer check KO\n");
91+
printf("res_master[%d]: %d, buf_master[%d]: %d\n",i,res_master[i],i,buf_master[i]);
92+
notify_completion(false);
93+
break;
94+
}
95+
}
96+
printf("Buffer check OK\n");
97+
98+
// Third transfer: Tx/Rx
99+
printf("\nThird transfer: Master Tx/Rx\n");
100+
master.transfer(ADDR, buf_master_tx, SIZE, buf_master_rx, SIZE, callback, I2C_EVENT_ALL, false);
101+
102+
while (!master_complete) {
103+
104+
int i = slave.receive();
105+
106+
if(i == I2CSlave::WriteAddressed) {
107+
slave.read(buf_slave_txrx, SIZE);
108+
for(int i = 0; i < SIZE; i++){
109+
buf_slave_txrx[i]++;
110+
}
111+
}
112+
if((i == I2CSlave::ReadAddressed) ) {
113+
slave.write(buf_slave_txrx, SIZE);
114+
}
115+
}
116+
if (why != I2C_EVENT_TRANSFER_COMPLETE) {
117+
printf("Transfer result: 0x%x\n", why);
118+
notify_completion(false);
119+
}
120+
master_complete = false;
121+
why = 0;
122+
printf("Transfer result: OK\n");
123+
124+
for(int i = 0; i < SIZE; i++) {
125+
if (buf_master_rx[i] != (buf_master_tx[i] + 1)) {
126+
printf("Buffer check KO\n");
127+
printf("buf_master_rx[%d]: %d, buf_master_tx[%d]: %d\n",i,buf_master_rx[i],i,buf_master_tx[i]);
128+
notify_completion(false);
129+
break;
130+
}
131+
}
132+
printf("Buffer check OK\n");
133+
notify_completion(success);
134+
}
135+

libraries/tests/mbed/i2c_slave/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ I2CSlave slave(p9, p10);
1616
I2CSlave slave(PA16, PA17);
1717
#elif defined(TARGET_SAMD21J18A) || defined(TARGET_SAMD21G18A) || defined(TARGET_SAML21J18A)
1818
I2CSlave slave(PA08, PA09);
19-
#elif defined TARGET_NUCLEO_F411RE
19+
#elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F429ZI) || defined(TARGET_NUCLEO_F446RE)
20+
//I2C master(PB_9, PB_8); // I2C_1 (Arduino: D14/D15)
2021
I2CSlave slave(PB_3, PB_10); // I2C_2 (Arduino: D3/D6)
2122
#elif defined(TARGET_FF_ARDUINO)
2223
I2CSlave slave(I2C_SDA, I2C_SCL);

tools/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,11 @@
11651165
"automated": True,
11661166
#"host_test" : "detect_auto",
11671167
},
1168+
{
1169+
"id": "I2C_MASTER_SLAVE_ASYNCH", "description": "i2c_master_slave_asynch",
1170+
"source_dir": join(TEST_DIR, "mbed", "i2c_master_slave_asynch"),
1171+
"dependencies": [MBED_LIBRARIES, TEST_MBED_LIB],
1172+
},
11681173

11691174
]
11701175

0 commit comments

Comments
 (0)