Skip to content

Commit 170d44b

Browse files
committed
feat(i2c_slave): Add new example for making i2c slave as a network sensor
1 parent e27594e commit 170d44b

File tree

9 files changed

+439
-0
lines changed

9 files changed

+439
-0
lines changed

examples/peripherals/.build-test-rules.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ examples/peripherals/i2c/i2c_eeprom:
6767
depends_components:
6868
- esp_driver_i2c
6969

70+
examples/peripherals/i2c/i2c_slave_network_sensor:
71+
disable:
72+
- if: SOC_I2C_SLAVE_CAN_GET_STRETCH_CAUSE != 1 or (SOC_WIFI_SUPPORTED != 1 and SOC_EMAC_SUPPORTED != 1)
73+
depends_components:
74+
- esp_driver_i2c
75+
7076
examples/peripherals/i2c/i2c_tools:
7177
disable:
7278
- if: SOC_I2C_SUPPORTED != 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
7+
8+
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
9+
idf_build_set_property(MINIMAL_BUILD ON)
10+
project(i2c_slave_network_sensor)
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
| Supported Targets | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
2+
| ----------------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- |
3+
4+
5+
# I2C slave example
6+
7+
This code demonstrates how to use the I2C slave driver to make a "network sensor". For illustration, the "sensor" will fetch some data from Github. You can attach this "sensor" to any I2C bus, then the master should be able to read the network data. This example uses the stretch mechanism to solve the problem of the i2c slave not knowing when it should send or receive data. In this example, we have a task to periodically update esp-idf's data on github. When the i2c master comes to visit, if the i2c slave fifo has data, it will return the data directly, if there is no data, it will set off the stretch interrupt, then there will be a callback to fill in the updated github data into the i2c fifo, release the stretch, and the master will return the data.
8+
9+
## Overview
10+
11+
This example demonstrates the possible usage of I2C slave driver
12+
13+
## How to use example
14+
15+
### Hardware Required
16+
17+
To run this example, you should have one ESP32-S, ESP32-C based development board. Also this example need wifi signal to access the internet.
18+
19+
#### Pin Assignment:
20+
21+
**Note:** The following pin assignments are used by default, you can change these in the `menuconfig` .
22+
23+
| | SDA | SCL |
24+
| ------------------------------ | -------------- | -------------- |
25+
| ESP I2C Master (this example) | I2C_MASTER_SDA | I2C_MASTER_SCL |
26+
| ESP I2C Slave (this example) | I2C_SLAVE_SDA | I2C_SLAVE_SCL |
27+
28+
For the actual default value of `I2C_SLAVE_SDA` and `I2C_SLAVE_SCL` see `Example Configuration` in `menuconfig`.
29+
30+
**Note:** There's no need to add an external pull-up resistors for SDA/SCL pin, because the driver will enable the internal pull-up resistors.
31+
32+
33+
### Command Assignment:
34+
35+
| Command | Content |
36+
| --------- | ------------ |
37+
| 0x10 | stars |
38+
| 0x20 | forks |
39+
| 0x30 | open_issues |
40+
| 0x40 | description |
41+
42+
43+
### Build and Flash
44+
45+
Enter `idf.py menuconfig` to config the internet information. In Example Connection Configuration option to config your wifi ssid and password.
46+
47+
Enter `idf.py -p PORT flash monitor` to build, flash and monitor the project.
48+
49+
(To exit the serial monitor, type ``Ctrl-]``.)
50+
51+
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
52+
53+
## Example Output
54+
55+
```
56+
I (11200) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us
57+
I (11210) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 25000, mt_pti: 0, mt_time: 10000
58+
I (11250) wifi:<ba-add>idx:0 (ifx:0, f6:0e:57:fd:ee:8d), tid:0, ssn:1, winSize:64
59+
I (11610) wifi:dp: 2, bi: 102400, li: 4, scale listen interval from 307200 us to 409600 us
60+
I (11620) wifi:AP's beacon interval = 102400 us, DTIM period = 2
61+
I (12460) example_connect: Got IPv6 event: Interface "example_netif_sta" address: fe80:0000:0000:0000:86f7:03ff:fe80:0284, type: ESP_IP6_ADDR_IS_LINK_LOCAL
62+
I (12720) esp_netif_handlers: example_netif_sta ip: 192.168.70.133, mask: 255.255.255.0, gw: 192.168.70.132
63+
I (12720) example_connect: Got IPv4 event: Interface "example_netif_sta" address: 192.168.70.133
64+
I (12730) example_common: Connected to example_netif_sta
65+
I (12730) example_common: - IPv4 address: 192.168.70.133,
66+
I (12740) example_common: - IPv6 address: fe80:0000:0000:0000:86f7:03ff:fe80:0284, type: ESP_IP6_ADDR_IS_LINK_LOCAL
67+
I (12750) gpio: GPIO[5]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0
68+
I (12760) gpio: GPIO[4]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0
69+
I (12770) main_task: Returned from app_main()
70+
I (13470) wifi:<ba-add>idx:1 (ifx:0, f6:0e:57:fd:ee:8d), tid:3, ssn:0, winSize:64
71+
Star count: 13401
72+
Forks count: 7248
73+
issue count: 1767
74+
the description is: Espressif IoT Development Framework. Official development framework for Espressif SoCs.
75+
I (16280) HTTP_CLIENT: HTTP GET Status = 200, content_length = 6165
76+
```
77+
78+
Then you can read the information from an I2C master device.
79+
80+
We can get information via `i2c_tool` example with another esp chip plays in a i2c master role:
81+
82+
```
83+
I (203) main_task: Calling app_main()
84+
85+
==============================================================
86+
| Steps to Use i2c-tools |
87+
| |
88+
| 1. Try 'help', check all supported commands |
89+
| 2. Try 'i2cconfig' to configure your I2C bus |
90+
| 3. Try 'i2cdetect' to scan devices on the bus |
91+
| 4. Try 'i2cget' to get the content of specific register |
92+
| 5. Try 'i2cset' to set the value of specific register |
93+
| 6. Try 'i2cdump' to dump all the register (Experiment) |
94+
| |
95+
==============================================================
96+
97+
98+
Type 'help' to get the list of commands.
99+
Use UP/DOWN arrows to navigate through command history.
100+
Press TAB when typing command name to auto-complete.
101+
I (303) main_task: Returned from app_main()
102+
i2c-tools> i2cconfig --port=0 --freq=100000 --sda=5 --scl=4
103+
i2c-tools> i2cdetect
104+
0 1 2 3 4 5 6 7 8 9 a b c d e f
105+
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
106+
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
107+
20: -- -- -- -- -- -- -- -- 28 -- -- -- -- -- -- --
108+
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
109+
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
110+
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
111+
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
112+
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
113+
i2c-tools> i2cget -c 0x28 -r 0x10 -l 4
114+
0x4b 0x35 0x00 0x00
115+
i2c-tools> i2cget -c 0x28 -r 0x20 -l 4
116+
0x75 0x1c 0x00 0x00
117+
i2c-tools> i2cget -c 0x28 -r 0x40 -l 100
118+
0x45 0x73 0x70 0x72 0x65 0x73 0x73 0x69 0x66 0x20 0x49 0x6f 0x54 0x20 0x44 0x65
119+
0x76 0x65 0x6c 0x6f 0x70 0x6d 0x65 0x6e 0x74 0x20 0x46 0x72 0x61 0x6d 0x65 0x77
120+
0x6f 0x72 0x6b 0x2e 0x20 0x4f 0x66 0x66 0x69 0x63 0x69 0x61 0x6c 0x20 0x64 0x65
121+
0x76 0x65 0x6c 0x6f 0x70 0x6d 0x65 0x6e 0x74 0x20 0x66 0x72 0x61 0x6d 0x65 0x77
122+
0x6f 0x72 0x6b 0x20 0x66 0x6f 0x72 0x20 0x45 0x73 0x70 0x72 0x65 0x73 0x73 0x69
123+
0x66 0x20 0x53 0x6f 0x43 0x73 0x2e 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
124+
0x00 0x00 0x00 0x00
125+
```
126+
127+
## Troubleshooting
128+
129+
(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(srcs "i2c_slave_main.c")
2+
3+
idf_component_register(SRCS ${srcs}
4+
PRIV_REQUIRES esp_http_client esp_wifi nvs_flash json esp_driver_i2c
5+
INCLUDE_DIRS ".")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
menu "Example Configuration"
2+
3+
menu "I2C Slave"
4+
config I2C_SLAVE_SCL
5+
int "SCL GPIO Num"
6+
default 4
7+
help
8+
GPIO number for I2C Slave clock line.
9+
10+
config I2C_SLAVE_SDA
11+
int "SDA GPIO Num"
12+
default 5
13+
help
14+
GPIO number for I2C Slave data line.
15+
16+
config I2C_SLAVE_ADDRESS
17+
hex "I2C SLAVE ADDRESS"
18+
default 0x28
19+
help
20+
Address of I2C slave
21+
22+
endmenu
23+
24+
endmenu

0 commit comments

Comments
 (0)