mini2440的pwm驅動程序和測試程序詳解

 一 pwm 驅動程序

位置: 內核/drivers/char/mini2440_pwm.c

代碼註解

    #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 <asm/irq.h>
    #include <asm/io.h>
    #include <linux/interrupt.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" //設備名 pwm

#define PWM_IOCTL_SET_FREQ 1 //定義宏常量,用於後面的ioctl中的switch case
#define PWM_IOCTL_STOP 2

static struct semaphore lock; //定義信號量 lock


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輸出
s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_TOUT0);

tcon = __raw_readl(S3C2410_TCON); //讀取寄存器TCON到tcon
tcfg1 = __raw_readl(S3C2410_TCFG1); //讀取寄存器TCFG1到tcfg1
tcfg0 = __raw_readl(S3C2410_TCFG0); //讀取寄存器TCFG0到tcfg0

//prescaler = 50
tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK; // S3C2410_TCFG_PRESCALER0_MASK定時器0和

 // 1的預分頻值的掩碼,TCFG[0~8]

tcfg0 |= (50 - 1); // 預分頻爲50

//mux = 1/16
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, "pclk"); //得到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; //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
__raw_writel(tcon, S3C2410_TCON);

tcon &= ~2; //clear manual update bit
__raw_writel(tcon, S3C2410_TCON); //把tcon寫到計數器控制寄存器S3C2410_TCON中
}

void PWM_Stop( void )
{
s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_OUTP); //設置GPB0爲輸出
s3c2410_gpio_setpin(S3C2410_GPB0, 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)
{
 up(&lock); //釋放信號量lock
 return 0;
}


static int s3c24xx_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=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,
 .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;

init_MUTEX(&lock); //初始化一個互斥鎖
ret = misc_register(&misc); //註冊一個misc設備

printk (DEVICE_NAME"\tinitialized\n");
 return ret;
}

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");
驅動程序END

 

 驅動分析:
1 計數器控制寄存器

1)配置定時器輸入時鐘

TCFG0-時鐘配置寄存器0,用於獲得預分頻值(1~255)

TCFG1-時鐘配置寄存器1,用於獲得分割值(2,4,8,16,32)

定時器輸入時鐘頻率=PLCK/{預分頻+1}/{分割值}

2)配置PWM的佔空比

TCNTB0-定時器0計數緩存寄存器 ,是由定時器的輸入時鐘分頻得到,是脈寬調製的頻率

TCMTB0-定時器0比較緩存寄存器 ,用於設定PWM的佔空比 ,寄存器值爲高定平的

假設TCNTB0的頻率是160,如果TCMTB0是110,則PWM在110個週期是高定平,50週期是低電平,從而佔空比爲11:5

3)定時器控制寄存器TCON

TCON[0~4]用於控制定時器0

2.__raw_readl和__raw_writel

讀端口寄存器用__raw_readl(a ),該函數從端口a 返回一個32 位的值。相關的定義在include/asm-arm/io.h 中。#define __raw_readl(a) (*(volatile unsigned int*)(a)),寫端口寄存器用__raw_writel(v,a),該函數將一個32 位的值寫入端口a 中。相關的定義在include/asm-arm/io.h 中。#define __raw_writel(v,a) (*(volatile unsigned int*)(a) = (v))。此處設置功能控制寄存器,將相應的引腳設爲輸出狀態。

3 .gpio操作

gpio_cfgpin 配置相應GPIO口的功能

gpio_setpin IO口爲輸出功能時,寫引腳

4 基於信號量的Llinux 的併發控制

在驅動程序中,當多個線程同時訪問相同的資源時,可能會引發“競態”,因此必須對共享資源進行併發控制。信號量(絕大多數作爲互斥鎖使用)是一種進行併發控制的手段(還有自旋鎖,它適合於保持時間非常短的時間)。信號量只能在進程的上下文中使用。

void init_MUTEX(&lock)初始化一個互斥鎖,即他把信號量lock設置爲1

void up (&lock) 釋放信號量,喚醒等待者

int down_trylock(&lock) 嘗試獲得信號量lock ,如果能夠立刻獲得,就獲得信號量,並返回爲0.否則返回非0.並且它不會導致休眠,可以在中斷上下文中使用。在PWM中,當計數值溢出時,就會引發計數中斷。所以在這裏用這個函數來獲得信號。

二 PWM的測試函數

pwm_test.c代碼註釋

#include //標準輸入輸出定義
#include //POSIX終端控制定義
#include //Unix 標準函數定義
#include //標準函數庫定義

#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 2

#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, "this problem should be run at a terminal\n");
 exit(1);
}
// save terminal setting
if(tcgetattr(STDIN_FILENO, &oldt) < 0) { //獲取終端的設置參數
 perror("save the terminal setting");
 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 termial setting");
 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");
 exit(1); //打開錯誤,則終止進程。退出參數爲1
}

// any function exit call will stop the buzzer
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 of the 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); //退出返回1
}
}

int main(int argc, char **argv)
{
int freq = 1000 ;

open_buzzer(); //打開蜂鳴器

printf( "\nBUZZER TEST ( PWM Control )\n" );
printf( "Press +/- to increase/reduce the frequency of the BUZZER\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;
 }
}
}

1、 STDIN_FILENO 標準輸入的文件描述符

 內核(kernel)利用文件描述符(file descriptor)來訪問文件。文件描述符是非負整數。打開現存文件或新建文件時,內核會返回一個文件描述符。讀寫文件也需要使用文件描述符來指定待讀寫的文件標準輸入(standard input)的文件描述符是 0,標準輸出(standard output)是 1,標準錯誤(standard error)是2。 POSIX 定義了 STDOUT_FILENO 和 STDERR_FILENO 來代替 0、1、2。這三個符號常量的定義位於頭文件 unistd.h。

2 串口終端的操作。定義在中

通過對串口終端的操作,實現從標準輸入得到一個int型的char

1) isatty(STDIN_FILENO) 判斷是否有串口與標準輸入相連,是返回0,否返回1

2) tcgetattr(STDIN_FILENO, &oldt) 讀取終端結構體oldt的屬性,獲取終端的相關參數。成功返回0,失敗返回-1

3) newt.c_lflag&=~(ICANON|ECH0)

c_lflag:本地模式標誌,控制終端編輯功能

參數ICANON 表示使用標準輸入模式;參數ECH0表示顯示輸入字符。

4)tcsetattr(STDIN_FILENO,TCSANOW,&oldt)

tcsetattr 函數用於設置終端參數,成功返回0,失敗返回-1。

TCSANOW:不等數據傳輸完畢就立即改變屬性。

轉自:http://blog.tianya.cn/blogger/post_show.asp?idWriter=0&Key=0&BlogID=2573752&PostID=21439200 http://blog.csdn.net/garby2004/archive/2009/09/29/4604039.aspx

發佈了10 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章