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