From 5d20cf6b57074f930e508396ea94fe85c18d9697 Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Wed, 8 Nov 2017 09:51:12 -0500 Subject: [PATCH] drivers device organize by nuttx/posix --- src/drivers/device/CMakeLists.txt | 23 ++-- src/drivers/device/device.h | 4 +- src/drivers/device/i2c.h | 4 +- .../device/{ => nuttx}/device_nuttx.cpp | 0 src/drivers/device/{ => nuttx}/device_nuttx.h | 0 src/drivers/device/{ => nuttx}/i2c_nuttx.cpp | 0 src/drivers/device/{ => nuttx}/i2c_nuttx.h | 2 +- src/drivers/device/{ => nuttx}/spi.cpp | 0 .../device/{ => posix}/device_posix.cpp | 0 src/drivers/device/{ => posix}/i2c_posix.cpp | 0 src/drivers/device/{ => posix}/i2c_posix.h | 0 src/drivers/device/{ => posix}/vdev.cpp | 0 src/drivers/device/{ => posix}/vdev.h | 0 src/drivers/device/{ => posix}/vdev_posix.cpp | 0 src/drivers/device/{ => posix}/vfile.cpp | 0 src/drivers/device/{ => posix}/vfile.h | 0 src/drivers/device/sim.cpp | 116 ------------------ src/drivers/device/sim.h | 112 ----------------- 18 files changed, 19 insertions(+), 242 deletions(-) rename src/drivers/device/{ => nuttx}/device_nuttx.cpp (100%) rename src/drivers/device/{ => nuttx}/device_nuttx.h (100%) rename src/drivers/device/{ => nuttx}/i2c_nuttx.cpp (100%) rename src/drivers/device/{ => nuttx}/i2c_nuttx.h (99%) rename src/drivers/device/{ => nuttx}/spi.cpp (100%) rename src/drivers/device/{ => posix}/device_posix.cpp (100%) rename src/drivers/device/{ => posix}/i2c_posix.cpp (100%) rename src/drivers/device/{ => posix}/i2c_posix.h (100%) rename src/drivers/device/{ => posix}/vdev.cpp (100%) rename src/drivers/device/{ => posix}/vdev.h (100%) rename src/drivers/device/{ => posix}/vdev_posix.cpp (100%) rename src/drivers/device/{ => posix}/vfile.cpp (100%) rename src/drivers/device/{ => posix}/vfile.h (100%) delete mode 100644 src/drivers/device/sim.cpp delete mode 100644 src/drivers/device/sim.h diff --git a/src/drivers/device/CMakeLists.txt b/src/drivers/device/CMakeLists.txt index 0d55c0e1ca..43a16b97f2 100644 --- a/src/drivers/device/CMakeLists.txt +++ b/src/drivers/device/CMakeLists.txt @@ -38,25 +38,30 @@ list(APPEND SRCS integrator.cpp ) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + if(${OS} STREQUAL "nuttx") + include_directories(nuttx) list(APPEND SRCS - device_nuttx.cpp + nuttx/device_nuttx.cpp cdev.cpp ) + if ("${CONFIG_I2C}" STREQUAL "y") - list(APPEND SRCS i2c_nuttx.cpp) + list(APPEND SRCS nuttx/i2c_nuttx.cpp) endif() + if ("${CONFIG_SPI}" STREQUAL "y") - list(APPEND SRCS spi.cpp) + list(APPEND SRCS nuttx/spi.cpp) endif() else() + include_directories(posix) list(APPEND SRCS - device_posix.cpp - vdev.cpp - vfile.cpp - vdev_posix.cpp - i2c_posix.cpp - sim.cpp + posix/device_posix.cpp + posix/vdev.cpp + posix/vfile.cpp + posix/vdev_posix.cpp + posix/i2c_posix.cpp ) endif() diff --git a/src/drivers/device/device.h b/src/drivers/device/device.h index f968604a30..1295780fe5 100644 --- a/src/drivers/device/device.h +++ b/src/drivers/device/device.h @@ -34,14 +34,14 @@ #pragma once #ifdef __PX4_NUTTX -#include "device_nuttx.h" +#include "nuttx/device_nuttx.h" #include #define ATOMIC_ENTER irqstate_t flags = px4_enter_critical_section() #define ATOMIC_LEAVE px4_leave_critical_section(flags) #elif defined (__PX4_POSIX) -#include "vdev.h" +#include "posix/vdev.h" #define ATOMIC_ENTER lock() #define ATOMIC_LEAVE unlock() diff --git a/src/drivers/device/i2c.h b/src/drivers/device/i2c.h index e6a4dffc83..3392aabba4 100644 --- a/src/drivers/device/i2c.h +++ b/src/drivers/device/i2c.h @@ -33,7 +33,7 @@ #pragma once #ifdef __PX4_NUTTX -#include "i2c_nuttx.h" +#include "nuttx/i2c_nuttx.h" #else -#include "i2c_posix.h" +#include "posix/i2c_posix.h" #endif diff --git a/src/drivers/device/device_nuttx.cpp b/src/drivers/device/nuttx/device_nuttx.cpp similarity index 100% rename from src/drivers/device/device_nuttx.cpp rename to src/drivers/device/nuttx/device_nuttx.cpp diff --git a/src/drivers/device/device_nuttx.h b/src/drivers/device/nuttx/device_nuttx.h similarity index 100% rename from src/drivers/device/device_nuttx.h rename to src/drivers/device/nuttx/device_nuttx.h diff --git a/src/drivers/device/i2c_nuttx.cpp b/src/drivers/device/nuttx/i2c_nuttx.cpp similarity index 100% rename from src/drivers/device/i2c_nuttx.cpp rename to src/drivers/device/nuttx/i2c_nuttx.cpp diff --git a/src/drivers/device/i2c_nuttx.h b/src/drivers/device/nuttx/i2c_nuttx.h similarity index 99% rename from src/drivers/device/i2c_nuttx.h rename to src/drivers/device/nuttx/i2c_nuttx.h index 00e4848500..7aa8478040 100644 --- a/src/drivers/device/i2c_nuttx.h +++ b/src/drivers/device/nuttx/i2c_nuttx.h @@ -40,7 +40,7 @@ #ifndef _DEVICE_I2C_H #define _DEVICE_I2C_H -#include "device.h" +#include "../device.h" #include diff --git a/src/drivers/device/spi.cpp b/src/drivers/device/nuttx/spi.cpp similarity index 100% rename from src/drivers/device/spi.cpp rename to src/drivers/device/nuttx/spi.cpp diff --git a/src/drivers/device/device_posix.cpp b/src/drivers/device/posix/device_posix.cpp similarity index 100% rename from src/drivers/device/device_posix.cpp rename to src/drivers/device/posix/device_posix.cpp diff --git a/src/drivers/device/i2c_posix.cpp b/src/drivers/device/posix/i2c_posix.cpp similarity index 100% rename from src/drivers/device/i2c_posix.cpp rename to src/drivers/device/posix/i2c_posix.cpp diff --git a/src/drivers/device/i2c_posix.h b/src/drivers/device/posix/i2c_posix.h similarity index 100% rename from src/drivers/device/i2c_posix.h rename to src/drivers/device/posix/i2c_posix.h diff --git a/src/drivers/device/vdev.cpp b/src/drivers/device/posix/vdev.cpp similarity index 100% rename from src/drivers/device/vdev.cpp rename to src/drivers/device/posix/vdev.cpp diff --git a/src/drivers/device/vdev.h b/src/drivers/device/posix/vdev.h similarity index 100% rename from src/drivers/device/vdev.h rename to src/drivers/device/posix/vdev.h diff --git a/src/drivers/device/vdev_posix.cpp b/src/drivers/device/posix/vdev_posix.cpp similarity index 100% rename from src/drivers/device/vdev_posix.cpp rename to src/drivers/device/posix/vdev_posix.cpp diff --git a/src/drivers/device/vfile.cpp b/src/drivers/device/posix/vfile.cpp similarity index 100% rename from src/drivers/device/vfile.cpp rename to src/drivers/device/posix/vfile.cpp diff --git a/src/drivers/device/vfile.h b/src/drivers/device/posix/vfile.h similarity index 100% rename from src/drivers/device/vfile.h rename to src/drivers/device/posix/vfile.h diff --git a/src/drivers/device/sim.cpp b/src/drivers/device/sim.cpp deleted file mode 100644 index 9a7b721251..0000000000 --- a/src/drivers/device/sim.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/**************************************************************************** - * - * Copyright (c) 2012-2015 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. - * - ****************************************************************************/ - -/** - * @file sim.cpp - * - * Base class for simulated devices. - * - * @todo Bus frequency changes; currently we do nothing with the value - * that is supplied. Should we just depend on the bus knowing? - */ - -#include -#include -#include -#include -#include "sim.h" - -namespace device -{ - -SIM::SIM(const char *name, - const char *devname, - int bus, - uint16_t address) : - // base class - Device(name), - // public - // protected - // private - _bus(bus), - _address(address), - _devname(devname) -{ - - PX4_DEBUG("SIM::SIM name = %s devname = %s", name, devname); - // fill in _device_id fields for a SIM device - _device_id.devid_s.bus_type = DeviceBusType_SIM; - _device_id.devid_s.bus = bus; - _device_id.devid_s.address = address; - // devtype needs to be filled in by the driver - _device_id.devid_s.devtype = 0; -} - -SIM::~SIM() -{ -} - -int -SIM::init() -{ - int ret = PX4_OK; - - // Assume the driver set the desired bus frequency. There is no standard - // way to set it from user space. - - // do base class init, which registers the virtual driver - ret = Device::init(); - - if (ret != PX4_OK) { - PX4_ERR("CDev::init failed"); - return ret; - } - - return ret; -} - -int -SIM::transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned recv_len) -{ - if (send_len > 0) { - PX4_DEBUG("SIM: sending %d bytes", send_len); - } - - if (recv_len > 0) { - PX4_DEBUG("SIM: receiving %d bytes", recv_len); - - // TODO - write data to recv; - } - - PX4_DEBUG("I2C SIM: transfer_4 on %s", _devname); - - return PX4_OK; -} - -} // namespace device diff --git a/src/drivers/device/sim.h b/src/drivers/device/sim.h deleted file mode 100644 index 1e84af3863..0000000000 --- a/src/drivers/device/sim.h +++ /dev/null @@ -1,112 +0,0 @@ -/**************************************************************************** - * - * Copyright (C) 2012 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. - * - ****************************************************************************/ - -/** - * @file sim.h - * - * Base class for devices on simulation bus. - */ - -#pragma once - -#include "vdev.h" - -namespace device __EXPORT -{ - -/** - * Abstract class for character device on SIM - */ -class __EXPORT SIM : public Device -{ - -public: - - /** - * Get the address - */ - int16_t get_address() const { return _address; } - -protected: - /** - * The number of times a read or write operation will be retried on - * error. - */ - unsigned _retries; - - /** - * The SIM bus number the device is attached to. - */ - int _bus; - - /** - * @ Constructor - * - * @param name Driver name - * @param devname Device node name - * @param bus SIM bus on which the device lives - * @param address SIM bus address, or zero if set_address will be used - */ - SIM(const char *name, - const char *devname, - int bus, - uint16_t address); - virtual ~SIM(); - - virtual int init(); - - /** - * Perform an SIM transaction to the device. - * - * At least one of send_len and recv_len must be non-zero. - * - * @param send Pointer to bytes to send. - * @param send_len Number of bytes to send. - * @param recv Pointer to buffer for bytes received. - * @param recv_len Number of bytes to receive. - * @return OK if the transfer was successful, -errno - * otherwise. - */ - virtual int transfer(const uint8_t *send, unsigned send_len, - uint8_t *recv, unsigned recv_len); - -private: - uint16_t _address; - const char *_devname; - - SIM(const device::SIM &); - SIM operator=(const device::SIM &); -}; - -} // namespace device -