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命令 ...
随机推荐
- 设计模式-工厂方法(Demo)
工厂方法 工厂方法跟简单工厂一样.都是创建型的设计模式.他攻克了简单工厂的违背开放封闭的缺点. 故事 主人--人家做饭好累的.女仆抱着我大腿说着.自从上次把她买进家.没做了几次饭就喊累--看着她那出处 ...
- [Effective C++ --007]为多态基类声明virtual析构函数
引言: 我们都知道类的一个很明显的特性是多态,比如我们声明一个水果的基类: class Fruit { public: Fruit() {}; ~Fruit(){}; } 那么我们根据这个Fruit基 ...
- Advanced Configuration Tricks
Advanced Configuration Tricks Configuration of zend-mvc applications happens in several steps: Initi ...
- [COCOS2DX]COCOS命令新建项目+编译安卓项目并成功运行
全程搭建过程参考网址: http://blog.csdn.net/lengxue789/article/details/38116475 http://blog.csdn.net/cbbbc/arti ...
- Matlab使用难点记忆
MATLAB的数据显示格式 虽然在MATLAB系统中数据的存储和计算都是双精度进行的,但MATLAB可以利用菜单或format命令来调整数据的显示格式.Format命令的格式和作用如下: l for ...
- 【Objective-C】3 -self关键字
一.Java中的this只能用在动态方法中,不能用在静态方法中 1.在动态方法中使用this关键字 1 public class Student { 2 private int age; 3 publ ...
- swift基本运算符
一.空合运算符(Nil Coalescing Operator) 形式:a??b,如果a包含值则解封,否则返回默认值b 条件:a必须为optional类型,这个就不多说了,就是可选类型:默认值b的类型 ...
- js内置对象处理-打印学生成绩单
效果图: 任务: 1.通过js的内置对象得到当前日期 var date=new Date(); var year=date.toString().slice(11,15); document.writ ...
- Java多线程并发
http://wangjianwei866.blog.163.com/blog/static/9295823201231665319314/ 基于以上网文,调整了一下格式,修改了一些标点和拼写错误. ...
- Java编写的文本编辑器(菜鸟作品)
//这是主窗体文件 Wordwin.java import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.sw ...