⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 motor.c

📁 周立功PXA270教学实验箱的motor驱动源代码
💻 C
字号:
/* mgc270_motor.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>

#define  PWM_CLK      	260000	
#define  CYCLE          (PWM_CLK/1000)

// direction speed
static char ctrl[2];

static int mgc270_motor_ctrl[] = {
	17,	//---   A   PWM1
	89,	//---   B   I/O
};
static int mgc270_motor_open(struct inode *inode, struct file *file)
{
	/* Set ALl pins GPO Low */
	pxa_gpio_mode(mgc270_motor_ctrl[0] | GPIO_OUT | GPIO_DFLT_LOW);
	pxa_gpio_mode(GPIO17_PWM1_MD);
	
	printk("mgc270-motor opened!\n");
	return 0;
}

static int mgc270_motor_release(struct inode *inode, struct file *file)
{
 	unsigned int i;
	
	/* Set ALl pins GPO Low */
	for( i = 0; i < 2; i++)
		pxa_gpio_mode(mgc270_motor_ctrl[i] | GPIO_OUT | GPIO_DFLT_LOW);
	
	printk("mgc270-motor released!\n");
	return 0;
}

static ssize_t mgc270_motor_write(struct file *file, const char __user *buffer, size_t len, loff_t *ppos)
{
	unsigned int duty;

	printk("mgc270-motor write!\n");

	if (copy_from_user(&ctrl, buffer, sizeof(ctrl)) ) return -EFAULT;

	printk("ctrl data is %d %d\n", ctrl[0]-0x30, ctrl[1]-0x30);

	if( !(ctrl[0] - 0x30) )
	{	// anticlockwise
		pxa_gpio_mode(mgc270_motor_ctrl[0] | GPIO_OUT | GPIO_DFLT_LOW);
		pxa_gpio_mode(mgc270_motor_ctrl[1] | GPIO_OUT | GPIO_DFLT_HIGH);
		mdelay(2000);
	} else {
		// clockwise PWM adjust
		pxa_gpio_mode(mgc270_motor_ctrl[0] | GPIO_OUT | GPIO_DFLT_HIGH);
		pxa_gpio_mode(GPIO17_PWM1_MD);
		
		//init PWM Ctrolort
		PWM_CTRL1 = (1<<6) | 49;
		PWM_PERVAL1 = CYCLE-1;
		duty = (CYCLE/10)*(ctrl[1]-0x30);
		PWM_PWDUTY1 = (0<<10) | (duty);			
		CKEN |= CKEN1_PWM1;
		mdelay(2000);
	}

	return sizeof(ctrl);
}

static int mgc270_motor_ioctl(struct inode *inode, struct file *file,
                        unsigned int cmd, unsigned long arg)
{
	printk("mgc270-motor ioctl\n");
	/* you can write code for ioctl() here */
	return 0;
}

static const struct file_operations mgc270_motor_fops = {
	.owner   = THIS_MODULE,
	.write   = mgc270_motor_write,
	.ioctl	 = mgc270_motor_ioctl,
	.open    = mgc270_motor_open,
	.release = mgc270_motor_release,
};

static struct miscdevice mgc270_motor_miscdev =
{
	 .minor	= MISC_DYNAMIC_MINOR,
	 .name	= "mgc270-motor",
	 .fops	= &mgc270_motor_fops
};


static int mgc270_motor_probe(struct platform_device *dev)
{
	int ret;

	printk("probing mgc270-motor!\n");
	ret = misc_register(&mgc270_motor_miscdev);
	if (ret)
		printk("Failed to register miscdev for MagicARM270 motor.\n");

	return ret;
}

static int mgc270_motor_remove(struct platform_device *dev)
{
	misc_deregister(&mgc270_motor_miscdev);
	printk("mgc270-motor removed!\n");
	
	return 0;
}

struct platform_device *mgc270_motor_device;

static struct platform_driver mgc270_motor_driver = {
        .driver = {
                .name    = "mgc270-motor",
                .owner   = THIS_MODULE,
        },
        .probe   = mgc270_motor_probe,
        .remove  = mgc270_motor_remove,
};


static int __init mgc270_motor_init(void)
{
	int rc;

	printk("mgc270-motor init......\n");
	mgc270_motor_device = platform_device_alloc("mgc270-motor", -1);
        if (!mgc270_motor_device)
                return -ENOMEM;

	rc = platform_device_add(mgc270_motor_device);
        if (rc < 0) {
                platform_device_put(mgc270_motor_device);
                return rc;
        }

	rc = platform_driver_register(&mgc270_motor_driver);
        if (rc < 0)
                platform_device_unregister(mgc270_motor_device);

        return rc;	
}

static void __exit mgc270_motor_exit(void)
{
        platform_driver_unregister(&mgc270_motor_driver);
        platform_device_unregister(mgc270_motor_device);
	printk("mgc270-motor exit!\n");
}

module_init(mgc270_motor_init);
module_exit(mgc270_motor_exit);

MODULE_AUTHOR("Abing <Linux@zlgmcu.com>");
MODULE_DESCRIPTION("ZHIYUAN MagicARM270 motor Driver");
MODULE_LICENSE("GPL");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -