Linux 驱动——Led驱动1
led_drv.c驱动文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/io.h> //包含 iomap 和 iounmap 函数
#include <asm/uaccess.h> //包含 copy_from_user 函数
#include <linux/device.h> //包含 class 类相关的处理函数
#include <linux/fs.h> //包含 file_operations 结构体
#define DRIVER_NAME "led_drv"
#define DEVICE_NAME "led_dev"
int major;
static struct class *leddrv_class;
static struct class_device *leddrv_class_dev;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int led_drv_open(struct inode *inode, struct file *file)
{
*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
return 0;
}
static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val, ret;
ret = copy_from_user(&val, buf, count);
if(ret!=0)
{
printk("led_drv_write error 1 \n");
}
if (val == 1)
{
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); //点灯
}
else
{
*gpfdat |= (1<<4) | (1<<5) | (1<<6); //灭灯
}
return 0;
}
static struct file_operations led_drv_fops = {
.owner = THIS_MODULE,
.open = led_drv_open,
.write = led_drv_write,
};
static int led_drv_init(void)
{
major = register_chrdev(0, DRIVER_NAME, &led_drv_fops);
leddrv_class = class_create(THIS_MODULE, DRIVER_NAME); //创建设备类
leddrv_class_dev = class_device_create(leddrv_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME); //在设备类下创建设备文件其实也就是设备节点,
//创建好的设备文件(设备节点)可以在中/dev
//下查看, 如果创建成功,则在/dev下应该有
//led_dev这个设备文件名,或者说是有
//led_dev这个设备节点
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
static void led_drv_exit(void)
{
unregister_chrdev(major, DRIVER_NAME);
class_device_unregister(leddrv_class_dev);
class_destroy(leddrv_class);
iounmap(gpfcon);
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
Makefile文件:
obj-m += led_drv.o
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c
led_app文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open("/dev/led_dev", O_RDWR);
if(fd<0)
{
printf("can not open \n");
}
if(argc != 2)
{
printf("%s <on|off> \n");
return 0;
}
if(strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 4);
return 0;
}
编译led_drv.c与led_app.c文件, 加载编译后的模块, 运行./led_app on 或 ./led_app off可以看到灯的亮灭。
Linux 驱动——Led驱动1的更多相关文章
- (笔记)linux设备驱动--LED驱动
linux设备驱动--LED驱动 最近正在学习设备驱动开发,因此打算写一个系列博客,即是对自己学习的一个总结,也是对自己的一个督促,有不对,不足,需要改正的地方还望大家指出,而且希望结识志同道合的朋友 ...
- Linux 驱动——Led驱动2
led_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/init ...
- 自己动手写最简单的Android驱动---LED驱动的编写【转】
本文转载自:http://blog.csdn.net/k_linux_man/article/details/7023824 转载注明出处,作者:K_Linux_Man, 薛凯 山东中医药大学,给文章 ...
- linux设备驱动归纳总结(五):4.写个简单的LED驱动【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-84693.html linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxx ...
- 全志A33 linux led驱动编程(附实测参考代码)
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 开发平台 * 芯灵思Sinl ...
- Linux驱动之LED驱动编写
从上到下,一个软件系统可以分为:应用程序.操作系统(内核).驱动程序.结构图如下:我们需要做的就是写出open.read.write等驱动层的函数.一个LED驱动的步骤如下: 1.查看原理图,确定需要 ...
- 【Linux开发】linux设备驱动归纳总结(五):4.写个简单的LED驱动
linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- 嵌入式Linux学习笔记(三) 字符型设备驱动--LED的驱动开发
在成功构建了一个能够运行在开发板平台的系统后,下一步就要正式开始应用的开发(这里前提是有一定的C语言基础,对ARM体系的软/硬件,这部分有疑问可能要参考其它教程),根据需求仔细分解任务,可以发现包含的 ...
- linux驱动之LED驱动
通过之前的学习,了解到linux驱动编写的流程是:先通过注册函数注册我们编写的入口函数,然后在入口函数中获取设备号->注册字符设备->自动创建设备节点->获取设备树信息,最后通过销毁 ...
随机推荐
- FutureBuilder的使用以及防止FutureBuilder不必要重绘的两种方法
https://blog.csdn.net/u011272795/article/details/83010974 https://segmentfault.com/a/119000001421934 ...
- WindowsAPI每日一练(2) 使用应用程序句柄
WindowsAPI每日一练系列 :https://www.cnblogs.com/LexMoon/category/1246238.html WindowsAPI每日一练() WinMain Win ...
- 如何退出vim
按ESC键 按ESC键 按ESC键 然后: 最下面出现一条能输入命令的地方 输入冒号 输入冒号 输入冒号 然后输入命令: :w 保存文件但不退出 :w file 将修改另外保存到 file 中,不退出 ...
- 剑指offer(45)扑克牌顺子
题目描述 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决 ...
- react开发初始配置和一些问题
1.npm run build之后,打开网页显示为空白的解决方案 初始使用的开发者应该都会使用create-react-app,初次尝试,启动没有问题,然后就测试一下build,结果发现本地文件ind ...
- 让JSON.js完全适应.NET
动机:.net 默认的时间序列化方式为\/Date(数字 时区)\/,序列化成JSON在前端很难处理,有没有一种好的处理方式呢? 改进: JSON = new function(){ this.dec ...
- eclipse 对 hadoop1.2.1 hdfs 文件操作
package com.hdfs; import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io ...
- 函数, arguments对象, eval,静态成员和实例成员
函数创建: 3种创建函数的方式 * 直接声明函数 function funcName(/*参数列表*/){ //函数体 } * 函数表达式 var funcName = function(){ ...
- Mac+Apache+PHP 安装 Xdebug 方法
MAC homebrew自2018/3/31之后弃用homebrew/php By 31st March we will deprecate and archive the Homebrew/php ...
- js验证银行卡号 luhn校验规则
<script type="text/javascript"> //luhn校验规则:16位银行卡号(19位通用): // 1.将未带校验位的 15(或18)位卡号从右 ...