文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>s3c2410 led驱动

s3c2410 led驱动

时间:2009-08-24  来源:huwpeng

//s3c2410_leds.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#define DEVICE_NAME        "leds"    //定义设备名
#define LED_MAJOR         231     //手动定义主设备号
#define LED_MINOR        0
//定义要操作的设备,把每一个led作为结构体的一个成员
static unsigned long led_table [] = {         
    S3C2410_GPF3,
    S3C2410_GPF4,
    S3C2410_GPF5,
    S3C2410_GPF6,
    S3C2410_GPF7,
     
};
//对设备进行设置,结构体的每一个成员是对对应led的设置
static unsigned int led_cfg_table [] = {
    S3C2410_GPF3_OUTP,
    S3C2410_GPF4_OUTP,
    S3C2410_GPF5_OUTP,
    S3C2410_GPF6_OUTP,
    S3C2410_GPF7_OUTP,
};
//设备驱动程序中对设备的I/O通道进行管理的函数,用来实现对led的操作
static int s3c2410_leds_ioctl(struct inode *inode,
       struct file *file, unsigned int cmd, unsigned long arg)
{
    switch(cmd) {
        case 0:
        case 1:
            if (arg > 4)
                return -EINVAL;
            s3c2410_gpio_setpin(led_table[arg], !cmd);
            return 0;
        default:
            return -EINVAL;
    }
}
static struct file_operations s3c2410_leds_fops = {
    .owner    =    THIS_MODULE,
    .ioctl    =    s3c2410_leds_ioctl,
};
/**
 * struct cdev of 'led_dev'
 */
struct cdev *my_cdev;
struct class *my_class;
//模块加载函数
static int __init s3c2410_leds_init(void)
{
    int err, i, devno = MKDEV(LED_MAJOR, LED_MINOR);
    /* register the 'dummy_dev' char device */
    my_cdev = cdev_alloc();
    cdev_init(my_cdev, &s3c2410_leds_fops);
    my_cdev->owner = THIS_MODULE;
    err = cdev_add(my_cdev, devno, 1);
    if (err != 0)
        printk("led device register failed!\n");
    /* creating your own class */
    my_class = class_create(THIS_MODULE, "led_class");
    if(IS_ERR(my_class)) {
        printk("Err: failed in creating class.\n");
        return -1;
    }
    /* register your own device in sysfs, and this will cause udevd to create corresponding device node */
    class_device_create(my_class, NULL, devno, NULL, DEVICE_NAME "%d", LED_MINOR );
   
    for (i = 0; i < 5; i++) {
        s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
        s3c2410_gpio_setpin(led_table[i], 1);
    }
   
    return 0;
}
//模块卸载函数
static void __exit s3c2410_leds_exit(void)
{
    cdev_del(my_cdev);
    class_device_destroy(my_class, MKDEV(LED_MAJOR, LED_MINOR));
    class_destroy(my_class);
}
module_init(s3c2410_leds_init);
module_exit(s3c2410_leds_exit);

测试程序如下:
//ledshow.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sys/types.h"
#include "sys/ioctl.h"
#include "stdlib.h"
#include "termios.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "sys/time.h"
int main()
{
    int on=1;
    int led;
    int fd;
   
    fd = open("/dev/leds0", 0);
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
      
    printf("leds test show. press ctrl+c to exit \n");
    while(1) {
        for(led=0;led<5;led++) {
            ioctl(fd, on, led);
            usleep(50000);
        }
        on=!on;
    }   
    close(fd);
   
    return 0;
}
文章出处:http://www.diybl.com/course/6_system/linux/Linuxjs/200896/139481.html
相关阅读 更多 +
排行榜 更多 +
试着飞手游下载

试着飞手游下载

休闲益智 下载
血染小镇(功能菜单)中文版下载

血染小镇(功能菜单)中文版下载

飞行射击 下载
泰坦之旅高爆版下载

泰坦之旅高爆版下载

角色扮演 下载