下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

最新资讯
简化任何设备的身份验证云连接。
利用 CoAP 设计节能型云连接 IoT 解决方案。
11.0.0 版 FreeRTOS 内核简介:
FreeRTOS 路线图和代码贡献流程。
使用 FreeRTOS 实现 OPC-UA over TSN。

FreeRTOS-Plus-IO 集成

FreeRTOS-Plus-CLI 是一个可扩展的框架, 应用程序编写者可以通过该框架定义并注册自己的命令行输入命令。此框架提供了单独的 文档页面,描述了 如何编写函数来 实现用户定义命令的行为 ,以及 如何 使用 FreeRTOS-Plus-CLI 注册用户定义命令。 此页详细描述了 如何将 FreeRTOS-Plus-CLI 移植到真实硬件上, 具体做法是通过提供输入输出 (IO) 例程和 FreeRTOS-Plus-CLI 任务。  

输入和输出

命令行接口可从输入接收字符,并写入 字符到输出。如何实现这一目标的低层次细节 取决于所使用的微控制器,以及由 微控制器提供的接口。 有一项 FreeRTOS-Plus-CLI 特色演示,使用了 FreeRTOS-Plus-IO FreeRTOS_read()FreeRTOS_write() API 函数,向 UART 提供必要的 输入和输出。它创建的命令行接口可通过 标准哑终端程序(如 HyperTerminal)进行访问。 还有另一项 FreeRTOS-Plus-CLI 特色演示,使用了 TCP/IP 套接字 接口,提供必要的输入和输出。其创建的命令行接口 可使用 telnet 客户端进行访问。FreeRTOS 中 任务结构体(运行 FreeRTOS-Plus-CLI 代码)在上述两种情况下差别不大, 具体请参阅下文。  

示例 FreeRTOS-Plus-CLI 任务

下列源代码可实现用于托管 FreeRTOS-Plus-CLI 命令解释器接口的任务 。FreeRTOS-Plus-IO FreeRTOS_read() 和 FreeRTOS_write() API 函数用于提供 IO 接口。它 假设 FreeRTOS-Plus-IO 描述符已经 打开, 并已配置为使用中断驱动 字符队列传输模式。 任务使用 FreeRTOS-Plus-CLI FreeRTOS_CLIProcessCommand() API 函数。 源代码中的注释提供了更多信息。请注意, 该函数并非可重入函数。
#define MAX_INPUT_LENGTH    50
#define MAX_OUTPUT_LENGTH   100

static const int8_t * const pcWelcomeMessage =
  "FreeRTOS command server.rnType Help to view a list of registered commands.rn";

void vCommandConsoleTask( void *pvParameters )
{
Peripheral_Descriptor_t xConsole;
int8_t cRxedChar, cInputIndex = 0;
BaseType_t xMoreDataToFollow;
/* The input and output buffers are declared static to keep them off the stack. */
static int8_t pcOutputString[ MAX_OUTPUT_LENGTH ], pcInputString[ MAX_INPUT_LENGTH ];

    /* This code assumes the peripheral being used as the console has already
    been opened and configured, and is passed into the task as the task
    parameter.  Cast the task parameter to the correct type. */
    xConsole = ( Peripheral_Descriptor_t ) pvParameters;

    /* Send a welcome message to the user knows they are connected. */
    FreeRTOS_write( xConsole, pcWelcomeMessage, strlen( pcWelcomeMessage ) );

    for( ;; )
    {
        /* This implementation reads a single character at a time.  Wait in the
        Blocked state until a character is received. */
        FreeRTOS_read( xConsole, &cRxedChar, sizeof( cRxedChar ) );

        if( cRxedChar == '\n' )
        {
            /* A newline character was received, so the input command string is
            complete and can be processed.  Transmit a line separator, just to
            make the output easier to read. */
            FreeRTOS_write( xConsole, "\r\n", strlen( "\r\n" );

            /* The command interpreter is called repeatedly until it returns
            pdFALSE.  See the "Implementing a command" documentation for an
            exaplanation of why this is. */
            do
            {
                /* Send the command string to the command interpreter.  Any
                output generated by the command interpreter will be placed in the
                pcOutputString buffer. */
                xMoreDataToFollow = FreeRTOS_CLIProcessCommand
                              (
                                  pcInputString,   /* The command string.*/
                                  pcOutputString,  /* The output buffer. */
                                  MAX_OUTPUT_LENGTH/* The size of the output buffer. */
                              );

                /* Write the output generated by the command interpreter to the
                console. */
                FreeRTOS_write( xConsole, pcOutputString, strlen( pcOutputString ) );

            } while( xMoreDataToFollow != pdFALSE );

            /* All the strings generated by the input command have been sent.
            Processing of the command is complete.  Clear the input string ready
            to receive the next command. */
            cInputIndex = 0;
            memset( pcInputString, 0x00, MAX_INPUT_LENGTH );
        }
        else
        {
            /* The if() clause performs the processing after a newline character
            is received.  This else clause performs the processing if any other
            character is received. */

            if( cRxedChar == '\r' )
            {
                /* Ignore carriage returns. */
            }
            else if( cRxedChar == '\b' )
            {
                /* Backspace was pressed.  Erase the last character in the input
                buffer - if there are any. */
                if( cInputIndex > 0 )
                {
                    cInputIndex--;
                    pcInputString[ cInputIndex ] = '';
                }
            }
            else
            {
                /* A character was entered.  It was not a new line, backspace
                or carriage return, so it is accepted as part of the input and
                placed into the input buffer.  When a n is entered the complete
                string will be passed to the command interpreter. */
                if( cInputIndex < MAX_INPUT_LENGTH )
                {
                    pcInputString[ cInputIndex ] = cRxedChar;
                    cInputIndex++;
                }
            }
        }
    }
}
An example of a task that implements a FreeRTOS-Plus-CLI command console
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.