SD card on GPIO7..to GPIO11 of ESP-WROOM-32

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby davdav » Tue Mar 06, 2018 2:10 pm

Hi everybody,

We have designed our product putting a 4-line SD card on pins 17-22 of ESP-WROOM-32 (GPIO6-GPIO11).
In previous edition of datasheet (see attachment) there were no mention to avoid those pins for SD card (and in general GPIO).

Now we are going to use the SD card with this code:

Code: Select all

	ESP_LOGD(TAG_SD, "Using SDMMC peripheral");
	sdmmc_host_t host = SDMMC_HOST_DEFAULT();
	host.slot = SDMMC_HOST_SLOT_0;

	// This initializes the slot without card detect (CD) and write protect (WP) signals.
	// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
	sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();

	slot_config.gpio_cd = GPIO_NUM_34;
	slot_config.width = 4;

	// GPIOs 7, 8, 9, 10, 11 should have external 10k pull-ups.
	// Internal pull-ups are not sufficient. However, enabling internal pull-ups
	// does make a difference some boards, so we do that here.

	gpio_set_pull_mode(GPIO_NUM_7, GPIO_PULLUP_ONLY);   // D0, needed in 4- and 1-line modes
	gpio_set_pull_mode(GPIO_NUM_8, GPIO_PULLUP_ONLY);   // D1, needed in 4-line mode only
	gpio_set_pull_mode(GPIO_NUM_9, GPIO_PULLUP_ONLY);   // D2, needed in 4-line mode only
	gpio_set_pull_mode(GPIO_NUM_10, GPIO_PULLUP_ONLY);	// D3, needed in 4- and 1-line modes
	gpio_set_pull_mode(GPIO_NUM_11, GPIO_PULLUP_ONLY);  // CMD, needed in 4- and 1- line modes

	gpio_set_pull_mode(GPIO_NUM_34, GPIO_PULLUP_ONLY);  // CD

	// Options for mounting the filesystem.
	// If format_if_mount_failed is set to true, SD card will be partitioned and
	// formatted in case when mounting fails.
	esp_vfs_fat_sdmmc_mount_config_t mount_config = {
		.format_if_mount_failed = false,
		.max_files = 5,
		.allocation_unit_size = 16 * 1024
	};

	// Use settings defined above to initialize SD card and mount FAT filesystem.
	// Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
	// Please check its source code and implement error recovery when developing
	// production applications.
	sdmmc_card_t* card;
	esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);

	if (ret != ESP_OK) {
		if (ret == ESP_FAIL) {
			ESP_LOGD(TAG_SD, "Failed to mount filesystem. "
				"If you want the card to be formatted, set format_if_mount_failed = true.");
		} else {
			ESP_LOGD(TAG_SD, "Failed to initialize the card (%d). "
				"Make sure SD card lines have pull-up resistors in place.", ret);
		}
	}
But it crashes. Is it the code right to use SD card on those pins?

We have also checked latest datasheet and it says to not use GPIO6 to GPIO11.. How to solve that? If we cannot use those pins is a big trouble.

Thanks.
Attachments
esp_wroom_32_datasheet_en.pdf
(274.36 KiB) Downloaded 1462 times

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby ESP_igrr » Tue Mar 06, 2018 3:42 pm

These pins are being used to connect the flash chip inside ESP-WROOM32. Since the flash chip is used for code execution, ESP32 may access the flash chip at any time. SD card does not know about this, so it may interfere with the SPI flash access, preventing ESP32 from fetching instructions, causing it to crash.
ESP32 can support an SD card on the above mentioned pins, but for this case the flash chip needs to be connected to different pins (for example, HSPI pins). So if you have an option to connect an external flash chip to other GPIOs, and then program Efuses so that ESP32 will use these GPIOs for the flash chip, then SD card on gpios 7-11 will work.

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby davdav » Tue Mar 06, 2018 4:15 pm

Thanks ESP_igrr, it is clear.
It is frustrating that documentation (as per document I attached in previous post) didn't mention this condition.

In practice we have to re-design the product..and also consider to have 6 pins less on the ESP-WROOM32 module.

To get a solution, what if we use a 1-wire mode for SDcard these pins?

- pin13 (GPIO14) for SD_CLK
- pin23 (GPIO15) for SD_CMD
- pin24 (GPIO2) for SD_DATA0

Do you think is it OK? Or do you suggest other pins to use?

Thanks

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby ESP_igrr » Tue Mar 06, 2018 4:57 pm

These three pins you mentioned are compatible with the SD card, so that would work.

I'm thinking now that there might be a terrible hack to support SD card on the SPI flash pins, if you don't care about read/write performance. Basically, all the code we need to run a single SD command is placed into IRAM, and we disable flash cache for the duration of the command. Something like this: https://gist.github.com/igrr/677184a0bf ... 3306be7f2a
Not verified whether this actually works. It will certainly crash if the SDMMC driver tries to log anything during the transaction, but in theory it should work if no logging happens. I'll get some hardware built and will check if this works in the next few days.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby WiFive » Tue Mar 06, 2018 6:20 pm

ESP_igrr wrote:I'm thinking now that there might be a terrible hack to support SD card on the SPI flash pins, if you don't care about read/write performance. Basically, all the code we need to run a single SD command is placed into IRAM, and we disable flash cache for the duration of the command. Something like this: https://gist.github.com/igrr/677184a0bf ... 3306be7f2a
Not verified whether this actually works. It will certainly crash if the SDMMC driver tries to log anything during the transaction, but in theory it should work if no logging happens. I'll get some hardware built and will check if this works in the next few days.
I half-tried this a long time ago and did not get it to work. I think there is also the matter of saving/restoring pin state between transactions.

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby davdav » Wed Mar 07, 2018 9:44 am

Thanks ESP_igrr.

I think we are not going to do this hack: too risky for us..

I'm going to test the SD card on the pin I proposed in last post and see if it works.

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby davdav » Wed Mar 07, 2018 11:16 am

@ESP_igrr,

I have checked that with my 1-wire SD card configuration (S_CLK to GPIO14, SD_CMD to GPIO15, SD_DATA0 to GPIO2), it works.

However, this open another issue..the use of GPIO2.

Since external pullup is required for SD_DATA0 (GPIO2) in order to work with SDcard, it confilcts with the download mode (which is achieved with GPIO0=0 and GPIO2=0). Infact I got this error.

Code: Select all

rst:0x10 (RTCWDT_RTC_RESET),boot:0xb (HSPI_FLASH_BOOT)
flash read err, 1000
ets_main.c 371 
Since in production we have to download at least the very first time the firmware on ESP-WROOM-32, this imply we have to provide two jumpers (one for GPIO0 and one for GPIO2) for downloading (we can't use buttons for cost reasons).

My question: is it possible to remap SD_DATA0 to another GPIO? For example, can I use SD_DATA1 (GPIO4) as 1-wire data signal for SD card? how to do?

Thanks.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby WiFive » Wed Mar 07, 2018 7:20 pm

No, but you could use another gpio to "activate" pull-up after boot

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby davdav » Thu Mar 08, 2018 8:02 am

@WiFive, this is not an option for two reasons:

-we are run out of pins since GPIO6-GPIO11 are not available..
-sd card needs an external pullup of 10Kohm as reported in sdcard example adn also as normal practice. Internal pull-up of another GPIO is not sufficient.

Thanks.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: SD card on GPIO7..to GPIO11 of ESP-WROOM-32

Postby WiFive » Thu Mar 08, 2018 9:33 am

Yes you could use the other gpio to drive the external pullup or use a fet. Otherwise use an external delay circuit or use the card detect switch. If you are using a pogo jig in production just put a pogo pad for gpio 2.

Who is online

Users browsing this forum: No registered users and 101 guests