scoopydoo 发表于 2025-1-17 13:30:47

[实验] 树莓派 Pico 超频并实现 IO 引脚快速翻转

本帖最后由 scoopydoo 于 2025-1-17 15:26 编辑

都是网上现成的东西,东抄一点西抄一点,折腾了几个小时,总算达成了超频到 400MHz,IO 引脚翻转输出 200MHz 的小目标。

需要注意的有两点:

首先要提高核心电压,根据老外的经验加到最高可能的电压 1.3V 可以稳定超频到 400MHz 以上。

其次要降低外置的 SPI Flash 存储器的时钟,也就是增大分频系数,由原来的二分频改为四分频。

超频之后程序长时间运行稳定可靠没任何异常,芯片本身发热也很低,估计温升不超过 5 度。

附上代码供参考,主程序 pico_oc_test.c(第 14 行把核心电压设置为 1.3V)

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/vreg.h"
#include "hardware/clocks.h"
#include "hardware/pio.h"
#include "square_wave.pio.h"

const uint LED_PIN = 25; // Pi Pico onboard LED
const uint OUT_PIN = 9;
const uint32_t SYS_FREQ_KHZ = 400000U; // BE CAREFUL, NOT ALL VALUES ARE VALID !!!

int main()
{
    vreg_set_voltage(VREG_VOLTAGE_1_30); // Set the core voltage to the highest possible option
    stdio_init_all();
    set_sys_clock_khz(SYS_FREQ_KHZ, true); // Set the new system clock (in kHz)
    setup_default_uart(); // The UART may not work properly if you do not reset it to default
    PIO pio = pio0; // Identifier for the first (PIO 0) hardware PIO instance
    uint offset = pio_add_program(pio, &PIOMaxSqaureWave_program); // Attempt to load the program
    uint sm = pio_claim_unused_sm(pio, true); // Claim a free state machine on a PIO instance
    PIOMaxSqaureWave_program_init(pio, sm, offset, OUT_PIN, 1.0f); // Initialize the program
    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
   
    while(true)
    {
      printf("\nPi Pico is running at %d.%03d MHz.\n", SYS_FREQ_KHZ / 1000, SYS_FREQ_KHZ % 1000);
      gpio_put(LED_PIN, 1);
      sleep_ms(50);
      gpio_put(LED_PIN, 0);
      sleep_ms(50);
    }
}


PIO 代码 square_wave.pio,文件名改过,代码是一字不变抄来的

.program PIOMaxSqaureWave ;Program name

;Generate a square wave on a GPIO pin

    set pindirs, 1 ;Set pin direction to output
.wrap_target ;Free 0 cycle unconditional jump
    set pins, 1 ;Drive pins high
    set pins, 0 ;Drive pins low
.wrap

;Helper function

%c-sdk{
    static inline void PIOMaxSqaureWave_program_init(PIO pio, uint sm, uint offset, uint pin, float clk_div){ //Program to initialize the PIO
      pio_sm_config c = PIOMaxSqaureWave_program_get_default_config(offset); //Get default configurations for the PIO state machine
      sm_config_set_set_pins(&c, pin, 1); //Set the state machine configurations on the given pin
      sm_config_set_clkdiv(&c, clk_div); //Set the state machine clock divider
      pio_gpio_init(pio, pin); //Setup the function select for a GPIO to use output from the given PIO instance
      pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); //Use a state machine to set the same pin direction for multiple consecutive pins for the PIO instance
      pio_sm_init(pio, sm, offset, &c); //Resets the state machine to a consistent state, and configures it
      pio_sm_set_enabled(pio, sm, true); //Enable or disable a PIO state machine
    }
%}


最后是 CMakeLists.txt 文件,其中增加了更改 SPI Flash 时钟分频设定的内容(第 55 - 57 行)

# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)
set(PICO_SDK_PATH "/home/gamalot/pico/pico-sdk")

set(PICO_BOARD pico CACHE STRING "Board type")

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)

if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()

project(pico_oc_test C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Add executable. Default name is the project name, version 0.1

add_executable(pico_oc_test pico_oc_test.c )

pico_set_program_name(pico_oc_test "pico_oc_test")
pico_set_program_version(pico_oc_test "0.1")

pico_enable_stdio_uart(pico_oc_test 1)
pico_enable_stdio_usb(pico_oc_test 1)

# Add the standard library to the build
target_link_libraries(pico_oc_test
      pico_stdlib)

# Add the standard include files to the build
target_include_directories(pico_oc_test PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
)

# Add any user requested libraries
target_link_libraries(pico_oc_test
      hardware_pio
      )

# Generate header from .pio file
pico_generate_pio_header(pico_oc_test ${CMAKE_CURRENT_LIST_DIR}/square_wave.pio)

# Increase the SPI Flash clock divider setting to 4.
pico_define_boot_stage2(slower_boot2 ${PICO_DEFAULT_BOOT_STAGE2_FILE})
target_compile_definitions(slower_boot2 PRIVATE PICO_FLASH_SPI_CLKDIV=4)
pico_set_boot_stage2(pico_oc_test slower_boot2)

# Add extra outputs
pico_add_extra_outputs(pico_oc_test)



JuncoJet 发表于 2025-1-17 14:19:53

本帖最后由 JuncoJet 于 2025-1-17 14:42 编辑

频率准确度呢,偏差正负3M?
我的梦想是把他当本振用
老外的实现目前只有 30Mhz 以内

量子隧道 发表于 2025-1-17 14:47:16

刚好上周为了学microPython买了片这个小板。此文真及时。
这个板设计好像不错。至少在SI上看起来如此。它的IO与地分配挺合理,任意信号与最近的地间距不超过2。不像有的小系统板,地针数量不够且太集中,即使芯片能超频到200M,用起来也是担心信号质量不行。

量子隧道 发表于 2025-1-17 15:05:14

JuncoJet 发表于 2025-1-17 14:19
频率准确度呢,偏差正负3M?
我的梦想是把他当本振用
老外的实现目前只有 30Mhz 以内

200M,400M,都是PLL倍频出来的,不太可能有3M这么大的偏差。楼主屏幕长余辉显示的应该是jitter。即使是jitter,都不一定是芯片带来的,也有可能是示波器采样和插值带来的。

scoopydoo 发表于 2025-1-17 15:16:44

量子隧道 发表于 2025-1-17 15:05
200M,400M,都是PLL倍频出来的,不太可能有3M这么大的偏差。楼主屏幕长余辉显示的应该是jitter。即使是j ...

哈哈哈,老兄所言极是,俺刚换个示波器截了图正准备回帖呢。

频率肯定是稳的,Jitter 确实有点儿大,不过考虑到这小板子并不是为射频目的设计的,感觉目前这样已经算不错了。

scoopydoo 发表于 2025-1-17 15:18:55

量子隧道 发表于 2025-1-17 14:47
刚好上周为了学microPython买了片这个小板。此文真及时。
这个板设计好像不错。至少在SI上看起来如此。它 ...

树莓派官方支持超频到 250MHz,再高就要加压、调整 SPI Flash 时钟了。

不知道老兄买的是哪一款板子,二代的树莓派 Pico 2 已经出来一段时间了,换了新型号的 MCU。

lxa000 发表于 2025-1-17 15:36:12

哇塞!楼主厉害!
这样以来可以玩不少新花样来 。

JuncoJet 发表于 2025-1-17 15:44:41

二代频率高点,状态机多点,差别不是很大
主要是价格翻倍,也没啥玩的意义

scoopydoo 发表于 2025-1-17 15:49:01

JuncoJet 发表于 2025-1-17 15:44
二代频率高点,状态机多点,差别不是很大
主要是价格翻倍,也没啥玩的意义

二代最大的改变是多了两个 RISC-V 核心,可以选择用 ARM 还是 RISC-V 核心,但是不能同时用。

量子隧道 发表于 2025-1-17 15:50:11

scoopydoo 发表于 2025-1-17 15:18
树莓派官方支持超频到 250MHz,再高就要加压、调整 SPI Flash 时钟了。

不知道老兄买的是哪一款板子, ...

我那个没标pico2,那么应该是1代。
我也不知道啥时候才会有时间把它转起来。这阵子比较忙,都没时间鼓捣业余小玩意儿了。

scoopydoo 发表于 2025-1-17 15:55:46

量子隧道 发表于 2025-1-17 15:50
我那个没标pico2,那么应该是1代。
我也不知道啥时候才会有时间把它转起来。这阵子比较忙,都没时间鼓捣 ...

没有 2 那就是第一代了,不过你既然是初学那就无所谓,更何况你觉得好用的话,早晚会买一堆回来的,俺还没买二代呢手里就已经有 5 片了,3 片普通的 2 片带无线的。

要过年了估计大家都忙,论坛人气儿也差了一些,俺最近也在瞎忙活,再加上儿子放假,上论坛时间也少了。

JuncoJet 发表于 2025-1-17 16:03:24

scoopydoo 发表于 2025-1-17 15:49
二代最大的改变是多了两个 RISC-V 核心,可以选择用 ARM 还是 RISC-V 核心,但是不能同时用。

哦 难怪看资料上 CM33 或 RISCV
还以为两种内核芯片随机

量子隧道 发表于 2025-1-17 16:04:26

scoopydoo 发表于 2025-1-17 15:55
没有 2 那就是第一代了,不过你既然是初学那就无所谓,更何况你觉得好用的话,早晚会买一堆回来的,俺还 ...

我是想,鼓捣了这么多年arduino,总得出息长进一点,就买了个这玩意打算练练手。:lol

JuncoJet 发表于 2025-1-17 16:12:40


2040主要是玩个便宜

scoopydoo 发表于 2025-1-17 22:00:23

量子隧道 发表于 2025-1-17 16:04
我是想,鼓捣了这么多年arduino,总得出息长进一点,就买了个这玩意打算练练手。

老兄有 Arduino 基础的话,鼓捣这个 Pico 那自然是不在话下! :lol
页: [1] 2 3
查看完整版本: [实验] 树莓派 Pico 超频并实现 IO 引脚快速翻转