//s3c2410_leds.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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
#include
#include
#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;
}
阅读(1909) | 评论(1) | 转发(0) |