Ok335xs PWM buzzer Linux driver hacking

Source: Internet
Author: User

/**************************************************************************** * 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.