mirror of
https://github.com/micropython/micropython.git
synced 2025-07-21 13:01:10 +02:00
Fixes a regression introduced in 3af006efb3
where WFE never blocked in `mp_wfe_or_timeout()` function and would
busy-wait instead. This increases power consumption measurably.
Root cause is that `mp_wfe_or_timeout()` calls soft timer functions that
(after the regression) call `recursive_mutex_enter()` and
`recursive_mutex_exit()`. The exit calls
`lock_internal_spin_unlock_with_notify()` and the default pico-sdk
implementation of this macro issues a SEV which negates the WFE that
follows it, meaning the CPU never suspends.
See https://forums.raspberrypi.com/viewtopic.php?p=2233908 for more
details.
The fix in this comment adds a custom "nowait" variant mutex that doesn't
do WFE/SEV, and uses this one for PendSV. This will use more power when
there's contention for the PendSV mutex as the other core will spin, but
this shouldn't happen very often.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
59 lines
2.5 KiB
C
59 lines
2.5 KiB
C
/*
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2023 Damien P. George
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
#ifndef MICROPY_INCLUDED_RP2_MUTEX_EXTRA_H
|
|
#define MICROPY_INCLUDED_RP2_MUTEX_EXTRA_H
|
|
|
|
#include "pico/mutex.h"
|
|
|
|
uint32_t recursive_mutex_enter_blocking_and_disable_interrupts(recursive_mutex_t *mtx);
|
|
void recursive_mutex_exit_and_restore_interrupts(recursive_mutex_t *mtx, uint32_t save);
|
|
|
|
// Alternative version of recursive_mutex_t that doesn't issue WFE and SEV
|
|
// instructions. This means it will use more power (busy-waits), but exiting
|
|
// this mutex doesn't disrupt the calling CPU's event state in the same way a
|
|
// recursive mutex does (because recurse_mutex_exit() executes SEV each time the
|
|
// mutex is released.)
|
|
//
|
|
// Implement as a wrapper type because no longer compatible with the normal
|
|
// recursive_mutex functions.
|
|
|
|
typedef struct {
|
|
recursive_mutex_t mutex;
|
|
} recursive_mutex_nowait_t;
|
|
|
|
inline static void recursive_mutex_nowait_init(recursive_mutex_nowait_t *mtx) {
|
|
recursive_mutex_init(&mtx->mutex);
|
|
}
|
|
|
|
inline static bool recursive_mutex_nowait_try_enter(recursive_mutex_nowait_t *mtx, uint32_t *owner_out) {
|
|
return recursive_mutex_try_enter(&mtx->mutex, owner_out);
|
|
}
|
|
|
|
void recursive_mutex_nowait_enter_blocking(recursive_mutex_nowait_t *mtx);
|
|
void recursive_mutex_nowait_exit(recursive_mutex_nowait_t *mtx);
|
|
|
|
#endif // MICROPY_INCLUDED_RP2_MUTEX_EXTRA_H
|