由于步進電機用到了I/O端口,而在ARM9中操縱端口要用虛擬地址而非實際的物理地址,所以要修改內核代碼。
修改文件內核源代碼中間的smdk.c,在結構體
static struct map_desc smdk_io_desc] __initdata = {
{ vCS8900_BASE, pCS8900_BASE, 0x00100000, DOMAIN_IO, 0, 1, 0, 0 },
{ vCF_MEM_BASE, pCF_MEM_BASE, 0x01000000, DOMAIN_IO, 0, 1, 0, 0 },
{ vCF_IO_BASE, pCF_IO_BASE, 0x01000000, DOMAIN_IO, 0, 1, 0, 0 },
LAST_DESC
};
中添加一行數組元素{ 0xd3000000, 0x28000000, 0x01000000, DOMAIN_IO, 0, 1, 0, 0 },則步進電機的物理地址0x28000006對應的虛擬地址為0xd3000006,在驅動程序中應對這個地址進行操縱。
定義全局變量num和status用來控制步進電機的速度和方向:
static int num=1;
static enum{off,clockwise,anticlockwise} status=off;
定義步進電機的整步模式正轉脈沖表:
unsigned char pulse_table[] =
{
0x05, 0x09, 0x0a, 0x06,
};
定義時鐘節拍函數time_tick()
static void time_tick(unsigned long data)
{
static int i=0;
switch(status)
{
case off: break;
case clockwise:
if(++i==num){
i=0;
if( row == 4 ) row = 0;
(*(char *)0xd3000006)=pulse_table[row++];
}
ttimer.expires=jiffies+1;
add_timer(&ttimer);
break;
case anticlockwise:
if(++i==num){
i=0;
if( row == -1 ) row = 3;
(*(char *)0xd3000006)=pulse_table[row--];
}
ttimer.expires=jiffies+1;
add_timer(&ttimer);
break;
case default: break;
}
}
在time_tick()函數中判定步進電機的狀態,是停止、正轉還是反轉。若是正轉,則按正向順序發送脈沖,并添加定時器ttimer;若是反轉,則按反向順序發送脈沖,并添加定時器ttimer;若是停止則不再發送脈沖,也不再添加定時器。
在stepper_module_init()函數中申請I/O端口,并初始化定時器ttimer:
if(check_region(0x28000006, 1)) //看該I/O端口是否已經被占用
{
printk("The stepper port is used by another module.\n");
return -1;
}
request_region(0x28000006, 1, DEVICE_NAME); //申請該I/O端口
init_timer(&ttimer); //初始化定時器ttimer
ttimer.function=time_tick; //填寫定時器處理函數為time_tick()
編寫ioctl函數用來接收應用程序對于步進電機的控制。
int device_ioctl( struct inode *inode, struct file *file, unsigned int ioctl_num,
unsigned long ioctl_param)
{
struct stepper * s;
/* 根據實際程序中的不同需求更改ioctl函數的調用*/
switch (ioctl_num)
{
case IOCTL_SET_MSG:
s = (struct stepper*) ioctl_param;
switch (s->CmdID)
{
case 0: /*開始*/
status=clockwise;
ttimer.expires=jiffies+1; //開啟定時器
add_timer(&ttimer);
break;
case 1: status=off; break; /*停止*/
case 2: /*反轉*/
if(status==clockwise){ status=anticlockwise; }
if(status==anticlockwise){ status=clockwise; }
break;
case 3: if(num!=1)num--; break; /*加速*/
case 4: num++; break; /*減速*/
}
}
return 0;
};
通過s指針得到stepper結構中的表示命令類型的參數,根據該參數判定命令類型,0是start起動,1是stop停止,2是reverse反向,3是up電機加速,4是down電機減速,通過改變全局變量num和status來控制電機。電機的起動是通過在start分支中起動一個定時器ttimer,然后在定時器處理函數time_tick中發送步進電機脈沖,并重新添加定時器,從而實現步進電機的轉動。