|
| 1 | +/** |
| 2 | + * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include "pico/stdlib.h" |
| 9 | +#include "pico/util/queue.h" |
| 10 | +#include "pico/multicore.h" |
| 11 | + |
| 12 | +#define FLAG_VALUE 123 |
| 13 | + |
| 14 | +typedef struct |
| 15 | +{ |
| 16 | + void *func; |
| 17 | + int32_t data; |
| 18 | +} queue_entry_t; |
| 19 | + |
| 20 | +queue_t call_queue; |
| 21 | +queue_t results_queue; |
| 22 | + |
| 23 | +void core1_entry() { |
| 24 | + while (1) { |
| 25 | + // Function pointer is passed to us via the queue_entry_t which also |
| 26 | + // contains the function parameter. |
| 27 | + // We provide an int32_t return value by simply pushing it back on the |
| 28 | + // return queue which also indicates the result is ready. |
| 29 | + |
| 30 | + queue_entry_t entry; |
| 31 | + |
| 32 | + queue_remove_blocking(&call_queue, &entry); |
| 33 | + |
| 34 | + int32_t (*func)() = (int32_t(*)())(entry.func); |
| 35 | + int32_t result = (*func)(entry.data); |
| 36 | + |
| 37 | + queue_add_blocking(&results_queue, &result); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +int32_t factorial(int32_t n) { |
| 42 | + int32_t f = 1; |
| 43 | + for (int i = 2; i <= n; i++) { |
| 44 | + f *= i; |
| 45 | + } |
| 46 | + return f; |
| 47 | +} |
| 48 | + |
| 49 | +int32_t fibonacci(int32_t n) { |
| 50 | + if (n == 0) return 0; |
| 51 | + if (n == 1) return 1; |
| 52 | + |
| 53 | + int n1 = 0, n2 = 1, n3; |
| 54 | + |
| 55 | + for (int i = 2; i <= n; i++) { |
| 56 | + n3 = n1 + n2; |
| 57 | + n1 = n2; |
| 58 | + n2 = n3; |
| 59 | + } |
| 60 | + return n3; |
| 61 | +} |
| 62 | + |
| 63 | +#define TEST_NUM 10 |
| 64 | + |
| 65 | +int main() { |
| 66 | + int32_t res; |
| 67 | + |
| 68 | + stdio_init_all(); |
| 69 | + printf("Hello, multicore_runner using queues!\n"); |
| 70 | + |
| 71 | + // This example dispatches arbitrary functions to run on the second core |
| 72 | + // To do this we run a dispatcher on the second core that accepts a function |
| 73 | + // pointer and runs it. The data is passed over using the queue library from |
| 74 | + // pico_utils |
| 75 | + |
| 76 | + queue_init(&call_queue, sizeof(queue_entry_t), 2); |
| 77 | + queue_init(&results_queue, sizeof(int32_t), 2); |
| 78 | + |
| 79 | + multicore_launch_core1(core1_entry); |
| 80 | + |
| 81 | + queue_entry_t entry = {&factorial, TEST_NUM}; |
| 82 | + queue_add_blocking(&call_queue, &entry); |
| 83 | + |
| 84 | + // We could now do a load of stuff on core 0 and get our result later |
| 85 | + |
| 86 | + queue_remove_blocking(&results_queue, &res); |
| 87 | + |
| 88 | + printf("Factorial %d is %d\n", TEST_NUM, res); |
| 89 | + |
| 90 | + // Now try a different function |
| 91 | + entry.func = &fibonacci; |
| 92 | + queue_add_blocking(&call_queue, &entry); |
| 93 | + |
| 94 | + queue_remove_blocking(&results_queue, &res); |
| 95 | + |
| 96 | + printf("Fibonacci %d is %d\n", TEST_NUM, res); |
| 97 | +} |
0 commit comments