RT-Thread Stdio入门RTT(双线程)

23.2.19

开发板:stm32f103c8t6

源码:吴广喆 (wu-guangzhe) - Gitee.com

创建线程

  • 新建文件(源文件、头文件),右键applications--新建--源文件/头文件

  • 添加头文件路径,构建配置--C/C++构建--设置--

按照官方的方式会报错!!!

  • 创建两个线程

//main.c
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-19     RT-Thread    first version
 */

#include <rtthread.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

//自己添加头文件
#include "my_application.h"
#include "board.h"
#include "drivers/pin.h"

void thread0_entry (void *parameter);
int dynamic_thread0_example (void);
//使用INIT_APP_EXPORT宏自动初始化,也可以通过在其他线程内调用dynamic_thread_example函数进行初始化
INIT_APP_EXPORT(dynamic_thread0_example);                    // 应用初始化

int main(void)
{
    thread_sample();
    return RT_EOK;
}
void thread0_entry(void *parameter)
{
    while(1)
    {
        rt_kprintf("dynamic thread is running.\n");
        rt_thread_mdelay(1000);
    }
}


// @brief       动态线程创建以及启动
int dynamic_thread0_example(void)
{
    // 线程控制块指针
    rt_thread_t tid1;
    // 创建动态线程
    tid1 = rt_thread_create("thread0",                      // 线程名称
        thread0_entry,                                      // 线程入口函数
        RT_NULL,                                            // 线程参数
        512,                                                // 512 个字节的栈空间
        5,                                                  // 线程优先级为5,数值越小,优先级越高,0为最高优先级。
                                                            // 可以通过修改rt_config.h中的RT_THREAD_PRIORITY_MAX宏定义(默认值为8)来修改最大支持的优先级
        5);                                                 // 时间片为5

    rt_kprintf("create dynamic thread.\n");
    if(tid1 != RT_NULL)                                     // 线程创建成功
    {
        rt_kprintf("thread0 dynamic thread create OK.\n");
        rt_thread_startup(tid1);                            // 运行该线程
    }
    else                                                    // 线程创建失败
    {
        rt_kprintf("thread0 dynamic thread create ERROR.\n");
        return 1;
    }

    return 0;
}
//my_application.c
#include <rtthread.h>
#include "board.h"
#include "drivers/pin.h"
#include "my_application.h"
#include "rtdbg.h"

#define THREAD_PRIORITY         25    //优先级
#define THREAD_STACK_SIZE       512   //栈大小
#define THREAD_TIMESLICE        5     //时间片


/* 使用静态线程时,线程的栈需要设置字节对齐 */
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t thread_stack[THREAD_STACK_SIZE];
static struct rt_thread tid1;

/* 线程 1 的入口函数 */
static void thread1_entry(void *parameter)
{
while (1)
{
    rt_kprintf("hello world\n");
    rt_thread_mdelay(500);
}
}

/* 线程示例 */
int thread_sample(void)
{
    /* 静态创建线程 */

    /* 初始化线程 1,名称是 thread1,入口是 thread1_entry*/
    rt_thread_init(&tid1,
                   "thread1",
                   thread1_entry,
                   RT_NULL,
                   thread_stack,
                   THREAD_STACK_SIZE,
                   THREAD_PRIORITY,
                   THREAD_TIMESLICE);
        /* 启动线程 */
        rt_thread_startup(&tid1);

    return 0;
}
//my_application.h

#ifndef APPLICATIONS_MY_APPLICATION_H_
#define APPLICATIONS_MY_APPLICATION_H_



int thread_sample(void);


#endif /* APPLICATIONS_MY_APPLICATION_H_ */


  • 打开终端查看输出(需要有ch340串口)


2023.7.21报错

静态创建线程,设置字节对齐时候会出现报错:

解决方法:将ALIGN改成rt_align

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值