Skip to content

Commit fc86b28

Browse files
committed
System Speed module for #24.
1 parent 5d3b139 commit fc86b28

File tree

8 files changed

+175
-0
lines changed

8 files changed

+175
-0
lines changed

firmware/PIMORONI_BADGER2040/micropython.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ target_compile_definitions(usermod_wakeup INTERFACE
3939
-DWAKEUP_PIN_VALUE=0b10000000000000010000000000
4040
)
4141

42+
# Use our LOCAL system_speed module from firmware/modules/system_speed
43+
include(firmware/modules/system_speed/micropython)
44+
4245
# Note: cppmem is *required* for C++ code to function on MicroPython
4346
# it redirects `malloc` and `free` calls to MicroPython's heap
4447
include(cppmem/micropython)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// This is a hack! Need to replace with upstream board definition.
22
#define MICROPY_HW_BOARD_NAME "Pimoroni Badger2040 2MB"
33
#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024)
4+
#define PICO_VBUS_PIN 24

firmware/PIMORONI_BADGER2040W/micropython.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ target_compile_definitions(usermod_wakeup INTERFACE
4040
-DWAKEUP_PIN_VALUE=0b10000000000010000000000
4141
)
4242

43+
# Use our LOCAL system_speed module from firmware/modules/system_speed
44+
include(firmware/modules/system_speed/micropython)
45+
4346
# Note: cppmem is *required* for C++ code to function on MicroPython
4447
# it redirects `malloc` and `free` calls to MicroPython's heap
4548
include(cppmem/micropython)

firmware/PIMORONI_BADGER2040W/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define CYW43_LWIP (1)
1212
#define CYW43_GPIO (1)
1313
#define CYW43_SPI_PIO (1)
14+
#define CYW43_WL_GPIO_VBUS_PIN (2)
1415

1516
// For debugging mbedtls - also set
1617
// Debug level (0-4) 1=warning, 2=info, 3=debug, 4=verbose
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
add_library(usermod_system_speed INTERFACE)
2+
3+
target_sources(usermod_system_speed INTERFACE
4+
${CMAKE_CURRENT_LIST_DIR}/system_speed.c
5+
${CMAKE_CURRENT_LIST_DIR}/system_speed.cpp
6+
)
7+
8+
target_include_directories(usermod_system_speed INTERFACE
9+
${CMAKE_CURRENT_LIST_DIR}
10+
)
11+
12+
target_compile_definitions(usermod_system_speed INTERFACE
13+
-DMODULE_SYSTEM_SPEED_ENABLED=1
14+
)
15+
16+
target_link_libraries(usermod INTERFACE usermod_system_speed
17+
hardware_vreg
18+
hardware_pll
19+
hardware_resets
20+
)
21+
22+
set_source_files_properties(
23+
${CMAKE_CURRENT_LIST_DIR}/system_speed.c
24+
PROPERTIES COMPILE_FLAGS
25+
"-Wno-discarded-qualifiers"
26+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "system_speed.h"
2+
3+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(system_speed_set_obj, system_speed_set);
4+
5+
STATIC const mp_map_elem_t system_speed_globals_table[] = {
6+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_system_speed) },
7+
{ MP_ROM_QSTR(MP_QSTR_set_speed), MP_ROM_PTR(&system_speed_set_obj) },
8+
9+
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_VERY_SLOW), MP_ROM_INT(0) },
10+
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_SLOW), MP_ROM_INT(1) },
11+
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_NORMAL), MP_ROM_INT(2) },
12+
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_FAST), MP_ROM_INT(3) },
13+
{ MP_ROM_QSTR(MP_QSTR_SYSTEM_TURBO), MP_ROM_INT(4) },
14+
};
15+
STATIC MP_DEFINE_CONST_DICT(mp_module_system_speed_globals, system_speed_globals_table);
16+
17+
const mp_obj_module_t system_speed_user_cmodule = {
18+
.base = { &mp_type_module },
19+
.globals = (mp_obj_dict_t*)&mp_module_system_speed_globals,
20+
};
21+
22+
#if MICROPY_VERSION <= 70144
23+
MP_REGISTER_MODULE(MP_QSTR_system_speed, system_speed_user_cmodule, MODULE_SYSTEM_SPEED_ENABLED);
24+
#else
25+
MP_REGISTER_MODULE(MP_QSTR_system_speed, system_speed_user_cmodule);
26+
#endif
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include "hardware/gpio.h"
2+
3+
extern "C" {
4+
#include "system_speed.h"
5+
#include "pico/stdlib.h"
6+
#include "hardware/vreg.h"
7+
#include "hardware/clocks.h"
8+
#include "hardware/pll.h"
9+
10+
#if defined CYW43_WL_GPIO_VBUS_PIN
11+
#include "lib/cyw43-driver/src/cyw43.h"
12+
#endif
13+
14+
#if MICROPY_HW_ENABLE_UART_REPL
15+
#include "uart.h"
16+
#endif
17+
18+
static void _set_system_speed(uint32_t selected_speed) {
19+
uint32_t sys_freq;
20+
21+
switch (selected_speed)
22+
{
23+
case 4: // TURBO: 250 MHZ, 1.2V
24+
vreg_set_voltage(VREG_VOLTAGE_1_20);
25+
set_sys_clock_khz(250000, true);
26+
return;
27+
case 3: // FAST: 133 MHZ
28+
vreg_set_voltage(VREG_VOLTAGE_1_10);
29+
set_sys_clock_khz(133000, true);
30+
return;
31+
32+
default:
33+
case 2: // NORMAL: 48 MHZ
34+
vreg_set_voltage(VREG_VOLTAGE_1_10);
35+
set_sys_clock_48mhz();
36+
return;
37+
38+
case 1: // SLOW: 12 MHZ, 1.0V
39+
sys_freq = 12 * MHZ;
40+
break;
41+
42+
case 0: // VERY_SLOW: 4 MHZ, 1.0V
43+
sys_freq = 4 * MHZ;
44+
break;
45+
}
46+
47+
// Set the configured clock speed, by dividing the USB PLL
48+
clock_configure(clk_sys,
49+
CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
50+
CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
51+
48 * MHZ,
52+
sys_freq);
53+
54+
clock_configure(clk_peri,
55+
0,
56+
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS,
57+
sys_freq,
58+
sys_freq);
59+
60+
clock_configure(clk_adc,
61+
0,
62+
CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
63+
48 * MHZ,
64+
sys_freq);
65+
66+
// No longer using the SYS PLL so disable it
67+
pll_deinit(pll_sys);
68+
69+
// Not using USB so stop the clock
70+
clock_stop(clk_usb);
71+
72+
// Drop the core voltage
73+
vreg_set_voltage(VREG_VOLTAGE_1_00);
74+
}
75+
76+
mp_obj_t system_speed_set(mp_obj_t speed) {
77+
uint32_t selected_speed = mp_obj_get_int(speed);
78+
bool vbus;
79+
#if defined CYW43_WL_GPIO_VBUS_PIN
80+
cyw43_gpio_get(&cyw43_state, CYW43_WL_GPIO_VBUS_PIN, &vbus);
81+
#else
82+
gpio_set_function(PICO_VBUS_PIN, GPIO_FUNC_SIO);
83+
bool vbus = gpio_get(PICO_VBUS_PIN);
84+
#endif
85+
if (vbus && selected_speed < 2) {
86+
// If on USB never go slower than normal speed.
87+
selected_speed = 2;
88+
}
89+
90+
_set_system_speed(selected_speed);
91+
92+
#if MICROPY_HW_ENABLE_UART_REPL
93+
setup_default_uart();
94+
mp_uart_init();
95+
#endif
96+
97+
// TODO Make this work...
98+
/*if (selected_speed >= 2) {
99+
spi_set_baudrate(PIMORONI_SPI_DEFAULT_INSTANCE, 12 * MHZ);
100+
}
101+
else {
102+
// Set the SPI baud rate for communicating with the display to
103+
// go as fast as possible (which is now 6 or 2 MHz)
104+
spi_get_hw(PIMORONI_SPI_DEFAULT_INSTANCE)->cpsr = 2;
105+
hw_write_masked(&spi_get_hw(PIMORONI_SPI_DEFAULT_INSTANCE)->cr0, 0, SPI_SSPCR0_SCR_BITS);
106+
}*/
107+
108+
return mp_const_none;
109+
}
110+
111+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "py/runtime.h"
2+
#include "py/objstr.h"
3+
4+
extern mp_obj_t system_speed_set(mp_obj_t speed);

0 commit comments

Comments
 (0)