FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

rp2/modmachine: Restore original CPU speed after lightsleep. · micropython/micropython@1ea77a5 · GitHub

Commit 1ea77a5

Browse files
authored andcommitted
rp2/modmachine: Restore original CPU speed after lightsleep.
This commit lets `machine.lightsleep` restore the original CPU and peripherals frequencies upon wakeup if they were previously set to different values than the defaults. The original implementation of `machine.lightsleep` would restore the default speed for CPU and peripherals as part of the wakeup procedure, which would ignore previously set values. These changes force an optional frequency values set once the MCU wakes up, by simply jumping into the internal implementation for `machine.freq`. The relevant lightsleep test was also updated to verify the changes in question. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent 11094ea commit 1ea77a5

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎ports/rp2/modmachine.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ static mp_obj_t mp_machine_get_freq(void) {
100100
return MP_OBJ_NEW_SMALL_INT(mp_hal_get_cpu_freq());
101101
}
102102

103+
static mp_obj_t frequency_overrides[2] = {};
104+
103105
static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
104106
mp_int_t freq = mp_obj_get_int(args[0]);
105107

@@ -141,6 +143,10 @@ static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
141143
psram_reinitialize();
142144
}
143145
#endif
146+
147+
if (n_args > 0) {
148+
memcpy(frequency_overrides, args, MIN(n_args, 2) * sizeof(mp_obj_t));
149+
}
144150
}
145151

146152
static void mp_machine_idle(void) {
@@ -364,6 +370,10 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
364370
// (higher up, inside the critical section)
365371
in_lightsleep = false;
366372
#endif
373+
374+
if (frequency_overrides[0] != MP_OBJ_NULL) {
375+
mp_machine_set_freq(1 + (frequency_overrides[1] != NULL ? 1 : 0), frequency_overrides);
376+
}
367377
}
368378

369379
MP_NORETURN static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) {

‎tests/ports/rp2/rp2_lightsleep.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,15 @@
2727
sys.stdout.write(chr(ord("a") + (n % 26)))
2828
lightsleep(2 ** (n % 10))
2929

30-
print("\nDONE")
30+
old_freq = machine.freq()
31+
machine.freq(50_000_000)
32+
print("\n{}".format(machine.freq()))
33+
lightsleep(500)
34+
print(machine.freq())
35+
machine.freq(51_000_000, 48_000_000)
36+
print(machine.freq())
37+
lightsleep(500)
38+
print(machine.freq())
39+
machine.freq(old_freq)
40+
41+
print("DONE")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv
2+
50000000
3+
50000000
4+
51000000
5+
51000000
26
DONE

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL