Merge pull request #316 from MichaelBell/earlier-power-init

Even better Badger wake handling
This commit is contained in:
Philip Howard 2022-03-29 23:12:55 +01:00 committed by GitHub
commit 9fc556d064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 57 deletions

View File

@ -17,7 +17,6 @@ woken_by_button = badger2040.woken_by_button() # Must be done before we clear_p
if badger2040.pressed_to_wake(badger2040.BUTTON_A) and badger2040.pressed_to_wake(badger2040.BUTTON_C):
# Pressing A and C together at start quits app
exited_to_launcher = badger_os.state_clear_running()
badger2040.clear_pressed_to_wake()
else:
# Otherwise restore previously running app
badger_os.state_launch()

View File

@ -16,7 +16,6 @@ MP_DEFINE_CONST_FUN_OBJ_2(Badger2040_pen_obj, Badger2040_pen);
MP_DEFINE_CONST_FUN_OBJ_2(Badger2040_thickness_obj, Badger2040_thickness);
MP_DEFINE_CONST_FUN_OBJ_2(Badger2040_pressed_obj, Badger2040_pressed);
MP_DEFINE_CONST_FUN_OBJ_2(Badger2040_pressed_to_wake2_obj, Badger2040_pressed_to_wake2);
MP_DEFINE_CONST_FUN_OBJ_1(Badger2040_clear_obj, Badger2040_clear);
MP_DEFINE_CONST_FUN_OBJ_3(Badger2040_pixel_obj, Badger2040_pixel);
MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_line_obj, 4, Badger2040_line);
@ -34,7 +33,6 @@ MP_DEFINE_CONST_FUN_OBJ_KW(Badger2040_measure_glyph_obj, 2, Badger2040_measure_g
MP_DEFINE_CONST_FUN_OBJ_3(Badger2040_command_obj, Badger2040_command);
MP_DEFINE_CONST_FUN_OBJ_1(Badger2040_pressed_to_wake_obj, Badger2040_pressed_to_wake);
MP_DEFINE_CONST_FUN_OBJ_0(Badger2040_clear_pressed_to_wake_obj, Badger2040_clear_pressed_to_wake);
MP_DEFINE_CONST_FUN_OBJ_1(Badger2040_halt_obj, Badger2040_halt);
MP_DEFINE_CONST_FUN_OBJ_0(Badger2040_woken_by_button_obj, Badger2040_woken_by_button);
@ -55,7 +53,6 @@ STATIC const mp_rom_map_elem_t Badger2040_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_thickness), MP_ROM_PTR(&Badger2040_thickness_obj) },
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&Badger2040_pressed_obj) },
{ MP_ROM_QSTR(MP_QSTR_pressed_to_wake), MP_ROM_PTR(&Badger2040_pressed_to_wake2_obj) },
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&Badger2040_clear_obj) },
{ MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&Badger2040_pixel_obj) },
@ -92,7 +89,6 @@ STATIC const mp_rom_map_elem_t badger2040_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_Badger2040), (mp_obj_t)&Badger2040_type },
{ MP_ROM_QSTR(MP_QSTR_pressed_to_wake), MP_ROM_PTR(&Badger2040_pressed_to_wake_obj) },
{ MP_ROM_QSTR(MP_QSTR_clear_pressed_to_wake), MP_ROM_PTR(&Badger2040_clear_pressed_to_wake_obj) },
{ MP_ROM_QSTR(MP_QSTR_woken_by_button), MP_ROM_PTR(&Badger2040_woken_by_button_obj) },
{ MP_ROM_QSTR(MP_QSTR_UPDATE_NORMAL), MP_ROM_INT(0) },

View File

@ -4,48 +4,29 @@
#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o))
namespace {
struct Badger2040_WakeUpInit {
Badger2040_WakeUpInit()
: state(gpio_get_all() & (0x1f << pimoroni::Badger2040::DOWN)) // Record state of front buttons
{
gpio_set_function(pimoroni::Badger2040::ENABLE_3V3, GPIO_FUNC_SIO);
gpio_set_dir(pimoroni::Badger2040::ENABLE_3V3, GPIO_OUT);
gpio_put(pimoroni::Badger2040::ENABLE_3V3, 1);
gpio_set_function(pimoroni::Badger2040::LED, GPIO_FUNC_SIO);
gpio_set_dir(pimoroni::Badger2040::LED, GPIO_OUT);
gpio_put(pimoroni::Badger2040::LED, 1);
}
bool any() const {
return state > 0;
}
bool get(uint32_t pin) const {
return state & (0b1 << pin);
}
bool get_once(uint32_t pin) {
uint32_t mask = 0b1 << pin;
bool value = state & mask;
state &= ~mask;
return value;
}
void clear() { state = 0; }
private:
uint32_t state;
};
Badger2040_WakeUpInit button_wake_state __attribute__ ((init_priority (101)));
};
extern "C" {
#include "badger2040.h"
#include "py/builtin.h"
#include "py/mpthread.h"
extern uint32_t badger_buttons_on_wake;
static bool Badger2040_wake_state_any() {
return badger_buttons_on_wake > 0;
}
static bool Badger2040_wake_state_get(uint32_t pin) {
return badger_buttons_on_wake & (0b1 << pin);
}
static bool Badger2040_wake_state_get_once(uint32_t pin) {
uint32_t mask = 0b1 << pin;
bool value = badger_buttons_on_wake & mask;
badger_buttons_on_wake &= ~mask;
return value;
}
std::string mp_obj_to_string_r(const mp_obj_t &obj) {
if(mp_obj_is_str_or_bytes(obj)) {
GET_STR_DATA_LEN(obj, str, str_len);
@ -214,7 +195,7 @@ MICROPY_EVENT_POLL_HOOK
}
mp_obj_t Badger2040_woken_by_button() {
return button_wake_state.any() ? mp_const_true : mp_const_false;
return Badger2040_wake_state_any() ? mp_const_true : mp_const_false;
}
mp_obj_t Badger2040_halt(mp_obj_t self_in) {
@ -269,25 +250,16 @@ mp_obj_t Badger2040_thickness(mp_obj_t self_in, mp_obj_t thickness) {
mp_obj_t Badger2040_pressed(mp_obj_t self_in, mp_obj_t button) {
_Badger2040_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Badger2040_obj_t);
self->badger2040->update_button_states();
bool wake_state = button_wake_state.get_once(mp_obj_get_int(button));
bool wake_state = Badger2040_wake_state_get_once(mp_obj_get_int(button));
bool state = self->badger2040->pressed(mp_obj_get_int(button));
return (state || wake_state) ? mp_const_true : mp_const_false;
}
mp_obj_t Badger2040_pressed_to_wake(mp_obj_t button) {
bool state = button_wake_state.get(mp_obj_get_int(button));
bool state = Badger2040_wake_state_get(mp_obj_get_int(button));
return state ? mp_const_true : mp_const_false;
}
mp_obj_t Badger2040_pressed_to_wake2(mp_obj_t self_in, mp_obj_t button) {
return Badger2040_pressed_to_wake(button);
}
mp_obj_t Badger2040_clear_pressed_to_wake() {
button_wake_state.clear();
return mp_const_none;
}
// pressed
// pressed_to_wake
// wait_for_press - implement in terms of MicroPython!

View File

@ -24,7 +24,6 @@ extern mp_obj_t Badger2040_pen(mp_obj_t self_in, mp_obj_t color);
extern mp_obj_t Badger2040_thickness(mp_obj_t self_in, mp_obj_t thickness);
extern mp_obj_t Badger2040_pressed(mp_obj_t self_in, mp_obj_t button);
extern mp_obj_t Badger2040_pressed_to_wake2(mp_obj_t self_in, mp_obj_t button);
extern mp_obj_t Badger2040_clear(mp_obj_t self_in);
extern mp_obj_t Badger2040_pixel(mp_obj_t self_in, mp_obj_t x, mp_obj_t y);
@ -43,5 +42,4 @@ extern mp_obj_t Badger2040_measure_glyph(size_t n_args, const mp_obj_t *pos_args
extern mp_obj_t Badger2040_command(mp_obj_t self_in, mp_obj_t reg, mp_obj_t data);
extern mp_obj_t Badger2040_pressed_to_wake(mp_obj_t button);
extern mp_obj_t Badger2040_clear_pressed_to_wake();
extern mp_obj_t Badger2040_woken_by_button();

View File

@ -0,0 +1,38 @@
.syntax unified
.cpu cortex-m0plus
.thumb
#include "pico/asm_helper.S"
// This macro tells the pico runtime to call __badger_init very early in boot
__pre_init __badger_init, 00003
.section .data.badger_init
.global badger_buttons_on_wake
.align 4
badger_buttons_on_wake:
.word 0x00000000
.section .text
.thumb_func
__badger_init:
// Read GPIO state for front buttons and store
movs r3, 0xd0
lsls r3, r3, 24
ldr r1, [r3, 4] // Read all GPIOs
movs r2, 0b11111 // Mask the front buttons (pins 15-11)
lsls r2, 11
ands r1, r1, r2
ldr r2, =badger_buttons_on_wake
str r1, [r2]
// Enable 3v3 pin on the badger
ldr r1, =0x40014054 // GPIO control register 10
movs r2, 5 // SIO function
str r2, [r1] // Set Enable 3v3 to SIO
str r2, [r1, 120] // Also set LED (25) to SIO
ldr r2, =0x02000400 // Pins 25 and 10
str r2, [r3, 36] // Enable pins out
str r2, [r3, 20] // Set pins high
bx lr

View File

@ -5,6 +5,7 @@ add_library(usermod_${MOD_NAME} INTERFACE)
target_sources(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.cpp
${CMAKE_CURRENT_LIST_DIR}/badgerinit.S
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/badger2040/badger2040.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../drivers/uc8151/uc8151.cpp
)
@ -19,4 +20,4 @@ target_compile_definitions(usermod_${MOD_NAME} INTERFACE
MODULE_BADGER2040_ENABLED=1
)
target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})
target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})