天天看點

Linux-4.9.2核心在mini2440上的移植(十四)——蜂鳴器驅動移植

本篇目的:移植蜂鳴器驅動,并測試。

本篇參考:http://singleboy.blog.163.com/blog/static/54900194201152921847149/

14.1 蜂鳴器驅動源碼添加

(1)添加源碼drivers/misc/mini2440_pwm.c

[email protected]:~/linux-4.9.2# vim drivers/misc/mini2440_pwm.c

添加如下源碼,本源碼已經修改并适配于linux-4.9.2核心。

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <mach/regs-irq.h>
#include <asm/mach/time.h>
#include <linux/clk.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <mach/gpio-samsung.h>
#include <mach/regs-gpio.h>
#include <plat/gpio-cfg.h>
 
 
#define DEVICE_NAME "pwm" /*裝置名*/
#define PWM_IOCTL_SET_FREQ 1 /*定義宏變量,用于後面的ioctl 中的switchcase*/
#define PWM_IOCTL_STOP 0 /*定義信号量 lock*/
 
#define S3C_TIMERREG(x) (S3C_VA_TIMER + (x))
#define S3C_TIMERREG2(tmr,reg) S3C_TIMERREG((reg)+0x0c+((tmr)*0x0c))
 
#define S3C2410_TCFG0        S3C_TIMERREG(0x00)
#define S3C2410_TCFG1        S3C_TIMERREG(0x04)
#define S3C2410_TCON         S3C_TIMERREG(0x08)
 
#define S3C2410_TCFG_PRESCALER0_MASK (255<<0)
#define S3C2410_TCFG_PRESCALER1_MASK (255<<8)
#define S3C2410_TCFG_PRESCALER1_SHIFT (8)
#define S3C2410_TCFG_DEADZONE_MASK  (255<<16)
#define S3C2410_TCFG_DEADZONE_SHIFT (16)
 
#define S3C2410_TCFG1_MUX0_DIV2  (0<<0)
#define S3C2410_TCFG1_MUX0_DIV4  (1<<0)
#define S3C2410_TCFG1_MUX0_DIV8  (2<<0)
#define S3C2410_TCFG1_MUX0_DIV16 (3<<0)
#define S3C2410_TCFG1_MUX0_TCLK0 (4<<0)
#define S3C2410_TCFG1_MUX0_MASK  (15<<0)
 
#define S3C2410_TCNTB(tmr)   S3C_TIMERREG2(tmr, 0x00)
#define S3C2410_TCMPB(tmr)   S3C_TIMERREG2(tmr, 0x04)
#define S3C2410_TCNTO(tmr)   S3C_TIMERREG2(tmr, (((tmr) == 4) ? 0x04 : 0x08))
 
static struct semaphore lock;
/* freq: pclk/50/16/65536 ~ pclk/50/16
* if pclk = 50MHz, freq is 1Hz to 62500Hz
* human ear : 20Hz~ 20000Hz
*/
 
static void PWM_Set_Freq( unsigned long freq ) /*設定pwm 的頻率,配置各個寄存器*/
{
         unsigned long tcon;
         unsigned long tcnt;
         unsigned long tcfg1;
         unsigned long tcfg0;
         struct clk *clk_p;
         unsigned long pclk;
         /*set GPB0 as tout0,pwm output 設定GPB0 為tout0,pwm 輸出*/
         s3c_gpio_cfgpin(S3C2410_GPB(0),S3C2410_GPB0_TOUT0);
         tcon =__raw_readl(S3C2410_TCON); /*讀取寄存器TCON 到tcon*/
         tcfg1 =__raw_readl(S3C2410_TCFG1); /*讀取寄存器TCFG1 到tcfg1*/
         tcfg0 =__raw_readl(S3C2410_TCFG0); /*讀取寄存器TCFG0 到tcfg0*/
        
         /*S3C2410_TCFG_PRESCALER0_MASK定時器0 和1 的預分頻值的掩碼,TCFG[0~8]*/
         tcfg0 &=~S3C2410_TCFG_PRESCALER0_MASK;
         tcfg0 |= (50 - 1); /* 預分頻為50*/
        
         tcfg1 &=~S3C2410_TCFG1_MUX0_MASK; /*S3C2410_TCFG1_MUX0_MASK 定時器0 分割值的掩碼TCFG1[0~3]*/
         tcfg1 |=S3C2410_TCFG1_MUX0_DIV16; /*定時器0 進行16 分割*/
         __raw_writel(tcfg1,S3C2410_TCFG1); /*把tcfg1 的值寫到分割寄存器S3C2410_TCFG1 中*/
         __raw_writel(tcfg0,S3C2410_TCFG0); /*把tcfg0 的值寫到預分頻寄存器S3C2410_TCFG0 中*/
         clk_p = clk_get(NULL,"timers"); /*得到pclk*/
         pclk =clk_get_rate(clk_p);
         tcnt =(pclk/50/16)/freq; /*得到定時器的輸入時鐘,進而設定PWM 的調制頻率*/
         __raw_writel(tcnt,S3C2410_TCNTB(0)); /*PWM 脈寬調制的頻率等于定時器的輸入時鐘*/
         __raw_writel(tcnt/2,S3C2410_TCMPB(0)); /*占空比是50%*/
         tcon &= ~0x1f;
         tcon |= 0xb; /*disabledeadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0*/
         __raw_writel(tcon, S3C2410_TCON);/*把tcon 寫到計數器控制寄存器S3C2410_TCON 中*/
         tcon &= ~2;/*clear manual update bit*/
         __raw_writel(tcon,S3C2410_TCON);
}
static void PWM_Stop(void)
{
         s3c_gpio_cfgpin(S3C2410_GPB(0),S3C2410_GPIO_OUTPUT); /*設定GPB0 為輸出*/
         gpio_set_value(S3C2410_GPB(0),0); /*設定GPB0 為低電平,使蜂鳴器停止*/
}
static int s3c24xx_pwm_open(struct inode *inode, struct file *file)
{
         if(!down_trylock(&lock)) /*是否獲得信号量,是down_trylock(&lock)=0,否則非0*/
                   return 0;
         else
                   return-EBUSY; /*傳回錯誤資訊:請求的資源不可用*/
}
static int s3c24xx_pwm_close(struct inode *inode, struct file *file)
{
         PWM_Stop();
         up(&lock); /*釋放信号量lock*/
         return 0;
}
/*cmd 是1,表示設定頻率;cmd 是2 ,表示停止pwm*/
static long s3c24xx_pwm_ioctl(struct file *file, unsigned int cmd,unsigned long arg)
{
         switch (cmd)
         {
         casePWM_IOCTL_SET_FREQ: /*if cmd=1 即進入case PWM_IOCTL_SET_FREQ*/
                   if (arg ==0) /*如果設定的頻率參數是0*/
                            return-EINVAL; /*傳回錯誤資訊,表示向參數傳遞了無效的參數*/
                   PWM_Set_Freq(arg);/*否則設定頻率*/
                   break;
         case PWM_IOCTL_STOP:/* if cmd=2 即進入case PWM_IOCTL_STOP*/
                   PWM_Stop();/*停止蜂鳴器*/
                   break;
         }
         return 0; /*成功傳回*/
}
/*初始化裝置的檔案操作的結構體*/
static struct file_operations dev_fops = {
         .owner = THIS_MODULE,
         .open =s3c24xx_pwm_open,
         .release =s3c24xx_pwm_close,
         .unlocked_ioctl =s3c24xx_pwm_ioctl,
};
static struct miscdevice misc = {
         .minor =MISC_DYNAMIC_MINOR,
         .name = DEVICE_NAME,
         .fops = &dev_fops,
};
static int __init dev_init(void)
{
         int ret;
         sema_init(&lock,1);/*初始化一個互斥鎖*/
         ret =misc_register(&misc); /*注冊一個misc 裝置*/
         if(ret < 0)
         {
                   printk(DEVICE_NAME"register falid!\n");
                   return ret;
         }
         printk (DEVICE_NAME"\tinitialized!\n");
         return 0;
}
static void __exit dev_exit(void)
{
         misc_deregister(&misc);/*登出裝置*/
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
MODULE_DESCRIPTION("S3C2410/S3C2440 PWM Driver");
           

儲存并退出

(2)添加Kconfig選項

[email protected]:~/linux-4.9.2# vim drivers/misc/Kconfig

在110行,接着上回的button下面添加,紅色部分

config MINI2440_BUTTONS

  tristate "Buttonsdriver for FriendlyARM Mini2440 development boards"

  depends on MACH_MINI2440

  default y if MACH_MINI2440

  help

   this is buttons driver forFriendlyARM Mini2440 development boards

config MINI2440_BUZZER

 tristate"Buzzer driver for FriendlyARM Mini2440 development boards"

 depends onMACH_MINI2440

 default yif MACH_MINI2440

 help

  this isbuzzer driver for FriendlyARM Mini2440 development boards

config DUMMY_IRQ

        tristate "DummyIRQ handler"

(3)添加Makefile支援

[email protected]:~/linux-4.9.2# vim drivers/misc/Makefile

在38行添加紅色部分

obj-$(CONFIG_MINI2440_ADC) += mini2440_adc.o

obj-$(CONFIG_LEDS_MINI2440) += mini2440_leds.o

obj-$(CONFIG_MINI2440_BUTTONS)+= mini2440_buttons.o

obj-$(CONFIG_MINI2440_BUZZER) += mini2440_pwm.o

obj-$(CONFIG_HMC6352)          += hmc6352.o

obj-y                          += eeprom/

obj-y                          += cb710/

完成後,使用如下指令

[email protected]:~/linux-4.9.2# make menuconfig

檢視一下

Device Drivers --->

      [*] Misc devices  ---> 

buzzer的配置,并儲存退出

14.2 編譯、測試

(1)編譯

[email protected]:~/linux-4.9.2# make -j8

[email protected]:~/linux-4.9.2# ./mkuImage.sh

(2)測試

重新開機開發闆,等LCD顯示QT界面之後,在QT界面的“友善之臂”頁籤,打開“蜂鳴器”應用,按下“start”按鈕,可以聽到蜂鳴器發聲,按+、-号,調節發聲頻率。