-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// This is a hack! Need to replace with upstream board definition. | ||
#define MICROPY_HW_BOARD_NAME "Pimoroni Badger2040 2MB" | ||
#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) | ||
#define PICO_VBUS_PIN 24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
add_library(usermod_system_speed INTERFACE) | ||
|
||
target_sources(usermod_system_speed INTERFACE | ||
${CMAKE_CURRENT_LIST_DIR}/system_speed.c | ||
${CMAKE_CURRENT_LIST_DIR}/system_speed.cpp | ||
) | ||
|
||
target_include_directories(usermod_system_speed INTERFACE | ||
${CMAKE_CURRENT_LIST_DIR} | ||
) | ||
|
||
target_compile_definitions(usermod_system_speed INTERFACE | ||
-DMODULE_SYSTEM_SPEED_ENABLED=1 | ||
) | ||
|
||
target_link_libraries(usermod INTERFACE usermod_system_speed | ||
hardware_vreg | ||
hardware_pll | ||
hardware_resets | ||
) | ||
|
||
if (MICROPY_PY_NETWORK_CYW43) | ||
target_link_libraries(usermod INTERFACE pico_cyw43_arch_none) | ||
endif () | ||
|
||
set_source_files_properties( | ||
${CMAKE_CURRENT_LIST_DIR}/system_speed.c | ||
PROPERTIES COMPILE_FLAGS | ||
"-Wno-discarded-qualifiers" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "system_speed.h" | ||
|
||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(system_speed_set_obj, system_speed_set); | ||
|
||
STATIC const mp_map_elem_t system_speed_globals_table[] = { | ||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_system_speed) }, | ||
{ MP_ROM_QSTR(MP_QSTR_set_speed), MP_ROM_PTR(&system_speed_set_obj) }, | ||
|
||
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_VERY_SLOW), MP_ROM_INT(0) }, | ||
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_SLOW), MP_ROM_INT(1) }, | ||
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_NORMAL), MP_ROM_INT(2) }, | ||
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_FAST), MP_ROM_INT(3) }, | ||
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_TURBO), MP_ROM_INT(4) }, | ||
}; | ||
STATIC MP_DEFINE_CONST_DICT(mp_module_system_speed_globals, system_speed_globals_table); | ||
|
||
const mp_obj_module_t system_speed_user_cmodule = { | ||
.base = { &mp_type_module }, | ||
.globals = (mp_obj_dict_t*)&mp_module_system_speed_globals, | ||
}; | ||
|
||
#if MICROPY_VERSION <= 70144 | ||
MP_REGISTER_MODULE(MP_QSTR_system_speed, system_speed_user_cmodule, MODULE_SYSTEM_SPEED_ENABLED); | ||
#else | ||
MP_REGISTER_MODULE(MP_QSTR_system_speed, system_speed_user_cmodule); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include "hardware/gpio.h" | ||
|
||
extern "C" { | ||
#include "system_speed.h" | ||
#include "hardware/vreg.h" | ||
#include "hardware/clocks.h" | ||
#include "hardware/pll.h" | ||
|
||
#if defined CYW43_WL_GPIO_VBUS_PIN | ||
#include "pico/cyw43_arch.h" | ||
#endif | ||
|
||
#if MICROPY_HW_ENABLE_UART_REPL | ||
#include "uart.h" | ||
#endif | ||
|
||
static void _set_system_speed(uint32_t selected_speed) { | ||
uint32_t sys_freq; | ||
|
||
switch (selected_speed) | ||
{ | ||
case 4: // TURBO: 250 MHZ, 1.2V | ||
vreg_set_voltage(VREG_VOLTAGE_1_20); | ||
set_sys_clock_khz(250000, true); | ||
return; | ||
case 3: // FAST: 133 MHZ | ||
vreg_set_voltage(VREG_VOLTAGE_1_10); | ||
set_sys_clock_khz(133000, true); | ||
return; | ||
|
||
default: | ||
case 2: // NORMAL: 48 MHZ | ||
vreg_set_voltage(VREG_VOLTAGE_1_10); | ||
set_sys_clock_48mhz(); | ||
return; | ||
|
||
case 1: // SLOW: 12 MHZ, 1.0V | ||
sys_freq = 12 * MHZ; | ||
break; | ||
|
||
case 0: // VERY_SLOW: 4 MHZ, 1.0V | ||
sys_freq = 4 * MHZ; | ||
break; | ||
} | ||
|
||
// Set the configured clock speed, by dividing the USB PLL | ||
clock_configure(clk_sys, | ||
CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX, | ||
CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB, | ||
48 * MHZ, | ||
sys_freq); | ||
|
||
clock_configure(clk_peri, | ||
0, | ||
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS, | ||
sys_freq, | ||
sys_freq); | ||
|
||
clock_configure(clk_adc, | ||
0, | ||
CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB, | ||
48 * MHZ, | ||
sys_freq); | ||
|
||
// No longer using the SYS PLL so disable it | ||
pll_deinit(pll_sys); | ||
|
||
// Not using USB so stop the clock | ||
clock_stop(clk_usb); | ||
|
||
// Drop the core voltage | ||
vreg_set_voltage(VREG_VOLTAGE_1_00); | ||
} | ||
|
||
mp_obj_t system_speed_set(mp_obj_t speed) { | ||
uint32_t selected_speed = mp_obj_get_int(speed); | ||
|
||
#if defined CYW43_WL_GPIO_VBUS_PIN | ||
bool vbus = cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN); | ||
#else | ||
bool vbus = gpio_get(PICO_VBUS_PIN); | ||
#endif | ||
if (vbus && selected_speed < 2) { | ||
// If on USB never go slower than normal speed. | ||
selected_speed = 2; | ||
} | ||
|
||
_set_system_speed(selected_speed); | ||
|
||
#if MICROPY_HW_ENABLE_UART_REPL | ||
setup_default_uart(); | ||
mp_uart_init(); | ||
#endif | ||
|
||
if (selected_speed >= 2) { | ||
spi_set_baudrate(PIMORONI_SPI_DEFAULT_INSTANCE, 12 * MHZ); | ||
} | ||
else { | ||
// Set the SPI baud rate for communicating with the display to | ||
// go as fast as possible (which is now 6 or 2 MHz) | ||
spi_get_hw(PIMORONI_SPI_DEFAULT_INSTANCE)->cpsr = 2; | ||
hw_write_masked(&spi_get_hw(PIMORONI_SPI_DEFAULT_INSTANCE)->cr0, 0, SPI_SSPCR0_SCR_BITS); | ||
} | ||
|
||
return mp_const_none; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include "py/runtime.h" | ||
#include "py/objstr.h" | ||
|
||
extern mp_obj_t system_speed_set(mp_obj_t speed); |