📄 gpio_motor.c
字号:
return 1;
}
motor_lr_step3();
time_delay_ms(SPEED);
if(motor_left_limited())
{
left_count = 0;
return 1;
}
motor_lr_step4();
time_delay_ms(SPEED);
if(motor_left_limited())
{
left_count = 0;
return 1;
}
motor_lr_step5();
time_delay_ms(SPEED);
if(motor_left_limited())
{
left_count = 0;
return 1;
}
motor_lr_step6();
time_delay_ms(SPEED);
if(motor_left_limited())
{
left_count = 0;
return 1;
}
motor_lr_step7();
time_delay_ms(SPEED);
if(motor_left_limited())
{
left_count = 0;
return 1;
}
motor_lr_step8();
time_delay_ms(SPEED);
left_count --;
}
return 0;
}
/*
* horizontial direction rotate right
* @param count: the angle of any rotation ( count*(5.625/8) )
* return 0:successfully rotated
* return 1:cannot rotate
*/
static int motor_rotate_right(unsigned char count)
{
unsigned char i;
for(i = 0;i < count;i ++)
{
if(motor_right_limited())
{
return 1;
}
motor_lr_step8();
time_delay_ms(SPEED);
if(motor_right_limited())
{
return 1;
}
motor_lr_step7();
time_delay_ms(SPEED);
if(motor_right_limited())
{
return 1;
}
motor_lr_step6();
time_delay_ms(SPEED);
if(motor_right_limited())
{
return 1;
}
motor_lr_step5();
time_delay_ms(SPEED);
if(motor_right_limited())
{
return 1;
}
motor_lr_step4();
time_delay_ms(SPEED);
if(motor_right_limited())
{
return 1;
}
motor_lr_step3();
time_delay_ms(SPEED);
if(motor_right_limited())
{
return 1;
}
motor_lr_step2();
time_delay_ms(SPEED);
if(motor_right_limited())
{
return 1;
}
motor_lr_step1();
time_delay_ms(SPEED);
left_count ++;
}
return 0;
}
/*
* rotate left and up
* @param count: the angle of any rotation
* return 0:successfully rotated
* return 1:cannot rotate
*/
static int motor_rotate_lefup(unsigned char count)
{
unsigned char i;
int left, up;
for (i = 0;i < count;i++)
{
left = motor_rotate_left(1);
up = motor_rotate_up(1);
if (left && up)
return 1;
}
return 0;
}
/*
* rotate right and up
* @param count: the angle of any rotation
* return 0:successfully rotated
* return 1:cannot rotate
*/
static int motor_rotate_rigup(unsigned char count)
{
unsigned char i;
int right,up;
for (i = 0;i < count;i++)
{
right = motor_rotate_right(1);
up = motor_rotate_up(1);
if (right && up)
return 1;
}
return 0;
}
/*
* rotate left and down
* @param count: the angle of any rotation
* return 0:successfully rotated
* return 1:cannot rotate
*/
static int motor_rotate_lefdown(unsigned char count)
{
unsigned char i;
int left,down;
for (i = 0;i < count;i++)
{
left = motor_rotate_left(1);
down = motor_rotate_down(1);
if (left && down)
return 1;
}
return 0;
}
/*
* rotate right and down
* @param count: the angle of any rotation
* return 0:successfully rotated
* return 1:cannot rotate
*/
static int motor_rotate_rigdown(unsigned char count)
{
unsigned char i;
int right, down;
for (i = 0;i < count;i++)
{
right = motor_rotate_right(1);
down = motor_rotate_down(1);
if (right && down)
return 1;
}
return 0;
}
/*
* rotate to the most left
*/
static void motor_to_left(void)
{
while(1)
{
if (motor_rotate_left(1))
{
break;
}
}
return;
}
/*
* rotate to the most right
*/
static void motor_to_right(void)
{
while(1)
{
if (motor_rotate_right(1))
{
break;
}
}
return;
}
/*
* rotate to the most up
*/
static void motor_to_up(void)
{
while(1)
{
if (motor_rotate_up(1))
{
break;
}
}
return;
}
/*
* rotate to the most down
*/
static void motor_to_down(void)
{
while(1)
{
if (motor_rotate_down(1))
{
break;
}
}
return;
}
/*
* rotate to the most left and up
*/
static void motor_to_rigdown(void)
{
while(1)
{
if ( motor_rotate_rigdown(1))
{
break;
}
}
return;
}
/*
* rotate to the most left and up
*/
static void motor_to_lefup(void)
{
while(1)
{
if ( motor_rotate_lefup(1))
{
break;
}
}
return;
}
/*
* motor self check routine
*/
static void self_check(void)
{
unsigned char i;
motor_to_rigdown();
motor_to_lefup();
for (i = 0;i < 195;i++)
{
motor_rotate_right(1);
if (i < 65)
{
motor_rotate_down(1);
}
}
}
/*
* open routine.
* do nothing.
*/
int motor_open(struct inode * inode, struct file * file)
{
return 0;
}
/*
* close routine.
* do nothing.
*/
int motor_close(struct inode * inode, struct file * file)
{
return 0;
}
/*
* iotcl routine.
*/
int motor_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
void __user *argp = (void __user *)arg;
unsigned long val;
unsigned int count;
unsigned int left,up;
if(copy_from_user(&val,argp,sizeof(val)))
{
printk("\tMOTOR_ERROR:wrong copy value from user!\n");
return -1;
}
switch(cmd)
{
case DIRECTION_UP:
{
count = val & 0xffff;
motor_rotate_up(count);
return 0;
}
case DIRECTION_DOWN:
{
count = val & 0xffff;
motor_rotate_down(count);
return 0;
}
case DIRECTION_LEFT:
{
count = val & 0xffff;
motor_rotate_left(count);
return 0;
}
case DIRECTION_RIGHT:
{
count = val & 0xffff;
motor_rotate_right(count);
return 0;
}
case DIRECTION_RUP:
{
count = val & 0xffff;
motor_rotate_rigup(count);
return 0;
}
case DIRECTION_LUP:
{
count = val & 0xffff;
motor_rotate_lefup(count);
return 0;
}
case DIRECTION_RDOWN:
{
count = val & 0xffff;
motor_rotate_rigdown(count);
return 0;
}
case DIRECTION_LDOWN:
{
count = val & 0xffff;
motor_rotate_lefdown(count);
return 0;
}
case MOST_UP:
{
motor_to_up();
return 0;
}
case MOST_DOWN:
{
motor_to_down();
return 0;
}
case MOST_LEFT:
{
motor_to_left();
return 0;
}
case MOST_RIGHT:
{
motor_to_right();
return 0;
}
case GET_PREPOSITION:
{
val = (left_count << 16) & up_count;
return copy_to_user(argp, &val, sizeof(val)) ? -1 : 0;
}
case SELF_CHECK:
{
self_check();
return 0;
}
case TO_PREPOSITION:
{
left = (val & 0xffff0000) >> 16;
up = val & 0x0000ffff;
motor_to_lefup();
motor_rotate_right(left);
motor_rotate_down(up);
return 0;
}
default:
printk("MOTOR_ERROR:cmd error!\n");
return -1;
}
}
static struct file_operations motor_fops = {
.owner = THIS_MODULE,
.open = motor_open,
.release = motor_close,
.ioctl = motor_ioctl,
};
static struct miscdevice motor_dev = {
MISC_DYNAMIC_MINOR,
"moterdev",
&motor_fops,
};
static unsigned int gpioinitialized =0;
/*
* initializes motor devices routine.
* @return value:0--success; 1--error.
*/
static int __init gpio_motor_init(void)
{
int ret;
unsigned char regvalue;
ret = misc_register(&motor_dev);
if(ret)
{
printk("\tMOTOR_ERROR: could not register motor devices. \n");
return ret;
}
if(gpioinitialized == 0)
{
gpio0_map_addr = (unsigned int*)ioremap(GPIO0_BASE,PAGE_SIZE);
gpio1_map_addr = (unsigned int*)ioremap(GPIO1_BASE,PAGE_SIZE);
if((gpio0_map_addr<0)||(gpio1_map_addr<0))
{
printk("NO memory!\n");
return -EFAULT;
}
GPIO0_BASE_MMAP = (unsigned int)gpio0_map_addr;
GPIO1_BASE_MMAP = (unsigned int)gpio1_map_addr;
regvalue = HW_REG(GPIO0_DIR);
regvalue |= 0xFF;
HW_REG(GPIO0_DIR) = regvalue;
HW_REG(GPIO0_DATA_UPDOWN) = 0x00;
HW_REG(GPIO0_DATA_LEFTRIGHT) = 0x00;
self_check();
gpioinitialized =1;
printk(KERN_INFO "motor Driver init successful!\n");
return 0;
}
else
{
printk("motor Driver has been initialized.\n");
return 0;
}
}
static void __exit gpio_motor_exit(void)
{
iounmap((void *)gpio0_map_addr);
iounmap((void *)gpio1_map_addr);
misc_deregister(&motor_dev);
gpioinitialized =0;
}
module_init(gpio_motor_init);
module_exit(gpio_motor_exit);
#ifdef MODULE
#include <linux/compile.h>
#endif
MODULE_INFO(build, UTS_VERSION);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zzvcom");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -