4

I'm trying to get multicore working on my 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++);
    }
}

This is a very basic task I'm trying to get to work. I'm trying to learn more about this multicore magic. Basically I'm starting waiting on core1 for some data to come through. Then I simply print it out and wait for the next piece of data. On core 0 I push a number onto the FIFO once every 250ms.
I don't get any error in compilation but running the code produces no output whatsoever.
What am I doing wrong here? Is there something that I should pat attention to?

I've tried quite a few things to get something multicore, but no use.

UPDATE This gives me some output. I added a wait for the USB to get connected and initialised. Now I get some message from core 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);
    }
}

This is the output I get. Notice the message from second core comes through only once.I confused, why?

Also changing the position of stdio_init_all() breaks something and no more output.

Output from minicom

Jonas
  • 121,568
  • 97
  • 310
  • 388
raider0ne
  • 83
  • 6
  • If you remove the call to `multicore_fifo_pop_blocking()` (or place a `printf()` statement before it), do you see any output? That would help differentiate between "`core1_main` is not running at all" vs "`core1_main` is blocking and the fifo is working as you expect". – larsks May 08 '22 at 01:57
  • [pico_multicore - C-API](https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore) provides both examples and reference. – David C. Rankin Mar 21 '23 at 04:11

2 Answers2

4

I cannot reproduce this problem. I started with a slightly modified version of your code (I wanted a loop in core1_main so that I didn't miss output before connecting to the serial port):

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

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

int main()
{
    stdio_init_all();

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

    uint32_t i = 0;

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

I have the following CMakeLists.txt, following the pico sdk documentation:

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)

If I build the code:

mkdir build
cd build
cmake ..
make

And then install the resulting multicore_test.uf2 onto the Pico, when I connect to the serial port (/dev/ttyACM0 on my system), I see:

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
...
larsks
  • 277,717
  • 41
  • 399
  • 399
0

I had the exact same problem. Spinning up core 1 and after a few seconds it just stopped. My code looked like this:

void main2() {
    printf("Entered core1 (core=%d)\n", get_core_num());

    while(1) {        
        printf("ping (core=%d)\n", get_core_num());        
        sleep_ms(1000);
    }
}

int main() {
    stdio_init_all();

    printf("Entered core0 (core=%d)\n", get_core_num());

    multicore_launch_core1(main2);    

    while(1) {
        printf("ping (core=%d)\n", get_core_num());
        sleep_ms(1000);
    }
}

And the output I got was:

Entered core0 (core=0)
ping (core=0)
Entered core1 (core=1)
ping (core=1)
ping (core=0)
ping (core=0)
ping (core=0)
ping (core=0)
ping (core=0)
ping (core=0)
...

Core 1 printed one, sometimes a couple of times and then stopped. The solution to the problem was to sleep after stdio_init_all().

void main2() {
    printf("Entered core1 (core=%d)\n", get_core_num());

    while(1) {        
        printf("ping (core=%d)\n", get_core_num());        
        sleep_ms(1000);
    }
}

int main() {
    stdio_init_all();

    // There isn't a strict rule that you must always call sleep_ms()
    // after stdio_init_all(). However, in some cases, it can be a helpful
    // precautionary measure to ensure that the UART has properly 
    // initialized and is ready to transmit data without any issues.
    sleep_ms(2000);

    printf("Entered core0 (core=%d)\n", get_core_num());

    multicore_launch_core1(main2);    

    while(1) {
        printf("ping (core=%d)\n", get_core_num());
        sleep_ms(1000);
    }
}

I tried multiple times with and without sleep(). Without it I always got the problem and with it the problem always went away.

I didn't figure this one out by myself but asked ChatGPT. It told me:

There isn't a strict rule that you must always call sleep_ms() after stdio_init_all(). However, in some cases, it can be a helpful precautionary measure to ensure that the UART has properly initialized and is ready to transmit data without any issues.

But it refused to tell me where this was documented. I googled it, but didn't find any other information about it. I am sure it is out there, I didn't look that hard.

cptcactus
  • 212
  • 1
  • 5