单片机_RTOS_架构

一. RTOS的概念

cpp 复制代码
// 经典单片机程序
void main()
{
	while (1)
    {
        喂一口饭();
        回一个信息();
    }
}
------------------------------------------------------
// RTOS程序    
喂饭()
{
    while (1)
    {
        喂一口饭();
    }
}

回信息()
{
    while (1)
    {
        回一个信息();
    }
}

void main()
{
    create_task(喂饭);
    create_task(回信息);
    start_scheduler();
    while (1)
    {
        sleep();
    }
}

二.FreeRTOS****目录结构

三.从官方源码裁剪

1. 下载

2. 删减目录

3. 编译、执行

4. 添加串口打印功能

  • 去掉无关的代码:LCD等

  • 增加串口打印功能

    • 初始化串口

    • 实现fputc

四. 创建任务

cpp 复制代码
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 // 任务句柄, 以后使用它来操作这个任务
                ); 

1.创建第一个任务

cpp 复制代码
void Task1Function(void*param)//函数指针
{
	while(1)
	{
		printf("1");
	}
}

/*-----------------------------------------------------------*/

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

	prvSetupHardware();

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

	xTaskCreate(Task1Function,"Task1",100,NULL,1,&xHandleTask1);//创建任务
	/* Start the scheduler. */
	vTaskStartScheduler();

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

不断的执行1

2.创建第二个任务

cpp 复制代码
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;
}

3.动态内存的使用

cpp 复制代码
        TCB_t * pxNewTCB;//任务控制块

        BaseType_t xReturn;

对于每一个任务都有一个TCB_t 结构体,对于这个结构体可以使用动态分配也可以静态分配。动态分配在程序里面使用 xTaskCreate();

cpp 复制代码
    TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
                                    const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
                                    const uint32_t ulStackDepth,
                                    void * const pvParameters,
                                    UBaseType_t uxPriority,
                                    StackType_t * const puxStackBuffer,
                                    StaticTask_t * const pxTaskBuffer )
    {
cpp 复制代码
void Task1Function(void*param)
{
	while(1)
	{
		printf("1");
	}
}
void Task2Function(void*param)
{
	while(1)
	{
		printf("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");

	xTaskCreate(Task1Function,"Task1",100,NULL,1,&xHandleTask1);
	xTaskCreate(Task2Function,"Task2",100,NULL,1,NULL);
	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;
}
相关推荐
Xueqian E5 分钟前
驱动策略和效率的整理
stm32·单片机·嵌入式硬件
电子工程师成长日记-C511 小时前
51单片机气压检测仪
单片机·嵌入式硬件·51单片机
嵌入式老菜鸟qq1252427732 小时前
nRF54H20 + Zephyr 开发环境(二):烧录与踩坑实录
stm32·单片机·嵌入式硬件
-Try hard-2 小时前
ARM | 点亮LED灯!
arm开发·单片机·嵌入式硬件
llilian_162 小时前
卫星时钟 时钟同步解决方案——基于高精度卫星时钟同步授时装置 卫星同步时钟 授时同步装置
功能测试·单片机·测试工具
日更嵌入式的打工仔2 小时前
电机三环控制
单片机·嵌入式硬件
weixin_669545202 小时前
JT8166A/B电容式六按键触摸控制芯片,JT8166B具备IIC通信接口
人工智能·单片机·嵌入式硬件·硬件工程
RFID舜识物联网2 小时前
RFID耐高温标签在汽车喷涂工艺中的创新应用
大数据·人工智能·科技·嵌入式硬件·物联网·汽车
泛凡(Linyongui)3 小时前
一款基于LP4057芯片的单节 4.2V 锂电池线性充电管理电路,带电源路径自动切换功能
arm开发·单片机·py32
9稳3 小时前
基于PLC的生产线自动升降机设计
开发语言·网络·数据库·嵌入式硬件·plc