forked from raspberrypi/pico-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
multicore_runner.c
83 lines (61 loc) · 1.93 KB
/
multicore_runner.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
/// \tag::multicore_dispatch[]
#define FLAG_VALUE 123
void core1_entry() {
while (1) {
// Function pointer is passed to us via the FIFO
// We have one incoming int32_t as a parameter, and will provide an
// int32_t return value by simply pushing it back on the FIFO
// which also indicates the result is ready.
int32_t (*func)() = (int32_t(*)()) multicore_fifo_pop_blocking();
int32_t p = multicore_fifo_pop_blocking();
int32_t result = (*func)(p);
multicore_fifo_push_blocking(result);
}
}
int32_t factorial(int32_t n) {
int32_t f = 1;
for (int i = 2; i <= n; i++) {
f *= i;
}
return f;
}
int32_t fibonacci(int32_t n) {
if (n == 0) return 0;
if (n == 1) return 1;
int n1 = 0, n2 = 1, n3 = 0;
for (int i = 2; i <= n; i++) {
n3 = n1 + n2;
n1 = n2;
n2 = n3;
}
return n3;
}
#define TEST_NUM 10
int main() {
int res;
stdio_init_all();
printf("Hello, multicore_runner!\n");
// This example dispatches arbitrary functions to run on the second core
// To do this we run a dispatcher on the second core that accepts a function
// pointer and runs it
multicore_launch_core1(core1_entry);
multicore_fifo_push_blocking((uintptr_t) &factorial);
multicore_fifo_push_blocking(TEST_NUM);
// We could now do a load of stuff on core 0 and get our result later
res = multicore_fifo_pop_blocking();
printf("Factorial %d is %d\n", TEST_NUM, res);
// Now try a different function
multicore_fifo_push_blocking((uintptr_t) &fibonacci);
multicore_fifo_push_blocking(TEST_NUM);
res = multicore_fifo_pop_blocking();
printf("Fibonacci %d is %d\n", TEST_NUM, res);
}
/// \end::multicore_dispatch[]