BetaFlight统一硬件配置文件研读之timer命令
- 1. 源由
- 2. 代码分析
- 3. 实例分析
- 4. 配置情况
- 4.1 AFn配置查表
- 4.2 timer
- 4.3 timer show
- 4.4 timer pin list
- 5. 参考资料
统一硬件配置文件的设计是一种非常好的设计模式,可以将硬件和软件的工作进行解耦。
1. 源由
cli命令中timer是对硬件引脚和timer资源进行绑定的一个操作,主要用于以下设备:
- 电机
- PPM接收机
- LED灯带
- 伺服器
2. 代码分析
cliTimer
├──> "timer"
│ └──> printTimer(DUMP_MASTER, NULL); return
├──> "timer list"
│ └──> "NOT IMPLEMENTED YET"
├──> <USE_TIMER_MAP_PRINT> "timer map"
│ └──> showTimerMap(); return
├──> "timer show"
│ └──> showTimers(); return
├──> <!pch || !strToPin(pch, &ioTag)> error return //边界检查
├──> <!IOGetByTag(ioTag)> error return //有效性检查
├──> [find existing entry, or go for next available] error return //如果entry满,失败
├──> <!pch> printTimerDetails(ioTag, timerIOConfig(timerIOIndex)->index, false, DUMP_MASTER, cliDumpPrintLinef); return
└──> <pch>
├──> "list"
│ ├──> for (unsigned index = 0; (timer = timerGetByTagAndIndex(ioTag, index + 1)); index++)
│ ├──> cliPrintLinef("# AF%d: TIM%d CH%d%s", timer->alternateFunction, timerGetTIMNumber(timer->tim), CC_INDEX_FROM_CHANNEL(timer->channel) + 1,timer->output & TIMER_OUTPUT_N_CHANNEL ? "N" : "");
│ └──> return
├──> "none"
│ └──> timerIndex = TIMER_INDEX_UNDEFINED;
├──> "af"
│ ├──> for (unsigned index = 0; (timer = timerGetByTagAndIndex(ioTag, index + 1)); index++)
│ ├──> for (unsigned index = 0; (timer = timerGetByTagAndIndex(ioTag, index + 1)); index++)
│ └──> <timer->alternateFunction == alternateFunction> timerIndex = index;
├──> [assign new io timer]
│ ├──> int oldTimerIndex = isExistingTimerOpt ? timerIOConfig(timerIOIndex)->index - 1 : -1;
│ ├──> timerIOConfigMutable(timerIOIndex)->ioTag = timerIndex == TIMER_INDEX_UNDEFINED ? IO_TAG_NONE : ioTag;
│ ├──> timerIOConfigMutable(timerIOIndex)->index = timerIndex + 1;
│ └──> timerIOConfigMutable(timerIOIndex)->dmaopt = DMA_OPT_UNUSED;
├──> <timerIndex == oldTimerIndex>
│ └──> cliPrintLinef("# timer %c%02d: no change: %s", IO_GPIOPortIdxByTag(ioTag) + 'A', IO_GPIOPinIdxByTag(ioTag), orgvalString)
└──> <timerIndex != oldTimerIndex>
└──> cliPrintLinef("# timer %c%02d: changed from %s to %s", IO_GPIOPortIdxByTag(ioTag) + 'A', IO_GPIOPinIdxByTag(ioTag), orgvalString, optvalString);
3. 实例分析
# timer
timer A10 AF1
# pin A10: TIM1 CH3 (AF1)
timer B00 AF2
# pin B00: TIM3 CH3 (AF2)
timer B01 AF2
# pin B01: TIM3 CH4 (AF2)
timer A00 AF2
# pin A00: TIM5 CH1 (AF2)
timer A01 AF2
# pin A01: TIM5 CH2 (AF2)
timer A02 AF2
# pin A02: TIM5 CH3 (AF2)
timer A03 AF2
# pin A03: TIM5 CH4 (AF2)
timer D12 AF2
# pin D12: TIM4 CH1 (AF2)
timer D13 AF2
# pin D13: TIM4 CH2 (AF2)
timer D14 AF2
# pin D14: TIM4 CH3 (AF2)
timer D15 AF2
# pin D15: TIM4 CH4 (AF2)
timer E05 AF4
# pin E05: TIM15 CH1 (AF4)
timer E06 AF4
# pin E06: TIM15 CH2 (AF4)
timer A08 AF1
# pin A08: TIM1 CH1 (AF1)
4. 配置情况
# help timer
timer - show/set timers
<> | <pin> list | <pin> [af<alternate function>|none|<option(deprecated)>] | list | show
4.1 AFn配置查表
4.2 timer
# timer
timer A10 AF1
# pin A10: TIM1 CH3 (AF1)
timer B00 AF2
# pin B00: TIM3 CH3 (AF2)
timer B01 AF2
# pin B01: TIM3 CH4 (AF2)
timer A00 AF2
# pin A00: TIM5 CH1 (AF2)
timer A01 AF2
# pin A01: TIM5 CH2 (AF2)
timer A02 AF2
# pin A02: TIM5 CH3 (AF2)
timer A03 AF2
# pin A03: TIM5 CH4 (AF2)
timer D12 AF2
# pin D12: TIM4 CH1 (AF2)
timer D13 AF2
# pin D13: TIM4 CH2 (AF2)
timer D14 AF2
# pin D14: TIM4 CH3 (AF2)
timer D15 AF2
# pin D15: TIM4 CH4 (AF2)
timer E05 AF4
# pin E05: TIM15 CH1 (AF4)
timer E06 AF4
# pin E06: TIM15 CH2 (AF4)
timer A08 AF1
# pin A08: TIM1 CH1 (AF1)
4.3 timer show
# timer show
Currently active Timers:
-----------------------
TIM1:
CH1 : LED_STRIP
TIM2: FREE
TIM3: FREE
TIM4: FREE
TIM5: FREE
TIM6: FREE
TIM7: FREE
TIM8:
CH1 : DSHOT_BITBANG 2
CH2 : DSHOT_BITBANG 1
TIM12: FREE
TIM13: FREE
TIM14: FREE
TIM15: FREE
TIM16: FREE
TIM17: FREE
4.4 timer pin list
# timer A10 list
# AF1: TIM1 CH3
5. 参考资料
【1】BetaFlight开源代码框架简介
【2】BetaFlight统一硬件资源简单配置修改
【3】BetaFlight统一硬件配置文件研读