剖析RT-Thread中console与finsh组件实现(2)[通俗易懂]

剖析RT-Thread中console与finsh组件实现(2)[通俗易懂]接上一章剖析RT-Thread中finsh组件实现(1),rt_device具体定义如下:其中内核基类定义如下:所以刚才串口1初始化后名称被初始化为了“usart1”,与刚才设置终端时入参刚好可以匹配。而这个标志是类型标志,串口类型即为RT_Object_Class_Device,同时也是一个静态类,所以会或上0x80其实rt_device中最重要的是传入了设备回调与操作函数指针,这些指针此时指向的是串口1的一系列操作函数。这些函数被初始化在串口1初始化的rt_hw_serial

大家好,又见面了,我是你们的朋友全栈君。

接上一章剖析RT-Thread中finsh组件实现(1)rt_device 具体定义如下:
在这里插入图片描述
其中内核基类定义如下:
在这里插入图片描述
所以刚才串口1初始化后名称被初始化为了 “usart1” ,与刚才设置终端时入参刚好可以匹配。而这个标志是类型标志,串口类型即为 RT_Object_Class_Device ,同时也是一个静态类,所以会或上0x80
在这里插入图片描述
其实 rt_device 中最重要的是传入了设备回调与操作函数指针,这些指针此时指向的是串口1的一系列操作函数。这些函数被初始化在串口1初始化的 rt_hw_serial_register 函数里在这里插入图片描述
当得到返回的 rt_device 后,紧接着是打开这个设备,然后把当前控制台设置为新开打的设备:
在这里插入图片描述
到这里控制台初始化结束,然后后面就一直运行到main函数里。而shell的设置很容易想到,和之前串口初始化一样,还有一部分也被设置在自动化初始化的段里。
在文件shell.c里最后一句话
在这里插入图片描述
这里把finsh的初始化放在了应用初始化里,即段后缀.6里。初始化具体内容如下:
在这里插入图片描述
tips

段名 + $$Base或$$Limit是MDK里用法,配合keep链接时保留一个段内的数据
 a$$Base代表段名为a的首地址
 a$$Limit代表段名为a的结尾

可以看到MDK链接选项卡参数是:
在这里插入图片描述
官方文档解释
finsh_system_function_init 函数也很简单,直接设置两个全局变量:
在这里插入图片描述
shell结构体定义如下:
在这里插入图片描述
我们先看一下信号量初始化:
在这里插入图片描述
最后finsh接收与发送其实都在finsh线程里完成:(删除掉了一些没有通过预编译的代码)

void finsh_thread_entry(void *parameter)
{ 
   
    char ch;

    /* normal is echo mode */
#ifndef FINSH_ECHO_DISABLE_DEFAULT
    shell->echo_mode = 1;	//开启回显,即能看到输入的cmd
#else
    shell->echo_mode = 0;
#endif
#ifndef RT_USING_POSIX
    /* set console device as shell device */
    if (shell->device == RT_NULL)
    { 
   
        rt_device_t console = rt_console_get_device();	//获取到之前设置的终端,即usart1
        if (console)
        { 
   
            finsh_set_device(console->parent.name);
        }
    }
#endif
    rt_kprintf(FINSH_PROMPT);

    while (1)
    { 
   
        ch = finsh_getchar();

        /* * handle control key * up key : 0x1b 0x5b 0x41 * down key: 0x1b 0x5b 0x42 * right key:0x1b 0x5b 0x43 * left key: 0x1b 0x5b 0x44 */
        if (ch == 0x1b)
        { 
   
            shell->stat = WAIT_SPEC_KEY;
            continue;
        }
        else if (shell->stat == WAIT_SPEC_KEY)
        { 
   
            if (ch == 0x5b)
            { 
   
                shell->stat = WAIT_FUNC_KEY;
                continue;
            }

            shell->stat = WAIT_NORMAL;
        }
        else if (shell->stat == WAIT_FUNC_KEY)
        { 
   
            shell->stat = WAIT_NORMAL;

            if (ch == 0x41) /* up key */
            { 
   
#ifdef FINSH_USING_HISTORY //显示历史cmd,完成↑翻看历史记录功能
                /* prev history */
                if (shell->current_history > 0)
                    shell->current_history --;
                else
                { 
   
                    shell->current_history = 0;
                    continue;
                }

                /* copy the history command */
                memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
                       FINSH_CMD_SIZE);
                shell->line_curpos = shell->line_position = strlen(shell->line);
                shell_handle_history(shell);
#endif
                continue;
            }
            else if (ch == 0x42) /* down key */
            { 
   
#ifdef FINSH_USING_HISTORY //显示历史cmd,完成↓翻看历史记录功能
                /* next history */
                if (shell->current_history < shell->history_count - 1)
                    shell->current_history ++;
                else
                { 
   
                    /* set to the end of history */
                    if (shell->history_count != 0)
                        shell->current_history = shell->history_count - 1;
                    else
                        continue;
                }

                memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
                       FINSH_CMD_SIZE);
                shell->line_curpos = shell->line_position = strlen(shell->line);
                shell_handle_history(shell);
#endif
                continue;
            }
            else if (ch == 0x44) /* left key */					//←功能
            { 
   
                if (shell->line_curpos)
                { 
   
                    rt_kprintf("\b");
                    shell->line_curpos --;
                }

                continue;
            }
            else if (ch == 0x43) /* right key */				//→功能 
            { 
   
                if (shell->line_curpos < shell->line_position)
                { 
   
                    rt_kprintf("%c", shell->line[shell->line_curpos]);
                    shell->line_curpos ++;
                }

                continue;
            }
        }

        /* received null or error */
        if (ch == '\0' || ch == 0xFF) continue;
        /* handle tab key */									//代码补全
        else if (ch == '\t')
        { 
   
            int i;
            /* move the cursor to the beginning of line */
            for (i = 0; i < shell->line_curpos; i++)
                rt_kprintf("\b");

            /* auto complete */
            shell_auto_complete(&shell->line[0]);
            /* re-calculate position */
            shell->line_curpos = shell->line_position = strlen(shell->line);

            continue;
        }
        /* handle backspace key */							//退格
        else if (ch == 0x7f || ch == 0x08)
        { 
   
            /* note that shell->line_curpos >= 0 */
            if (shell->line_curpos == 0)
                continue;

            shell->line_position--;
            shell->line_curpos--;

            if (shell->line_position > shell->line_curpos)
            { 
   
                int i;

                rt_memmove(&shell->line[shell->line_curpos],
                           &shell->line[shell->line_curpos + 1],
                           shell->line_position - shell->line_curpos);
                shell->line[shell->line_position] = 0;

                rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);

                /* move the cursor to the origin position */
                for (i = shell->line_curpos; i <= shell->line_position; i++)
                    rt_kprintf("\b");
            }
            else
            { 
   
                rt_kprintf("\b \b");
                shell->line[shell->line_position] = 0;
            }

            continue;
        }

        /* handle end of line, break */
        if (ch == '\r' || ch == '\n')
        { 
   
#ifdef FINSH_USING_HISTORY
            shell_push_history(shell);						//显示历史内容到shell 
#endif

#ifdef FINSH_USING_MSH
            if (msh_is_used() == RT_TRUE)
            { 
   
                if (shell->echo_mode)
                    rt_kprintf("\n");
                msh_exec(shell->line, shell->line_position);	//实际对比指令
            }
            else
#endif
            { 
   
            }

            rt_kprintf(FINSH_PROMPT);
            memset(shell->line, 0, sizeof(shell->line));
            shell->line_curpos = shell->line_position = 0;
            continue;
        }

        /* it's a large line, discard it */
        if (shell->line_position >= FINSH_CMD_SIZE)
            shell->line_position = 0;

        /* normal character */
        if (shell->line_curpos < shell->line_position)
        { 
   
            int i;

            rt_memmove(&shell->line[shell->line_curpos + 1],
                       &shell->line[shell->line_curpos],
                       shell->line_position - shell->line_curpos);
            shell->line[shell->line_curpos] = ch;
            if (shell->echo_mode)
                rt_kprintf("%s", &shell->line[shell->line_curpos]);

            /* move the cursor to new position */
            for (i = shell->line_curpos; i < shell->line_position; i++)
                rt_kprintf("\b");
        }
        else
        { 
   
            shell->line[shell->line_position] = ch;
            if (shell->echo_mode)
                rt_kprintf("%c", ch);
        }

        ch = 0;
        shell->line_position ++;
        shell->line_curpos++;
        if (shell->line_position >= FINSH_CMD_SIZE)
        { 
   
            /* clear command line */
            shell->line_position = 0;
            shell->line_curpos = 0;
        }
    } /* end of device read */
}

整个while里两个关键函数 finsh_set_devicefinsh_getchar
一个是把设备与终端连接起来,另一个是接收函数。先看 finsh_set_device
在这里插入图片描述
上一章在串口初始化的时候串口1并没有初始化接收回调,就是为了在这里把接收回调设为shell接收回调。实际操作如下:
在这里插入图片描述
回调函数具体内容如下:
在这里插入图片描述
可以看到就是释放一个shell的接收信号量,也就是在接收到数据后会主动释放一个 rx_sem 信号量。
释放具体过程如下:
在这里插入图片描述
下面分析 finsh_getchar 函数,函数内容如下:
在这里插入图片描述
其中 rt_device_read 函数不做展开,里面其实就是调用传入device的read函数指针,读取成功返回读取到的数据长度,数据存于ch内。我们看一下 rt_sem_take 这个函数,内容如下:
在这里插入图片描述
这里第二个入参time为-1,所以会一直等待,直到接收到数据触发接收回调,线程恢复,处理数据。
输入内容与所存cmd对比函数为 msh_exec ,内容如下:
在这里插入图片描述
实际操作在 _msh_exec_cmd 里:
在这里插入图片描述
msh_get_cmd 函数内容如下:
在这里插入图片描述
msh_split 用来分隔命令后面带有的参数,返回值为参数个数(参数间一般是用空格来切分的)。先把argv这个临时数组变量清空,然后把字符串存于argv。这个地方和main()函数类似,在很多地方都规定了main函数格式为

int main(int argc,char **argv);

实际运行就一句:
在这里插入图片描述
运行结果存于retp里。

剖析RT-Thread中console与finsh组件实现(3)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/144976.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • glassfish安装教程_Glass Shatters

    glassfish安装教程_Glass Shatters一、下载页面:https://glassfish.java.net/downloads/3.1.2.2-final.html二、下载地址:http://download.java.net/glassfish/3.1.2.2/release/glassfish-3.1.2.2-ml.zip三、下载后解压到/srv/目录下.四、修改启动脚本:/srv/glassfi…

    2022年8月20日
    9
  • pycharm2022.01.13专业版注册激活-激活码分享

    (pycharm2022.01.13专业版注册激活)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~747E…

    2022年3月31日
    57
  • Sub-process /usr/bin/dpkg returned an error code_sub-process /usr/bin/dpkg

    Sub-process /usr/bin/dpkg returned an error code_sub-process /usr/bin/dpkgE:Sub-process/usr/bin/dpkgreturnedanerrorcode(1)解决办法安装libapache2-svn出现了这个错误,是由于apt-get安装软件时出现了类似于:dpkg:errorprocessingpackagelibapache2-mod-svn(–configure):subprocessinstalledpost-i…

    2022年10月7日
    1
  • bzero memset_perl注释

    bzero memset_perl注释1)void*memset(void*s,intc,size_tn)       总的作用:将已开辟内存空间s的首n个字节的值设为值c。       2).memset()函数常用于内存空间初始化。如:           charstr[100];           memset(str,0,100);       3).memset可以方便

    2022年10月13日
    2
  • excel如何去掉换行符号_去掉文本中所有换行符

    excel如何去掉换行符号_去掉文本中所有换行符excel中添加换行符::alt+enter去掉excel中的换行符有三种方法:注:解决过程中翻阅其他博客,看到如下方式:1、看到有的说全选后“取消自动换行”,保存后,再打开,依然存在换行符

    2022年8月4日
    64
  • JDBC之ResultSet

    JDBC之ResultSet

    2021年9月13日
    66

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号