游乐游手机版
首页/AI热点日报/热点详情

树莓派Pico Flash驱动踩坑与解决经验

类型:热点整理2026-07-20
树莓派PicoFlash驱动开发中,使用OpenOCD将程序下载到Flash运行后,调用flash_range_erase等擦除函数会导致程序跑飞,原因是SDK为这些函数添加了__no_inline_not_in_flash_func属性,禁止在Flash中执行。解决方法是改用UF2文件下载,或确保此类函数在RAM中运行。

树莓派 Pico 内置了 2MB 的 Flash 存储器,在基于官方 Pico C/C++ SDK 对接 Flash 驱动时,开发者可能会遇到一些意想不到的坑。本文总结了我在实际开发中踩过的雷和对应的解决方案,希望能帮助你顺利实现树莓派 Pico 的 Flash 读写操作,少走弯路。

准备工作

开发环境基于 CLionPicoProbe 对树莓派 Pico 进行下载与调试。如果你还不熟悉这个流程,可以翻阅我之前的相关文章,了解如何搭建环境并开始 Pico 开发。


一、Flash Read 的实现

官方 SDK 没有提供专门的 Flash 读取 API,因此我们直接使用 memcpyXIP(eXecute In Place) 映射地址读取数据。具体实现如下:

int _flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
{
    rt_memcpy(buf, (const void *)(XIP_BASE + addr), size);
    return size;
}

提示: XIP_BASE 是 Flash 在内存中的映射基地址,直接通过指针访问即可完成读取,需要注意指针地址的正确性。

二、测试中遇到的问题

问题现象

我基于官方 examples 中的例程改写了一个测试函数,代码如下(片段):

#define FLASH_TARGET_OFFSET (256 * 1024)
const uint8_t *flash_target_contents = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);

void print_buf(const uint8_t *buf, size_t len) {
    for (size_t i = 0; i < len; ++i) {
        rt_kprintf("%02x", buf[i]);
        if (i % 16 == 15)
            rt_kprintf("\n");
        else
            rt_kprintf(" ");
    }
}

int flash_test() {
    uint8_t read_data[FLASH_PAGE_SIZE];
    uint8_t random_data[FLASH_PAGE_SIZE];
    for (int i = 0; i < FLASH_PAGE_SIZE; ++i)
        random_data[i] = i;

    rt_kprintf("Generated random data:\n");
    print_buf(random_data, FLASH_PAGE_SIZE);

    // Note that a whole number of sectors must be erased at a time.
    rt_kprintf("\nErasing target region...\n");
    _flash_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
    
    rt_kprintf("Done. Read back target region:\n");
    // print_buf(flash_target_contents, FLASH_PAGE_SIZE);

    rt_kprintf("\nProgramming target region...\n");
    _flash_write(FLASH_TARGET_OFFSET, random_data, FLASH_PAGE_SIZE);
    rt_kprintf("Done. Read back target region:\n");
    _flash_read(FLASH_TARGET_OFFSET, read_data, FLASH_PAGE_SIZE);
    print_buf(read_data, FLASH_PAGE_SIZE);

    bool mismatch = false;
    for (int i = 0; i < FLASH_PAGE_SIZE; ++i) {
        if (random_data[i] != flash_target_contents[i])
            mismatch = true;
    }
    if (mismatch)
        rt_kprintf("Programming failed!\n");
    else
        rt_kprintf("Programming successful!\n");
}
MSH_CMD_EXPORT(flash_test, flash test);

运行到 rt_kprintf("\nErasing target region...\n") 以后,程序就卡住了。通过 debug 发现,程序在执行 Flash 擦除时 跑飞了,无法继续执行。

根因分析

查看 SDK 中 flash_range_erase 的实现:

void __no_inline_not_in_flash_func(flash_range_erase)

关键点在于修饰符 __no_inline_not_in_flash_func,它表明该函数不能在 Flash 中调用。而我当时使用 OpenOCD 将程序下载到 Flash 中运行,程序在执行擦除操作时,自身代码也在 Flash 中,因此导致程序跑飞,这是典型的 Pico Flash 驱动开发陷阱。

三、解决办法

改用常规的 UF2 文件 下载程序到 Pico 后,测试正常通过。以下是正确的运行输出:

msh >flash_test
Generated random data:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f
90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f
a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
Erasing target region...
Done. Read back target region:
Programming target region...
Done. Read back target region:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f
90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f
a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
Programming successful!

小提示: 如果你仍在 OpenOCD 环境下调试,不要调用 flash_range_eraseflash_range_program 等标有 __no_inline_not_in_flash_func 的函数。如果必须调用,请确保你的程序运行在 RAM 中(例如通过 SRAM 启动或使用 boot2 重定位)。这是避免 Pico Flash 驱动故障的关键。

四、常见问题与解答

Q1:为什么使用 OpenOCD 下载到 Flash 后,擦除/写入函数会跑飞?

A: 官方 SDK 中 Flash 擦除和写入函数带有 __no_inline_not_in_flash_func 属性,强制要求这些代码不能在 Flash 存储区中执行(否则会导致指令读取冲突)。OpenOCD 默认将用户程序直接下载到 Flash 并执行,此时程序自身就位于 Flash 中,调用这些函数会引发错误。推荐使用 UF2 文件 或先将关键函数复制到 RAM 中再调用,这是 Pico 开发中常见的注意事项。

Q2:如何将 Flash 操作相关的函数放到 RAM 中执行?

A: SDK 已经提供了 __no_inline_not_in_flash_func 宏,你可以在自己的代码中对需要从 RAM 执行的函数添加该宏。另外,使用 __attribute__((section(".ram_code"))) 也可以将指定函数放置到 RAM 的特定段中。更简单的做法是直接使用 UF2 方式下载程序,让 Bootloader 自动将代码复制到 RAM 中运行,从而规避 Flash 执行冲突。

Q3:官方例程中的 flash_range_eraseflash_range_program 到底能不能在 Flash 中调用?

A: 不能。这两类函数都带有 __no_inline_not_in_flash_func 属性,官方文档也明确说明它们必须在 RAM 中执行。例程通常通过 UF2 文件下载,此时 Bootloader 会把代码先加载到 RAM 执行,所以没有问题。如果你直接用 debugger 下载并运行,就会踩坑,这也是很多开发者初次接触 Pico Flash 驱动时容易犯的错误。


通过以上调整,你就可以顺利地在树莓派 Pico 上实现 Flash 读写驱动了。记住:永远不要在 Flash 中调用 Flash 擦除/写入函数,这是 SDK 设计上的硬性约束,也是 Pico 开发中必须牢记的原则。

来源:https://m.elecfans.com/article/2274803.html

相关热点

继续查看同栏目近期热点。

延伸阅读

补充最近整理过的热点入口。