//This file for explain how to use a simple driver
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// BUZZER DEVICE MAJOR
#define SIMPLE_BUZZER_MAJOR 152
#define OURS_BUZZER_DEBUG
#define VERSION "PXA270RP-V1.00-090225"
//define LED STATUS
#define BUZZER_ON 0
#define BUZZER_OFF 1
struct buzzer_dev
{
struct cdev cdev;
unsigned char value;
};
struct buzzer_dev *buzzer_devp;
static int buzzer_major=SIMPLE_BUZZER_MAJOR;
void showversion(void)
{
printk("*********************************************\n");
printk("\t %s \t\n", VERSION);
printk("*********************************************\n\n");
}
// ------------------- READ ------------------------
ssize_t SIMPLE_BUZZER_read (struct file * file ,char * buf, size_t count, loff_t * f_ops)
{
#ifdef OURS_BUZZER_DEBUG
printk ("SIMPLE_BUZZER_read [ --kernel--]\n");
#endif
return count;
}
// ------------------- WRITE -----------------------
ssize_t SIMPLE_BUZZER_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops)
{
#ifdef OURS_BUZZER_DEBUG
printk ("SIMPLE_BUZZER_write [ --kernel--]\n");
#endif
return count;
}
// ------------------- IOCTL -----------------------
ssize_t SIMPLE_BUZZER_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long data)
{
#ifdef OURS_BUZZER_DEBUG
printk ("SIMPLE_BUZZER_ioctl [ --kernel--]\n");
#endif
switch (cmd)
{
case BUZZER_ON : {GPCR0 |= (0x1<<16);
break;}
case BUZZER_OFF: {GPSR0 |= (0x1<<16);
break;}
default :
{printk ("control : no cmd run [ --kernel-- ]\n");}
}
return (-ENOTTY);
}
// ------------------- OPEN ------------------------
ssize_t SIMPLE_BUZZER_open (struct inode * inode ,struct file * file)
{
#ifdef OURS_BUZZER_DEBUG
printk ("SIMPLE_BUZZER_open [ --kernel--]\n");
#endif
return 0;
}
// ------------------- RELEASE/CLOSE ---------------
ssize_t SIMPLE_BUZZER_release (struct inode * inode ,struct file * file)
{
#ifdef OURS_BUZZER_DEBUG
printk ("SIMPLE_BUZZER_release [ --kernel--]\n");
#endif
return 0;
}
// -------------------------------------------------
struct file_operations buzzer_fops ={
.open= SIMPLE_BUZZER_open,
.read= SIMPLE_BUZZER_read,
.write= SIMPLE_BUZZER_write,
.ioctl= SIMPLE_BUZZER_ioctl,
.release= SIMPLE_BUZZER_release,
};
static void buzzer_setup_cdev(struct buzzer_dev *dev,int index)
{
int err, devno=MKDEV(buzzer_major,index);
cdev_init(&dev->cdev,&buzzer_fops);
dev->cdev.owner=THIS_MODULE;
dev->cdev.ops=&buzzer_fops;
err=cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_NOTICE "Error %d adding buzzer%d",err,index);
}
int buzzer_init(void)
{
int result;
dev_t dev=MKDEV(buzzer_major,0);
if(buzzer_major)
result=register_chrdev_region(dev,1,"buzzer");
else
{
result=alloc_chrdev_region(&dev,0,1,"buzzer");
buzzer_major=MAJOR(dev);
}
if(result<0)
return result;
buzzer_devp =kmalloc (sizeof(struct buzzer_dev),GFP_KERNEL);
if (!buzzer_devp)
{ result= - ENOMEM;
goto fail_malloc;
}
memset(buzzer_devp,0,sizeof(struct buzzer_dev));
buzzer_setup_cdev(buzzer_devp,0);
//init BUZZER
GAFR0_U &=(0xfffffffc); //SET GPIO16 GPIO MODE
GPDR0 |= (0x1<<16); // SET GPIO16 OUTPUT MODE
GPSR0 |= (0x1<<16); // OFF THE BUZZER
GAFR0_L &=(0xfffffff3);//SET GPIO1 GPIO MODE
GPDR0 |=(0x1<<1); //SET GPIO1 OUTPUT MODE
GPSR0 |=(0x1<<1); //SET GPIO1 HIGHT
printk("buzzer_init\n");
showversion();
return 0;
fail_malloc:unregister_chrdev_region(dev,1);
return result;
}
void buzzer_cleanup(void)
{
cdev_del(&buzzer_devp->cdev);
kfree(buzzer_devp);
unregister_chrdev_region(MKDEV(buzzer_major,0),1);
printk("buzzer_cleanup\n");
}
MODULE_DESCRIPTION("simple buzzer driver module");
MODULE_AUTHOR("guliangzeng");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_ALIAS("buzzer module");
module_init(buzzer_init);
module_exit(buzzer_cleanup);
#include
#include
#include
#include // open() close()
#include // rea() write()
#define DEVICE_NAME "/dev/BUZZER_ctl"
//------------------------------------- main ----------------------------------------------------------------
int main(void)
{
int fd;
int ret;
int i;
printf("\nstart buzzer_driver test\n\n");
fd = open(DEVICE_NAME, O_RDWR);
printf("fd = %d\n",fd);
if (fd == -1)
{
printf("open device %s error\n",DEVICE_NAME);
}
else
{
for(i=0;i<5;i++)
{ ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(1);
ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(1);
ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(1);
ioctl(fd,0);
sleep(0.7);
ioctl(fd,1);
sleep(1);
ioctl(fd,0);
sleep(0.7);
ioctl(fd,1);
sleep(1);
ioctl(fd,0);
sleep(0.6);
ioctl(fd,1);
sleep(1);
ioctl(fd,0);
sleep(1.5);
ioctl(fd,1);
sleep(1.5);
ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(0.8);
ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(0.7);
ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(0.5);
ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(0.5);
ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(0.2);
ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(0.2);
ioctl(fd,0);
sleep(1);
ioctl(fd,1);
sleep(0.8);
}
// close
ret = close(fd);
printf ("ret=%d\n",ret);
printf ("close buzzer_driver test\n");
}
return 0;
}// end main
阅读(2317) | 评论(0) | 转发(0) |