ULP execution flow

jeromeh
Posts: 31
Joined: Thu Dec 22, 2016 5:41 am

ULP execution flow

Postby jeromeh » Tue Jan 24, 2017 10:50 am

Hi,

I'm working on a project to collect sensor data from SPI through ULP. My intension is to implement bit-bang logic according to the interface requirement. The way I'm thinking about is like below:
1. create a ulp_main, wrap the code calling ULP instructions, and enter deep_sleep after that.
2. main function will call ulp_main() right after reset
3. Implement a deep sleep stub to decide if really need to wake up main MCU.

Code: Select all

void ulp_main(){
	const ulp_insn_t program[] = {
		I_MOVI(R3, 16),         // R3 <- 16
		.....  // toggle IOs here
		I_END(0)
	};
	size_t load_addr = 0;
	size_t size = sizeof(program)/sizeof(ulp_insn_t);
	ulp_process_macros_and_load(load_addr, program, &size);
	ulp_run(load_addr);
	
    esp_deep_sleep_enable_timer_wakeup(TIMER_INTERVAL0_SEC*1000000);
    esp_deep_sleep_start();
}

void RTC_IRAM_ATTR esp_wake_deep_sleep(void) {
    esp_default_wake_deep_sleep();
    // TODO .... check if really wake up main CPU here, goto deep sleep again if not
}

void app_main(void)
{
    nvs_flash_init();
    ulp_main();
}
This is actually a typical low power sensor sampling case, I don't see a complete example in esp-idf, so I have to come up with a way from what I understood so far. But now I run into an issue:

the IO toggeling being implemented in ulp_main, was excuted repeatedly (around every 4ms), though I set the timer TIMER_INTERVAL0_SEC=1.5 second, I had expect this should be executed only once every TIMER_INTERVAL0_SEC time. But it seems some logic controls the execution of ulp processor, which is unknown to me. Please help!

any comments will be welcome, thansks!

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

Re: ULP execution flow

Postby WiFive » Tue Jan 24, 2017 12:02 pm

I think what you want to do is loop and sleep within the ulp program itself and then have the ulp trigger the wakeup if your condition is met.

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

Re: ULP execution flow

Postby ESP_igrr » Tue Jan 24, 2017 2:34 pm

esp_deep_sleep_enable_timer_wakeup will cause the main CPU to wake up after given amount of time. This doesn't affect ULP behaviour.

jeromeh
Posts: 31
Joined: Thu Dec 22, 2016 5:41 am

Re: ULP execution flow

Postby jeromeh » Wed Jan 25, 2017 1:40 am

WiFive wrote:I think what you want to do is loop and sleep within the ulp program itself and then have the ulp trigger the wakeup if your condition is met.
Yes, that's right. Following question is:
1. How to sleep within a ulp program?
2. How to wake up a ulp program when it's in sleep mode? By specify a certain amount of cycles or wait for some other events?

jeromeh
Posts: 31
Joined: Thu Dec 22, 2016 5:41 am

Re: ULP execution flow

Postby jeromeh » Wed Jan 25, 2017 1:45 am

ESP_igrr wrote:esp_deep_sleep_enable_timer_wakeup will cause the main CPU to wake up after given amount of time. This doesn't affect ULP behaviour.
Yes. that's also my understanding. But the problem I cannot explain is that: every time when main MCU is wake up(the period I set here is ~1.5 sec), it should execute the ulp program only once, but my observation is ulp code was executed repeatedly(every ~4ms).

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

Re: ULP execution flow

Postby ESP_igrr » Wed Jan 25, 2017 2:51 am

ULP program starts executing in the background once you call ulp_run. It gets started by a timer (which is not related to the timer used to wake up main CPU from deep sleep).

ULP timer period is controlled using SENS_ULP_CP_SLEEP_CYCx_REG, x=0..4.

There is a sleep instruction in the ULP which sends it into sleep mode, and sets up the timer to start ULP again. This instruction gets 'x' as an argument, and sets up the wake up time to the value of SENS_ULP_CP_SLEEP_CYCx_REG. After this instruction ULP goes into sleep mode again until timer wakes it up next time.

Now, it seems like there may be a bug there, as I just did a test and I see that the wakeup time is not accurate (i.e not the time that is written into the corresponding SENS_ULP_CP_SLEEP_CYCx register). I'll look into this...

jeromeh
Posts: 31
Joined: Thu Dec 22, 2016 5:41 am

Re: ULP execution flow

Postby jeromeh » Wed Jan 25, 2017 3:15 am

Thanks ESP_igrr for explaining the concept of ULP timer and sleep instruction, which is really great design and help clearify a lot of puzzles.

Is there any official(non-official is also OK) documents describing the register details? With a simple example can be event better. In terms of the accuracy of the timer, it should be OK for me as long as it's linear to the reg value being programmed and consistent.

Again, thanks for you notice on this, and waiting for your next update:)

jeromeh
Posts: 31
Joined: Thu Dec 22, 2016 5:41 am

Re: ULP execution flow

Postby jeromeh » Mon Jan 30, 2017 2:42 pm

Any update on this topic? @ESP_igrr

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

Re: ULP execution flow

Postby WiFive » Mon Jan 30, 2017 11:42 pm

Probably no updates until next week due to holiday.

jeromeh
Posts: 31
Joined: Thu Dec 22, 2016 5:41 am

Re: ULP execution flow

Postby jeromeh » Fri Feb 03, 2017 5:22 am

One follow up question:
1. how to wake-up main MCU from ULP?

I see there is a I_END(wakeup) instruction in ULP document, but will that terminate the ULP program execution? In my case, I want to let ULP program keep running, and only signal main MCU when certain condition is met, is that possible?

Who is online

Users browsing this forum: Bing [Bot] and 96 guests