点击查看代码
Xenomai3驱动程序:
驱动C文件: 自定义Mydriver.c,用于xenomai内核设备驱动相关
原始源文件路径:xenomai3.x/kernel/drivers/testing/rtdmtest.c
相关函数解析:
static int __init rtdm_test_init(void){
#Todo初始化模块工作,可同时打开多个设备
}
static void __exit rtdm_test_exit(void)
{
#Todo退出模块工作
}
static struct rtdm_driver rtdm_basic_driver = {
.profile_info = RTDM_PROFILE_INFO(rtdm_test_basic, //在proc/devices/查看的设备
RTDM_CLASS_TESTING,
RTDM_SUBCLASS_RTDMTEST,
RTTST_PROFILE_VER),
.device_flags = RTDM_NAMED_DEVICE | RTDM_EXCLUSIVE,
.device_count = 2,
.context_size = sizeof(struct rtdm_basic_context),
.ops = { //相关操作函数定义
.open = rtdm_basic_open,
.close = rtdm_basic_close,
.write_rt = rtdm_basic_write_rt,
.ioctl_rt = rtdm_basic_ioctl_rt,
.ioctl_nrt = rtdm_basic_ioctl_nrt,
},
};
static struct rtdm_device device[3] = {
[0 ... 1] = {
.driver = &rtdm_basic_driver,
.label = "rtdm%d", //作为打开的设备名%d=1,2
},
[2] = {
.driver = &rtdm_actor_driver,
.label = "rtdmx", //设备打开的名字为rtdmx
}
};
rtdm_dev_register(device); #注册设备
rtdm_dev_unregister( device ); #反注册设备
可以同时注册,反注册多个设备,设备数量在结构体rtdm_driver的成员device_count定义
module_init(rtdm_test_init); #定义模块初始接口函数
module_exit(rtdm_test_exit); #定义模块卸载接口函数
安装模块指令 insmod xxx.ko , 卸载模块指令 rmmod xxx #xxx为生成的ko模块名称
dmesg -T 指令用于显示printk 内核日志打印
###################################################################
用户端调用相关的xenomai-api函数 (部分代码解析)
int devFd ;
#define DEIVICE_NAME "rtdm1" //定义相应的设备名称,对应上面device结构体的label
devFd = rt_dev_open(DEVICE_NAME, 0); //xeno2.x 位置<trank/rtdm/rtdm.h>
devFd = rtdm_open(DEVICE_NAME, 0); //xeno3.x 位置<cobalt/kernerl/rtdm/rtdm.h>
#设备读写函数
rt_dev_write,rt_dev_read; //原型位置<trank/rtdm/rtdm.h>
rtdm_write,rtdm_read; //原型位置<cobalt/kernerl/rtdm/rtdm.h>
############################################################
## 内核程序 Makefile
#may be need this define empty
CONFIG_STACK_VALIDATION=
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
obj-m += rtdmtest.o
all: modules
modules:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules_install
clean:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules clean
#############################################################
## ------- 用户端程序Makefile -------------
###### CONFIGURATION ######
### List of applications to be build
APPLICATIONS = mydrivertest
### Note: to override the search path for the xeno-config script, use "make XENO=..."
###### USER SPACE BUILD (no change required normally) ######
ifneq ($(APPLICATIONS),)
### Default Xenomai installation path
XENO ?= /usr/xenomai
XENOCONFIG=$(shell PATH=$(XENO):$(XENO)/bin:$(PATH) which xeno-config 2>/dev/null)
### Sanity check
ifeq ($(XENOCONFIG),)
all::
@echo ">>> Invoke make like this: /"make XENO=/path/to/xeno-config/" <<<"
@echo
endif
CC=$(shell $(XENOCONFIG) --cc)
CFLAGS=$(shell $(XENOCONFIG) --skin=native --cflags) $(MY_CFLAGS)
LDFLAGS=$(MY_LDFLAGS)
LDLIBS=$(shell $(XENOCONFIG) --skin=native --ldflags) /
$(shell $(XENOCONFIG) --skin=rtdm --ldflags --no-auto-init)
# This includes the library path of given Xenomai into the binary to make live
# easier for beginners if Xenomai's libs are not in any default search path.
LDFLAGS+=-Xlinker -rpath -Xlinker $(shell $(XENOCONFIG) --libdir)
all:: $(APPLICATIONS)
clean::
$(RM) $(APPLICATIONS) *.o
endif
########################################################
# 若make出现** No rule to make target 'tools/objtool/objtool' ,
# 在可添加make CONFIG_STACK_VALIDATION= 来编译.
#######################################################
变量解析:
rtdm_driver结构体中的 devie_flags
RTDM_NAMED_DEVICE 通过名称做设备寻找
RTDM_EXCLUSIVE 设备独占
RTDM_PROTOCOL_DEVICE 通过协议ID,SocketType 寻址
C知识引申:
//在#define中,标准只定义了#和##两种操作。
//#用来把参数转换成字符串,##则用来连接两个前后两个参数,把它们变成一个字符串。
#define Conn(x,y) x##y //连接x,y
#define ToChar(x) #@x //加单引号
#define ToString(x) #x //加双引号
参考博客:
https://blog.csdn.net/woshidahuaidan2011/article/details/53522792
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/274966.html