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
+ }
0 commit comments