GPIO13 overridden by SD card... which we desperately need

Resch2061
Posts: 40
Joined: Mon May 01, 2017 1:56 pm

GPIO13 overridden by SD card... which we desperately need

Postby Resch2061 » Thu Sep 21, 2017 11:51 am

Hello,

As it appears, GPIO13 is overridden by the SD card library, even when it appears to not be used. I desperately need GPIO13 to reset a device, but since the SD card library is keeping it high, I can't!

I'm starting to panic a little, and I'm running out of ideas...
And no, sadly we cannot reassign the pin - we're already using a lot of the pins, and we desperately need both this pin and the SD card! I can't enable it before resetting the device either, since initializing the SD card takes seconds... If I were to reset the device and then follow by initializing the SD card, the device would have rebooted entirely. The protocol I'm attempting to use is extremely dependant on precise timing and such, so that's an option that sadly wouldn't work.

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

Re: GPIO13 overridden by SD card... which we desperately need

Postby WiFive » Thu Sep 21, 2017 7:01 pm

You are using 1-bit mode? Are you sure this isn't because the pin is configured as a jtag pin at boot and you have to reconfigure it as a gpio before you use it?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: GPIO13 overridden by SD card... which we desperately need

Postby ESP_Angus » Fri Sep 22, 2017 12:25 am

As WiFive says, if you configure slot_config->width to 1 before calling sdmmc_host_init_slot() then the sdmmc host driver shouldn't touch GPIO13 at all. Can you please share some of the code you're using to initialise the SD card driver?

Resch2061
Posts: 40
Joined: Mon May 01, 2017 1:56 pm

Re: GPIO13 overridden by SD card... which we desperately need

Postby Resch2061 » Fri Sep 22, 2017 2:02 pm

Funny, I thought I was using 1-bit mode because I set the flag... I wasn't aware of the width variable. So that part appears to work now. The code is exactly the same as the SD card example, just with the width variable set to 1 now. Thanks!

However, I'm currently facing another issue... We use GPIO16 and GPIO34 as the UART2 TXD and RXD respectively. If I use a scope to read both these pins, I can see the TXD line correctly goes up and down like I would expect. However, RXD does nothing... And I'm quite certain the device responds on the receiving side, it's just that the SD card functions appear to use GPIO34 as well, because when I disable the SD card, everything works as expected. For reference, I'm using the STK500v2 protocol to flash the AVR on the other side. This works correctly when I comment the SD card initializing.

I've been looking through your code for a bit, but I can't quite figure out why this is happening...

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

Re: GPIO13 overridden by SD card... which we desperately need

Postby WiFive » Fri Sep 22, 2017 9:01 pm

Gpio 34 is an input only so I don't think it could drive the line to affect a scope trace.

llewellyn
Posts: 33
Joined: Fri May 05, 2017 8:22 am

Re: GPIO13 overridden by SD card... which we desperately need

Postby llewellyn » Sun Sep 24, 2017 5:09 pm

HI,

Even GPIO4 is controlled by SD library in spite of using 1-bit mode.

Regards

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

Re: GPIO13 overridden by SD card... which we desperately need

Postby ESP_igrr » Sun Sep 24, 2017 6:17 pm

You can force the SD slot to be initialized in 1-bit mode (so that GPIO4 is not used by the peripheral):

Code: Select all

    sdmmc_host_t host = SDMMC_HOST_DEFAULT();
    // this tells SD host to keep using 1-line bus mode
    host.flags = SDMMC_HOST_FLAG_1BIT;

    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
    // this tells the driver to only initialize 1 data line
    slot_config.width = 1;

Resch2061
Posts: 40
Joined: Mon May 01, 2017 1:56 pm

Re: GPIO13 overridden by SD card... which we desperately need

Postby Resch2061 » Mon Sep 25, 2017 9:45 am

WiFive wrote:Gpio 34 is an input only so I don't think it could drive the line to affect a scope trace.
That's why I find it strange that not enabling the SD card allows me to do what I need to do, while enabling it breaks it. I've done some digging in the documentations and source code, and I can't find any reference to GPIO34 anywhere (sdmmc_host.h and .c, sdmmc_cmd, and any other file related in this stack trace), as far as the SD card is concerned... I also looked at the IO MUX definitions. So why this happens is completely beyond me.
ESP_igrr wrote:You can force the SD slot to be initialized in 1-bit mode (so that GPIO4 is not used by the peripheral):

Code: Select all

    sdmmc_host_t host = SDMMC_HOST_DEFAULT();
    // this tells SD host to keep using 1-line bus mode
    host.flags = SDMMC_HOST_FLAG_1BIT;

    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
    // this tells the driver to only initialize 1 data line
    slot_config.width = 1;
Yes, this is what I did, which works. Thanks again for the help!

Resch2061
Posts: 40
Joined: Mon May 01, 2017 1:56 pm

Re: GPIO13 overridden by SD card... which we desperately need

Postby Resch2061 » Mon Sep 25, 2017 1:32 pm

Code: Select all

 int pins[3][4] = {
        { JOS_UART0_TXD_PIN, JOS_UART0_RXD_PIN, JOS_UART0_RTS_PIN, JOS_UART0_CTS_PIN },
        { JOS_UART1_TXD_PIN, JOS_UART1_RXD_PIN, JOS_UART1_RTS_PIN, JOS_UART1_CTS_PIN },
        { JOS_UART2_TXD_PIN, JOS_UART2_RXD_PIN, JOS_UART2_RTS_PIN, JOS_UART2_CTS_PIN },
    };

    uart_config_t config = {
        .baud_rate = JOS_UART_BAUDRATE,
        .data_bits = JOS_UART_WORD_LENGTH,
        .parity = JOS_UART_PARITY,
        .stop_bits = JOS_UART_STOP_BITS,
        .flow_ctrl = JOS_UART_FLOWCONTROL
    };

    uart_port_t port = 2;

    ESP_LOGI(TAG, "Initializing.");
    ESP_LOGI(TAG, "UART%d: TXD:%d RXD:%d RTS:%d CTS:%d", port, pins[port][0], pins[port][1], pins[port][2], pins[port][3]);

    // port, tx, rx, rts, cts
    uart_set_pin(port, pins[port][0], pins[port][1], UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    uart_param_config(port, &config);
    uart_driver_install(port, _buffer_size * 2, 0, 0, NULL, 0);
JOS_UART2_TXD_PIN and JOS_UART2_RXD_PIN are header-defined, which are 16 and 34 respectively. As you can see, only those two are currently being used - we've temporarily cut the lines for the others, and we don't even use CTS and RTS because we need those pins.

Code: Select all

int get_uart(uart_port_t port, uint8_t *data, int timeout)
{
	int ticks = 0;
	int ret = -1;
	int delay = 50 / portTICK_RATE_MS;
	while (ticks <= timeout)
	{
		int len = uart_read_bytes(port, data, _buffer_size, delay);
		//ESP_LOGD(TAG, "%d ticks: %d len", ticks, len);
		if (len > 0)
		{
			ESP_LOGD(TAG, "%d bytes received on UART%d", len, port);
			if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE)
				hexdump((char*)data, len, 40);
			ret = len;
			return ret;
		}
		/*if (!_uart_buff_handled && _data_len > 0)
		{
			_uart_buff_handled = 1;
			memcpy(data, _data, _data_len);
			return _data_len;
		}*/
		ticks += delay;
	}
	return -1;
}
This all looks like one would expect...

Code: Select all

int length = get_uart(port, readbytes, 1000);
	if (length == -1)
	{
		ESP_LOGW(TAG, "No response received!");
		return -1;
	}
Finally this is where the UART actually gets used. The -1 check goes through when I disable a single line of code... Which is this, in main:

Code: Select all

sd_card_enable();
Here is the code for initializing the SD card:

Code: Select all

void sd_card_enable() {
	gpio_pulldown_dis(GPIO_NUM_2);
	gpio_pullup_en(GPIO_NUM_2);
	WAIT(250);
	ESP_LOGI(TAG, "Enabling SD card...");
	//sdmmc_slot_config_t conf;

	sdmmc_host_t _sd_host = SDMMC_HOST_DEFAULT();
	// 1-line SD mode
	_sd_host.flags = SDMMC_HOST_FLAG_1BIT;

	// 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.width = 1;

	// 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 = true,
		.max_files = 5
	};

	// 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", &_sd_host, &slot_config, &mount_config, &card);
	if (ret != ESP_OK) {
		if (ret == ESP_FAIL) {
			ESP_LOGE(TAG, "Failed to mount filesystem. If you want the card to be formatted, set format_if_mount_failed = true.");
		} else {
			ESP_LOGE(TAG, "Failed to initialize the card (%d). Make sure SD card lines have pull-up resistors in place.", ret);
		}
		return;
	}

	// Card has been initialized, print its properties
	ESP_LOGI(TAG, "SD card enabled.");
	if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE)
	{
		ESP_LOGV(TAG, "Printing SD card properties...");
		sdmmc_card_print_info(stdout, card);
	}
}

Resch2061
Posts: 40
Joined: Mon May 01, 2017 1:56 pm

Re: GPIO13 overridden by SD card... which we desperately need

Postby Resch2061 » Wed Sep 27, 2017 12:42 pm

Turns out the issue I'm running into is not caused by the SD card.

Who is online

Users browsing this forum: No registered users and 67 guests