It has been a while since I last updated. Yesterday, I saw that the official SDK for running FreeRTOS on the core was released. Coincidentally, today is the weekend, so I did a git pull for the latest code and tried it out.
1. How to Add RTOS Task Code?
First, I took a quick look at the code. In the SDK under FreeRTOS, there is a folder named civtek. I opened the task file directly. There are two files inside: one is common, and the other is main.
I opened main.c, and without looking at the rest, I found main_cvirtos(), which is the entry function for FreeRTOS.
int main(void){ pre_system_init(); printf("CVIRTOS Build Date:%s (Time :%s) \n", __DATE__, __TIME__);#ifndef __riscv mmu_enable(); printf("enable I/D cache & MMU done\n");#endif /* Configure the hardware ready to run the demo. */ prvSetupHardware();#if ( configUSE_TRACE_FACILITY == 1 ) vTraceEnable(TRC_START);#endif post_system_init();
#ifdef CVIRTOS { main_cvirtos(); // Entry function }#else#error "Not correct running definition"#endif
/* Don't expect to reach here. */ return 0;}
So where is main_cvirtos()? I continued to open the common file and found it! The path is: task/comm/src/riscv64. The source code is very simple:
void main_cvirtos(void){ printf("create cvi task\n"); /* Start the tasks and timer running. */ Here! Here! Here you can add the source code! /* If all is well, the scheduler will now be running, and the following line will never be reached. If the following line does execute, then there was either insufficient FreeRTOS heap memory available for the idle and/or timer tasks to be created, or vTaskStartScheduler() was called from User mode. See the memory management section on the FreeRTOS web site for more details on the FreeRTOS heap http://www.freertos.org/a00111.html. The mode from which main() is called is set in the C start up code and must be a privileged mode (not user mode). */ printf("cvi task end\n");
for (;;) ;}
Next, let’s create a task using the FreeRTOS API!
2. A Simple Attempt: Let’s Create a FreeRTOS Version of Hello World!
Here, since the official uart0 driver is already set up, I can simply use printf. The modified common_main.c is as follows:
/* Standard includes. */#include <stdio.h>
/* Kernel includes. */#include "FreeRTOS.h"#include "task.h"#include "semphr.h"#include "mmio.h"#include "delay.h"
/* cvitek includes. */#include "printf.h"#include "rtos_cmdqu.h"#include "cvi_mailbox.h"#include "intr_conf.h"#include "top_reg.h"#include "memmap.h"#include "comm.h"#include "cvi_spinlock.h"
//#define __DEBUG__
#ifdef __DEBUG__#define debug_printf printf#else#define debug_printf(...)#endif
/**************************************************************************** * Function prototypes ****************************************************************************/void my_task_test();
/**************************************************************************** * Global parameters ****************************************************************************/
/* mailbox parameters */volatile struct mailbox_set_register *mbox_reg;volatile struct mailbox_done_register *mbox_done_reg;volatile unsigned long *mailbox_context; // mailbox buffer context is 64 Bytes
/**************************************************************************** * Function definitions ****************************************************************************/
DEFINE_CVI_SPINLOCK(mailbox_lock, SPIN_MBOX);
void main_cvirtos(void){ printf("create cvi task\n");
xTaskCreate(my_task_test, "my_task", 1024 * 8, NULL, 1, NULL); vTaskStartScheduler();
/* Start the tasks and timer running. */
/* If all is well, the scheduler will now be running, and the following line will never be reached. If the following line does execute, then there was either insufficient FreeRTOS heap memory available for the idle and/or timer tasks to be created, or vTaskStartScheduler() was called from User mode. See the memory management section on the FreeRTOS web site for more details on the FreeRTOS heap http://www.freertos.org/a00111.html. The mode from which main() is called is set in the C start up code and must be a privileged mode (not user mode). */ printf("cvi task end\n");
for (;;) ;}
void my_task_test(){ int index = 0; for (;;) { printf("test the RTOS: %d\r\n", index); vTaskDelay(100); index++; }
After recompiling and connecting to serial port 0, I used the official baseboard. After powering on, I found that FreeRTOS was running normally. Since the serial port is shared with Linux, I could see that the serial port resources were constantly being preempted.

It can be seen that even when the large core’s Linux is running, FreeRTOS running on the small core is still printing counts.
After entering the Linux system startup, RTOS remained as follows:

Finally, no matter what was printed, when I typed halt in the Linux command line, both cores stopped.

3. Issues Encountered and Solutions.
-
You need to modify the FreeRTOS config configuration file to disable configUSE_TICK_HOOK, otherwise it will prompt an error!
-
At first, I thought it was similar to the ESP32 idf development, where you just need to write task programs without enabling the scheduler. However, I found that it cannot run unless the scheduler is enabled at the end!
Alright, that’s all for sharing. Friends who are interested can give it a try!