zl程序教程

您现在的位置是:首页 >  其它

当前栏目

K210 FreeRTOS多任务多核系统调度

多任务 freeRTOS 多核
2023-09-11 14:21:44 时间

一、目的

众所周知,K210这款AI新品是一款64bit 双核芯片,其支持裸机编程,并且官方也提供freertos sdk,方便开发者在其上进行多任务应用开发。那么如何进行任务创建和多核开发呢。

二、参考

#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"

static void task_0(void *args) {
    while (1) {
        printf("task 0 on core %d is polling\n", (int)uxPortGetProcessorId());
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    vTaskDelete(NULL);
}

static void task_1(void *args) {
    while (1) {
        printf("task 1 on core %d is polling\n", (int)uxPortGetProcessorId());
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    vTaskDelete(NULL);
}

int main() {
    printf("Hello K210!!!\n");
    //在core 0上创建一个优先级为5的任务task_0
    xTaskCreateAtProcessor(0, task_0, "task0", 1024, NULL, 5, NULL);
    //在core 1上创建一个优先级为5的任务task_0
    xTaskCreateAtProcessor(1, task_1, "task1", 1024, NULL, 5, NULL);
    while (1) {
        printf("main on core %d is polling\n", (int)uxPortGetProcessorId());
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
}

在上一篇博文中,我们已经介绍过,main函数是freertos在core 0上创建的一个线程,构建烧写后从日志上可以看到跟预期一下。

--- Miniterm on /dev/ttyUSB0  115200,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
Hello K210!!!
task 0 on core 0 is polling
task 1  nocer0 i  polling
g
task 0 on core 0 is polling
task 1 on core 1 is polling
task 0 on core 0 is polling
main on core 0 ta ko1lon 
ore 1 is polling
task 0 on core 0 is polling
task 1 on core 1 is polling
task 0 on core 0 is polling
main on core 0 is polling
task 1 on core 1 is polling
task 0 on core 0 is polling
task 1 on core 1 is polling
task 0 on core 0 is polling
main on core 0 is polling
task 1 on core 1 is polling

至此,一个最简单的多任务多核应用介绍结束。