您的位置:首页 > 其它

一个简单的LED驱动

2011-07-13 20:15 274 查看
/*
* Simple - REALLY simple memory mapping demonstration.
*///#include <linux/config.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/mm.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/arch-s3c2410/regs-gpio.h>
//#define GPFDAT *(volatile unsigned int *)S3C2410_GPFDAT
//#define GPFCON *(volatile unsigned int *)S3C2410_GPFCON
#define GPFDAT 0x56000054
#define GPFCON 0x56000050
#define GPF4_ON 0x4800
#define GPF4_OFF 0x4801
static int simple_major = 248;
module_param(simple_major, int, 0);
MODULE_AUTHOR("farsight");
MODULE_LICENSE("Dual BSD/GPL");
static volatile unsigned int *gpfcon;
static volatile unsigned int *gpfdat;
//static int flag = 0;
/*
* Open the device; in fact, there's nothing to do here.
*/
int simple_open (struct inode *inode, struct file *filp)
{
//do ioremap
gpfcon = ioremap(GPFCON, 0x04); //get virtual address of GPFCON
gpfdat = ioremap(GPFDAT, 0x04); //get virtual address of GPFDAT
*gpfcon &= (~(3<<8))+(1<<8); //set GPF4 as output port
*gpfdat |= (1<<4); //set GPF4 as high level
return 0;
}
ssize_t simple_read(struct file *file, char __user *buff, size_t count, loff_t *offp)
{
return 0;
}
ssize_t simple_write(struct file *file, const char __user *buff, size_t count, loff_t *offp)
{
return 0;
}
void led_stop( void )
{
*gpfdat = *gpfdat | (1<<4);
printk("stop\n");
//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
//s3c2410_gpio_setpin(S3C2410_GPF4,0);
}
void led_start( void )
{
*gpfdat &= (~(1<<4));
printk("start\n");
//s3c2410_gpio_pullup(S3C2410_GPF4,1);
//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
//s3c2410_gpio_setpin(S3C2410_GPF4,1);
}
static int simple_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
switch ( cmd ) {
case GPF4_ON:
{
led_start();
break;
}
case GPF4_OFF:
{
led_stop();
break;
}
default:
{
break;
}
}
return 0;
}
static int simple_release(struct inode *node, struct file *file)
{
return 0;
}
/*
* Set up the cdev structure for a device.
*/
static void simple_setup_cdev(struct cdev *dev, int minor,
struct file_operations *fops)
{
int err, devno = MKDEV(simple_major, minor);

cdev_init(dev, fops);
dev->owner = THIS_MODULE;
dev->ops = fops;
err = cdev_add (dev, devno, 1);
/* Fail gracefully if need be */
if (err)
printk (KERN_NOTICE "Error %d adding simple%d", err, minor);
}

/*
* Our various sub-devices.
*/
/* Device 0 uses remap_pfn_range */
static struct file_operations simple_remap_ops = {
.owner = THIS_MODULE,
.open = simple_open,
.release = simple_release,
.read = simple_read,
.write = simple_write,
.ioctl = simple_ioctl,
};
/*
* We export two simple devices. There's no need for us to maintain any
* special housekeeping info, so we just deal with raw cdevs.
*/
static struct cdev SimpleDevs;
/*
* Module housekeeping.
*/
static int simple_init(void)
{
int result;
dev_t dev = MKDEV(simple_major, 0);
/* Figure out our device number. */
if (simple_major)
result = register_chrdev_region(dev, 1, "simple");
else {
result = alloc_chrdev_region(&dev, 0, 1, "simple");
simple_major = MAJOR(dev);
}
if (result < 0) {
printk(KERN_WARNING "simple: unable to get major %d\n", simple_major);
return result;
}
if (simple_major == 0)
simple_major = result;
printk("<1>hello GPIO_TESTdrv!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
/* Now set up two cdevs. */
simple_setup_cdev(&SimpleDevs, 0, &simple_remap_ops);
printk("simple device installed, with major %d\n", simple_major);
return 0;
}

static void simple_cleanup(void)
{
//do iounmap
iounmap(gpfcon);
iounmap(gpfdat);
cdev_del(&SimpleDevs);
unregister_chrdev_region(MKDEV(simple_major, 0), 2);
printk("simple device uninstalled\n");
}

module_init(simple_init);
module_exit(simple_cleanup);

测试程序:
/*
* main.c : test demo driver
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define GPF4_ON 0x4800
#define GPF4_OFF 0x4801
int main()
{
int i = 0;
int n = 20;
int dev_fd;
dev_fd = open("/dev/led",O_RDWR | O_NONBLOCK);
if ( dev_fd == -1 ) {
printf("Cann't open file /dev/led\n");
exit(1);
}
while(n--)
{
ioctl(dev_fd,GPF4_ON,0);
sleep(1);
ioctl(dev_fd,GPF4_OFF,0);
sleep(1);
}
//getchar();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: