Ubuntu11.04下测试并口驱动程序过程--看经典驱动书LDD实践。
来源:互联网 发布:portraiture mac 编辑:程序博客网 时间:2024/05/21 12:44
一、前言
一直想多学习一下Linux,总得找一个方向吧。书太多,也看过许许多多的Linux书,代码太多,看过比较多的代码,可是为啥一直在入门ing,从未进入过。。。。。。好吧,最近找完工作,时间也蛮多,于是想系统的学习一下Linux下的驱动程序设计。一是为了学习,二是为了实验室实际项目需要。所以我买了传说中的经典书籍LDD<<Linux设备驱动程序>>,之前早已听闻这本书。
花了大概大半个月时间把这本书的前十二章看完了,于是想自己动手写写驱动程序,来实际验证究竟是不是像书上讲的那样写,就能在Linux下实现驱动程序控制设备的功能。于是找了书中的例子,拿并口驱动程序做实验。此文主要想记录我实验的详细过程和实验过程中遇到的问题和解决办法。
以下是我的实验环境:
1、一台带并口的PC机:研华工业计算机,酷睿CPU
2、ubuntu11.04,安装盘原装内核2.6.38.8。
二、实验过程
1、确保并口是功能完好的。
我刚开始没想到先验证并口是否可用,于是就先按照书上讲的方法先写了一个简单的驱动程序测试并口。结果当然是并口并不能受我的驱动程序控制,于是就很难定位问题出在哪里,很可能是驱动程序问题吧?因为我第一次写驱动,于是一遍一遍的查找是不是程序有问题。。。。。最后发现我找的第一台电脑的并口就是坏的,然后才找到研华工控机的并口是好的。
检查方法我试过二种,第一种是在windows系统下,上网上找并口测试程序,然后用万用表去量并口电压是否受能按设定改变。第二种是在Ubuntu下用原装系统自带的并口驱动程序提供的接口写用户空间测试程序,照样是用万用表去量并口管脚,主要参照http://mockmoon-cybernetics.ch/computer/linux/programming/parport.html。以下是我亲自在我平台上测试通过的:
//Access via raw IO (not recommended)在用户空间获得权限直接访问并口I/O端口,只在X86平台支持。#include <stdio.h>#include <sys/io.h>#define BASEADDR 0x378int main(int argc, char **argv){int i;if(ioperm(BASEADDR, 4, 1) < 0) {fprintf(stderr, "could not get i/o permission. are you root ?\n");return 5;}for(i=0; i < 256; i++) {outb(i, BASEADDR);}ioperm(BASEADDR, 4, 0);return 0;}
//这是利用UBUNTU自带的并口驱动ppdev和并口设备/dev/parport0(ubuntu已建好)进行并口测试。//lsmod可以看见lp,ppdev parport_pc parport等并口驱动,cat /dev/ioports能看见并口端口被占的情况#include<stdio.h>#include<fcntl.h>#include<sys/types.h>#include<sys/stat.h>#include<asm/ioctl.h>#include<linux/parport.h>#include<linux/ppdev.h>#define DEVICE "/dev/parport0"int main(){struct ppdev_frob_struct frob;int fd;int mode;char data = 0xFF;fd = open(DEVICE, O_RDWR);if(fd < 0){printf("open error:\n");return -1;}if(ioctl(fd, PPCLAIM)) //这里也可以是其它一些测试代码,按照上面网站教程所说。{perror("PPCLAIM");close(fd);return -1;}ioctl(fd, PPWDATA, &data );ioctl(fd, PPRELEASE);close(fd);return 0;}
2、按照LDD书或者网上各种资料写自己的并口驱动程序,简单的主要完成控制并口电压,驱动程序通过ioctl或者write方法都行。
#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/types.h>#include <linux/fcntl.h>#include <asm/uaccess.h>#include <asm/io.h>#define DEV_NAME "phoenix_port"#define DEV_MAJOR 223#define PORT_WRITE_ADDR 0X378#define PORT_READ_ADDR 0X379 int phoenix_open(struct inode *inode,struct file *filp){return 0;}int phoenix_release(struct inode *inode,struct file *filp){return 0;}ssize_t phoenix_read(struct file *filp, char *buf, size_t count, loff_t *f_pos){//this fun read one char,not the count you passunsigned char status;//inb is a function that can read a byte form addrstatus=inb(PORT_READ_ADDR);//put the byte to usr bufput_user(status, (char *)buf);return 1;}ssize_t phoenix_write(struct file *filp, char *buf, size_t count, loff_t *fpos){//write one char ,no matter how many you want to write;//get param from usr to kernelunsigned char status;get_user(status, (char*)buf);outb(status, PORT_WRITE_ADDR);printk(KERN_ALERT "write io value:%x",status);return 1;} struct file_operations oper_struct={.owner=THIS_MODULE,.open=phoenix_open,.release=phoenix_release,.read=phoenix_read,.write=phoenix_write,};int phoenix_init(void){int tmp;tmp=register_chrdev(DEV_MAJOR, DEV_NAME, &oper_struct);if(tmp<0){ printk(KERN_ALERT "module_init error !\n"); return tmp;}if(request_region(0x378, 8, DEV_NAME)){printk(KERN_ALERT "get io port region");}else{printk(KERN_ALERT "can not get io port region");}return 0;}void phoenix_exit(void){release_region(0x378, 8);unregister_chrdev(DEV_MAJOR,DEV_NAME);}module_init(phoenix_init);module_exit(phoenix_exit);MODULE_LICENSE("Dual BSD/GPL");
#include <stdio.h>#include <sys/types.h>#include <sys/ioctl.h>#include <fcntl.h>#include <sys/stat.h>#include <unistd.h>#define DEV_NAME "/dev/phoenix_port" //必须和mknod建立的文件名一致int main(){//open phoenix_portint dev;dev=open(DEV_NAME,O_RDWR|O_NDELAY);if(dev<0){ printf("open dev error!\n"); return -1;}//open rightprintf("have open dev!\n");int count = 1; char state =0x00;while(1){if(!(count % 2))//every two second{state = state ^ 0x01;printf("write state:%x",state); write(dev, &state, 1);}if(!(count % 1))//every one second{state = state ^ 0x04;printf("write state:%x",state);write(dev, &state, 1);}count++;sleep(1);}//close devclose(dev);return 0;}
6、下一步计划
用FPGA开发外围设备卡,通过PCI总线或者ISA总线挂在研华的工控机上,自己做外围电路和写驱动程序。
- Ubuntu11.04下测试并口驱动程序过程--看经典驱动书LDD实践。
- ldd(linux设备驱动程序)scull驱动
- Ubuntu11.04下ns2.34安装过程
- 在VMware中测试LDD并口程序时无法加载short模块的解决办法
- Ubuntu11.04下BCM4311无线网卡驱动安装
- ubuntu11.04下Qt之helloworld 详细的入门过程
- 实践测试驱动开发
- 实践测试驱动开发
- 测试驱动实践
- 实践测试驱动开发
- 测试驱动开发实践
- Linux并口设备驱动程序设置
- Linux并口设备驱动程序设置
- 并口驱动编程参考资料
- 并口驱动。。。摸索中。。。
- 并口驱动安装
- linux并口驱动概述
- RUP测试过程实践
- AIX,Redhat下实现nfs文件系统开机自动mount
- C++ const用法及注意事项
- Flash Media Live Encoder 使用帮助
- add-apt-repository添加源
- abstract factory模式
- Ubuntu11.04下测试并口驱动程序过程--看经典驱动书LDD实践。
- 关于“云计算”
- JAVA简介及视频教程
- C语言总结之函数的返回值
- structs2的学习笔记----(helloworld开发环境的搭建)
- ZOJ Alice's Print Service
- 用gdb配合内核转储文件瞬间定位段错误
- 【ffmpeg】试图从raw buffer中读取数据的ff_read_packet 浅显分析
- iOS 响应链