Skip to content

Commit 1de76d1

Browse files
SohKamYung-EspressifYanKE01
authored andcommitted
feature(sdcard_example): Add SD Card operating with ESP-Hosted example
- Added an example showing the SD Card operating with ESP-Hosted on a ESP32-P4, using SDIO as ESP-Hosted transport. - Add dummy init and deinit function for host of the SD card Co-authored-by: yanke <yanke2@espressif.com>
1 parent 6352450 commit 1de76d1

19 files changed

+825
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 2.6.5
4+
5+
### Features
6+
7+
- Add example showing concurrent use of a SD Card and ESP-Hosted.
8+
39
## 2.6.4
410

511
- Fix the esp_wifi_deinit() call from host
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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.22)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
7+
idf_build_set_property(MINIMAL_BUILD ON)
8+
9+
project(sd_card)
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
| Supported Hosts | ESP32-P4 | ESP32-S3 |
2+
|-----------------|----------|----------|
3+
4+
# SD Card (SDMMC) with ESP-Hosted example
5+
6+
> [!IMPORTANT]
7+
> The ESP-IDF `master` branch has an issue that prevents the SD card and ESP-Hosted from working together when both are using SDMMC. See [ESP-IDF Issue 16233](https://github.com/espressif/esp-idf/issues/16233) for more information. This code contains a workaround for this issue.
8+
9+
This example demonstrates how to use an SD card on an ESP32-P4 dev board when ESP-Hosted is using SDIO to communicate with the on-board ESP32-C6. It has been modified from the standard [ESP-IDF SD Card example](https://github.com/espressif/esp-idf/tree/master/examples/storage/sd_card/sdmmc).
10+
11+
This example does the following steps:
12+
13+
1. Initialise ESP-Hosted and Wi-Fi
14+
1. Use an "all-in-one" `esp_vfs_fat_sdmmc_mount` function to:
15+
- initialize SDMMC peripheral,
16+
- probe and initialize an SD card,
17+
- mount FAT filesystem using FATFS library
18+
- register FAT filesystem in VFS, enabling C standard library and POSIX functions to be used.
19+
1. Print information about the card, such as name, type, capacity, and maximum supported frequency.
20+
1. Perform a Wi-Fi Scan
21+
1. Create a file using `fopen` and write to it using `fprintf`.
22+
1. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function.
23+
1. Open renamed file for reading, read back the line, and print it to the terminal.
24+
1. Perform another Wi-Fi Scan
25+
26+
This example supports SD (SDSC, SDHC, SDXC) cards and eMMC chips.
27+
28+
## Hardware
29+
30+
This example requires an ESP32-P4 development board with an SD card slot and an SD card. On this board, the SD card slot is assigned to SDMMC Slot 0, while the on-board ESP32-C6 is connected to the ESP32-P4 on SDMMC Slot 1.
31+
32+
Although it is possible to connect an SD card breakout adapter, keep in mind that connections using breakout cables are often unreliable and have poor signal integrity. You may need to use lower clock frequency when working with SD card breakout adapters.
33+
34+
This example doesn't utilize card detect (CD) and write protect (WP) signals from SD card slot.
35+
36+
### Pin assignments for SDMMC Slot 0 on ESP32-P4
37+
38+
On ESP32-P4, SDMMC Slot 0 GPIO pins cannot be customized. The GPIO assigned in the example should not be modified.
39+
40+
The table below lists the default pin assignments.
41+
42+
ESP32-P4 pin | SD card pin | Notes
43+
--------------|-------------|------------
44+
GPIO43 | CLK | 10k pullup
45+
GPIO44 | CMD | 10k pullup
46+
GPIO39 | D0 | 10k pullup
47+
GPIO40 | D1 | not used in 1-line SD mode; 10k pullup in 4-line mode
48+
GPIO41 | D2 | not used in 1-line SD mode; 10k pullup in 4-line mode
49+
GPIO42 | D3 | not used in 1-line SD mode, but card's D3 pin must have a 10k pullup
50+
51+
### 4-line and 1-line SD modes
52+
53+
By default, this example uses 4 line SD mode, utilizing 6 pins: CLK, CMD, D0 - D3. It is possible to use 1-line mode (CLK, CMD, D0) by changing "SD/MMC bus width" in the example configuration menu (see `CONFIG_EXAMPLE_SDMMC_BUS_WIDTH_1`).
54+
55+
Note that even if card's D3 line is not connected to the ESP chip, it still has to be pulled up, otherwise the card will go into SPI protocol mode.
56+
57+
## How to use example
58+
59+
### Build and flash
60+
61+
Build the project and flash it to the board, then run monitor tool to view serial output:
62+
63+
```
64+
idf.py -p PORT flash monitor
65+
```
66+
67+
(Replace PORT with serial port name.)
68+
69+
(To exit the serial monitor, type ``Ctrl-]``.)
70+
71+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
72+
73+
74+
## Example output
75+
76+
A Wi-Fi Scan is performed before and after accessing the SD Card. This shows that both SDMMC Slots work as expected.
77+
78+
```
79+
I (2103) transport: Received INIT event from ESP32 peripheral
80+
I (2103) transport: EVENT: 12
81+
I (2113) transport: Identified slave [esp32c6]
82+
I (2113) transport: EVENT: 11
83+
I (2113) transport: capabilities: 0xd
84+
I (2123) transport: Features supported are:
85+
I (2123) transport: * WLAN
86+
I (2123) transport: - HCI over SDIO
87+
I (2133) transport: - BLE only
88+
I (2133) transport: EVENT: 13
89+
I (2133) transport: ESP board type is : 13
90+
91+
I (2143) transport: Base transport is set-up, TRANSPORT_TX_ACTIVE
92+
I (2143) H_API: Transport active
93+
I (2143) transport: Slave chip Id[12]
94+
I (2153) transport: raw_tp_dir[-], flow_ctrl: low[60] high[80]
95+
I (2153) transport: transport_delayed_init
96+
I (2163) esp_cli: Remove any existing deep_sleep cmd in cli
97+
I (2163) esp_cli: Registering command: crash
98+
I (2173) esp_cli: Registering command: reboot
99+
I (2173) esp_cli: Registering command: mem-dump
100+
I (2173) esp_cli: Registering command: task-dump
101+
I (2183) esp_cli: Registering command: cpu-dump
102+
I (2183) esp_cli: Registering command: heap-trace
103+
I (2193) esp_cli: Registering command: sock-dump
104+
I (2193) esp_cli: Registering command: host-power-save
105+
I (2203) hci_stub_drv: Host BT Support: Disabled
106+
I (2203) H_SDIO_DRV: Received INIT event
107+
I (2203) H_SDIO_DRV: Event type: 0x22
108+
I (2213) H_SDIO_DRV: Write thread started
109+
I (2643) example: Initializing SD card
110+
I (2643) example: Using SDMMC peripheral
111+
W (2643) ldo: The voltage value 0 is out of the recommended range [500, 2700]
112+
I (2643) RPC_WRAP: ESP Event: wifi station started
113+
I (2653) RPC_WRAP: ESP Event: wifi station started
114+
I (2743) example: Mounting filesystem
115+
I (2743) sdmmc_periph: sdmmc_host_init: SDMMC host already initialized, skipping init flow
116+
I (2953) example: Filesystem mounted
117+
I (2953) example: Doing Wi-Fi Scan
118+
I (3653) rpc_req: Scan start Req
119+
120+
W (3653) rpc_rsp: Hosted RPC_Resp [0x21a], uid [4], resp code [12298]
121+
I (6183) RPC_WRAP: ESP Event: StaScanDone
122+
I (6203) example: Total APs scanned = 11, actual AP number ap_info holds = 10
123+
Name: SD16G
124+
Type: SDHC
125+
Speed: 40.00 MHz (limit: 40.00 MHz)
126+
Size: 14868MB
127+
CSD: ver=2, sector_size=512, capacity=30449664 read_bl_len=9
128+
SSR: bus_width=4
129+
I (6213) example: Opening file /sdcard/hello.txt
130+
I (6303) example: File written
131+
I (6323) example: Renaming file /sdcard/hello.txt to /sdcard/foo.txt
132+
I (6333) example: Reading file /sdcard/foo.txt
133+
I (6333) example: Read from file: 'Hello SD16G!'
134+
I (6333) example: Opening file /sdcard/nihao.txt
135+
I (6353) example: File written
136+
I (6353) example: Reading file /sdcard/nihao.txt
137+
I (6363) example: Read from file: 'Nihao SD16G!'
138+
I (6363) example: Card unmounted
139+
I (6363) example: Doing another Wi-Fi Scan
140+
I (6363) rpc_req: Scan start Req
141+
142+
I (8863) RPC_WRAP: ESP Event: StaScanDone
143+
I (8883) example: Total APs scanned = 11, actual AP number ap_info holds = 10
144+
I (8883) main_task: Returned from app_main()
145+
146+
Done
147+
```
148+
149+
## Troubleshooting
150+
151+
### Card fails to initialize with `sdmmc_init_sd_scr: send_scr (1) returned 0x107` error
152+
153+
Check connections between the card and the ESP32. For example, if you have disconnected GPIO2 to work around the flashing issue, connect it back and reset the ESP32 (using a button on the development board, or by pressing Ctrl-T Ctrl-R in IDF Monitor).
154+
155+
### Card fails to initialize with `sdmmc_check_scr: send_scr returned 0xffffffff` error
156+
157+
Connections between the card and the ESP32 are too long for the frequency used. Try using shorter connections, or try reducing the clock speed of SD interface.
158+
159+
### Failure to mount filesystem
160+
161+
```
162+
example: Failed to mount filesystem.
163+
```
164+
The example will be able to mount only cards formatted using FAT32 filesystem.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set(srcs "sd_card_example_main.c"
2+
"sd_card_functions.c"
3+
"esp_hosted_wifi.c")
4+
5+
idf_component_register(SRCS ${srcs}
6+
INCLUDE_DIRS "."
7+
REQUIRES fatfs esp_wifi
8+
WHOLE_ARCHIVE)
9+
10+
if(NOT CONFIG_SOC_SDMMC_HOST_SUPPORTED)
11+
fail_at_build_time(sdmmc ""
12+
"Only ESP32, ESP32-S3, ESP32-P4 targets are supported."
13+
"Please refer README.md for more details")
14+
endif()
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
menu "SD/MMC Example Configuration"
2+
3+
config EXAMPLE_SDMMC_SLOT_RANGE_MAX
4+
int
5+
default 0 if IDF_TARGET_ESP32P4
6+
default 1 if IDF_TARGET_ESP32S3
7+
8+
config EXAMPLE_SDMMC_SLOT
9+
int "SD/MMC slot to use"
10+
default 0
11+
range 0 EXAMPLE_SDMMC_SLOT_RANGE_MAX
12+
help
13+
Select the slot to use
14+
For ESP32-P4, only Slot 0 for SD Card is available as ESP-Hosted uses Slot 1
15+
16+
choice EXAMPLE_SDMMC_BUS_WIDTH
17+
prompt "SD/MMC bus width"
18+
default EXAMPLE_SDMMC_BUS_WIDTH_4
19+
help
20+
Select the bus width of SD or MMC interface.
21+
Note that even if 1 line mode is used, D3 pin of the SD card must have a pull-up resistor connected.
22+
Otherwise the card may enter SPI mode, the only way to recover from which is to cycle power to the card.
23+
24+
config EXAMPLE_SDMMC_BUS_WIDTH_4
25+
bool "4 lines (D0 - D3)"
26+
27+
config EXAMPLE_SDMMC_BUS_WIDTH_1
28+
bool "1 line (D0)"
29+
endchoice
30+
31+
config EXAMPLE_PIN_CARD_POWER_RESET
32+
int
33+
depends on SOC_SDMMC_IO_POWER_EXTERNAL
34+
prompt "Card Power Reset"
35+
default 45
36+
37+
config EXAMPLE_PIN_CMD
38+
int
39+
prompt "CMD GPIO number" if SOC_SDMMC_USE_GPIO_MATRIX
40+
default 35 if IDF_TARGET_ESP32S3
41+
range 44 44 if IDF_TARGET_ESP32P4
42+
43+
config EXAMPLE_PIN_CLK
44+
int
45+
prompt "CLK GPIO number" if SOC_SDMMC_USE_GPIO_MATRIX
46+
default 36 if IDF_TARGET_ESP32S3
47+
range 43 43 if IDF_TARGET_ESP32P4
48+
49+
config EXAMPLE_PIN_D0
50+
int
51+
prompt "D0 GPIO number" if SOC_SDMMC_USE_GPIO_MATRIX
52+
default 37 if IDF_TARGET_ESP32S3
53+
range 39 39 if IDF_TARGET_ESP32P4
54+
55+
if EXAMPLE_SDMMC_BUS_WIDTH_4
56+
57+
config EXAMPLE_PIN_D1
58+
int
59+
prompt "D1 GPIO number" if SOC_SDMMC_USE_GPIO_MATRIX
60+
default 38 if IDF_TARGET_ESP32S3
61+
range 40 40 if IDF_TARGET_ESP32P4
62+
63+
config EXAMPLE_PIN_D2
64+
int
65+
prompt "D2 GPIO number" if SOC_SDMMC_USE_GPIO_MATRIX
66+
default 33 if IDF_TARGET_ESP32S3
67+
range 41 41 if IDF_TARGET_ESP32P4
68+
69+
config EXAMPLE_PIN_D3
70+
int
71+
prompt "D3 GPIO number" if SOC_SDMMC_USE_GPIO_MATRIX
72+
default 34 if IDF_TARGET_ESP32S3
73+
range 42 42 if IDF_TARGET_ESP32P4
74+
75+
endif # EXAMPLE_SDMMC_BUS_WIDTH_4
76+
77+
config EXAMPLE_SD_PWR_CTRL_LDO_INTERNAL_IO
78+
depends on SOC_SDMMC_IO_POWER_EXTERNAL
79+
bool "SD power supply comes from internal LDO IO (READ HELP!)"
80+
default y
81+
help
82+
Only needed when the SD card is connected to specific IO pins which can be used for high-speed SDMMC.
83+
Please read the schematic first and check if the SD VDD is connected to any internal LDO output.
84+
Unselect this option if the SD card is powered by an external power supply.
85+
86+
config EXAMPLE_SD_PWR_CTRL_LDO_IO_ID
87+
depends on SOC_SDMMC_IO_POWER_EXTERNAL && EXAMPLE_SD_PWR_CTRL_LDO_INTERNAL_IO
88+
int "LDO ID"
89+
default 4 if IDF_TARGET_ESP32P4
90+
help
91+
Please read the schematic first and input your LDO ID.
92+
endmenu
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
/**
7+
* ESP-Hosted Wi-Fi Functions for SD Card Example
8+
*/
9+
#include <string.h>
10+
11+
#include "esp_check.h"
12+
#include "esp_log.h"
13+
#include "esp_wifi.h"
14+
15+
#include "esp_hosted_wifi.h"
16+
17+
static const char *TAG = "sd_card_wifi";
18+
19+
void init_wifi(void)
20+
{
21+
ESP_ERROR_CHECK(esp_netif_init());
22+
ESP_ERROR_CHECK(esp_event_loop_create_default());
23+
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
24+
assert(sta_netif);
25+
26+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
27+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
28+
29+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
30+
ESP_ERROR_CHECK(esp_wifi_start());
31+
}
32+
33+
void do_wifi_scan(void)
34+
{
35+
uint16_t number = 10;
36+
wifi_ap_record_t ap_info[number];
37+
uint16_t ap_count = 0;
38+
memset(ap_info, 0, sizeof(ap_info));
39+
40+
esp_wifi_scan_start(NULL, true);
41+
42+
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
43+
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
44+
ESP_LOGI(TAG, "Total APs scanned = %u, actual AP number ap_info holds = %u", ap_count, number);
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
#ifndef __ESP_HOSTED_WIFI_H__
7+
#define __ESP_HOSTED_WIFI_H__
8+
9+
void init_wifi(void);
10+
void do_wifi_scan(void);
11+
12+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dependencies:
2+
espressif/esp_wifi_remote:
3+
version: "*"
4+
rules:
5+
- if: "target in [esp32p4]"
6+
espressif/esp_hosted:
7+
version: "*"
8+
rules:
9+
- if: "target in [esp32p4]"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
#ifndef __SD_CARD_EXAMPLE_COMMON_H__
7+
#define __SD_CARD_EXAMPLE_COMMON_H__
8+
9+
#define EXAMPLE_MAX_CHAR_SIZE 64
10+
11+
// enable this to perform Wi-Fi functions in example
12+
#define EXAMPLE_USE_WIFI 1
13+
14+
#endif

0 commit comments

Comments
 (0)