FreeRTOS Symmetric Multiprocessing (SMP) is a recent version of the RTOS that can schedule tasks across multiple controller cores. It's currently in test phase, and they have a version for the RP2040. In this blog post, I add a first blinky task to my own little SMP project in VSCode. |
Create a Blinky task
Our first attempt to write a custom task is a very simple blinky. It's a simplification of the RP2040 FreeRTOS Blinky example. First a few constants that will be used by the task:
#define mainTASK_LED (PICO_DEFAULT_LED_PIN) #define mainBLINK_TASK_PRIORITY (tskIDLE_PRIORITY +1) #define mainBLINK_TASK_FREQUENCY_MS (1000 / portTICK_PERIOD_MS)
The first one defines the LED. he second line the priority (lowest, just above the idle priority. The third definition is the frequency for toggling the LED (1 second). I'm trying to stay close to the FreeRTOS naming and formatting conventions.
To make the task a little bit interesting, I used a mechanism that wakes up the task on a certain interval - not after a time delay. This is mimicking what real-time designs do.
static void prvBlinkTask(void *pvParameters) { (void)pvParameters; TickType_t xNextWakeTime; /* Initialise xNextWakeTime - this only needs to be done once. */ xNextWakeTime = xTaskGetTickCount(); for (;;) { gpio_xor_mask(1u << mainTASK_LED); xTaskDelayUntil(&xNextWakeTime, mainBLINK_TASK_FREQUENCY_MS); } }
Before entering the task loop, we initialise the tick counter. The loop first toggles the LED. Then it tells the scheduler to deactivate the task and wake it up again when 1 second expired since the stored tick count. The OS will update that tick count when the focus returns. On the image below, you can see that the count is 2000 after 2 sleep sessions.
Register the task
An API function is called to register the task in the scheduler. This has to be done before the scheduler is started. Creating the task does not start it. The scheduler will do that as soon as it is kicked off, and there's a slot for the task to run in.
prvSetupHardware(); xTaskCreate(prvBlinkTask, "blink", configMINIMAL_STACK_SIZE, NULL, mainBLINK_TASK_PRIORITY, NULL); /* Start the tasks and timer running. */ vTaskStartScheduler();
You can now build and run the project. The result is that the LED will toggle every second.