当前位置: 首页 > article >正文

freeRTOS创建任务

一.动态创建任务

1.函数xTaskCreate()

BaseType_t xTaskCreate( 
            TaskFunction_t pxTaskCode, // 函数指针, 任务函数
            const char * const pcName, // 任务的名字
            const configSTACK_DEPTH_TYPE usStackDepth, // 栈大小,单位为word,10表示40字节
            void * const pvParameters, // 调用任务函数时传入的参数
            UBaseType_t uxPriority, // 优先级
            TaskHandle_t * const pxCreatedTask ); // 任务句柄, 以后使用它来操作这个任务

2.实现 

void Task1Function(void*param)
{
	while(1)
	{
		printf("1");
	}
}

void Task2Function(void*param)
{
	while(1)
	{
		printf("2");
	}
}

int main( void )
{

	TaskHandle_t xHandleTask1;
#ifdef DEBUG
  debug();
#endif

	prvSetupHardware();

	printf("hello,world,nimade!\r\n");

	xTaskCreate(Task1Function,"Task1",100,NULL,1,&xHandleTask1);
	xTaskCreate(Task2Function,"Task2",100,NULL,1,NULL);
	/* Start the scheduler. */
	vTaskStartScheduler();

	/* Will only get here if there was not enough heap space to create the
	idle task. */
	return 0;
}

二.静态创建任务

TCB结构体得事先分配好栈,栈要事先分配好定义一个数组。

1.函数xTaskCreateStatic()

    TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
                                    const char * const pcName, 
                                    const uint32_t ulStackDepth,
                                    void * const pvParameters,
                                    UBaseType_t uxPriority,
                                    StackType_t * const puxStackBuffer,
                                    StaticTask_t * const pxTaskBuffer )

配置这个函数要先配置configSUPPORT_STATIC_ALLOCATION

 同时还要配置Idle线程

StackType_t xIdleTaskStack[100];//100*4字节
StaticTask_t xIdleTaskTCB;

void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
                                    StackType_t ** ppxIdleTaskStackBuffer,
                                    uint32_t * pulIdleTaskStackSize )
{
    *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
    *ppxIdleTaskStackBuffer = xIdleTaskStack;
    *pulIdleTaskStackSize = 100;
}

2.实现

void Task4Function(void*param)
{
	while(1)
	{     
        printf("3");
	}
}

StackType_t xTask4Stack[100];//100*4字节
StaticTask_t xTask4TCB;

StackType_t xIdleTaskStack[100];//100*4字节
StaticTask_t xIdleTaskTCB;

void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
                                    StackType_t ** ppxIdleTaskStackBuffer,
                                    uint32_t * pulIdleTaskStackSize )
{
    *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
    *ppxIdleTaskStackBuffer = xIdleTaskStack;
    *pulIdleTaskStackSize = 100;
}

int main( void )
{
    LED_Init();
	TaskHandle_t xHandleTask1;
#ifdef DEBUG
  debug();
#endif

	prvSetupHardware();

	printf("hello,world,nimade!\r\n");

	xTaskCreateStatic(Task4Function,"Task4",100,NULL,1,xTask4Stack,&xTask4TCB);

	/* Start the scheduler. */
	vTaskStartScheduler();

	/* Will only get here if there was not enough heap space to create the
	idle task. */
	return 0;
}


http://www.kler.cn/news/157543.html

相关文章:

  • rust入门(rust教程、rust安装方法)
  • 【云原生 | Docker】Docker核心概念 应用上手最佳流程
  • Unity版本使用情况统计(更新至2023年10月)
  • ESP32-Web-Server编程-通过 Web 下载文本
  • 基于APM(PIX)飞控和mission planner制作遥控无人车-从零搭建自主pix无人车普通舵机转向无人车-1(以乐迪crossflight飞控为例)
  • 关于#c++#的问题:将输入的字符串a复制给字符串b,然后用数组名a和b输出两个字符串(相关搜索:指针变量)
  • qt使用wimlib-imagex,做windows系统备份还原
  • python爬虫非对称加密RSA案例:某观鸟网站
  • 【离散数学】——期末刷题题库(集合)
  • 如何使用Node.js快速创建本地HTTP服务器并实现异地远程访问
  • 源码安装git
  • php之zip文件中压缩、解压、增加文件、删除
  • Linux系统下Nginx的安装步骤
  • 10、SQL注入——数据库基础
  • JS浮点数精度问题及解决方案
  • kk(kubeadmin)在麒麟与centos安装k8s集群踩坑记录
  • Unity优化篇:对于unity DrawCall/Mesh/纹理压缩/内存等方面的常规调试和优化手段
  • css实现正六边形嵌套圆心
  • 网络安全(二)-- Linux 基本安全防护技术
  • 丢掉破解版,官方免费了!!!
  • Mybatis相关API(Sqlsession和sqlsessionFactroy)
  • 海云安参与制定《信息安全技术 移动互联网应用程序(App)软件开发工具包(SDK)安全要求》标准正式发布
  • 电脑如何录音?适合初学者的详细教程
  • python获取阿里云云解析dns的域名解析记录
  • 互联网大厂技术活动+实践分享
  • MySQL中的存储引擎
  • C语言--求一个十进制整数中1的个数
  • 【qml入门教程系列】:qml列表控件ListView用法介绍
  • 4.grid_sample理解与使用
  • 【模电】基本共射放大电路的工作原理及波形分析