【linux驱动】-基于s5p6818智能车电机驱动
基于s5p6818的arm cortexA53智能车电机驱动
#include
#include
#include
#include
#include
#include
#include
#include #define DEVICE_NAME "tcar"#define KP_ROW0 S5P6818_GPH3(0)
#define KP_ROW1 S5P6818_GPH3(1)
#define KP_ROW2 S5P6818_GPH3(2)
#define KP_COL0 S5P6818_GPH2(0)
#define KP_COL1 S5P6818_GPH2(1)
#define KP_COL2 S5P6818_GPH2(2)#define TCAR_ENA (KP_ROW2)
#define TCAR_IN1 (KP_COL2)
#define TCAR_IN2 (KP_ROW1)
#define TCAR_IN3 (KP_COL1)
#define TCAR_IN4 (KP_ROW0)
#define TCAR_ENB (KP_COL0)#define TCAR_FORWARD (0x2001)
#define TCAR_BACKWARD (0x2002)
#define TCAR_LEFT (0x2003)
#define TCAR_RIGHT (0x2004)
#define TCAR_STOP (0x2005)struct timer_list tcar_timer;/*左右转动时间,单位毫秒,时间越大转动越明显,但过大容易打坏变速齿轮*/
#define TCAR_TIMER 10typedef struct _tcar_gpios {/* TCAR PIN output port */unsigned int gpio_no;const char *name;unsigned int cfg_val;unsigned int pin_val;
} tcar_gpios_t;tcar_gpios_t gpio_pins[] = {{.gpio_no = TCAR_ENA,.name = "GPH3_2",.cfg_val = S3C_GPIO_SFN(1),.pin_val = 1,},{.gpio_no = TCAR_IN1,.name = "GPH2_2",.cfg_val = S3C_GPIO_SFN(1),.pin_val = 1,},{.gpio_no = TCAR_IN2,.name = "GPH3_1",.cfg_val = S3C_GPIO_SFN(1),.pin_val = 1,},{.gpio_no = TCAR_IN3,.name = "GPH2_1",.cfg_val = S3C_GPIO_SFN(1),.pin_val = 1,},{.gpio_no = TCAR_IN4,.name = "GPH3_0",.cfg_val = S3C_GPIO_SFN(1),.pin_val = 1,},{.gpio_no = TCAR_ENB,.name = "GPH2_0",.cfg_val = S3C_GPIO_SFN(1),.pin_val = 1,}
};static void tcar_gpio_init(void)
{int i;int pins_num;int ret;printk(KERN_INFO " %s\n", __FUNCTION__);pins_num = ARRAY_SIZE(gpio_pins);for (i = 0; i < pins_num; i++) {if (gpio_is_valid(gpio_pins[i].gpio_no)) {ret = gpio_request(gpio_pins[i].gpio_no, gpio_pins[i].name);if (ret) {printk(KERN_ERR "failed to get GPIO for TCAR\n");}s3c_gpio_cfgpin(gpio_pins[i].gpio_no, gpio_pins[i].cfg_val);gpio_direction_output(gpio_pins[i].gpio_no, gpio_pins[i].pin_val);}}printk(KERN_INFO " %s end\n", __FUNCTION__);return;
}static void tcar_forward(void){gpio_direction_output(gpio_pins[0].gpio_no,1);gpio_direction_output(gpio_pins[1].gpio_no,1);gpio_direction_output(gpio_pins[2].gpio_no,0);return;
}
static void tcar_backward(void){gpio_direction_output(gpio_pins[0].gpio_no,1);gpio_direction_output(gpio_pins[1].gpio_no,0);gpio_direction_output(gpio_pins[2].gpio_no,1);return;
}
void tcar_timer_handler(unsigned long data)
{gpio_direction_output(gpio_pins[5].gpio_no,1);gpio_direction_output(gpio_pins[3].gpio_no,0);gpio_direction_output(gpio_pins[4].gpio_no,0);
}static void tcar_left(void){gpio_direction_output(gpio_pins[5].gpio_no,1);gpio_direction_output(gpio_pins[3].gpio_no,1);gpio_direction_output(gpio_pins[4].gpio_no,0);tcar_timer.expires = jiffies + TCAR_TIMER;tcar_timer.function = tcar_timer_handler;tcar_timer.data = 0;//dogadd_timer(&tcar_timer);return;
}
static void tcar_right(void){gpio_direction_output(gpio_pins[5].gpio_no,1);gpio_direction_output(gpio_pins[3].gpio_no,0);gpio_direction_output(gpio_pins[4].gpio_no,1);tcar_timer.expires = jiffies + TCAR_TIMER;tcar_timer.function = tcar_timer_handler;tcar_timer.data = 0;//dogadd_timer(&tcar_timer);return;
}static void tcar_stop(void){gpio_direction_output(gpio_pins[0].gpio_no,1);gpio_direction_output(gpio_pins[5].gpio_no,1);gpio_direction_output(gpio_pins[1].gpio_no,0);gpio_direction_output(gpio_pins[2].gpio_no,0);gpio_direction_output(gpio_pins[3].gpio_no,0);gpio_direction_output(gpio_pins[4].gpio_no,0);return;
}static int tcar_ioctl(struct inode *inode, struct file *file, unsigned int cmd_idx, unsigned long val)
{switch (cmd_idx){case TCAR_FORWARD:tcar_forward();break;case TCAR_BACKWARD:tcar_backward();break;case TCAR_LEFT:tcar_left();break;case TCAR_RIGHT:tcar_right();break;case TCAR_STOP:tcar_stop();break;default:printk("cmd_idx=%d,val=%d\n",cmd_idx,val);break;}return 0;
}static int tcar_open(struct inode *inode, struct file *file)
{return 0;}
static int tcar_close(struct inode *inode, struct file *file)
{return 0;
}static struct file_operations tcar_fops=
{.owner=THIS_MODULE,.ioctl=tcar_ioctl,.open = tcar_open,.release =tcar_close,
};static struct miscdevice tcar_miscdev = {.minor = MISC_DYNAMIC_MINOR,.name = DEVICE_NAME,.fops = &tcar_fops,
};static int tcar_init(void)
{int ret;tcar_gpio_init();ret = misc_register(&tcar_miscdev);if(ret < 0) {printk(DEVICE_NAME "can't register major number\n");return ret;}init_timer(&tcar_timer);return 0;
}static void tcar_exit(void)
{int pins_num;int i;del_timer(&tcar_timer);misc_deregister(&tcar_miscdev);pins_num = ARRAY_SIZE(gpio_pins);for (i = 0; i < pins_num; i++) {if (gpio_is_valid(gpio_pins[i].gpio_no)) {gpio_free(gpio_pins[i].gpio_no);}}
} module_init(tcar_init);
module_exit(tcar_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("tarena");
MODULE_DESCRIPTION("TCAR Driver");
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
