1 Star 0 Fork 0

重剑无锋/DriverFramework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

DriverFramework

Build Status

Driver framework for POSIX based userspace drivers.

Overview

The top level namespace is DriverFramework.

The main classes are:

  • Framework: Used to start and stop the driver framework
  • DevMgr: Used to:
    • Register and unregister device drivers
    • get and release DevHandle objects
  • WorkMgr: Used by drivers to:
    • schedule periodic tasks
    • create and destroy WorkHandles
  • DevObj: The base class of all drivers
    • periodic callback method virtual void _measure()

The framework consists of one worker thread (class HRTWorkQueue) that periodically executes the method virtual void DevObj::_measure(), that is implemented by the corresponding device driver to update its data.

The framework provides three intermediate driver classes as a base for new drivers:

  • VirtDevObj: Provides a base class for simulated drivers
  • I2CDevObj: Provides a base class for I2C drivers
  • SPIDevObj: Provides a base class for SPI drivers

Framework Initialization

The framework must be initialized before the other classes can be used. There is no C++ static initialization used in the framework to allow environements that do not support C++ static initialization.

DriverFramework::initialize();
...
DriverFramework::shutdown();

If the exit condition is an async event and you need to block the calling thread you can use:

DriverFramework::waitForShutdown();

This will block until DriverFramework::shutdown() is called from another thread;

Driver Initialization

When the driver is initialized, it will register with the framework and provide an instance of the base class path.

For instance, if the driver was created with:

#define GYRO_BASE_PATH "/dev/gyro"

class Gyro : public I2CDevObj
{
public:
	Gyro() :
		I2CDevObj("Gyro", GYRO_BASE_PATH, 1000)
	{}
protected:
	virtual void _measure();
...
};

...
void run()
{
	DriverFramework::initialize();
	Gyro myGyro();
	myGyro.init(); // register the driver

	...

	DevHandle h;
	DevMgr::getHandle("/dev/gyro0", h); // Starts the driver

	if (!h.isvalid()) {
		printf("failed to get device handle\n");
	}

	h.read(...), h.write(...), ...
	...

	DevMgr::releaseHandle(h);

	DriverFramework::waitForShutdown(); // handles are freed on shutdown

	// Otherwise h releases the handle when destructed
}

The init() call would create a virtual /dev/gyro0 node. A second driver also regsitering with GYRO_BASE_PATH would create a virtual /dev/gyro1 node.

A handle to the device can be acquired using:

myGyro.init();
...
DevMgr::getHandle("/dev/gyro0", h);

DevHandle objects are used to access the driver via a device path from anywhere in the code after the driver was started. DevHandle objects cannot be copied.

Starting and Stopping the Driver

By default, the driver is initialized and started the first time a handle is opened to the device (if it is not running already). It keeps running when the last handle is released.

The above use case utilizes handles to communicate with the driver, but there is also a second use case. The use of DevHandle is optional and the driver can be explicitly started or stopped using start() or stop(). In this case there is typically a project-specific wrapper class that inherits from the driver and directly handles the data (e.g. by publishing a message).

myGyro.start();
...
myGyro.stop();

Calling POSIX functions

The read, write and ioctl calls are wrapped and provided as calls via the DevHandle.

The functions follow the POSIX sematics and the errno value can be accessed via getError().

DevHandle h;
DevMgr::getHandle("/dev/gyro0", h); // Starts the driver

SomeDataStruct data[3];
int ret = h.read(data, sizeof(data));
if (ret < 0) {
	printf("Error read failed (%d)\n", h.getError());
}

Testing

To run the unit tests build it and run the test app:

Testing on the PC

make linux
build_linux/test/df_testapp

Testing on Snapdragon Flight

Attach the Snapdragon Flight board via adb.

$ make qurt
$ cd build_qurt
$ make df_testapp-load df_imu_test-load df_mag_test-load df_pressure_test-load

Run mini-dm to see the output from the DSP

${HEXAGON_SDK_ROOT}/tools/mini-dm/Linux_Debug/mini-dm

Open an new shell and run the application on the target.

$ adb shell
# cd /home/linaro
# ./df_testapp
# ./df_imu_test
# ./df_pressure_test

The following unit test seems to be failing

# ./df_mag_test

Hardware Support

Please refer to http://dev.px4.io for an overview of currently supported targets, as PX4 is using DriverFramework. Late 2016 this included

  • Mac OS
  • Linux (RPI / Navio 2)
  • QuRT (Snapdragon)

空文件

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sdkmsdn_admin/DriverFramework.git
git@gitee.com:sdkmsdn_admin/DriverFramework.git
sdkmsdn_admin
DriverFramework
DriverFramework
master

搜索帮助