BetaFlight模块设计之三十:Cli模块分析

Posted lida2003

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BetaFlight模块设计之三十:Cli模块分析相关的知识,希望对你有一定的参考价值。

BetaFlight模块设计之三十:Cli模块分析

基于BetaFlight开源代码框架简介的框架设计,逐步分析内部模块功能设计。

Cli模块

描述:Cli模块主要用于处理Cli命令,并根据命令显示系统状态或系统配置参数修改。

src/main/cli/
├── cli.c
├── cli_debug_print.h
├── cli.h
├── settings.c
└── settings.h

0 directories, 5 files

注:该模块主要支持BetaFlight模块设计之二:SERIAL任务分析

Cli接口

extern bool cliMode;                             //判断系统处于Cli模式

void cliProcess(void);                           //处理Cli命令
void cliEnter(struct serialPort_s *serialPort);  //进入Cli模式
bool resetConfigToCustomDefaults(void);          //支持命令行“defaults”命令
bool hasCustomDefaults(void);                    //支持命令行“defaults”命令

Cli框架

  1. cliMode:判断当前Cli模块状态(TRUE为Cli模式;反之则不在该模式)
  2. cliEnter:触发Cli模块进入工作模式
  3. cliProcess:处理Cli命令
  4. cliExit:“CTRL + D”,退出Cli模式 //这个命令是内嵌在CliProcess函数内部

Cli命令结构

一个Cli命令通过name(命令名字),description(命令描述),args(命令参数表),cliCommand(命令函数)组成。

typedef void cliCommandFn(const char* name, char *cmdline);

typedef struct 
    const char *name;
#ifndef MINIMAL_CLI
    const char *description;
    const char *args;
#endif
    cliCommandFn *cliCommand;
 clicmd_t;

例如:help命令

CLI_COMMAND_DEF("help", "display command help", "[search string]", cliHelp),

主要函数分析

cliProcess函数

cliProcess
 ├──> <!cliWriter>  //Cli模式下处理自定义命令或者尚未进入Cli模式,直接返回
 │   └──> return
 ├──> cliWriterFlush();
 └──> <serialRxBytesWaiting(cliPort)>  //非阻塞式数据等待
     ├──> c = serialRead(cliPort);
     └──> processCharacterInteractive(c); //交互式串口字节处理

processCharacterInteractive函数

processCharacterInteractive
 ├──> <c == '\\t' || c == '?'> //实际在配置软件里面tab可以,?无法使用
 │   ├──> [遍历cmdTable,匹配相近的命令(pstart, pend)]
 │   ├──> [如果只有一个命令,尝试补全命令]
 │   └──> [如果多个匹配命令,反馈命令列表]
 ├──> <!bufferIndex && c == 4> // CTRL-D
 │   ├──> cliExit("", cliBuffer); //退出Cli模式
 │   └──> return
 ├──> <c == 12>  // NewPage(CTRL-L) 清屏
 │   ├──> cliPrint("\\033[2J\\033[1;1H");
 │   └──> cliPrompt();
 ├──> <c == 127><bufferIndex>  //回退一个字符
 │   ├──> cliBuffer[--bufferIndex] = 0;
 │   └──> cliPrint("\\010 \\010");
 └──> <other scenarios>
     └──> processCharacter(c);

注:ASCII表,详见链接

processCharacter函数

processCharacter
 ├──> <bufferIndex && (c == '\\n' || c == '\\r')>
 │   ├──> cliPrintLinefeed();
 │   ├──> <processingCustomDefaults>  //定制命令处理场景,配合cliPrompt打印
 │   │   └──> cliPrint("d: ");
 │   ├──> [Strip comment starting with # from line]  //注释行屏蔽
 │   ├──> [Strip trailing whitespace] //尾部空格屏蔽
 │   ├──> <bufferIndex > 0)>  // Process non-empty lines
 │   │   ├──> [遍历列表寻找命令index]
 │   │   └──> [执行index命令指定的命令函数执行,异常反馈错误类型]
 │   ├──> memset(cliBuffer, 0, sizeof(cliBuffer));
 │   ├──> <!cliMode>
 │   │   └──> return;
 │   └──> cliPrompt();
 └──> <bufferIndex < sizeof(cliBuffer) && c >= 32 && c <= 126>
     ├──> <!bufferIndex && c == ' '>
     │   └──> return // 最前面的空格忽略,直接丢弃
     ├──> cliBuffer[bufferIndex++] = c;  //符合条件的字符保存下来,供后续处理使用
     └──> cliWrite(c);

Cli命令汇总

注:主要介绍BetaFlight4.3版本

Cli串口help命令

# help
adjrange - configure adjustment ranges
	<index> <unused> <range channel> <start> <end> <function> <select channel> [<center> <scale>]
aux - configure modes
	<index> <mode> <aux> <start> <end> <logic>
batch - start or end a batch of commands
	start | end
beacon - enable/disable Dshot beacon for a condition
	list
	<->[name]
beeper - enable/disable beeper for a condition
	list
	<->[name]
bind_rx - initiate binding for RX SPI or SRXL2
bl - reboot into bootloader
	[rom]
board_name - get / set the name of the board model
	[board name]
color - configure colors
defaults - reset to defaults and reboot
	[nosave|bare|show]
diff - list configuration changes from default
	[master|profile|rates|hardware|all] defaults|bare
dma - show/set DMA assignments
	<> | <device> <index> list | <device> <index> [<option>|none] | list | show
dshot_telemetry_info - display dshot telemetry info and stats
dshotprog - program DShot ESC(s)
	<index> <command>+
dump - dump configuration
	[master|profile|rates|hardware|all] defaults|bare
exit
feature - configure features
	list
	<->[name]
flash_erase - erase flash chip
flash_info - show flash chip info
flash_read
	<length> <address>
flash_scan - scan flash device for errors
flash_write
	<address> <message>
get - get variable value
	[name]
gpspassthrough - passthrough gps to serial
gyroregisters - dump gyro config registers contents
help - display command help
	[search string]
led - configure leds
manufacturer_id - get / set the id of the board manufacturer
	[manufacturer id]
map - configure rc channel order
	[<map>]
mcu_id - id of the microcontroller
mixer - configure mixer
	list
	<name>
mmix - custom motor mixer
mode_color - configure mode and special colors
motor - get/set motor
	<index> [<value>]
msc - switch into msc mode
	[<timezone offset minutes>]
play_sound
	[<index>]
profile - change profile
	[<index>]
rateprofile - change rate profile
	[<index>]
rc_smoothing_info - show rc_smoothing operational settings
resource - show/set resources
	<> | <resource name> <index> [<pin>|none] | show [all]
rxfail - show/set rx failsafe settings
rxrange - configure rx channel ranges
save - save and reboot
sd_info - sdcard info
serial - configure serial ports
serialpassthrough - passthrough serial data data from port 1 to VCP / port 2
	<id1> [<baud1>] [<mode1>] [none|<dtr pinio>|reset] [<id2>] [<baud2>] [<mode2>]
servo - configure servos
set - change setting
	[<name>=<value>]
signature - get / set the board type signature
	[signature]
smix - servo mixer
	<rule> <servo> <source> <rate> <speed> <min> <max> <box>
	reset
	load <mixer>
	reverse <servo> <source> r|n
status - show status
tasks - show task stats
timer - show/set timers
	<> | <pin> list | <pin> [af<alternate function>|none|<option(deprecated)>] | list | show
version - show version
vtx - vtx channels on switch
	<index> <aux_channel> <vtx_band> <vtx_channel> <vtx_power> <start_range> <end_range>
vtx_info - vtx power config dump
vtxtable - vtx frequency table
	<band> <bandname> <bandletter> [FACTORY|CUSTOM] <freq> ... <freq>

Cli内部命令列表

const clicmd_t cmdTable[] = 
    CLI_COMMAND_DEF("adjrange", "configure adjustment ranges", "<index> <unused> <range channel> <start> <end> <function> <select channel> [<center> <scale>]", cliAdjustmentRange),
    CLI_COMMAND_DEF("aux", "configure modes", "<index> <mode> <aux> <start> <end> <logic>", cliAux),
#ifdef USE_CLI_BATCH
    CLI_COMMAND_DEF("batch", "start or end a batch of commands", "start | end", cliBatch),
#endif
#if defined(USE_BEEPER)
#if defined(USE_DSHOT)
    CLI_COMMAND_DEF("beacon", "enable/disable Dshot beacon for a condition", "list\\r\\n"
        "\\t<->[name]", cliBeacon),
#endif
    CLI_COMMAND_DEF("beeper", "enable/disable beeper for a condition", "list\\r\\n"
        "\\t<->[name]", cliBeeper),
#endif // USE_BEEPER
#if defined(USE_RX_BIND)
    CLI_COMMAND_DEF("bind_rx", "initiate binding for RX SPI or SRXL2", NULL, cliRxBind),
#endif
#if defined(USE_FLASH_BOOT_LOADER)
    CLI_COMMAND_DEF("bl", "reboot into bootloader", "[flash|rom]", cliBootloader),
#else
    CLI_COMMAND_DEF("bl", "reboot into bootloader", "[rom]", cliBootloader),
#endif
#if defined(USE_BOARD_INFO)
    CLI_COMMAND_DEF("board_name", "get / set the name of the board model", "[board name]", cliBoardName),
#endif
#ifdef USE_LED_STRIP_STATUS_MODE
        CLI_COMMAND_DEF("color", "configure colors", NULL, cliColor),
#endif
#if defined(USE_CUSTOM_DEFAULTS)
    CLI_COMMAND_DEF("defaults", "reset to defaults and reboot", "show nosave bare group_id <id>", cliDefaults),
#else
    CLI_COMMAND_DEF("defaults", "reset to defaults and reboot", "nosave", cliDefaults),
#endif
    CLI_COMMAND_DEF("diff", "list configuration changes from default", "[master|profile|rates|hardware|all] defaults|bare", cliDiff),
#ifdef USE_RESOURCE_MGMT

#ifdef USE_DMA
#ifdef USE_DMA_SPEC
    CLI_COMMAND_DEF("dma", "show/set DMA assignments", "<> | <device> <index> list | <device> <index> [<option>|none] | list | show", cliDma),
#else
    CLI_COMMAND_DEF("dma", "show DMA assignments", "show", cliDma),
#endif
#endif

#endif
#ifdef USE_DSHOT_TELEMETRY
    CLI_COMMAND_DEF("dshot_telemetry_info", "display dshot telemetry info and stats", NULL, cliDshotTelemetryInfo),
#endif
#ifdef USE_DSHOT
    CLI_COMMAND_DEF("dshotprog", "program DShot ESC(s)", "<index> <command>+", cliDshotProg),
#endif
    CLI_COMMAND_DEF("dump", "dump configuration",
        "[master|profile|rates|hardware|all] defaults|bare", cliDump),
#ifdef USE_ESCSERIAL
    CLI_COMMAND_DEF("escprog", "passthrough esc to serial", "<mode [sk/bl/ki/cc]> <index>", cliEscPassthrough),
#endif
    CLI_COMMAND_DEF("exit", NULL, NULL, cliExit),
    CLI_COMMAND_DEF("feature", "configure features",
        "list\\r\\n"
        "\\t<->[name]", cliFeature),
#ifdef USE_FLASHFS
    CLI_COMMAND_DEF("flash_erase", "erase flash chip", NULL, cliFlashErase),
    CLI_COMMAND_DEF("flash_info", "show flash chip info", NULL, cliFlashInfo),
#ifdef USE_FLASH_TOOLS
    CLI_COMMAND_DEF("flash_read", NULL, "<length> <address>", cliFlashRead),
    CLI_COMMAND_DEF("flash_scan", "scan flash device for errors", NULL, cliFlashVerify),
    CLI_COMMAND_DEF("flash_write", NULL, "<address> <message>", cliFlashWrite),
#endif
#endif
    CLI_COMMAND_DEF("get", "get variable value", "[name]", cliGet),
#ifdef USE_GPS
    CLI_COMMAND_DEF("gpspassthrough", "passthrough gps to serial", NULL, cliGpsPassthrough),
#endif
#if defined(USE_GYRO_REGISTER_DUMP) && !defined(SIMULATOR_BUILD)
    CLI_COMMAND_DEF("gyroregisters", "dump gyro config registers contents", NULL, cliDumpGyroRegisters),
#endif
    CLI_COMMAND_DEF("help", "display command help", "[search string]", cliHelp),
#ifdef USE_LED_STRIP_STATUS_MODE
        CLI_COMMAND_DEF("led", "configure leds", NULL, cliLed),
#endif
#if defined(USE_BOARD_INFO)
    CLI_COMMAND_DEF("manufacturer_id", "get / set the id of the board manufacturer", "[manufacturer id]", cliManufacturerId),
#endif
    CLI_COMMAND_DEF("map", "configure rc channel order", "[<map>]", cliMap),
    CLI_COMMAND_DEF("mcu_id", "id of the microcontroller", NULL, cliMcuId),
#ifndef USE_QUAD_MIXER_ONLY
    CLI_COMMAND_DEF("mixer", "configure mixer", "list\\r\\n\\t<name>", cliMixer),
#endif
    CLI_COMMAND_DEF("mmix", "custom motor mixer", NULL, cliMotorMix),
#ifdef USE_LED_STRIP_STATUS_MODE
    CLI_COMMAND_DEF("mode_color", "configure mode and special colors", NULL, cliModeColor),
#endif
    CLI_COMMAND_DEF("motor",  "get/set motor", "<index> [<value>]", cliMotor),
#ifdef USE_USB_MSC
#ifdef USE_RTC_TIME
    CLI_COMMAND_DEF("msc", "switch into msc mode", "[<timezone offset minutes>]", cliMsc),
#else
    CLI_COMMAND_DEF("msc", "switch into msc mode", NULL, cliMsc),
#endif
#endif
#ifndef MINIMAL_CLI
    CLI_COMMAND_DEF("play_sound", NULL, "[<index>]", cliPlaySound),
#endif
    CLI_COMMAND_DEF("profile", "change profile", "[<index>]", cliProfile),
    CLI_COMMAND_DEF("rateprofile", "change rate profile", "[<index>]", cliRateProfile),
#ifdef USE_RC_SMOOTHING_FILTER
    CLI_COMMAND_DEF("rc_smoothing_info", "show rc_smoothing operational settings", NULL, cliRcSmoothing),
#endif // USE_RC_SMOOTHING_FILTER
#ifdef USE_RESOURCE_MGMT
    CLI_COMMAND_DEF("resource", "show/set resources", "<> | <resource name> <index> [<pin>|none] | show [all]", cliResource),
#endif
    CLI_COMMAND_DEF("rxfail", "show/set rx failsafe settings", NULL, cliRxFailsafe),
    CLI_COMMAND_DEF("rxrange", "configure rx channel ranges", NULL, cliRxRange),
    CLI_COMMAND_DEF("save", "save and reboot", NULL, cliSave),
#ifdef USE_SDCARD
    CLI_COMMAND_DEF("sd_info", "sdcard info", NULL, cliSdInfo),
#endif
    CLI_COMMAND_DEF("serial", "configure serial ports", NULL, cliSerial),
#if defined(USE_SERIAL_PASSTHROUGH)
#if defined(USE_PINIO)
    CLI_COMMAND_DEF("serialpassthrough", "passthrough serial data data from port 1 to VCP / port 2", "<id1> [<baud1>] [<mode1>] [none|<dtr pinio>|reset] [<id2>] [<baud2>] [<mode2>]", cliSerialPassthrough),
#else
    CLI_COMMAND_DEF("serialpassthrough", "passthrough serial data from port 1 to VCP / port 2", "<id1> [<baud1>] [<mode1>] [none|reset] [<id2>] [<baud2>] [<mode2>]", cliSerialPassthrough),
#endif
#endif
#ifdef USE_SERVOS
    CLI_COMMAND_DEF("servo", "configure servos", NULL, cliServo),
#endif
    CLI_COMMAND_DEF("set", "change setting", "[<name>=<value>]", cliSet),
#if defined(USE_SIGNATURE)
    CLI_COMMAND_DEF("signature", "get / set the board type signature", "[signature]", cliSignature),
#endif
#if defined(USE_SIMPLIFIED_TUNING)
    CLI_COMMAND_DEF("simplified_tuning", "applies or disables simplified tuning", "apply | disable", cliSimplifiedTuning),
#endif
#ifdef USE_SERVOS
    CLI_COMMAND_DEF("smix", "servo mixer", "<rule> <servo> <source> <rate> <speed> <min> <max> <box>\\r\\n"
        "\\treset\\r\\n"
        "\\tload <mixer>\\r\\n"
        "\\treverse <servo> <source> r|n", cliServoMix),
#endif
    CLI_COMMAND_DEF("status", "show status", NULL, cliStatus),
    CLI_COMMAND_DEF("tasks", "show task stats", NULL, cliTasks),
#ifdef USE_TIMER_MGMT
    CLI_COMMAND_DEF("timer", "show/set timers", "<> | <pin> list | <pin> [af<alternate function>|none|<option(deprecated)>] | list | show", cliTimer),
#endif
    CLI_COMMAND_DEF("version", "show version", NULL, cliVersion),
#ifdef USE_VTX_CONTROL
#ifdef MINIMAL_CLI
    CLI_COMMAND_DEF("vtx", "vtx channels on switch", NULL, cliVtx),
#else
    CLI_COMMAND_DEF("vtx", "vtx channels on switch", "<index> <aux_channel> <vtx_band> <vtx_channel> <vtx_power> <start_range> <end_range>", cliVtx),
#endif
#endif
#ifdef USE_VTX_TABLE
    CLI_COMMAND_DEF("vtx_info", "vtx power config dump", NULL, cliVtxInfo),
    CLI_COMMAND_DEF("vtxtable", "vtx frequency table", "<band> <bandname> <bandletter> [FACTORY|CUSTOM] <freq> ... <freq>\\r\\n", cliVtxTable),
#endif
;

以上是关于BetaFlight模块设计之三十:Cli模块分析的主要内容,如果未能解决你的问题,请参考以下文章

BetaFlight模块设计之三十五:RSSI信号强度&链路稳定性分析

BetaFlight深入传感设计之三:IMU传感模块

BetaFlight深入传感设计之三:IMU传感模块

PX4模块设计之三十五:MulticopterAttitudeControl模块

PX4模块设计之三十六:MulticopterPositionControl模块

PX4模块设计之三十七:MulticopterRateControl模块