现在的位置: 首页 > 综合 > 正文

[RTT例程练习] 6.2 在 Finsh 中运行自定义函数

2013年08月31日 ⁄ 综合 ⁄ 共 1189字 ⁄ 字号 评论关闭

可以直接在Finsh中运行自己定义的函数。官方有三种方式,我觉得第二种比较方便,其他就不介绍了。

首先需要在application.c 中包含 finsh.h 头文件,然后在函数下方添加宏

FINSH_FUNCTION_EXPORT(fun_with_arg, function with a argument);

程序

#include <rtthread.h>

#include <finsh.h>

static rt_thread_t tid = RT_NULL;

static void thread_entry(void *parameter)
{
    while (1)
    {
        rt_thread_delay(1000);
    }
}

void fun_with_arg(int a)
{
    rt_kprintf("fun's arg is: %d.\n", a);
}
FINSH_FUNCTION_EXPORT(fun_with_arg, function with a argument);

int rt_application_init()
{

    
    tid = rt_thread_create("tid",
        thread_entry, RT_NULL,
        1024,
        8, 50);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
    
    return 0;
}

可以看到这是一个带参的函数。

在Finsh中

finsh>>list() 
--Function List: 
fun_with_arg -- funciton with a argument 
list_mem -- list memory usage information 
hello -- say hello world 
version -- show RT-Thread version information 
list_thread -- list thread 
list_sem -- list semaphone in system 
list_event -- list event in system 
list_mutex -- list mutex in system 
list_mailbox -- list mail box in system 
list_msgqueue -- list message queue in system 
list_mempool -- list memory pool in system 
list_timer -- list timer in system 
list_device -- list device in system 
list -- list all symbol in system 
[l] fun 
--Variable List: 
dummy -- dummy variable for finsh 
0, 0x00000000 

可以看到,我们的

fun_with_arg

已经被添加其中。

然后试着在Finsh中运行

fun_with_arg(7)

就能得到

finsh>>fun_with_arg(7) 
fun's arg is: 7 
0, 0x00000000 


抱歉!评论已关闭.