Question about machine.idle()
Posted: Wed Jul 05, 2017 6:30 pm
The ESP32 is at the moment very power hungry. It needs between 140 to 180 mA.
And that is far too much for battery powered devices.
So I try to tame the power consumption , without success.
ESP firmware here is the latest (branch esp32, commit message: esp32: Update to latest ESP-IDF)
Currently in esp32 firmware in modmachine,c the machine.idle() is implemented as:
STATIC mp_obj_t machine_idle(void) {
taskYIELD();
return mp_const_none;
}
With next snippet I tested the current firmware:
now = time.ticks_ms()
count = 0
while count < 1000:
machine.idle()
count += 1
now1 = time.ticks_ms()
diff = now1- now
print("1000 loops in %s ms. Per loop %s ms" %( diff, diff/1000) )
result: 1000 loops in 20 ms. Per loop 0.02 ms
Quite fast indeed!
I replaced the code with the code as found in the esp8266:
STATIC mp_obj_t machine_idle(void) {
uint32_t t = mp_hal_ticks_cpu();
asm("waiti 0");
t = mp_hal_ticks_cpu() - t;
return MP_OBJ_NEW_SMALL_INT(t);
}
build and flashed the firmware.
Tested with same python snippet as above.
Result: 1000 loops in 9912 ms. Per loop 9.912 ms
This shows that the cpu really sleeps 10 ms at each machine.idle(). So far so good
(apparently there is a timer interrupt each 10 ms. Heartbeat of underlying RTOS?)
But even with the CPU idle, the power consumption stays exactly the same.
So whats wrong or missing here?
And that is far too much for battery powered devices.
So I try to tame the power consumption , without success.
ESP firmware here is the latest (branch esp32, commit message: esp32: Update to latest ESP-IDF)
Currently in esp32 firmware in modmachine,c the machine.idle() is implemented as:
STATIC mp_obj_t machine_idle(void) {
taskYIELD();
return mp_const_none;
}
With next snippet I tested the current firmware:
now = time.ticks_ms()
count = 0
while count < 1000:
machine.idle()
count += 1
now1 = time.ticks_ms()
diff = now1- now
print("1000 loops in %s ms. Per loop %s ms" %( diff, diff/1000) )
result: 1000 loops in 20 ms. Per loop 0.02 ms
Quite fast indeed!
I replaced the code with the code as found in the esp8266:
STATIC mp_obj_t machine_idle(void) {
uint32_t t = mp_hal_ticks_cpu();
asm("waiti 0");
t = mp_hal_ticks_cpu() - t;
return MP_OBJ_NEW_SMALL_INT(t);
}
build and flashed the firmware.
Tested with same python snippet as above.
Result: 1000 loops in 9912 ms. Per loop 9.912 ms
This shows that the cpu really sleeps 10 ms at each machine.idle(). So far so good
(apparently there is a timer interrupt each 10 ms. Heartbeat of underlying RTOS?)
But even with the CPU idle, the power consumption stays exactly the same.
So whats wrong or missing here?