nRF5使用CLI扩展库

/ 0评 / 1

使用CLI扩展库应该能提高调试效率,甚至是量产后还能内置一些修改调试的命令.想想就很鸡冻.

CLI的使用无非3个关键函数.cli_init => cli_start => cli_process(死循环)

/* 实例名,串口号,发送缓冲区大小,接收缓冲区大小. */

NRF_CLI_UART_DEF(m_cli_uart_transport, 0, 64, 16);

/* 实例名,提示符,发送器指针(当前用串口),换行符,记录日志深度 */ 

NRF_CLI_DEF(m_cli_uart,"uart_cli:~$ ",&m_cli_uart_transport.transport,'
',4);



static void cli_init(void)

{

    nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;

    uart_config.pseltxd = TX_PIN_NUMBER;

    uart_config.pselrxd = RX_PIN_NUMBER;

    uart_config.hwfc    = NRF_UART_HWFC_DISABLED;

    /* 实例,UART配置,是否使用颜色提示,是否记录日志,日志记录路径. */

    nrf_cli_init(&m_cli_uart, &uart_config, true, false, NRF_LOG_SEVERITY_NONE);

}



static void cli_start(void)

{

    nrf_cli_start(&m_cli_uart);

}



int main(void)

{

    while (true)

    {

        cli_process();

    }

}

特别注意的是,如果使能了LOG,就必须给LOG赋予储存空间,以及执行LOG程序.

创建最简单的命令.

static void cmd_python(nrf_cli_t const * p_cli, size_t argc, char **argv)

{

    UNUSED_PARAMETER(argc);

    UNUSED_PARAMETER(argv);



    nrf_cli_error(p_cli, "Nice joke ;)");

}



NRF_CLI_CMD_REGISTER(python, NULL, "python", cmd_python);

打python这个命令,输出一段固定的文字.

注册函数第一个参数,是函数名,第二个是子命令(类似参数),没有可以设置为NULL,第三个是函数说明,第四个是处理函数.执行后就像普通Linux执行程序一样,传入argc和argv.

子命令用法.

NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_print)

{

    NRF_CLI_CMD(all,   NULL, "Print all entered parameters.", cmd_print_all),

    NRF_CLI_CMD(param, NULL, "Print each parameter in new line.", cmd_print_param),

    NRF_CLI_SUBCMD_SET_END

};

NRF_CLI_CMD_REGISTER(print, &m_sub_print, "print", cmd_print);

此时输入print命令,得出子命令用法和描述.并且子命令有子命令的一些用法用途,用不同函数进行处理.另外,子命令还可以继续包含子命令.

如果要针对不同条件下,显示不同的子函数,还要用到NRF_CLI_CREATE_DYNAMIC_CMD定义,需返回p_static结构体,结构体内容就是子命令内容.

/* dynamic command creation */

static void dynamic_cmd_get(size_t idx, nrf_cli_static_entry_t * p_static)

{

    ASSERT(p_static);



    if (idx < m_dynamic_cmd_cnt)

    {

        /* m_dynamic_cmd_buffer must be sorted alphabetically to ensure correct CLI completion */

        p_static->p_syntax = m_dynamic_cmd_buffer[idx];

        p_static->handler  = NULL;

        p_static->p_subcmd = NULL;

        p_static->p_help = "Show dynamic command name.";

    }

    else

    {

        /* if there are no more dynamic commands available p_syntax must be set to NULL */

        p_static->p_syntax = NULL;

    }

}



NRF_CLI_CREATE_DYNAMIC_CMD(m_sub_dynamic_set, dynamic_cmd_get);

NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_dynamic)

{

    NRF_CLI_CMD(add, NULL,

        "Add a new dynamic command.
Example usage: [ dynamic add test ] will add "

        "a dynamic command 'test'.
In this example, command name length is limited to 32 chars. "

        "You can add up to 20 commands. Commands are automatically sorted to ensure correct "

        "CLI completion.",

        cmd_dynamic_add),

    NRF_CLI_CMD(execute, &m_sub_dynamic_set, "Execute a command.", cmd_dynamic_execute),

    NRF_CLI_CMD(remove, &m_sub_dynamic_set, "Remove a command.", cmd_dynamic_remove),

    NRF_CLI_CMD(show, NULL, "Show all added dynamic commands.", cmd_dynamic_show),

    NRF_CLI_SUBCMD_SET_END

};

NRF_CLI_CMD_REGISTER(dynamic,

                     &m_sub_dynamic,

                     "Demonstrate dynamic command usage.",

                     cmd_dynamic);

上述代码中,就是存入一些参数,用cmd_dynamic_add函数存入,然后remove时候可以列出现在有什么已经存入参数,所以是动态变化,所以要get出来,用dynamic_cmd_get函数.一切变得更自由,做什么都行了.