建设网站火车票预订,门户网站如何运营,找工作58同城最新招聘附近,开发公司空置房拨款合同一、字符设备驱动开发
1.1 字符设备驱动简介 字符设备是 Linux 驱动中最基本的一类设备驱动#xff0c;字符设备就是一个一个字节#xff0c;按照字节流进行读写操作的设备#xff0c;读写数据是分先后顺序的。比如我们最常见的点灯、按键、IIC、SPI#xff0c;LCD 等等都…一、字符设备驱动开发
1.1 字符设备驱动简介 字符设备是 Linux 驱动中最基本的一类设备驱动字符设备就是一个一个字节按照字节流进行读写操作的设备读写数据是分先后顺序的。比如我们最常见的点灯、按键、IIC、SPILCD 等等都是字符设备这些设备的驱动就叫做字符设备驱动。 1.2 驱动模块加载和卸载 1、字符设备驱动模块加载和卸载函数模板 /* 驱动入口函数 */
static int __init xxx_init(void)
{/* 入口函数具体内容 */return 0;
}/* 驱动出口函数 */
static void __exit xxx_exit(void)
{/* 出口函数具体内容 */
}/* 将上面两个函数指定为驱动的入口和出口函数 */
module_init(xxx_init);
module_exit(xxx_exit);驱动编译完成以后扩展名为.ko有两种命令可以加载驱动模块insmod和 modprobe推荐使用modprobe此命令用于加载指定的.ko 模块比如加载 drv.ko 这个驱动模块命令如下
modprobe drv.ko驱动模块的卸载使用命令“rmmod”即可比如要卸载 drv.ko使用如下命令即可
rmmod drv.ko1.3 驱动模块注册与注销
字符设备的注册和注销函数原型如下所示:
static inline int register_chrdev(unsigned int major, const char *name,const struct file_operations *fops)
static inline void unregister_chrdev(unsigned int major, const char *name)
/*
register_chrdev 函数用于注册字符设备此函数一共有三个参数这三个参数的含义如下
major主设备号Linux 下每个设备都有一个设备号设备号分为主设备号和次设备号两
部分关于设备号后面会详细讲解。
name设备名字指向一串字符串。
fops结构体 file_operations 类型指针指向设备的操作函数集合变量。
unregister_chrdev 函数用户注销字符设备此函数有两个参数这两个参数含义如下
major要注销的设备对应的主设备号。
name要注销的设备对应的设备名。
*/一般字符设备的注册在驱动模块的入口函数 xxx_init 中进行字符设备的注销在驱动模块的出口函数 xxx_exit 中进行。内容如下所示
static struct file_operations test_fops;/* 驱动入口函数 */
static int __init xxx_init(void)
{/* 入口函数具体内容 */int retvalue 0;/* 注册字符设备驱动 */retvalue register_chrdev(200, chrtest, test_fops);if(retvalue 0){/* 字符设备注册失败,自行处理 */}return 0;
}/* 驱动出口函数 */
static void __exit xxx_exit(void)
{/* 注销字符设备驱动 */unregister_chrdev(200, chrtest);
}/* 将上面两个函数指定为驱动的入口和出口函数 */
module_init(xxx_init);
module_exit(xxx_exit);1.4 实现设备具体操作函数 file_operations 结构体就是设备的具体操作函数
/* 打开设备 */
static int chrtest_open(struct inode *inode, struct file *filp)
{/* 用户实现具体功能 */return 0;
}/* 从设备读取 */
static ssize_t chrtest_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{/* 用户实现具体功能 */return 0;
}/* 向设备写数据 */
static ssize_t chrtest_write(struct file *filp,
const char __user *buf,size_t cnt, loff_t *offt)
{/* 用户实现具体功能 */return 0;
}/* 关闭/释放设备 */
static int chrtest_release(struct inode *inode, struct file *filp)
{/* 用户实现具体功能 */return 0;
}static struct file_operations test_fops {.owner THIS_MODULE, .open chrtest_open,.read chrtest_read,.write chrtest_write,.release chrtest_release,
};/* 驱动入口函数 */
static int __init xxx_init(void)
{/* 入口函数具体内容 */int retvalue 0;/* 注册字符设备驱动 */retvalue register_chrdev(200, chrtest, test_fops);if(retvalue 0){/* 字符设备注册失败,自行处理 */}return 0;
}/* 驱动出口函数 */
static void __exit xxx_exit(void)
{/* 注销字符设备驱动 */unregister_chrdev(200, chrtest);
}/* 将上面两个函数指定为驱动的入口和出口函数 */
module_init(xxx_init);
module_exit(xxx_exit);1.5 添加LICENSE信息
MODULE_LICENSE() //添加模块 LICENSE 信息
MODULE_AUTHOR() //添加模块作者信息加入 LICENSE 和作者信息完成以后的内容如下
/* 打开设备 */
static int chrtest_open(struct inode *inode, struct file *filp)
{
/* 用户实现具体功能 */
return 0;
}
....../* 将上面两个函数指定为驱动的入口和出口函数 */
module_init(xxx_init);
module_exit(xxx_exit);MODULE_LICENSE(GPL); //采用GPL协议
MODULE_AUTHOR(hsd); //添加作者名字1.6 设备号的分配
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
/*
函数 alloc_chrdev_region 用于申请设备号此函数有 4 个参数
dev保存申请到的设备号。
baseminor次设备号起始地址alloc_chrdev_region 可以申请一段连续的多个设备号这
些设备号的主设备号一样但是次设备号不同次设备号以 baseminor 为起始地址地址开始递
增。一般 baseminor 为 0也就是说次设备号从 0 开始。
count要申请的设备号数量。
name设备名字。
注销字符设备之后要释放掉设备号设备号释放函数如下*/
void unregister_chrdev_region(dev_t from, unsigned count)
/*
此函数有两个参数
from要释放的设备号。
count表示从 from 开始要释放的设备号数量。
*/1.7 物理内存与虚拟内存转换函数
1、ioremap 函数
#define ioremap(cookie,size) __arm_ioremap((cookie), (size), MT_DEVICE)/*
cookie要映射给的物理起始地址。
size要映射的内存空间大小。
*/2、iounmap 函数
void iounmap (volatile void __iomem *addr)
1.8 IO内存访问函数 1、读操作函数
u8 readb(const volatile void __iomem *addr)
u16 readw(const volatile void __iomem *addr)
u32 readl(const volatile void __iomem *addr)2、写操作函数
void writeb(u8 value, volatile void __iomem *addr)
void writew(u16 value, volatile void __iomem *addr)
void writel(u32 value, volatile void __iomem *addr)遇见问题 1、uboot下载系统失败以前都能成功突然不能下载怎么解决 首先保证正个网段内开发板的IP地址和ubuntu的IP地址是唯一的测试哪个IP地址有冲突比如ubuntu的192.168.1.66有被其他设备占用如果有占用就改一个没被占用的IP地址。 文章转载自: http://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.krywy.cn.gov.cn.krywy.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.xknmn.cn.gov.cn.xknmn.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.21r000.cn.gov.cn.21r000.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.bpmmq.cn.gov.cn.bpmmq.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.zmpsl.cn.gov.cn.zmpsl.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.drgmr.cn.gov.cn.drgmr.cn http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.rykx.cn.gov.cn.rykx.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.rfwkn.cn.gov.cn.rfwkn.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.gnfkl.cn.gov.cn.gnfkl.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.rkyw.cn.gov.cn.rkyw.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.fgxr.cn.gov.cn.fgxr.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn http://www.morning.sltfk.cn.gov.cn.sltfk.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.rnzwh.cn.gov.cn.rnzwh.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.wscfl.cn.gov.cn.wscfl.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.jkzjs.cn.gov.cn.jkzjs.cn http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.ychrn.cn.gov.cn.ychrn.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.mxbks.cn.gov.cn.mxbks.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn