Function call from flash vs. IRAM

daniSi
Posts: 47
Joined: Thu Dec 23, 2021 9:43 am

Function call from flash vs. IRAM

Postby daniSi » Wed Apr 13, 2022 6:26 am

Dear,

I have tried to compare the performance when a function is called from flash and then when its called from the IRAM. The idea was to set a pin high before the function call and then resetting to zero once the function is entered. The pulse width should approximately reveal the time, needed to load the function.

The code is as following:

Code: Select all

void IRAM_ATTR test_iram(void)
{
	gpio_set_level(TEST_IRAM_PIN, 0);

	int a = 0;

	do{
		a++;
	}while(a<5000);

	do{
		a--;
	}while(a>0);
}

void test_flash(void)
{
	gpio_set_level(TEST_FLASH_PIN, 0);

	int a = 0;

	do{
		a++;
	}while(a<5000);

	do{
		a--;
	}while(a>0);
}

void app_main(void)
{
	gpio_set_direction(TEST_IRAM_PIN, GPIO_MODE_OUTPUT);
	gpio_set_direction(TEST_FLASH_PIN, GPIO_MODE_OUTPUT);

	while(1){

		for(int i=0; i < 10; i++){
			gpio_set_level(TEST_IRAM_PIN, 1);
			test_iram();
		}


		for(int i=0; i < 10; i++){
			gpio_set_level(TEST_FLASH_PIN, 1);
			test_flash();
		}
			vTaskDelay(pdMS_TO_TICKS(10));
	}
}
Results:
The function in the flash first takes time after boot t_flash_first_exe=2.26us, after this the execution lowers to t_flash_normal=0.34us. I guess the reason is that since there are no other function its still in the cache and therefore needs from then on lower execution time? The IRAM function takes after first boot t_iram_first_exe=3.28us, after this the execution lowers to t_iram_normal=0.56us.
First, why the flash function takes always less time then the one called out of iram? Its because its after the first call already in the cache and the iram function is always called from the IRAM? Second, why then there is even a difference in the execution time of the IRAM function if it should be already be stored in the IRAM at boot. Is it because there are also used the gpio functions which are not stored in the iram(i guess), but why then anyway the time is still higher at boot compared to the flash function?

regards

ESP_Sprite
Posts: 9770
Joined: Thu Nov 26, 2015 4:08 am

Re: Function call from flash vs. IRAM

Postby ESP_Sprite » Wed Apr 13, 2022 3:01 pm

You possibly also want to put your main function in iram.

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: Function call from flash vs. IRAM

Postby chegewara » Wed Apr 13, 2022 8:38 pm

I am suggesting to add small delay between gpio_set_level (2 times) and then deduct that value. gpio_set_level cant handle that quick switching and this may impact the results. Also you may want to increase while loops count.

Who is online

Users browsing this forum: No registered users and 100 guests