欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

蜂鸣器驱动(s3c3440)

程序员文章站 2022-07-05 21:14:03
buzzer_mwm.c#include #include #include #include #include #include #include #include

buzzer_mwm.c

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/gpio.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>



#define DEVICE_NAME     "pwm"

#define PWM_IOCTL_SET_FREQ		1
#define PWM_IOCTL_STOP			0
#define BUZZER_MINORS	       109
volatile unsigned long *gpbcon = NULL;
volatile unsigned long *gpbdat = NULL;

static struct class *buzzerdrv_class;


static int mini2440_pwm_mwm_open(struct inode *inode, struct file *file)
{
	*gpbcon |= (0x1<<(0*2));
	return 0;
}

static int mini2440_pwm_mwm_close(struct inode *inode, struct file *file)
{
	
    return 0;
}

static ssize_t mini2440_pwm_mwm_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	int val;
	copy_from_user(&val,buf,count);
	if(val)
	{
		*gpbdat &= ~(1<<0);
	}
	else
	{
		*gpbdat |= (1<<0);
	}
	return 0;
}

static struct file_operations dev_fops = {
    .owner   =   THIS_MODULE,
    .open    =   mini2440_pwm_mwm_open,
    .release =   mini2440_pwm_mwm_close, 
    .write   =   mini2440_pwm_mwm_write,
};

static int __init dev_init(void)
{
	int res;
	res = register_chrdev(BUZZER_MINORS,"buzzer_drv",&dev_fops);
	if (res)		
		goto out;
		buzzerdrv_class = class_create(THIS_MODULE, "buzzer_drv");	//
		device_create(buzzerdrv_class, NULL, MKDEV(BUZZER_MINORS,0),"%s","buzzer_drv");

		gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);	
		gpbdat = gpbcon + 1;

	return 0;
out:
	printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
	return res;
}

static void __exit dev_exit(void)
{
	iounmap(gpbcon);
	device_destroy(buzzerdrv_class,MKDEV(BUZZER_MINORS,0));
	class_destroy(buzzerdrv_class);
	unregister_chrdev(BUZZER_MINORS, "buzzer_drv");
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

Makefile

KERN_DIR = /home/mao/work0630/linux-2.6.32.2
all:
	     make -C $(KERN_DIR) M=`pwd` modules
clean:
	     make -C $(KERN_DIR) M=`pwd` modules clean
	     rm -rf modules.order
obj-m += buzzer_mwm.o

buzzertest.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,char **argv)
{   
    int fd;
    int val = 1;
    fd = open("/dev/buzzer_drv",O_RDWR);
    if (fd <0)
    {
        printf("can't open ! \n");
    }
    if (argc != 2)
    {
        printf("Usage :\n");
        printf("%s <on|off> \n");
        return 0;
    }
    if(strcmp(argv[1],"on") == 0)
    {
        val = 1;
    }
    else
        val = 0;

    write(fd,&val,4);
    return 0;
}

本文地址:https://blog.csdn.net/laomao_mwm/article/details/107568846