【问题标题】:How to run code on the second core on Raspberry Pico如何在 Raspberry Pico 的第二个核心上运行代码
【发布时间】:2022-05-20 06:34:59
【问题描述】:

我正在尝试让多核在我的 pico 上工作,

#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"

void core1_main()
{

    stdio_init_all();

    while (1)
    {
        uint32_t t = multicore_fifo_pop_blocking();
        printf("hellow world %d \n", t);
    }
}

int main()
{
    multicore_launch_core1(&core1_main);
    uint32_t i = 0;

    while (1)
    {
        sleep_ms(250);
        multicore_fifo_push_blocking(i++);
    }
}

这是我试图开始工作的一项非常基本的任务。我正在尝试更多地了解这种多核魔法。基本上我开始在 core1 上等待一些数据通过。然后我只需将其打印出来并等待下一条数据。 在核心 0 上,我每 250 毫秒将一个数字推入 FIFO 一次。
我在编译中没有收到任何错误,但运行代码不会产生任何输出。
我在这里做错了什么?有什么需要注意的吗?

我已经尝试了很多东西来获得多核,但没有用。

更新这给了我一些输出。 我添加了等待 USB 连接和初始化的时间。现在我从核心 2 收到一些消息。

#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"

// const uint led = PICO_DEFAULT_LED_PIN;

void core1_main()
{
    printf("hellow world from second core");
    printf("hellow world from second core");
    printf("hellow world from second core");
}

int main()
{
    stdio_init_all();
    while (!stdio_usb_connected())
        ;
    while (!stdio_usb_init())
        ;

    multicore_launch_core1(core1_main);
    printf("hellow wow \n");

    uint32_t i = 0;

    while (1)
    {
        printf("hellow nice %d\n", i++);
        sleep_ms(1000);
    }
}

这是我得到的输出。注意来自第二个核心的消息只通过一次。我很困惑,为什么?

同时改变stdio_init_all() 的位置会破坏一些东西并且没有更多的输出。

【问题讨论】:

  • 如果您删除对multicore_fifo_pop_blocking() 的调用(或在其前面放置printf() 语句),您会看到任何输出吗?这将有助于区分“core1_main 根本没有运行”与“core1_main 正在阻塞并且 fifo 正在按预期工作”。
  • @larsks 我在帖子中添加了更多信息。谢谢

标签: c microcontroller cortex-m raspberry-pi-pico


【解决方案1】:

我无法重现此问题。我从您的代码的略微修改版本开始(我想要在core1_main 中循环,以便在连接到串行端口之前不会错过输出):

#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"

void core1_main()
{
    while (1) {
        printf("hello world from second core
");
        sleep_ms(1000);
    }
}

int main()
{
    stdio_init_all();

    multicore_launch_core1(core1_main);
    printf("hello wow
");

    uint32_t i = 0;

    while (1)
    {
        printf("hello nice %u
", i++);
        sleep_ms(1000);
    }
}

在 pico sdk 文档之后,我有以下CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

# initialize the SDK based on PICO_SDK_PATH
# note: this must happen before project()
include(pico_sdk_import.cmake)

project(my_project)

# initialize the Raspberry Pi Pico SDK
pico_sdk_init()

add_executable(multicore_test
    multicore_test.c
)

target_link_libraries(multicore_test pico_stdlib pico_multicore)

pico_enable_stdio_usb(multicore_test 1)
pico_enable_stdio_uart(multicore_test 0)


pico_add_extra_outputs(multicore_test)

如果我构建代码:

mkdir build
cd build
cmake ..
make

然后将生成的multicore_test.uf2 安装到 Pico 上,当我连接到串行端口(我的系统上的/dev/ttyACM0)时,我看到:

hello world from second core
hello nice 3
hello world from second core
hello nice 4
hello world from second core
hello nice 5
hello world from second core
hello nice 6
...

【讨论】:

    猜你喜欢
    • 2021-06-23
    • 1970-01-01
    • 2022-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    相关资源
    最近更新 更多