From 14cf92d8b224aa07d7f0cbacf6994f149e8cb9a0 Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Mon, 9 Jul 2018 01:50:05 -0400 Subject: [PATCH] lsm303agr split main --- .../magnetometer/lsm303agr/CMakeLists.txt | 1 + .../magnetometer/lsm303agr/LSM303AGR.cpp | 135 -------------- .../magnetometer/lsm303agr/lsm303agr_main.cpp | 172 ++++++++++++++++++ 3 files changed, 173 insertions(+), 135 deletions(-) create mode 100644 src/drivers/magnetometer/lsm303agr/lsm303agr_main.cpp diff --git a/src/drivers/magnetometer/lsm303agr/CMakeLists.txt b/src/drivers/magnetometer/lsm303agr/CMakeLists.txt index 44e6d0fb4c..879fa3aab9 100644 --- a/src/drivers/magnetometer/lsm303agr/CMakeLists.txt +++ b/src/drivers/magnetometer/lsm303agr/CMakeLists.txt @@ -34,5 +34,6 @@ px4_add_module( MODULE drivers__magnetometer__lsm303agr MAIN lsm303agr SRCS + lsm303agr_main.cpp LSM303AGR.cpp ) diff --git a/src/drivers/magnetometer/lsm303agr/LSM303AGR.cpp b/src/drivers/magnetometer/lsm303agr/LSM303AGR.cpp index f199e239e8..bcee090037 100644 --- a/src/drivers/magnetometer/lsm303agr/LSM303AGR.cpp +++ b/src/drivers/magnetometer/lsm303agr/LSM303AGR.cpp @@ -48,8 +48,6 @@ #define DIR_WRITE (0<<7) #define ADDR_INCREMENT (1<<6) -#define LSM303AGR_DEVICE_PATH_MAG "/dev/lsm303agr_mag" - /* Max measurement rate is 100Hz */ #define CONVERSION_INTERVAL (1000000 / 100) /* microseconds */ @@ -63,8 +61,6 @@ static constexpr uint8_t LSM303AGR_WHO_AM_I_M = 0x40; */ #define LSM303AGR_TIMER_REDUCTION 200 -extern "C" { __EXPORT int lsm303agr_main(int argc, char *argv[]); } - LSM303AGR::LSM303AGR(int bus, const char *path, uint32_t device, enum Rotation rotation) : SPI("LSM303AGR", path, bus, device, SPIDEV_MODE3, 8 * 1000 * 1000), _mag_sample_perf(perf_alloc(PC_ELAPSED, "LSM303AGR_mag_read")), @@ -524,134 +520,3 @@ LSM303AGR::print_info() perf_print_counter(_bad_registers); perf_print_counter(_bad_values); } - -/** - * Local functions in support of the shell command. - */ -namespace lsm303agr -{ - -LSM303AGR *g_dev; - -void start(enum Rotation rotation); -void info(); -void usage(); - -/** - * Start the driver. - * - * This function call only returns once the driver is - * up and running or failed to detect the sensor. - */ -void -start(enum Rotation rotation) -{ - int fd_mag = -1; - - if (g_dev != nullptr) { - errx(0, "already started"); - } - - /* create the driver */ -#if defined(PX4_SPIDEV_LSM303A_M) && defined(PX4_SPIDEV_LSM303A_X) - g_dev = new LSM303AGR(PX4_SPI_BUS_SENSOR5, LSM303AGR_DEVICE_PATH_MAG, PX4_SPIDEV_LSM303A_M, rotation); -#else - errx(0, "External SPI not available"); -#endif - - if (g_dev == nullptr) { - PX4_ERR("alloc failed"); - goto fail; - } - - if (OK != g_dev->init()) { - goto fail; - } - - fd_mag = px4_open(LSM303AGR_DEVICE_PATH_MAG, O_RDONLY); - - /* don't fail if open cannot be opened */ - if (0 <= fd_mag) { - if (px4_ioctl(fd_mag, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) { - goto fail; - } - } - - px4_close(fd_mag); - - exit(0); -fail: - - if (g_dev != nullptr) { - delete g_dev; - g_dev = nullptr; - } - - errx(1, "driver start failed"); -} - -/** - * Print a little info about the driver. - */ -void -info() -{ - if (g_dev == nullptr) { - errx(1, "driver not running\n"); - } - - printf("state @ %p\n", g_dev); - g_dev->print_info(); - - exit(0); -} - -void -usage() -{ - PX4_INFO("missing command: try 'start', 'info', 'reset'"); - PX4_INFO("options:"); - PX4_INFO(" -X (external bus)"); - PX4_INFO(" -R rotation"); -} - -} // namespace - -int -lsm303agr_main(int argc, char *argv[]) -{ - int ch; - enum Rotation rotation = ROTATION_NONE; - - /* jump over start/off/etc and look at options first */ - while ((ch = getopt(argc, argv, "XR:a:")) != EOF) { - switch (ch) { - case 'R': - rotation = (enum Rotation)atoi(optarg); - break; - - default: - lsm303agr::usage(); - exit(0); - } - } - - const char *verb = argv[optind]; - - /* - * Start/load the driver. - - */ - if (!strcmp(verb, "start")) { - lsm303agr::start(rotation); - } - - /* - * Print driver information. - */ - if (!strcmp(verb, "info")) { - lsm303agr::info(); - } - - errx(1, "unrecognized command, try 'start', 'info'"); -} diff --git a/src/drivers/magnetometer/lsm303agr/lsm303agr_main.cpp b/src/drivers/magnetometer/lsm303agr/lsm303agr_main.cpp new file mode 100644 index 0000000000..241df06d69 --- /dev/null +++ b/src/drivers/magnetometer/lsm303agr/lsm303agr_main.cpp @@ -0,0 +1,172 @@ +/**************************************************************************** + * + * Copyright (c) 2018 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include "LSM303AGR.hpp" + +#include +#include + +#define LSM303AGR_DEVICE_PATH_MAG "/dev/lsm303agr_mag" + +extern "C" { __EXPORT int lsm303agr_main(int argc, char *argv[]); } + +/** + * Local functions in support of the shell command. + */ +namespace lsm303agr +{ + +LSM303AGR *g_dev; + +void start(enum Rotation rotation); +void info(); +void usage(); + +/** + * Start the driver. + * + * This function call only returns once the driver is + * up and running or failed to detect the sensor. + */ +void +start(enum Rotation rotation) +{ + int fd_mag = -1; + + if (g_dev != nullptr) { + errx(0, "already started"); + } + + /* create the driver */ +#if defined(PX4_SPIDEV_LSM303A_M) && defined(PX4_SPIDEV_LSM303A_X) + g_dev = new LSM303AGR(PX4_SPI_BUS_SENSOR5, LSM303AGR_DEVICE_PATH_MAG, PX4_SPIDEV_LSM303A_M, rotation); +#else + errx(0, "External SPI not available"); +#endif + + if (g_dev == nullptr) { + PX4_ERR("alloc failed"); + goto fail; + } + + if (OK != g_dev->init()) { + goto fail; + } + + fd_mag = px4_open(LSM303AGR_DEVICE_PATH_MAG, O_RDONLY); + + /* don't fail if open cannot be opened */ + if (0 <= fd_mag) { + if (px4_ioctl(fd_mag, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) { + goto fail; + } + } + + px4_close(fd_mag); + + exit(0); +fail: + + if (g_dev != nullptr) { + delete g_dev; + g_dev = nullptr; + } + + errx(1, "driver start failed"); +} + +/** + * Print a little info about the driver. + */ +void +info() +{ + if (g_dev == nullptr) { + errx(1, "driver not running\n"); + } + + printf("state @ %p\n", g_dev); + g_dev->print_info(); + + exit(0); +} + +void +usage() +{ + PX4_INFO("missing command: try 'start', 'info', 'reset'"); + PX4_INFO("options:"); + PX4_INFO(" -X (external bus)"); + PX4_INFO(" -R rotation"); +} + +} // namespace + +int +lsm303agr_main(int argc, char *argv[]) +{ + int ch; + enum Rotation rotation = ROTATION_NONE; + + /* jump over start/off/etc and look at options first */ + while ((ch = getopt(argc, argv, "XR:a:")) != EOF) { + switch (ch) { + case 'R': + rotation = (enum Rotation)atoi(optarg); + break; + + default: + lsm303agr::usage(); + exit(0); + } + } + + const char *verb = argv[optind]; + + /* + * Start/load the driver. + + */ + if (!strcmp(verb, "start")) { + lsm303agr::start(rotation); + } + + /* + * Print driver information. + */ + if (!strcmp(verb, "info")) { + lsm303agr::info(); + } + + errx(1, "unrecognized command, try 'start', 'info'"); +}