应用程序调用ioctl():
[code]
#include
#include
#include
#include

int main(int argc, char **argv)
{
int on;
int led_no;
int fd;
if (argc != 3 || sscanf(argv[1], "%d", led_no) != 1 || sscanf(argv[2],"%d", on) != 1 ||
on < 0 || on > 1 || led_no < 0 || led_no > 3) {
fprintf(stderr, "Usage: leds led_no 0|1\n");
exit(1);
}
fd = open("/dev/led", 0);
if (fd < 0) {
perror("can not open device led");
exit(1);
}
ioctl(fd, on, led_no);
close(fd);
return 0;
}
[/code]

GPIO驱动部分:
[code]
#include
#include
#include
#include

#include
#include
#include
#include
#include
#include

#include
#include
#include
#include
#include
#include
#include
#include

#define DEVICE_NAME"led"
#define LED_MAJOR 233

MODULE_AUTHOR("Luofuchong");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_ALIAS("led");

static unsigned long led_table [] = {
S3C2410_GPB7,
S3C2410_GPB8,
S3C2410_GPB9,
S3C2410_GPB10,
S3C2410_GPB7_OUTP,
S3C2410_GPB8_OUTP,
S3C2410_GPB9_OUTP,
S3C2410_GPB10_OUTP,
};

static int leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > 3) {
return -EINVAL;
}
s3c2410_gpio_setpin(led_table[arg],!cmd);
break;
default:
return -EINVAL;
}
return 0;
}

static struct file_operations leds_fops = {
.owner = THIS_MODULE,
.ioctl = leds_ioctl,
};

static struct class *led_class;

static int __init leds_init(void)
{
int err = 0;
int i;

if(register_chrdev(LED_MAJOR,"led",leds_fops)){
printk("led driver:Unable to register driver\n");
return -ENODEV;
}

led_class = class_create(THIS_MODULE, "led");
if(IS_ERR(led_class)){
err = PTR_ERR(led_class);
goto out_chrdev;
}
class_device_create(led_class,MKDEV(LED_MAJOR, 0),NULL,"led");

err = devfs_mk_cdev(MKDEV(LED_MAJOR,0),
S_IFCHR | S_IRUGO | S_IWUSR,"led");
if(err)
goto out_class;

for(i=0;i<4;i ){
s3c2410_gpio_cfgpin(led_table[i],led_table[i 4]);
s3c2410_gpio_setpin(led_table[i],1);
}
printk("led driver initialized\n");
goto out;

out_class:
class_device_destroy(led_class,MKDEV(LED_MAJOR, 0));
class_destroy(led_class);
out_chrdev:
unregister_chrdev(LED_MAJOR, "led");
out:
return err;
}

static void __exit leds_exit(void)
{
class_device_destroy(led_class,MKDEV(LED_MAJOR,0));
class_destroy(led_class);
unregister_chrdev(LED_MAJOR,"led");
devfs_remove("led");
printk("led driver removed\n");
}

module_init(leds_init);
module_exit(leds_exit);
[/code]

转自:
http://www.hhcn.com/cgi-bin/topic.cgi?forum=3topic=367

《Linux Device Driver》example link:
ftp://ftp.ora.com/pub/examples/
ftp://ftp.ora.com/pub/examples/

转载于:https://blog.51cto.com/hanxuefei432/992119

GPIO驱动实例:操作LED开关相关推荐

  1. android gpio驱动实例,Android/Linux 驱动层对GPIO口的操作方法和相关代码

    实例: unsigned int otg_en_pin=952; ----->高通的gpio的基础值为902,这里我们时间的gpio为50,即这里定义值是要设置为952. //young.yan ...

  2. SylixOS下 i.MX RT1050的GPIO驱动实例

    实例源码 下面是 i.MX RT1050 的GPIO驱动实现源码: /***************************************************************** ...

  3. android gpio驱动实例,安卓gpio操作示例

    GPIO值在RK3288中的计算方法为:bank×32+pin,如: GPIO7A3: 7×32 + 0*8 +3=227 GPIO0B5: 0×32+ 1*8 +5=13 1. 导出 /sys/cl ...

  4. RK3288开发板PopMetal上的GPIO驱动实例

    2019独角兽企业重金招聘Python工程师标准>>> 楼主在这边给大家介绍下如何使用PopMetal的GPIO.先讲过程,再讲原理吧, 该驱动需要涉及到的知识点:1,DTS设备树的 ...

  5. Proteus仿真STM32F103R6微控制器的GPIO(按键控制LED开关)

    Proteus仿真STM32F103R6微控制器的GPIO,检查按键,控制LED灯的反转.. 输入:按键检测:输出:高低电平,控制LED. 一.原理图: 二.源码: #include "st ...

  6. linux驱动开发(一)—GPIO驱动框架

    前言 GPIO驱动是Linux驱动开发中最基础.但却是很常用.很重要的驱动.比如你要点亮一个LED灯.键盘扫描.输出高低电平等等.而Linux内核的强大之处在于对最底层的GPIO硬件操作层的基础上封装 ...

  7. linux gpio设备驱动程序,嵌入式Linux设备驱动开发之:GPIO驱动程序实例-嵌入式系统-与非网...

    11.3  GPIO驱动程序实例 11.3.1  GPIO工作原理 FS2410开发板的S3C2410处理器具有117个多功能通用I/O(GPIO)端口管脚,包括GPIO 8个端口组,分别为GPA(2 ...

  8. 全志A33驱动开发 之 LED操作

    全志A33驱动开发 之 LED操作 一.整体说明 二.注意事项 三.实现led驱动 1.源文件 2.Makefile文件 四.实现测试的应用程序 五.原理图 一.整体说明   本次使用的开发板是锐尔威 ...

  9. gpio驱动广播Android,[RK3288][Android6.0] 调试笔记 --- 通用GPIO驱动控制LED【转】

    Platform: ROCKCHIP OS: Android 6.0 Kernel: 3.10.92 由于板子没有lcd无法得知sd卡升级是否完成,因此使用LED显示. Recovery中升级完成后控 ...

  10. 字符设备驱动基础篇5——驱动如何操控硬件(动静态映射操作LED)

    以下内容源于朱有鹏嵌入式课程的学习,,如有侵权,请告知删除. 参考资料:http://www.cnblogs.com/biaohc/p/6575074.html 这里的映射,是指物理地址和虚拟地址的对 ...

最新文章

  1. 如何在机器学习的框架里实现隐私保护?
  2. NR 5G 关于gNB-CU和gNB-DU
  3. POJ1573-Robot Motion
  4. POJ3608(旋转卡壳--求两凸包的最近点对距离)
  5. angular6继承类注意几点:
  6. 批量改名_手把手教你用Python批量给图片添加水印 | 知了干货分享
  7. 07.30《jQuery》——1.1DOM对和jQuery对象的转化
  8. 基于 HTML5 WebGL 的 3D 工控裙房系统
  9. 前端设计必备-Font awesome 插件使用菜鸟言语
  10. 采用dlopen、dlsym、dlclose加载动态链接库
  11. 用python模拟clark变换和park变换
  12. 第1138期AI100_机器学习日报(2017-10-30)
  13. K8S的pod探针(livenessProbe,readinessProbe),kubelet对pod的状态检查(kubelet-exec,httpGet,tcpSocket)
  14. 原生JS实现中文简繁切换
  15. 请仔细核对自己的信息
  16. [转]经验之谈:成为Debian Maintainer前要做的事
  17. C# 使用Redis实现粉丝好友互粉数据存储和查询
  18. 如何利用Slack客户端漏洞窃取Slack用户下载的所有文件
  19. gaussdb200 理论
  20. flash的读写擦除

热门文章

  1. Ubuntu使用技巧集锦(持续追加中……)
  2. Clash of Clans通关秘诀
  3. read -p 命令--shell 脚本
  4. win10下JDK安装,配置环境变量后出现error:could not open '...jvm.cfg'
  5. (转载)web.xml中 IntrospectorCleanupListener的作用
  6. Java web切面编程
  7. poj-3176 Cow Bowling poj-1163 The Triangle hihocoder #1037 : 数字三角形 (基础dp)
  8. 韩国被申遗 (转自果壳)
  9. 关于字节对齐(关于align)
  10. CA SDK 使用简介