Why can MicroPython not use the ULP WAKE instruction

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
ellisjr
Posts: 24
Joined: Sun May 17, 2020 8:30 pm

Why can MicroPython not use the ULP WAKE instruction

Post by ellisjr » Sun Nov 29, 2020 7:39 pm

Apologies before we start - complete NOOB here... :mrgreen:

I'm very excited by Thomas Waldmann's MicroPython ULP assembler and I've been trying to use it because I want to program in Python but I need low power consumption while waiting for a trigger situation.

So, all I want to do is read the ADC and WAKE the main cores if certain values are found, but all that seems available is esp32.wake_on_touch(wake), esp32.wake_on_ext0(pin, level) and esp32.wake_on_ext1(pin, level), none of which suit my purpose. Can anyone tell me why there is not a esp32.wake_on_wake() function? It would seem the easiest of all!

Here us my ULP program:
data: .long 0
entry: move r3, data # load address of data into r3
move r2, 0 # initialise r2 to sum 4 results
adc r0, 0, 7 ## use adc on pin 34 into r1 Ch6 plus one
move r1, r2
add r2, r1, r0 # add in adc val in r1
adc r0, 0, 7 ## use adc on pin 34 into r1 Ch6 plus one
move r1, r2
add r2, r1, r0 # add in adc val in r1
adc r0, 0, 7 ## use adc on pin 34 into r1 Ch6 plus one
move r1, r2
add r2, r1, r0 # add in adc val in r1
adc r0, 0, 7 ## use adc on pin 34 into r1 Ch6 plus one
move r1, r2
add r2, r1, r0 # add in adc val in r1
rsh r2, r2, 0x02 # shift right twice to divide by 4
st r2, r3, 0 # store r2 contents into data ([r3+0]) to be picked up by MP
exit: HALT // Stop the ULP program

This just gets the average value over 4 samples from the ADC and stores it ready to be picked up by MP which is in deepsleep for 30 secs but I want to run the ULP in a loop (or on the ULP timer) and use a ulp WAKE instruction to return to MicroPython when required.

I have considered using IO pins to trigger WAKE, but so far the ulp REG_RD instruction to read RTCIO is unfathomable to me and Thomas doesn't yet support the macros that can be used for simple pin IO so, please, if anybody knows about any of this, please chime in!

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Why can MicroPython not use the ULP WAKE instruction

Post by mattyt » Mon Nov 30, 2020 12:59 am

The ULP is a super interesting component of the ESP32 - and Thomas' library is fantastic. Glad to see people interested in this slightly-odd peripheral!

This is a guess but I'd place a small bet that it wasn't implemented because it didn't work on rev 0 and 1 versions of the ESP32 - and I think the MicroPython implementation may have arrived before ULP wakeup was stable.

It looks to be a relatively straight-forward addition (you can see how the other wake_on methods work in modesp32.c), though we should check that we're not running on rev 0/1 versions of the ESP32. Want to give it a go? I'll review... :)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Why can MicroPython not use the ULP WAKE instruction

Post by pythoncoder » Mon Nov 30, 2020 2:53 pm

@ellisjr I'm even more of a n00b on the ULP never having tried it but this example seems to imply periodic wakeup. Apologies in advance if I've got the wrong end of the stick ;)
Peter Hinch
Index to my micropython libraries.

ellisjr
Posts: 24
Joined: Sun May 17, 2020 8:30 pm

Re: Why can MicroPython not use the ULP WAKE instruction

Post by ellisjr » Tue Dec 01, 2020 8:14 pm

@mattyt Yes, I'll give it a go. However, my first problem seems to be that all of my 5 ESP32 boards have revision 1 ESP32s, even though they were bought recently. This is as reported by Arduino upload: Chip is ESP32D0WDQ6 (revision 1)

So, that's a bit disappointing given that I only bought them recently. I guess I need a Revision V3, which is where Espressif seem to have gone after Revision 1. But who sells a dev board with V3s on?

I guess I'll take a look at the code while I wait to source a V3 chip dev board :lol:

I have to say though, that I did not fully understand the note on the esp_sleep_enable_ulp_wakeup(void) where it says: In revisions 0 and 1 of the ESP32, ULP wakeup source cannot be used when RTC_PERIPH power domain is forced to be powered on (ESP_PD_OPTION_ON) Does it mean that because I need to read the RTC accessible ADC, that RTC_PERIPH is forced to be on, thus the WAKE command will not work? I guess that is the only interpretation that makes sense in the context of what we've discussed so far.
John Ellis
What cannot go wrong, will. What definitely cannot go wrong absolutely will... :roll:

ellisjr
Posts: 24
Joined: Sun May 17, 2020 8:30 pm

Re: Why can MicroPython not use the ULP WAKE instruction

Post by ellisjr » Tue Dec 01, 2020 8:32 pm

@pythoncoder Yes, I think what is happening in that example, if my understanding is correct, is that the ULP is periodically woken up by a timer and increments the count, which is being monitored and displayed by MicroPython running continuously on the main core, so the chip's running at full power, just using the ULP as well.

What I want to do is shut down the main cores (that run MicroPython) and leave the ULP in charge, just looking at the ADC, and when the reading goes over a certain value, then for the ULP to run its WAKE instruction, which basically boots up the main cores and MicroPython runs and realises that this situation has occurred and sends out a message over the wifi (and does other stuff).

So, the difference is about which processors are sleeping and need waking up. It took me while to get my head around it, assuming I've got it right :lol: I know that you know a lot (and a lot) about Python and I've read loads of posts where you've helped people out and I bet i'll be a candidate for your assistance before long ;)
John Ellis
What cannot go wrong, will. What definitely cannot go wrong absolutely will... :roll:

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Why can MicroPython not use the ULP WAKE instruction

Post by pythoncoder » Wed Dec 02, 2020 6:15 am

When you figure this out it would be great if you could write a guide to using the ULP to do this kind of thing with some code samples. It would be a very useful resource. There are many uses for a battery powered device that wakes on an analog or digital state change, reads some data and sends it to a server, then goes back to sleep. Also where the wakeup is periodic.

I know how to do all this (bar analog wakeup) with a Pyboard D, but not with an ESP32.
Peter Hinch
Index to my micropython libraries.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Why can MicroPython not use the ULP WAKE instruction

Post by pythoncoder » Wed Dec 02, 2020 5:12 pm

ellisjr wrote:
Tue Dec 01, 2020 8:14 pm
...This is as reported by Arduino upload: Chip is ESP32D0WDQ6 (revision 1)...
I have just taken delivery of two "ESP32-DEVKITC with ESP-WROOM-32" boards off eBay. I use quotes because I'm convinced they are clones; however I've put MP on one and it seems to work OK. The other is still in its packing. If you tell me how to check the chip revision I can point you to the vendor if it's right.
Peter Hinch
Index to my micropython libraries.

ellisjr
Posts: 24
Joined: Sun May 17, 2020 8:30 pm

Re: Why can MicroPython not use the ULP WAKE instruction

Post by ellisjr » Thu Dec 03, 2020 12:16 pm

Well, as I understand it, the simplest way to check is to load an Arduino sketch to the ESP32. This is what I saw (I highlighted the crucial line in italics):
Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes.
esptool.py v2.6
Serial port COM4
Connecting....
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: 24:6f:28:7b:a6:dc
Uploading stub...
Running stub...
Stub running...


Of course, AFAIK, you have to reload MP from scratch if you load an Arduino sketch so you might want to save some effort and use your unwrapped one for this test. I guess I should post this question about V3 as a new topic on the forum (after another detailed search).
Andreas Spiess gives a low level explanation about the fuses used to record revision if that's preferable, though I don't see how they record V3 unless there is another bit involved too, which presumably is the case.

BTW, my boards are all from A-Z Delivery in Germany.
John Ellis
What cannot go wrong, will. What definitely cannot go wrong absolutely will... :roll:

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Why can MicroPython not use the ULP WAKE instruction

Post by pythoncoder » Thu Dec 03, 2020 6:12 pm

I haven't any Arduino software but I hooked up a terminal to the unit with its delivered firmware and hit the reset button. This is what it said. There doesn't seem to be any explicit chip data but the first line suggests that the firmware, if not the chip itself, is old.

Code: Select all

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5656
load:0x40078000,len:0
ho 12 tail 0 room 4
load:0x40078000,len:13844
entry 0x40078fc4
I (30) boot: ESP-IDF v3.0.7 2nd stage bootloader
I (30) boot: compile time 09:04:31
I (30) boot: Enabling RNG early entropy source...
I (35) boot: SPI Speed      : 40MHz
I (39) boot: SPI Mode       : DIO
I (43) boot: SPI Flash Size : 4MB
I (47) boot: Partition Table:
I (50) boot: ## Label            Usage          Type ST Offset   Length
I (58) boot:  0 phy_init         RF data          01 01 0000f000 00001000
I (65) boot:  1 otadata          OTA data         01 00 00010000 00002000
I (73) boot:  2 nvs              WiFi data        01 02 00012000 0000e000
I (80) boot:  3 at_customize     unknown          40 00 00020000 000e0000
I (88) boot:  4 ota_0            OTA app          00 10 00100000 00180000
I (95) boot:  5 ota_1            OTA app          00 11 00280000 00180000
I (103) boot: End of partition table
I (107) boot: No factory image, trying OTA 0
I (112) esp_image: segment 0: paddr=0x00100020 vaddr=0x3f400020 size=0x20614 (132628) map
I (167) esp_image: segment 1: paddr=0x0012063c vaddr=0x3ffc0000 size=0x02d7c ( 11644) load
I (172) esp_image: segment 2: paddr=0x001233c0 vaddr=0x40080000 size=0x00400 (  1024) load
I (174) esp_image: segment 3: paddr=0x001237c8 vaddr=0x40080400 size=0x0c848 ( 51272) load
I (204) esp_image: segment 4: paddr=0x00130018 vaddr=0x400d0018 size=0xdfc80 (916608) map
I (525) esp_image: segment 5: paddr=0x0020fca0 vaddr=0x4008cc48 size=0x02504 (  9476) load
I (530) esp_image: segment 6: paddr=0x002121ac vaddr=0x400c0000 size=0x00064 (   100) load
I (541) boot: Loaded app from partition at offset 0x100000
I (541) boot: Disabling RNG early entropy source...
1.1.3
I (662) wifi: wifi firmware version: 703e53b
I (663) wifi: config NVS flash: enabled
I (663) wifi: config nano formating: disabled
I (673) wifi: Init dynamic tx buffer num: 32
I (673) wifi: Init data frame dynamic rx buffer num: 32
I (673) wifi: Init management frame dynamic rx buffer num: 32
I (679) wifi: wifi driver task: 3ffdee34, prio:23, stack:3584
I (684) wifi: Init static rx buffer num: 10
I (687) wifi: Init dynamic rx buffer num: 32
I (692) wifi: wifi power manager task: 0x3ffe369c prio: 21 stack: 2560
I (725) wifi: mode : softAP (24:62:ab:e6:a0:e5)
I (733) wifi: mode : sta (24:62:ab:e6:a0:e4) + softAP (24:62:ab:e6:a0:e5)
I (736) wifi: mode : softAP (24:62:ab:e6:a0:e5)
I (740) wifi: set country: cc=CN schan=1 nchan=13 policy=1
Peter Hinch
Index to my micropython libraries.

ellisjr
Posts: 24
Joined: Sun May 17, 2020 8:30 pm

Re: Why can MicroPython not use the ULP WAKE instruction

Post by ellisjr » Thu Dec 03, 2020 8:36 pm

Yes, ets Jun 8 2016 00:22:57 is what I have on my Rev 1 boards. I agree that it would seem unlikely that they would issue a new chip version without there being need or desire to have a later version of the firmware.

So, are these V3 chips on dev boards basically hens' teeth!? I eagerly await info. from the forum in respect of my question
John Ellis
What cannot go wrong, will. What definitely cannot go wrong absolutely will... :roll:

Post Reply