昨天的跑马灯模拟步进电机得驱动现在实现了,但是问题又来了,我使用内核提供得msdelay();函数实现的延时,资料上说内核延时精度只能精确到10ms,但是我用示波器观察下来,数据如下。
msdelay(1);//1ms延时 示波器显示为20ms
msdelay(2);//2ms延时 示波器显示为20ms (和资料上得10ms好像有差距)
msdelay(10);//10ms延时 示波器显示为30ms
msdelay(20);//20ms延时 示波器显示为50ms
msdelay(30);//30ms延时 示波器显示为70ms
msdelay(40);//40ms延时 示波器显示为90ms
从数据上看好像每增加10ms得延时 实际增加20ms得误差,又或是我的驱动程序有问题?现在要想步进电机正常工作起码要1ms的延时。请问还有什么方法可以达到1ms延时或者更短。
驱动程序如下,请大家帮忙看下:
#include <linux/types.h>
#include <linux/ioport.h>
#include <linux/fcntl.h>
#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#define DEVICE_NAME "Led" //定义设备名
#define LED_MAJOR 250 //手动定义主设备号
MODULE_AUTHOR("Peter_zhang");
MODULE_DESCRIPTION("S3C2440 LED Driver");
MODULE_LICENSE("GPL");
static unsigned char m_step[8]={0xef,0xef,0xdf,0xdf,0xbf,0xbf,0x7f,0x7f};//4相6线步进电机时序
static unsigned int flag=0;
static int flag_1=7;
static unsigned long GPFDAT,GPFCON;
static int s3c2440_leds_open(struct inode *inode,struct file *file)
{
/* int i;
for(i=0;i<4;i++)
{
s3c2410_gpio_cfgpin(led_table,led_cfg_table);
}
*/
GPFCON=(unsigned long)ioremap(0x56000050,4);//内存映射
GPFDAT=(unsigned long)ioremap(0x56000054,4);
GPFCON |=0x5500; //设在F端口4 5 6 7为输出
return 0;
}
//cmd 选择延时时间,arg 电机转动方向
static int s3c2440_leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
if(arg==1)
{
(*(volatile unsigned long *)GPFDAT) =m_step[flag++];
msleep(cmd);
if(flag>7)flag=0;
return 0;
}
else if(arg==0)
{
(*(volatile unsigned long *)GPFDAT) =m_step[flag_1--];
msleep(cmd);
if(flag_1<0)flag_1=7;
return 0;
}
else
{
return -EINVAL;
}
}
//定义s3c2440_led_fops:结构体提供基本函数入口点
static struct file_operations s3c2440_leds_fops =
{
.owner = THIS_MODULE,
.open = s3c2440_leds_open,
.ioctl = s3c2440_leds_ioctl,
};
//模块加载函数
static int __init s3c2440_leds_init(void)
{
int ret;
//注册设备号/* register_chrdev是注册字符设备的函数*/
ret=register_chrdev(LED_MAJOR,DEVICE_NAME,&s3c2440_leds_fops);
if (ret < 0)
{
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
printk(DEVICE_NAME " initialized\n");/*打印出驱动加载成功的信息*/
return 0;
}
//模块卸载函数
static void __exit s3c2440_leds_exit(void)
{
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}
//指定驱动程序的初始化函数和卸载函数
module_init(s3c2440_leds_init);
module_exit(s3c2440_leds_exit);