|
| 1 | +#include <Arduino.h> |
| 2 | +#include "pico/async_context_threadsafe_background.h" |
| 3 | + |
| 4 | +bool core1_separate_stack = true; |
| 5 | + |
| 6 | +volatile bool operational = false; |
| 7 | +volatile bool serial_ready = false; |
| 8 | +static volatile uint64_t timestamp_exit = 0; |
| 9 | +static volatile uint64_t timestamp_enter = 0; |
| 10 | + |
| 11 | +static async_context_threadsafe_background_t asyncCtx; |
| 12 | + |
| 13 | +uint32_t doSomeWork(void* param) { |
| 14 | + const auto value = static_cast<uint32_t*>(param); |
| 15 | + (*value)++; |
| 16 | + // Log timestamp right before sem_release will happen |
| 17 | + if (const auto worker = static_cast<async_when_pending_worker*>(asyncCtx.core.when_pending_list)) { |
| 18 | + Serial1.printf("[INFO][%u][%llu] Pre-sem_release worker state:\n" |
| 19 | + " address: %p\n" |
| 20 | + " next: %p\n" |
| 21 | + " do_work: %p\n" |
| 22 | + " work_pending: %d\n" |
| 23 | + " value: %d\n", |
| 24 | + get_core_num(), |
| 25 | + to_us_since_boot(get_absolute_time()), |
| 26 | + worker, |
| 27 | + worker->next, |
| 28 | + worker->do_work, |
| 29 | + worker->work_pending, |
| 30 | + *value); |
| 31 | + } |
| 32 | + return *value; |
| 33 | +} |
| 34 | + |
| 35 | +void setup() { |
| 36 | + Serial1.setRX(PIN_SERIAL1_RX); |
| 37 | + Serial1.setTX(PIN_SERIAL1_TX); |
| 38 | + Serial1.setPollingMode(true); |
| 39 | + Serial1.begin(115200); |
| 40 | + |
| 41 | + while (!Serial1) { |
| 42 | + delay(10); |
| 43 | + } |
| 44 | + |
| 45 | + RP2040::enableDoubleResetBootloader(); |
| 46 | + serial_ready = true; |
| 47 | + while (!operational) { |
| 48 | + delay(10); |
| 49 | + } |
| 50 | + |
| 51 | + Serial1.printf("C0 ready...\n"); |
| 52 | +} |
| 53 | + |
| 54 | +void setup1() { |
| 55 | + while (!serial_ready) { |
| 56 | + delay(10); |
| 57 | + } |
| 58 | + async_context_threadsafe_background_config_t cfg = async_context_threadsafe_background_default_config(); |
| 59 | + operational = async_context_threadsafe_background_init(&asyncCtx, &cfg); |
| 60 | + assert(operational); |
| 61 | + Serial1.printf("C1 ready...\n"); |
| 62 | +} |
| 63 | + |
| 64 | +void loop() { |
| 65 | + static unsigned long c0_counter = 0; |
| 66 | + static uint32_t myNumber = 0; |
| 67 | + |
| 68 | + if (c0_counter % 111 == 0) { |
| 69 | + timestamp_enter = to_us_since_boot(get_absolute_time()); |
| 70 | + const auto rc = async_context_execute_sync(&asyncCtx.core, doSomeWork, &myNumber); |
| 71 | + assert(rc == myNumber); |
| 72 | + timestamp_exit = to_us_since_boot(get_absolute_time()); |
| 73 | + Serial1.printf("value: %d; enter: %llu; exit: %llu\n", rc, timestamp_enter, timestamp_exit); |
| 74 | + } |
| 75 | + c0_counter++; |
| 76 | + delay(1); |
| 77 | +} |
| 78 | + |
| 79 | +void loop1() { |
| 80 | + tight_loop_contents(); |
| 81 | +} |
0 commit comments