'How to run code on the second core on Raspberry Pico
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.
Solution 1:[1]
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
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | larsks |

