BetaFlight统一硬件配置文件研读之resource命令
统一硬件配置文件的设计是一种非常好的设计模式,可以将硬件和软件的工作进行解耦。
1. 源由
cli命令中resource是对硬件引脚资源进行配置的最为基础的一个命令。通过该命令使用MCU引脚控制:
- 电机
- 蜂鸣器
- 伺服器
- LED灯带
- PPM接收机
- 串口RxTx
- I2C(气压计)
- SPI(FLASH、模拟OSD 7456、陀螺仪)
- ADC(电压、电流、RSSI等)
- PINIO
注:resource还可以一起显示timer/dma,不过本章节不做展开。
const char * const ownerNames[OWNER_TOTAL_COUNT] = {
"FREE",
"PWM",
"PPM",
"MOTOR",
"SERVO",
"LED",
"ADC",
"ADC_BATT",
"ADC_CURR",
"ADC_EXT",
"ADC_RSSI",
"SERIAL_TX",
"SERIAL_RX",
"DEBUG",
"TIMER",
"SONAR_TRIGGER",
"SONAR_ECHO",
"SYSTEM",
"SPI_SCK",
"SPI_SDI",
"SPI_SDO",
"I2C_SCL",
"I2C_SDA",
"SDCARD",
"SDIO_CK",
"SDIO_CMD",
"SDIO_D0",
"SDIO_D1",
"SDIO_D2",
"SDIO_D3",
"SDCARD_CS",
"SDCARD_DETECT",
"FLASH_CS",
"BARO_CS",
"GYRO_CS",
"OSD_CS",
"RX_SPI_CS",
"SPI_CS",
"GYRO_EXTI",
"BARO_EOC",
"COMPASS_EXTI",
"USB",
"USB_DETECT",
"BEEPER",
"OSD",
"RX_BIND",
"INVERTER",
"LED_STRIP",
"TRANSPONDER",
"VTX_POWER",
"VTX_CS",
"VTX_DATA",
"VTX_CLK",
"COMPASS_CS",
"RX_BIND_PLUG",
"ESCSERIAL",
"CAMERA_CONTROL",
"TIMUP",
"RANGEFINDER",
"RX_SPI",
"PINIO",
"USB_MSC_PIN",
"MCO",
"RX_SPI_BIND",
"RX_SPI_LED",
"PREINIT",
"RX_SPI_EXTI",
"RX_SPI_CC2500_TX_EN",
"RX_SPI_CC2500_LNA_EN",
"RX_SPI_CC2500_ANT_SEL",
"QSPI_CLK",
"QSPI_BK1IO0",
"QSPI_BK1IO1",
"QSPI_BK1IO2",
"QSPI_BK1IO3",
"QSPI_BK1CS",
"QSPI_BK2IO0",
"QSPI_BK2IO1",
"QSPI_BK2IO2",
"QSPI_BK2IO3",
"QSPI_BK2CS",
"BARO_XCLR",
"PULLUP",
"PULLDOWN",
"DSHOT_BITBANG",
"SWD",
"RX_SPI_EXPRESSLRS_RESET",
"RX_SPI_EXPRESSLRS_BUSY",
};
2. 代码分析
cliResource
├──> <!pch> printResource(DUMP_MASTER | HIDE_UNUSED, NULL); return// 当前配置打印输出
├──> "show" // 当前资源配置情况
│ ├──> cliPrintLine("Currently active IO resource assignments:\r\n(reboot to update)")
│ ├──> cliRepeat('-', 20)
│ ├──> for DEFIO_IO_USED_COUNT
│ │ ├──> cliPrintf("%c%02d: %s", IO_GPIOPortIdx(ioRecs + i) + 'A', IO_GPIOPinIdx(ioRecs + i), owner);
│ │ └──> <ioRecs[i].index > 0> cliPrintf(" %d", ioRecs[i].index)
│ ├──> "all"
│ │ ├──> <USE_TIMER_MGMT> cliTimer(cmdName, "show")
│ │ └──> <USE_DMA> cliDma(cmdName, "show")
│ └──> return
├──> [Find resource in resourceTable/ownerNames, get resourceIndex] error return
├──> [Check resource index below maxIndex, get index] error return
├──> ioTag_t *tag = getIoTag(resourceTable[resourceIndex], index) // 获取引脚IOTAG
├──> <IO_TAG_NONE>
│ └──> cliPrintLine("Resource is freed")
├──> <!IO_TAG_NONE>
│ ├──> ioRec_t *rec = IO_Rec(IOGetByTag(*tag))
│ ├──> resourceCheck(resourceIndex, index, *tag)
│ └──> cliPrintLinef("\r\nResource is set to %c%02d", IO_GPIOPortIdx(rec) + 'A', IO_GPIOPinIdx(rec))
└──> cliShowParseError(cmdName)
注:常见情况MINIMAL_CLI无定义。
3. 实例分析
AOCODARC H7DUAL配置文件中,相关resource部分:
# resources
resource BEEPER 1 A15
resource MOTOR 1 B00
resource MOTOR 2 B01
resource MOTOR 3 A00
resource MOTOR 4 A01
resource MOTOR 5 A02
resource MOTOR 6 A03
resource MOTOR 7 D12
resource MOTOR 8 D13
resource SERVO 1 E05
resource SERVO 2 E06
resource PPM 1 A10
resource LED_STRIP 1 A08
resource SERIAL_TX 1 A09
resource SERIAL_TX 2 D05
resource SERIAL_TX 3 D08
resource SERIAL_TX 4 B09
resource SERIAL_TX 6 C06
resource SERIAL_TX 7 E08
resource SERIAL_TX 8 E01
resource SERIAL_RX 1 A10
resource SERIAL_RX 2 D06
resource SERIAL_RX 3 D09
resource SERIAL_RX 4 B08
resource SERIAL_RX 6 C07
resource SERIAL_RX 7 E07
resource SERIAL_RX 8 E00
resource I2C_SCL 1 B06
resource I2C_SCL 2 B10
resource I2C_SDA 1 B07
resource I2C_SDA 2 B11
resource LED 1 E03
resource LED 2 E04
resource SPI_SCK 1 A05
resource SPI_SCK 2 B13
resource SPI_SCK 3 B03
resource SPI_SCK 4 E12
resource SPI_MISO 1 A06
resource SPI_MISO 2 B14
resource SPI_MISO 3 B04
resource SPI_MISO 4 E13
resource SPI_MOSI 1 D07
resource SPI_MOSI 2 B15
resource SPI_MOSI 3 B05
resource SPI_MOSI 4 E14
resource ESCSERIAL 1 C07
resource ADC_BATT 1 C00
resource ADC_RSSI 1 C05
resource ADC_CURR 1 C01
resource ADC_EXT 1 C04
resource PINIO 1 D10
resource PINIO 2 D11
resource FLASH_CS 1 D03
resource OSD_CS 1 B12
resource GYRO_EXTI 1 B02
resource GYRO_EXTI 2 E15
resource GYRO_CS 1 C15
resource GYRO_CS 2 E11
resource USB_DETECT 1 E02
4. 配置情况
# help resource
resource - show/set resources
<> | <resource name> <index> [<pin>|none] | show [all]
4.1 resource
将配置命令重新整理输出,格式:resource + name + index + pin
# resource
resource BEEPER 1 A15
resource MOTOR 1 B00
resource MOTOR 2 B01
resource MOTOR 3 A00
resource MOTOR 4 A01
resource MOTOR 5 A02
resource MOTOR 6 A03
resource MOTOR 7 D12
resource MOTOR 8 D13
resource SERVO 1 E05
resource SERVO 2 E06
resource PPM 1 A10
resource LED_STRIP 1 A08
resource SERIAL_TX 1 A09
resource SERIAL_TX 2 D05
resource SERIAL_TX 3 D08
resource SERIAL_TX 4 B09
resource SERIAL_TX 6 C06
resource SERIAL_TX 7 E08
resource SERIAL_TX 8 E01
resource SERIAL_RX 1 A10
resource SERIAL_RX 2 D06
resource SERIAL_RX 3 D09
resource SERIAL_RX 4 B08
resource SERIAL_RX 6 C07
resource SERIAL_RX 7 E07
resource SERIAL_RX 8 E00
resource I2C_SCL 1 B06
resource I2C_SCL 2 B10
resource I2C_SDA 1 B07
resource I2C_SDA 2 B11
resource LED 1 E03
resource LED 2 E04
resource SPI_SCK 1 A05
resource SPI_SCK 2 B13
resource SPI_SCK 3 B03
resource SPI_SCK 4 E12
resource SPI_MISO 1 A06
resource SPI_MISO 2 B14
resource SPI_MISO 3 B04
resource SPI_MISO 4 E13
resource SPI_MOSI 1 D07
resource SPI_MOSI 2 B15
resource SPI_MOSI 3 B05
resource SPI_MOSI 4 E14
resource ESCSERIAL 1 C07
resource ADC_BATT 1 C00
resource ADC_RSSI 1 C05
resource ADC_CURR 1 C01
resource ADC_EXT 1 C04
resource PINIO 1 D10
resource PINIO 2 D11
resource FLASH_CS 1 D03
resource OSD_CS 1 B12
resource GYRO_EXTI 1 B02
resource GYRO_EXTI 2 E15
resource GYRO_CS 1 C15
resource GYRO_CS 2 E11
resource USB_DETECT 1 E02
4.2 resource show
按照引脚顺序,对资源进行整理打印输出,格式: pin: resource_name + index
# resource show
Currently active IO resource assignments:
(reboot to update)
--------------------
A00: MOTOR 3
A01: MOTOR 4
A02: FREE
A03: FREE
A04: FREE
A05: SPI_SCK 1
A06: SPI_MISO 1
A07: FREE
A08: LED_STRIP
A09: FREE
A10: SERIAL_RX 1
A11: USB
A12: USB
A13: SWD
A14: SWD
A15: BEEPER
B00: MOTOR 1
B01: MOTOR 2
B02: GYRO_EXTI
B03: SPI_SCK 3
B04: SPI_MISO 3
B05: SPI_MOSI 3
B06: I2C_SCL 1
B07: I2C_SDA 1
B08: FREE
B09: FREE
B10: I2C_SCL 2
B11: I2C_SDA 2
B12: OSD_CS
B13: SPI_SCK 2
B14: SPI_MISO 2
B15: SPI_MOSI 2
C00: ADC_BATT
C01: ADC_CURR
C02: FREE
C03: FREE
C04: FREE
C05: FREE
C06: FREE
C07: FREE
C08: FREE
C09: FREE
C10: FREE
C11: FREE
C12: FREE
C13: FREE
C14: FREE
C15: GYRO_CS 1
D00: FREE
D01: FREE
D02: FREE
D03: FLASH_CS
D04: FREE
D05: FREE
D06: FREE
D07: SPI_MOSI 1
D08: FREE
D09: FREE
D10: PINIO 1
D11: PINIO 2
D12: FREE
D13: FREE
D14: FREE
D15: FREE
E00: FREE
E01: FREE
E02: FREE
E03: LED 1
E04: LED 2
E05: FREE
E06: FREE
E07: FREE
E08: FREE
E09: FREE
E10: FREE
E11: GYRO_CS 2
E12: SPI_SCK 4
E13: SPI_MISO 4
E14: SPI_MOSI 4
E15: GYRO_EXTI
F00: FREE
F01: FREE
F02: FREE
F03: FREE
F04: FREE
F05: FREE
F06: FREE
F07: FREE
F08: FREE
F09: FREE
F10: FREE
F11: FREE
F12: FREE
F13: FREE
F14: FREE
F15: FREE
G00: FREE
G01: FREE
G02: FREE
G03: FREE
G04: FREE
G05: FREE
G06: FREE
G07: FREE
G08: FREE
G09: FREE
G10: FREE
G11: FREE
G12: FREE
G13: FREE
G14: FREE
G15: FREE
5. 参考资料
【1】BetaFlight开源代码框架简介
【2】BetaFlight统一硬件资源简单配置修改
【3】BetaFlight统一硬件配置文件研读