/*******************************
*
*杂项设备驱动: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驱动的更多相关文章

  1. Linux下实现流水灯等功能的LED驱动代码及测试实例

    驱动代码: #include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> ...

  2. linux设备驱动归纳总结(五):4.写个简单的LED驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-84693.html linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxx ...

  3. 【Linux开发】linux设备驱动归纳总结(五):4.写个简单的LED驱动

    linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  4. 很好的linux下GPIO驱动详解文章

    原文地址  http://blog.csdn.net/llxmedici/article/details/6282372 打算跟着友善之臂的<mini2440 linux移植开发指南>来做 ...

  5. linux下scsi共享磁盘的简单搭建

    linux下scsi共享磁盘的简单搭建 Scsi 共享磁盘需要我先有空余的分区,或者可以在虚拟机里面添加一块磁盘,安装所需的软件我在虚拟机里面添加了一块硬盘,分了一个主分区,sdb1 1G,将这个用s ...

  6. Linux下的GitHub安装与简单配置教程 ~ 转载

    Linux下的GitHub安装与简单配置教程   1.GitHub简介 Git是一个分布式版本控制系统,与其相对的是CVS.SVN等集中式的版本控制系统. 2.Git的安装 1)安装Git a.查看与 ...

  7. [stm32] 一个简单的stm32vet6驱动2.4寸240X320的8位并口tft屏DEMO

    书接上文: 最近在研究用低速.低RAM的单片机来驱动小LCD或TFT彩屏实现动画效果 首先我用一个16MHz晶振的m0内核的8位单片机nRF51822尝试驱动一个1.77寸的4线SPI屏(128X16 ...

  8. [stm32] 一个简单的stm32vet6驱动的天马4线SPI-1.77寸LCD彩屏DEMO

    书接上文<1.一个简单的nRF51822驱动的天马4线SPI-1.77寸LCD彩屏DEMO> 我们发现用16MHz晶振的nRF51822驱动1.77寸的spi速度达不到要求 本节主要采用7 ...

  9. 在Linux下,如何分析一个程序达到性能瓶颈的原因

    0.在Linux下,如何分析一个程序达到性能瓶颈的原因,请分别从CPU.内存.IO.网络的角度判断是谁导致的瓶颈?注意现在的机器CPU是多核 1.用sar -n DEV 1 10 2.用iotop命令 ...

随机推荐

  1. C_数据结构_链表的链式实现

    传统的链表不能实现数据和链表的分离,一旦数据改变则链表就不能用了,就要重新开发. 如上说示:外层是Teacher,里面小的是node. #ifndef _MYLINKLIST_H_ #define _ ...

  2. mysql-connector-python

    wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz tar mysql- ...

  3. 了解undefined、null、NaN的区别

    1.常规的解释,null是个对象,表示空值,undefined也是个对象,表示没有定义 2.详细分析 null 书上的解释(Javascript权威指南),Javascript的关键词null是一种特 ...

  4. [015]staic成员及staic成员函数

    C++primer里面讲过:static成员它不像普通的数据成员,static数据成员独立于该类的任意对象而存在,每个static数据成员是与类关联的对象,并不与该类的对象相关联!这句话可能比较拗口, ...

  5. [006]为什么C++会被叫做是C++?

    先了解一下自增和自减的运算符: 自增(++)和自减(--)操作符为对象提供加1或减1操作: int i = 0, j; j = ++i; // j = 1, i = 1: prefix yields ...

  6. cocos2d-x引擎实现$1Unistroke Recognizer手势识别

    $1 Unistroke(单笔画) Recognizer官网 http://depts.washington.edu/aimgroup/proj/dollar/ (在官网还有多笔画的识别库) 代码下载 ...

  7. Views

    Views Views are the visual side of the Nova, they are the HTML output of the pages. Views can be loc ...

  8. Cannot open URL…

    启动intellij时出现cannot open URl,原来是过滤器写错,把所有地址都拦截了..

  9. web前端开发前景怎么样?

    对于web前端开发,对现今前端的发展,中国的发展还很落后,中国没有Jquery,没有Node.js,其中最主要的一点是,中国的前端比较封锁,大家都没有分享的觉悟.回头看看,那些发展比较快的行业.软件, ...

  10. IPC with pipes, also dup2 redirect stream handle

    #include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unist ...