diff --git a/boards/holybro/kakutef7/init/rc.board_extras b/boards/holybro/kakutef7/init/rc.board_extras index 19cf94598d6..309ed17768f 100644 --- a/boards/holybro/kakutef7/init/rc.board_extras +++ b/boards/holybro/kakutef7/init/rc.board_extras @@ -5,7 +5,7 @@ if ! param compare OSD_ATXXXX_CFG 0 then - atxxxx start + atxxxx start -s fi # DShot telemetry is always on UART7 diff --git a/boards/omnibus/f4sd/init/rc.board_extras b/boards/omnibus/f4sd/init/rc.board_extras index b35e2a6dbd9..25e6c269a85 100644 --- a/boards/omnibus/f4sd/init/rc.board_extras +++ b/boards/omnibus/f4sd/init/rc.board_extras @@ -5,7 +5,7 @@ if ! param compare OSD_ATXXXX_CFG 0 then - atxxxx start + atxxxx start -s fi diff --git a/src/drivers/osd/atxxxx/atxxxx.cpp b/src/drivers/osd/atxxxx/atxxxx.cpp index 45d6ac639b1..b016a880144 100644 --- a/src/drivers/osd/atxxxx/atxxxx.cpp +++ b/src/drivers/osd/atxxxx/atxxxx.cpp @@ -44,56 +44,15 @@ using namespace time_literals; -static constexpr uint32_t OSD_UPDATE_RATE{500_ms}; // 2 Hz +static constexpr uint32_t OSD_UPDATE_RATE{50_ms}; // 20 Hz -OSDatxxxx::OSDatxxxx(int bus) : - SPI("OSD", nullptr, bus, PX4_MK_SPI_SEL(bus, OSD_SPIDEV), SPIDEV_MODE0, OSD_SPI_BUS_SPEED), +OSDatxxxx::OSDatxxxx(I2CSPIBusOption bus_option, int bus, int devid, int bus_frequency, spi_mode_e spi_mode) : + SPI("OSD", nullptr, bus, devid, spi_mode, bus_frequency), ModuleParams(nullptr), - ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(get_device_id())) + I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus) { } -OSDatxxxx::~OSDatxxxx() -{ - ScheduleClear(); -} - -int -OSDatxxxx::task_spawn(int argc, char *argv[]) -{ - int ch; - int myoptind = 1; - const char *myoptarg = nullptr; - int spi_bus = OSD_BUS; - - while ((ch = px4_getopt(argc, argv, "b:", &myoptind, &myoptarg)) != EOF) { - switch (ch) { - case 'b': - spi_bus = (uint8_t)atoi(myoptarg); - break; - } - } - - OSDatxxxx *osd = new OSDatxxxx(spi_bus); - - if (!osd) { - PX4_ERR("alloc failed"); - return PX4_ERROR; - } - - if (osd->init() != PX4_OK) { - delete osd; - return PX4_ERROR; - } - - _object.store(osd); - _task_id = task_id_is_work_queue; - - osd->start(); - - return PX4_OK; -} - int OSDatxxxx::init() { @@ -519,7 +478,7 @@ OSDatxxxx::reset() } void -OSDatxxxx::Run() +OSDatxxxx::RunImpl() { if (should_exit()) { exit_and_cleanup(); @@ -531,13 +490,9 @@ OSDatxxxx::Run() update_screen(); } -int -OSDatxxxx::print_usage(const char *reason) +void +OSDatxxxx::print_usage() { - if (reason) { - printf("%s\n\n", reason); - } - PRINT_MODULE_DESCRIPTION( R"DESCR_STR( ### Description @@ -547,21 +502,61 @@ It can be enabled with the OSD_ATXXXX_CFG parameter. )DESCR_STR"); PRINT_MODULE_USAGE_NAME("atxxxx", "driver"); - PRINT_MODULE_USAGE_COMMAND_DESCR("start", "Start the driver"); - PRINT_MODULE_USAGE_PARAM_INT('b', -1, 0, 100, "SPI bus (default: use board-specific bus)", true); + PRINT_MODULE_USAGE_COMMAND("start"); + PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(false, true); PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); - - return 0; } -int -OSDatxxxx::custom_command(int argc, char *argv[]) +I2CSPIDriverBase *OSDatxxxx::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator, + int runtime_instance) { - return print_usage("unrecognized command"); + OSDatxxxx *instance = new OSDatxxxx(iterator.configuredBusOption(), iterator.bus(), iterator.devid(), + cli.bus_frequency, cli.spi_mode); + + if (!instance) { + PX4_ERR("alloc failed"); + return nullptr; + } + + if (OK != instance->init()) { + delete instance; + return nullptr; + } + + instance->start(); + + return instance; } int atxxxx_main(int argc, char *argv[]) { - return OSDatxxxx::main(argc, argv); + using ThisDriver = OSDatxxxx; + BusCLIArguments cli{false, true}; + cli.spi_mode = SPIDEV_MODE0; + cli.default_spi_frequency = OSD_SPI_BUS_SPEED; + + const char *verb = cli.parseDefaultArguments(argc, argv); + + if (!verb) { + ThisDriver::print_usage(); + return -1; + } + + BusInstanceIterator iterator(MODULE_NAME, cli, DRV_OSD_DEVTYPE_ATXXXX); + + if (!strcmp(verb, "start")) { + return ThisDriver::module_start(cli, iterator); + } + + if (!strcmp(verb, "stop")) { + return ThisDriver::module_stop(iterator); + } + + if (!strcmp(verb, "status")) { + return ThisDriver::module_status(iterator); + } + + ThisDriver::print_usage(); + return -1; } diff --git a/src/drivers/osd/atxxxx/atxxxx.h b/src/drivers/osd/atxxxx/atxxxx.h index 5b90f40c09f..27db949d4f1 100644 --- a/src/drivers/osd/atxxxx/atxxxx.h +++ b/src/drivers/osd/atxxxx/atxxxx.h @@ -46,25 +46,12 @@ #include #include #include -#include +#include #include #include #include #include -/* Configuration Constants */ -#ifdef PX4_SPI_BUS_OSD -#define OSD_BUS PX4_SPI_BUS_OSD -#else -#error "add the required spi bus from board_config.h here" -#endif - -#ifdef PX4_SPIDEV_OSD -#define OSD_SPIDEV PX4_SPIDEV_OSD -#else -#error "add the required spi device from board_config.h here" -#endif - #define OSD_SPI_BUS_SPEED (2000000L) /* 2 MHz */ #define DIR_READ(a) ((a) | (1 << 7)) @@ -78,36 +65,24 @@ extern "C" __EXPORT int atxxxx_main(int argc, char *argv[]); -class OSDatxxxx : public device::SPI, public ModuleBase, public ModuleParams, public px4::ScheduledWorkItem +class OSDatxxxx : public device::SPI, public ModuleParams, public I2CSPIDriver { public: - OSDatxxxx(int bus = OSD_BUS); + OSDatxxxx(I2CSPIBusOption bus_option, int bus, int devid, int bus_frequency, spi_mode_e spi_mode); + virtual ~OSDatxxxx() = default; - ~OSDatxxxx(); + static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator, + int runtime_instance); + static void print_usage(); - virtual int init(); + int init() override; - /** - * @see ModuleBase::custom_command - */ - static int custom_command(int argc, char *argv[]); - - /** - * @see ModuleBase::print_usage - */ - static int print_usage(const char *reason = nullptr); - - /** - * @see ModuleBase::task_spawn - */ - static int task_spawn(int argc, char *argv[]); + void RunImpl(); protected: - virtual int probe(); + int probe() override; private: - void Run() override; - int start(); int reset();