/**************************************************************************** * OK335xS PWM buzzer Linux Driver Hacking * Statement: * This article is only to know how to use PWM to control the buzzer, has reached the control of different sound frequency. * 2015-10-7 rain shenzhen Nanshan Ping Shan village Zengjianfeng ************************************************************ ***************/#include<linux/init.h>#include<linux/export.h>#include<linux/module.h>#include<linux/moduleparam.h>#include<linux/errno.h>#include<linux/miscdevice.h>#include<linux/types.h>#include<linux/io.h>#include<linux/pwm/pwm.h>#include<linux/fs.h>#defineDEBUG#ifDefined (DEBUG)#defineDPRINTK (Fmt,arg) PRINTK (fmt,# #arg);#else#defineDPRINTK (Fmt,arg ...)#endif#defineBuzzer_freqency 1#defineDev_name "Buzzer"/*PWM for this buzzer*/structPwm_device *PWM =NULL;Static intBuzzer_open (structInode *inode,structFile *Filp) { if(PWM! =NULL)return-Ebusy; /** * Apply for a PWM device*/PWM= Pwm_request ("ECAP.1", -1,"buzzer"); if(PWM = =NULL) {PRINTK ("Buzzer Open error.\n"); } PRINTK ("buzzer open\n"); return 0;}Static intBuzzer_release (structInode *inode,structFile *Filp) { /** * Close and logout of a PWM device*/pwm_stop (PWM); Pwm_release (PWM); PWM=NULL; PRINTK ("buzzer release\n"); return 0;}Static LongBuzzer_ioctl (structFile *Filp, unsignedintCMD, unsignedLongArg) { if(PWM = =NULL)return-EINVAL; if(Arg >20000|| Arg <0) return-EINVAL; Switch(cmd) { CaseBuzzer_freqency://Set Frequency if(arg==0) Pwm_stop (PWM); Else{Pwm_set_period_ns (PWM,1000000000/Arg); Pwm_set_duty_ns (PWM,25000); Pwm_start (PWM); } Break; default: Break; } return 0;}Static structFile_operations Buzzer_fops ={. Owner=this_module,. Unlocked_ioctl=buzzer_ioctl,. Open=Buzzer_open,. Release=Buzzer_release,};Static structMiscdevice Buzzer_miscdev ={. Minor=Misc_dynamic_minor,. Name=dev_name,. FoPs= &Buzzer_fops,};Static int__init Buzzer_init (void){ /** * Registration of miscellaneous equipment*/Misc_register (&Buzzer_miscdev); return 0;}Static void__exit Buzzer_exit (void) {Misc_deregister (&buzzer_miscdev);} Module_init (Buzzer_init); Module_exit (buzzer_exit);
Ok335xs PWM buzzer Linux driver hacking