Linux下GPIO驱动(一) ----一个简单的LED驱动
/*******************************
*
*杂项设备驱动:miscdevice
*majior=10;
*
* *****************************/ #include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/init.h> //#include <linux/moduleparam.h>
//#include <linux/slab.h>//kcalloc,kzalloc等内存分配函数 //---------ioctl------------
#include <linux/ioctl.h> //---------misc_register----
#include <linux/miscdevice.h> //----------cdev--------------
#include <linux/cdev.h> //----------delay-------------
#include <linux/delay.h> //----------GPIO---------------
#include <mach/gpio.h>
#include <mach/regs-gpio.h>
#include <plat/gpio-cfg.h> #define DEVICE_NAME "leds" static int led_gpios[] = {
S5PV210_MP04(),
S5PV210_MP04(),
S5PV210_MP04(),
S5PV210_MP04(),
};//4个LED #define LED_NUM ARRAY_SIZE(led_gpios) static long fl210_leds_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
switch(cmd) {
case :
case :
if (arg > LED_NUM) {
return -EINVAL;
} gpio_set_value(led_gpios[arg], !cmd);//根据cmd设置LED的暗灭
printk(DEVICE_NAME": %ld %d\n", arg, cmd);
break; default:
return -EINVAL;
} return ;
} static struct file_operations fl210_led_dev_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = fl210_leds_ioctl,
}; //----------------miscdevice------------------
static struct miscdevice fl210_led_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &fl210_led_dev_fops,
};
//-------------------------------------------- static int __init fl210_led_dev_init(void) {
int ret;
int i; for (i = ; i < LED_NUM; i++) {
ret = gpio_request(led_gpios[i], "LED");//申请GPIO口
if (ret) {
printk("%s: request GPIO %d for LED failed, ret = %d\n", DEVICE_NAME,
led_gpios[i], ret);
return ret;
} s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT);//设置GPIO口为输出
gpio_set_value(led_gpios[i], );//初始化GPIO口的值
} ret = misc_register(&fl210_led_dev);//注册杂项设备 printk(DEVICE_NAME"\tinitialized\n");
printk("led num is: %d\n",LED_NUM);
return ret;
} static void __exit fl210_led_dev_exit(void) {
int i; for (i = ; i < LED_NUM; i++) {
gpio_free(led_gpios[i]);//释放GPIO口
} misc_deregister(&fl210_led_dev);//注销设备
} module_init(fl210_led_dev_init);
module_exit(fl210_led_dev_exit); MODULE_LICENSE("GPL");
MODULE_AUTHOR("");
S5PV210_MP04宏定义在linux/arch/arm/mach-s5pv210/include/mach/gpio.h
#define S5PV210_MP04(_nr) (S5PV210_GPIO_MP04_START + (_nr))
S5PV210_GPIO_MP04_START = S5PV210_GPIO_NEXT(S5PV210_GPIO_MP03),
#define S5PV210_GPIO_NEXT(__gpio) \ ((__gpio##_START) + (__gpio##_NR) + CONFIG_S3C_GPIO_SPACE + 1)
//这里的CONFIG_S3C_GPIO_SPAC是内核配置选项,在.config中可以找到,我的配置为: CONFIG_S3C_GPIO_SPACE = 0
上述代码用到以下几个函数:
gpio_set_value();
s3c_gpio_cfgpin();
gpio_request();
gpio_free();
misc_register();
misc_deregister();
后面会逐个分析。
测试程序如下:
#include <stdio.h>
//#include "sys/types.h"
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>//read,write等等
//#include "termios.h"
//#include "sys/stat.h"
#include <fcntl.h> #define LED2_ON 0x1
#define LED2_OFF 0x0 main(int argc,char *argv[])
{
int fd; if ((fd=open("/dev/leds",O_RDWR /*| O_NDELAY | O_NOCTTY*/)) < )
{
printf("Open Device failed.\r\n");
exit();
}
else
{
printf("Open Device successed.\r\n");
}
if (argc<)
{
/* code */
printf("Usage: %s <on|off num>\n",argv[]);
exit();
}
if(!strcmp(argv[],"on"))
{
printf("led1 will on!!\n");
if(ioctl(fd,LED2_ON,atoi(argv[]))<)
{
printf("ioctl err!!\n");
} }
if(!strcmp(argv[],"off"))
{
printf("led1 will off!!\n");
if(ioctl(fd,LED2_OFF,atoi(argv[]))<)
{
printf("ioctl err!!\n");
}
}
close(fd);
}
Linux下GPIO驱动(一) ----一个简单的LED驱动的更多相关文章
- Linux下实现流水灯等功能的LED驱动代码及测试实例
		驱动代码: #include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> ... 
- linux设备驱动归纳总结(五):4.写个简单的LED驱动【转】
		本文转载自:http://blog.chinaunix.net/uid-25014876-id-84693.html linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxx ... 
- 【Linux开发】linux设备驱动归纳总结(五):4.写个简单的LED驱动
		linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ... 
- 很好的linux下GPIO驱动详解文章
		原文地址 http://blog.csdn.net/llxmedici/article/details/6282372 打算跟着友善之臂的<mini2440 linux移植开发指南>来做 ... 
- linux下scsi共享磁盘的简单搭建
		linux下scsi共享磁盘的简单搭建 Scsi 共享磁盘需要我先有空余的分区,或者可以在虚拟机里面添加一块磁盘,安装所需的软件我在虚拟机里面添加了一块硬盘,分了一个主分区,sdb1 1G,将这个用s ... 
- Linux下的GitHub安装与简单配置教程 ~ 转载
		Linux下的GitHub安装与简单配置教程 1.GitHub简介 Git是一个分布式版本控制系统,与其相对的是CVS.SVN等集中式的版本控制系统. 2.Git的安装 1)安装Git a.查看与 ... 
- [stm32] 一个简单的stm32vet6驱动2.4寸240X320的8位并口tft屏DEMO
		书接上文: 最近在研究用低速.低RAM的单片机来驱动小LCD或TFT彩屏实现动画效果 首先我用一个16MHz晶振的m0内核的8位单片机nRF51822尝试驱动一个1.77寸的4线SPI屏(128X16 ... 
- [stm32] 一个简单的stm32vet6驱动的天马4线SPI-1.77寸LCD彩屏DEMO
		书接上文<1.一个简单的nRF51822驱动的天马4线SPI-1.77寸LCD彩屏DEMO> 我们发现用16MHz晶振的nRF51822驱动1.77寸的spi速度达不到要求 本节主要采用7 ... 
- 在Linux下,如何分析一个程序达到性能瓶颈的原因
		0.在Linux下,如何分析一个程序达到性能瓶颈的原因,请分别从CPU.内存.IO.网络的角度判断是谁导致的瓶颈?注意现在的机器CPU是多核 1.用sar -n DEV 1 10 2.用iotop命令 ... 
随机推荐
- Sublime Text 3 史上最性感的编辑器
			下载 / 安装 windows / MAC OS 官网下载,双击安装,这个都会吧- linux linux下安装,一种办法是从官网下载 tar.bz ,手动安装. 这里介绍用 apt-get 自己主动 ... 
- 【jQuery插件】用jQuery Masonry快速构建一个pinterest网站布局(转)
			[jQuery插件]用jQuery Masonry快速构建一个pinterest网站布局 时间:2011年03月21日作者:愚人码头查看次数:29,744 views评论次数:25条评论 前段时间领导 ... 
- RHEL6中ulimit的nproc限制
			http://blog.csdn.net/kumu_linux/article/details/8301760 当前shell下更改用户可打开进程数 修改limits.conf配置文件生效 [root ... 
- Eclipse 各种包说明
			2001年11月7日 ,Eclipse 1.0发布 半年之后,2002年6月27日Eclipse进入了2.0时代.2.0时代的Eclipse经历了2.0和2.1两个大的版本.其中2.0在 之后又推出了 ... 
- 程序员谈学习:我为什么要学习Linux?
			http://kb.cnblogs.com/page/196876/ 好长时间没好好写点东西了,前段时间由于项目的需要出差了一个多月,期间各种加班,每天晚上加班到十点,回到宾馆实现是没什么精力再写博客 ... 
- case,cast
			UPDATE dbo.Dat_Camera SET Cam_Config='<xml><cam><type>2</type>'+CASE WHEN Ca ... 
- [置顶]        Objective-C开发环境介绍以及Cocoa,以及第一个程序
			Objective-C 起源与发展 Brad J. Cox designed the Objective-C language in the early 1980 . 布兰德于1980年设计的 ... 
- [Laravel] 获取执行的Sql
			获取数据库操作记录 $queries = DB::getQueryLog(); //取最后一条是 $lastSql = end($queries); 不过这样输出的,不是真正的sql,输出的是类似PD ... 
- jQuery+php实现ajax文件即时上传
			很多项目中需要用到即时上传功能,比如,选择本地图片后,立即上传并显示图像.本文结合实例讲解如何使用jQuery和PHP实现Ajax即时上传文件的功能,用户只需选择本地图片确定后即实现上传,并显示上传进 ... 
- JS完美运动框架
			这套框架实现了多物体,任意值,链式运动,多值运动,基本满足常见的需求. /* 功能:完美运动框架,可以实现多物体,任意值,链式运动,多值运动 版本:V1.0 兼容性:Chrome,FF,IE8+ (o ... 
