📄 stepmotor.c
字号:
/* mgc270_stepmotor.c MagicARM270 Stepmotir driver. GUANGZHOU ZHIYUAN Copyright (c) 2006 GUANGZHOU ZHIYUAN ELECTRONICS CO.LTD By Chenxibing <Linux@zlgmcu.com> */#include <linux/device.h>#include <linux/fs.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/platform_device.h>#include <asm/uaccess.h>#include <linux/miscdevice.h>#include <linux/types.h>#include <asm/arch/hardware.h>#include <asm/arch/pxa-regs.h>#include <linux/delay.h>// direction speed stepsstatic char ctrl[3];spinlock_t lock;static int mgc270_stepmotor_ctrl[] = { 81, //---A 82, //---B 83, //---C 84, //---D};static int mgc270_stepmotor_open(struct inode *inode, struct file *file){ unsigned int i; /* Set ALl pins GPO Low */ for( i = 0; i < 4; i++) pxa_gpio_mode(mgc270_stepmotor_ctrl[i] | GPIO_OUT | GPIO_DFLT_LOW); printk("mgc270-stepmotor opened!\n"); return 0;}static int mgc270_stepmotor_release(struct inode *inode, struct file *file){ unsigned int i; /* Set ALl pins GPO Low */ for( i = 0; i < 4; i++) pxa_gpio_mode(mgc270_stepmotor_ctrl[i] | GPIO_OUT | GPIO_DFLT_LOW); printk("mgc270-stepmotor released!\n"); return 0;}static ssize_t mgc270_stepmotor_write(struct file *file, const char __user *buffer, size_t len, loff_t *ppos){ unsigned char n, i; unsigned long flags; printk("mgc270-stepmotor write!\n"); spin_lock_irqsave(&lock, flags) ; if (copy_from_user(&ctrl, buffer, sizeof(ctrl)) ) return -EFAULT; printk("ctrl data is %d %d %d \n", ctrl[0]-0x30, ctrl[1]-0x30, ctrl[2]-0x30); for( n = 0; n < ctrl[2]-0x30; n++ ) { if(!(ctrl[0]-0x30)) //anticlockwise { // printk("ANTI CLOCKWISE!!\n"); for( i = 0; i < 4; i++ ) { GPCR2 |= 0x0F << 17; GPSR2 |= 3 << ( 17 + i ); if( i == 3 ) GPSR2 |= 1 << 17; mdelay((ctrl[1]-0x30)*10); } } else { //clockwise // printk("CLOCK WISE!!\n"); for( i = 0; i < 4; i++ ) { GPSR2 |= 0x0F << 17; GPCR2 |= 3 << ( 20 - i ); if( i == 3 ) GPCR2 |= 1 << 20; mdelay((ctrl[1]-0x30)*10); } } } spin_unlock_irqrestore(&lock, flags); return sizeof(ctrl);}static int mgc270_stepmotor_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ printk("mgc270-stepmotor ioctl\n"); /* you can write code for ioctl() here */ return 0;}static const struct file_operations mgc270_stepmotor_fops = { .owner = THIS_MODULE, .write = mgc270_stepmotor_write, .ioctl = mgc270_stepmotor_ioctl, .open = mgc270_stepmotor_open, .release = mgc270_stepmotor_release,};static struct miscdevice mgc270_stepmotor_miscdev ={ .minor = MISC_DYNAMIC_MINOR, .name = "mgc270-stepmotor", .fops = &mgc270_stepmotor_fops};static int mgc270_stepmotor_probe(struct platform_device *dev){ int ret; printk("probing mgc270-stepmotor!\n"); ret = misc_register(&mgc270_stepmotor_miscdev); if (ret) printk("Failed to register miscdev for MagicARM270 stepmotor.\n"); return ret;}static int mgc270_stepmotor_remove(struct platform_device *dev){ misc_deregister(&mgc270_stepmotor_miscdev); printk("mgc270-stepmotor removed!\n"); return 0;}struct platform_device *mgc270_stepmotor_device;static struct platform_driver mgc270_stepmotor_driver = { .driver = { .name = "mgc270-stepmotor", .owner = THIS_MODULE, }, .probe = mgc270_stepmotor_probe, .remove = mgc270_stepmotor_remove,};static int __init mgc270_stepmotor_init(void){ int rc; printk("mgc270-stepmotor init......\n"); mgc270_stepmotor_device = platform_device_alloc("mgc270-stepmotor", -1); if (!mgc270_stepmotor_device) return -ENOMEM; rc = platform_device_add(mgc270_stepmotor_device); if (rc < 0) { platform_device_put(mgc270_stepmotor_device); return rc; } rc = platform_driver_register(&mgc270_stepmotor_driver); if (rc < 0) platform_device_unregister(mgc270_stepmotor_device); spin_lock_init(&lock); return rc; }static void __exit mgc270_stepmotor_exit(void){ platform_driver_unregister(&mgc270_stepmotor_driver); platform_device_unregister(mgc270_stepmotor_device); printk("mgc270-stepmotor exit!\n");}module_init(mgc270_stepmotor_init);module_exit(mgc270_stepmotor_exit);MODULE_AUTHOR("Abing <Linux@zlgmcu.com>");MODULE_DESCRIPTION("ZHIYUAN MagicARM270 stepmotor Driver");MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -