BetaFlight模块设计之二十三:CRSF V3串口速率协商任务分析
基于BetaFlight开源代码框架简介的框架设计,逐步分析内部模块功能设计。
注:CRSF_V3引入了串口速率协商机制,这里的speedNegotiationProcess就是为了自协商功能引入的。
CRSF V3串口速率协商任务
描述:通过串口速率协商从而保证Tx/Rx能在较好的稳定握手速率下通信,尤其是距离导致通信链路状态变差的情况下,协商机制提供了重新握手的可能。
├──> 初始化
│ ├──> [v]硬件初始化crsfRxInit
│ └──> [x]业务初始化
├──> 任务
│ ├──> [x]实时任务
│ ├──> [x]事件任务
│ └──> [v]时间任务[TASK_SPEED_NEGOTIATION] = DEFINE_TASK("SPEED_NEGOTIATION", NULL, NULL, speedNegotiationProcess, TASK_PERIOD_HZ(100), TASK_PRIORITY_LOW),
├──> 驱动
│ ├──> [x]查询
│ └──> [v]中断crsfDataReceive
└──> 接口
└──> TBD
默认配置
PG_REGISTER_WITH_RESET_FN(rxConfig_t, rxConfig, PG_RX_CONFIG, 3);
void pgResetFn_rxConfig(rxConfig_t *rxConfig)
{
RESET_CONFIG_2(rxConfig_t, rxConfig,
.halfDuplex = 0,
.serialrx_provider = SERIALRX_PROVIDER,
.serialrx_inverted = 0,
.spektrum_bind_pin_override_ioTag = IO_TAG(SPEKTRUM_BIND_PIN),
.spektrum_bind_plug_ioTag = IO_TAG(BINDPLUG_PIN),
.spektrum_sat_bind = 0,
.spektrum_sat_bind_autoreset = 1,
.midrc = RX_MID_USEC,
.mincheck = 1050,
.maxcheck = 1900,
.rx_min_usec = RX_MIN_USEC, // any of first 4 channels below this value will trigger rx loss detection
.rx_max_usec = RX_MAX_USEC, // any of first 4 channels above this value will trigger rx loss detection
.rssi_src_frame_errors = false,
.rssi_channel = 0,
.rssi_scale = RSSI_SCALE_DEFAULT,
.rssi_offset = 0,
.rssi_invert = 0,
.rssi_src_frame_lpf_period = 30,
.fpvCamAngleDegrees = 0,
.airModeActivateThreshold = 25,
.max_aux_channel = DEFAULT_AUX_CHANNEL_COUNT,
.rc_smoothing_mode = 1,
.rc_smoothing_setpoint_cutoff = 0,
.rc_smoothing_feedforward_cutoff = 0,
.rc_smoothing_throttle_cutoff = 0,
.rc_smoothing_debug_axis = ROLL,
.rc_smoothing_auto_factor_rpy = 30,
.rc_smoothing_auto_factor_throttle = 30,
.srxl2_unit_id = 1,
.srxl2_baud_fast = true,
.sbus_baud_fast = false,
.crsf_use_rx_snr = false,
.msp_override_channels_mask = 0,
.crsf_use_negotiated_baud = false,
);
#ifdef RX_CHANNELS_TAER
parseRcChannels("TAER1234", rxConfig);
#else
parseRcChannels("AETR1234", rxConfig);
#endif
}
Rx协商端处理方法
定时反馈处理
speedNegotiationProcess
├──> <crsfSpeed.hasPendingReply>
│ └──> [收到速度协商报文后,构建反馈报文,并发送]
├──> <crsfSpeed.isNewSpeedValid>
│ └──> [反馈报文发送后,至少4ms后生效新串口波特率]
└──> <!featureIsEnabled(FEATURE_TELEMETRY) && crsfRxUseNegotiatedBaud()>
└──> [不支持协商功能,反馈给请求设备,拒绝更换波特率]
中断报文处理
crsfRxInit初始化串口时hook接受报文函数crsfDataReceive,在该函数内部处理[CRSF_FRAMETYPE_COMMAND, CRSF_COMMAND_SUBCMD_GENERAL, CRSF_COMMAND_SUBCMD_GENERAL_CRSF_SPEED_PROPOSAL]报文。
调用关系
crsfDataReceive
└──> crsfProcessCommand
├──> crsfProcessSpeedNegotiationCmd
└──> crsfScheduleSpeedNegotiationResponse