1. pwm驅動程式(核心版本linux-2.6.28.7)
#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 <plat/regs-timer.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>
#define DEVICE_NAME "pwm" //裝置名
#define pwm_MAJOR 232
#define PWM_IOCTL_SET_FREQ 1 //定義宏變量,用于後面的 ioctl 中的控制指令
#define PWM_IOCTL_STOP 0 //定義信号量 lock
//定義信号量 lock用于互斥,是以,該驅動程式隻能同時有一個程序使用
static struct semaphore lock;
/* freq: pclk/50/16/65536 ~ pclk/50/16
* if pclk = 50MHz, freq is 1Hz to 62500Hz
* human ear(人兒能辨識的) : 20Hz~ 20000Hz
*/
/*配置各個寄存器,設定PWM的頻率*/
static void PWM_Set_Freq( unsigned long freq )
{
unsigned long tcon;
unsigned long tcnt;
unsigned long tcfg1;
unsigned long tcfg0;
struct clk *clk_p;
unsigned long pclk;
//設定GPB0 為TOUT0,pwm 輸出
s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_TOUT0);
/*讀取定時器控制寄存器的數值*/
tcon = __raw_readl(S3C2410_TCON);
/*讀取定時器配置寄存器1的值*/
tcfg1 = __raw_readl(S3C2410_TCFG1);
/*讀取定時器配置寄存器0的值*/
tcfg0 = __raw_readl(S3C2410_TCFG0);
/*設定prescaler = 50*/
//S3C2410_TCFG_PRESCALER0_MASK定時器0 和1 的預分頻值的掩碼,TCFG[0~7]
tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
tcfg0 |= (50 - 1);
/*設定分頻值為16*/
//S3C2410_TCFG1_MUX0_MASK 定時器0 分割值的掩碼TCFG1[0~3]
tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK;
//定時器0 進行16 分割
tcfg1 |= S3C2410_TCFG1_MUX0_DIV16;
/*将設定的參數值寫入相應的寄存器中*/
//把tcfg1 的值寫到分割寄存器S3C2410_TCFG1 中
__raw_writel(tcfg1, S3C2410_TCFG1);
//把tcfg0 的值寫到預分頻寄存器S3C2410_TCFG0 中
__raw_writel(tcfg0, S3C2410_TCFG0);
/*開啟對應時鐘源,并擷取pclk*/
clk_p = clk_get(NULL, "pclk");
//獲得pclk的時鐘頻率
pclk = clk_get_rate(clk_p);
/*得到定時器的輸入時鐘,進而設定PWM的調制頻率和占空比*/
tcnt = (pclk/50/16)/freq;
//PWM 脈寬調制的頻率等于定時器的輸入時鐘
__raw_writel(tcnt, S3C2410_TCNTB(0));
//占空比是50%
__raw_writel(tcnt/2, S3C2410_TCMPB(0));
/*失能死區,開啟自動重載, 關閉變相, 更新TCNTB0&TCMPB0, 啟動timer0*/
tcon &= ~0x1f;
tcon |= 0xb;
//把tcon 寫到計數器控制寄存器S3C2410_TCON 中
__raw_writel(tcon, S3C2410_TCON);
//clear manual update bit
tcon &= ~2;
__raw_writel(tcon, S3C2410_TCON);
}
static void pwm_stop(void)
{
//設定GPB0 為輸出
s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPIO_OUTPUT);
//設定GPB0 為低電平,使蜂鳴器停止
s3c2410_gpio_setpin(S3C2410_GPB0, 0);
}
static int pwm_open(struct inode *inode, struct file *file)
{
if (!down_trylock(&lock)) //是否獲得信号量,是down_trylock(&lock)=0,否則非0
return 0;
else
return -EBUSY; //傳回錯誤資訊:請求的資源不可用
}
static int pwm_close(struct inode *inode, struct file *file)
{
pwm_stop();
//釋放信号量lock
up(&lock);
return 0;
}
/*cmd 是1,表示設定頻率;cmd 是2 ,表示停止pwm*/
static int pwm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case PWM_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=0 即進入case PWM_IOCTL_STOP
pwm_stop(); //停止蜂鳴器
break;
}
return 0; //成功傳回
}
/*初始化裝置的檔案操作的結構體*/
static struct file_operations pwm_fops = {
.owner = THIS_MODULE,
.open = pwm_open,
.release = pwm_close,
.ioctl = pwm_ioctl,
};
static int __init pwm_init(void)
{
int ret;
init_MUTEX(&lock); //初始化一個互斥鎖
ret = register_chrdev(pwm_MAJOR, DEVICE_NAME, &pwm_fops);
if(ret < 0)
{
printk(DEVICE_NAME "register falid!\n");
return ret;
}
printk (DEVICE_NAME " initialized!\n");
return 0;
}
static void __exit pwm_exit(void)
{
unregister_chrdev(pwm_MAJOR, DEVICE_NAME, &pwm_fops);
}
module_init(pwm_init);
module_exit(pwm_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("DreamCatcher");
MODULE_DESCRIPTION("MINI2440 PWM Driver");
2. pwm測試程式
#include <stdio.h>
#include <termios.h> //POSIX終端控制定義
#include <unistd.h> //Unix 标準函數定義
#include <stdlib.h>
#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0
#define ESC_KEY 0x1b //定義ESC_KEY為ESC
//按鍵的鍵值
static int getch(void) //定義函數在終端上獲得輸入,并把輸入的量(int)傳回
{
struct termios oldt,newt; //終端結構體struct termios
int ch;
if (!isatty(STDIN_FILENO)) { //判斷序列槽是否與标準輸入相連
fprintf(stderr, "thisproblem should be run at a terminal\n");
exit(1);
}
// save terminal setting
if(tcgetattr(STDIN_FILENO, &oldt) < 0) { //擷取終端的設定參數
perror("save the terminalsetting");
exit(1);
}
// set terminal as need
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO ); //控制終端編輯功能參數ICANON表示使用标準輸入模式;參數ECH0表示顯示輸入字元
if(tcsetattr(STDIN_FILENO,TCSANOW,&newt) < 0) { //儲存新的終端參數
perror("set terminal");
exit(1);
}
ch = getchar();
// restore termial setting
if(tcsetattr(STDIN_FILENO,TCSANOW,&oldt) < 0) { //恢複儲存舊的終端參數
perror("restore the termialsetting");
exit(1);
}
return ch;
}
static int fd = -1;
static void close_buzzer(void);
static void open_buzzer(void) //打開蜂鳴器
{
fd = open("/dev/pwm", 0); //打開pwm裝置驅動檔案
if (fd < 0) {
perror("open pwm_buzzer device"); //打開錯誤,則終止程序。退出參數為1
exit(1);
}
// any function exit call will stop thebuzzer
atexit(close_buzzer); //退出回調close_buzzer
}
static void close_buzzer(void) //關閉蜂鳴器
{
if (fd >= 0) {
ioctl(fd, PWM_IOCTL_STOP); //停止蜂鳴器
close(fd); //關閉裝置驅動檔案
fd = -1;
}
}
static void set_buzzer_freq(int freq)
{
// this IOCTL command is the key to set frequency
int ret = ioctl(fd, PWM_IOCTL_SET_FREQ, freq); //設定頻率
if(ret < 0) { //如果輸入的頻率錯誤
perror("set the frequency ofthe buzzer");
exit(1); //退出,傳回1
}
}
static void stop_buzzer(void) //關閉蜂鳴器
{
int ret = ioctl(fd, PWM_IOCTL_STOP);
if(ret < 0) {
perror("stop the buzzer");
exit(1);
}
}
int main(int argc, char **argv)
{
int freq = 1000 ;
open_buzzer(); //打開蜂鳴器
printf( "\nBUZZER TEST ( PWMControl )\n" );
printf( "Press +/- to increase/reduce the frequency of theBUZZER\n" ) ;
printf( "Press 'ESC' key to Exit this program\n\n" );
while(1)
{
int key;
set_buzzer_freq(freq); //設定蜂鳴器頻率
printf( "\tFreq =%d\n", freq );
key = getch();
switch(key) {
case '+':
if( freq < 20000 )
freq += 10;
break;
case '-':
if( freq > 11 )
freq -= 10 ;
break;
case ESC_KEY:
case EOF:
stop_buzzer();
exit(0);
default:
break;
}
}
}