1
+ #include " hardware/gpio.h"
2
+
3
+ extern " C" {
4
+ #include " system_speed.h"
5
+ #include " hardware/vreg.h"
6
+ #include " hardware/clocks.h"
7
+ #include " hardware/pll.h"
8
+
9
+ #if defined CYW43_WL_GPIO_VBUS_PIN
10
+ #include " pico/cyw43_arch.h"
11
+ #endif
12
+
13
+ #if MICROPY_HW_ENABLE_UART_REPL
14
+ #include " uart.h"
15
+ #endif
16
+
17
+ static void _set_system_speed (uint32_t selected_speed) {
18
+ uint32_t sys_freq;
19
+
20
+ switch (selected_speed)
21
+ {
22
+ case 4 : // TURBO: 250 MHZ, 1.2V
23
+ vreg_set_voltage (VREG_VOLTAGE_1_20);
24
+ set_sys_clock_khz (250000 , true );
25
+ return ;
26
+ case 3 : // FAST: 133 MHZ
27
+ vreg_set_voltage (VREG_VOLTAGE_1_10);
28
+ set_sys_clock_khz (133000 , true );
29
+ return ;
30
+
31
+ default :
32
+ case 2 : // NORMAL: 48 MHZ
33
+ vreg_set_voltage (VREG_VOLTAGE_1_10);
34
+ set_sys_clock_48mhz ();
35
+ return ;
36
+
37
+ case 1 : // SLOW: 12 MHZ, 1.0V
38
+ sys_freq = 12 * MHZ;
39
+ break ;
40
+
41
+ case 0 : // VERY_SLOW: 4 MHZ, 1.0V
42
+ sys_freq = 4 * MHZ;
43
+ break ;
44
+ }
45
+
46
+ // Set the configured clock speed, by dividing the USB PLL
47
+ clock_configure (clk_sys,
48
+ CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
49
+ CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
50
+ 48 * MHZ,
51
+ sys_freq);
52
+
53
+ clock_configure (clk_peri,
54
+ 0 ,
55
+ CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS,
56
+ sys_freq,
57
+ sys_freq);
58
+
59
+ clock_configure (clk_adc,
60
+ 0 ,
61
+ CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
62
+ 48 * MHZ,
63
+ sys_freq);
64
+
65
+ // No longer using the SYS PLL so disable it
66
+ pll_deinit (pll_sys);
67
+
68
+ // Not using USB so stop the clock
69
+ clock_stop (clk_usb);
70
+
71
+ // Drop the core voltage
72
+ vreg_set_voltage (VREG_VOLTAGE_1_00);
73
+ }
74
+
75
+ mp_obj_t system_speed_set (mp_obj_t speed) {
76
+ uint32_t selected_speed = mp_obj_get_int (speed);
77
+
78
+ #if defined CYW43_WL_GPIO_VBUS_PIN
79
+ bool vbus = cyw43_arch_gpio_get (CYW43_WL_GPIO_VBUS_PIN);
80
+ #else
81
+ bool vbus = gpio_get (PICO_VBUS_PIN);
82
+ #endif
83
+ if (vbus && selected_speed < 2 ) {
84
+ // If on USB never go slower than normal speed.
85
+ selected_speed = 2 ;
86
+ }
87
+
88
+ _set_system_speed (selected_speed);
89
+
90
+ #if MICROPY_HW_ENABLE_UART_REPL
91
+ setup_default_uart ();
92
+ mp_uart_init ();
93
+ #endif
94
+
95
+ if (selected_speed >= 2 ) {
96
+ spi_set_baudrate (PIMORONI_SPI_DEFAULT_INSTANCE, 12 * MHZ);
97
+ }
98
+ else {
99
+ // Set the SPI baud rate for communicating with the display to
100
+ // go as fast as possible (which is now 6 or 2 MHz)
101
+ spi_get_hw (PIMORONI_SPI_DEFAULT_INSTANCE)->cpsr = 2 ;
102
+ hw_write_masked (&spi_get_hw (PIMORONI_SPI_DEFAULT_INSTANCE)->cr0 , 0 , SPI_SSPCR0_SCR_BITS);
103
+ }
104
+
105
+ return mp_const_none;
106
+ }
107
+
108
+ }
0 commit comments