Badger2040: Read the button state and enable 3v3 before initializing crystal

This commit is contained in:
Mike Bell 2022-03-26 11:55:56 +00:00
parent 522c83dc19
commit e1a4d56993
3 changed files with 46 additions and 18 deletions

View File

@ -4,41 +4,34 @@
#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o))
extern "C" uint32_t badger_gpio_on_wake;
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);
// Only keep the front buttons
badger_gpio_on_wake &= 0x1f << pimoroni::Badger2040::DOWN;
}
bool any() const {
return state > 0;
return badger_gpio_on_wake > 0;
}
bool get(uint32_t pin) const {
return state & (0b1 << pin);
return badger_gpio_on_wake & (0b1 << pin);
}
bool get_once(uint32_t pin) {
uint32_t mask = 0b1 << pin;
bool value = state & mask;
state &= ~mask;
bool value = badger_gpio_on_wake & mask;
badger_gpio_on_wake &= ~mask;
return value;
}
void clear() { state = 0; }
private:
uint32_t state;
void clear() { badger_gpio_on_wake = 0; }
};
Badger2040_WakeUpInit button_wake_state __attribute__ ((init_priority (101)));
Badger2040_WakeUpInit button_wake_state;
};
extern "C" {

View File

@ -0,0 +1,34 @@
.syntax unified
.cpu cortex-m0plus
.thumb
#include "pico/asm_helper.S"
__pre_init __badger_init, 00003
.section .data.badger_init
.global badger_gpio_on_wake
.align 4
badger_gpio_on_wake:
.word 0x00000000
.section .text
.thumb_func
__badger_init:
// Read GPIO state and store
movs r3, 0xd0
lsls r3, r3, 24
ldr r1, [r3, 4]
ldr r2, =badger_gpio_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})