欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

嵌入式linux字符设备驱动--步进电机驱动

程序员文章站 2022-07-14 16:36:25
...

步进电机驱动主要要用到内核定时器,内核定时器就是在内核驱动程序里面定时

stepmotor.c

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/cdev.h>
#include <linux/device.h>


/* 寄存器物理地址 */
#define CCM_CCGR1_BASE (0X020C406C) 
#define SW_MUX_GPIO1_IO04_BASE (0X020E006c)
#define SW_PAD_GPIO1_IO04_BASE (0X020E02F8)
#define SW_MUX_GPIO1_IO05_BASE (0X020E0070)
#define SW_PAD_GPIO1_IO05_BASE (0X020E02Fc)
#define SW_MUX_GPIO1_IO06_BASE (0X020E0074)
#define SW_PAD_GPIO1_IO06_BASE (0X020E0300)
#define SW_MUX_GPIO1_IO07_BASE (0X020E0078)
#define SW_PAD_GPIO1_IO07_BASE (0X020E0304)
#define GPIO1_DR_BASE (0X0209C000)
#define GPIO1_GDIR_BASE (0X0209C004)

/* 映射后的寄存器虚拟地址指针 */
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO04;
static void __iomem *SW_PAD_GPIO1_IO04;
static void __iomem *SW_MUX_GPIO1_IO05;
static void __iomem *SW_PAD_GPIO1_IO05;
static void __iomem *SW_MUX_GPIO1_IO06;
static void __iomem *SW_PAD_GPIO1_IO06;
static void __iomem *SW_MUX_GPIO1_IO07;
static void __iomem *SW_PAD_GPIO1_IO07;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;

static int stepmotor_angle = 0;
#define stepmotor_CNT 1 /* 设备号个数 */
#define stepmotor_NAME "mystepmotor" /* 名字 */

/* stepmotor 设备结构体 */
struct stepmotor_dev{
	dev_t devid; /* 设备号 */
	struct cdev cdev; /* cdev */
	struct class *class; /* 类 */
	struct device *device; /* 设备 */
	int major; /* 主设备号 */
	int minor; /* 次设备号 */
};
 
struct stepmotor_dev stepmotor; /* stepmotor 设备 */
static struct timer_list tm;


static void stepmotor_gpio_config(void)
{
	u32 val = 0;

	/* 初始化 stepmotor */
	/* 1、寄存器地址映射 */
	IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
	SW_MUX_GPIO1_IO04 = ioremap(SW_MUX_GPIO1_IO04_BASE, 4);
	SW_PAD_GPIO1_IO04 = ioremap(SW_PAD_GPIO1_IO04_BASE, 4);
	SW_MUX_GPIO1_IO05 = ioremap(SW_MUX_GPIO1_IO05_BASE, 4);
	SW_PAD_GPIO1_IO05 = ioremap(SW_PAD_GPIO1_IO05_BASE, 4);
	SW_MUX_GPIO1_IO06 = ioremap(SW_MUX_GPIO1_IO06_BASE, 4);
	SW_PAD_GPIO1_IO06 = ioremap(SW_PAD_GPIO1_IO06_BASE, 4);
	SW_MUX_GPIO1_IO07 = ioremap(SW_MUX_GPIO1_IO07_BASE, 4);
	SW_PAD_GPIO1_IO07 = ioremap(SW_PAD_GPIO1_IO07_BASE, 4);
	GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
	GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);

	/* 2、使能 GPIO1 时钟 */
	val = readl(IMX6U_CCM_CCGR1);
	val &= ~(3 << 26); /* 清除以前的设置 */
	val |= (3 << 26); /* 设置新值 */
	writel(val, IMX6U_CCM_CCGR1);

	/* 3、设置 GPIO1_IO04 5 6 7 的复用功能,将其复用为
	 * GPIO1_IO03,最后设置 IO 属性。
	*/
	writel(5, SW_MUX_GPIO1_IO04);
	writel(5, SW_MUX_GPIO1_IO05);
	writel(5, SW_MUX_GPIO1_IO06);
	writel(5, SW_MUX_GPIO1_IO07);

	/* 寄存器 SW_PAD_GPIO1_IO03 设置 IO 属性 */
	writel(0x10B0, SW_PAD_GPIO1_IO04);
	writel(0x10B0, SW_PAD_GPIO1_IO05);
	writel(0x10B0, SW_PAD_GPIO1_IO06);
	writel(0x10B0, SW_PAD_GPIO1_IO07);

	/* 4、设置 GPIO1_IO04 5 6 7 为输出功能 */
	val = readl(GPIO1_GDIR);
	val &= ~((1 << 4)|(1 << 5)|(1 << 6)|(1 << 7)); /* 清除以前的设置 */
	val |= (1 << 4)|(1 << 5)|(1 << 6)|(1 << 7); /* 设置为输出 */
	writel(val, GPIO1_GDIR);

	/* 5、默认关闭 stepmotor */
	val = readl(GPIO1_DR);
	val &= ~((1 << 4)|(1 << 5)|(1 << 6)|(1 << 7)); 
	writel(val, GPIO1_DR);
}

static void stepmotor_call(void)
{
	unsigned long tmp = 0;
	static char steps = 0;
	
#define MOTOR_IO_H(X) {tmp = readl(GPIO1_DR);\
		tmp |= (1 << X);\
		writel(tmp, GPIO1_DR);}
#define MOTOR_IO_L(X) {tmp = readl(GPIO1_DR);\
		tmp &= ~(1 << X);\
		writel(tmp, GPIO1_DR);}
	if(stepmotor_angle > 0)
	{steps++;stepmotor_angle--;}
	else if(stepmotor_angle < 0)
	     {steps--;stepmotor_angle++;}
	     else
		return ;
	if(steps > 3 || steps < 0)
		steps = 0;
	switch(steps)
	{
		case 0:MOTOR_IO_L(4);MOTOR_IO_H(5);MOTOR_IO_H(6);MOTOR_IO_H(7);
			break;
		case 1:MOTOR_IO_H(4);MOTOR_IO_L(5);MOTOR_IO_H(6);MOTOR_IO_H(7);
			break;
		case 2:MOTOR_IO_H(4);MOTOR_IO_H(5);MOTOR_IO_L(6);MOTOR_IO_H(7);
			break;
		case 3:MOTOR_IO_H(4);MOTOR_IO_H(5);MOTOR_IO_H(6);MOTOR_IO_L(7);
			break;
			default:steps = 0;break;
	}
}

void iTimer_callback(unsigned long arg)  
{  
    	//struct timeval tv;  
   	char *strp = (char*)arg;
  
    	printk(KERN_EMERG "%s: %lu, %s\n", __func__, jiffies, strp);  
	stepmotor_call();
    	mod_timer(&tm,jiffies+2*HZ); //使用mod_timer或再次add_timer函数重新触发
    	//tm.expires = jiffies+1*HZ;
    	//add_timer(&tm);
}  

void iTimer_init(void)
{
	init_timer(&tm);  
    	tm.function= iTimer_callback;  
   	tm.data = (unsigned long)"I am timer";
    	tm.expires = jiffies+1*HZ;  
    	add_timer(&tm);  
}

static int stepmotor_open (struct inode * inode, struct file * file)
{
	printk(" device open\n");
	file->private_data = &stepmotor; /* 设置私有数据 */
	return 0;
}

static int stepmotor_release (struct inode * inode, struct file * file)
{
	printk(" device release\n");
	return 0;
}

ssize_t stepmotor_read (struct file *filp, char __user * buf, size_t count, loff_t *ppos)
{
	printk(" device read\n");
	return 0;
}

ssize_t stepmotor_write (struct file *filp, const char __user * buf, size_t cnt, loff_t *ppos)
{
	int retvalue;
	unsigned char databuf[2];
	int stepmotorstat;
	
	retvalue = copy_from_user(databuf, buf, cnt);
	if(retvalue < 0) 
	{
		printk("kernel write faistepmotor!\r\n");
		return -EFAULT;
	}
	stepmotorstat = databuf[0]*256 + databuf[1]; /* 获取状态值 */
	stepmotor_angle = stepmotorstat;

	printk(" device write %d \n",stepmotor_angle);
	return 0;
}

/* 设备操作函数 */
static struct file_operations stepmotor_fops = {
	.owner = THIS_MODULE,
	.open = stepmotor_open,
	.read = stepmotor_read,
	.write = stepmotor_write,
	.release = stepmotor_release,
};

static int __init stepmotor_init(void)
{
	stepmotor_gpio_config();
	iTimer_init();
	
	/* 1、创建设备号 */
	if (stepmotor.major) 
	{ /* 定义了设备号 */
		stepmotor.devid = MKDEV(stepmotor.major, 0);
		register_chrdev_region(stepmotor.devid, stepmotor_CNT, stepmotor_NAME);
	} 
	else 
	{ /* 没有定义设备号 */
		alloc_chrdev_region(&stepmotor.devid, 0, stepmotor_CNT,stepmotor_NAME); /* 申请设备号 */
		stepmotor.major = MAJOR(stepmotor.devid); /* 获取主设备号 */
		stepmotor.minor = MINOR(stepmotor.devid); /* 获取次设备号 */
	}
	printk("newchestepmotor major=%d,minor=%d\r\n",stepmotor.major,stepmotor.minor);

	/* 2、初始化 cdev */
	stepmotor.cdev.owner = THIS_MODULE;
	cdev_init(&stepmotor.cdev, &stepmotor_fops);

	/* 3、添加一个 cdev */
	cdev_add(&stepmotor.cdev, stepmotor.devid, stepmotor_CNT);

	/* 4、创建类 */
	stepmotor.class = class_create(THIS_MODULE, stepmotor_NAME);
	if (IS_ERR(stepmotor.class)) 
	{
		return PTR_ERR(stepmotor.class);
	}
	 
	/* 5、创建设备 */
	stepmotor.device = device_create(stepmotor.class, NULL,
	stepmotor.devid, NULL, stepmotor_NAME);
	if (IS_ERR(stepmotor.device)) 
	{
		return PTR_ERR(stepmotor.device);
	}

	printk(" device insmod ok\n");
	return 0;
}

static void __exit stepmotor_exit(void)
{
	/* 取消映射 */
	iounmap(IMX6U_CCM_CCGR1);
	iounmap(SW_MUX_GPIO1_IO04);
	iounmap(SW_PAD_GPIO1_IO04);
	iounmap(SW_MUX_GPIO1_IO05);
	iounmap(SW_PAD_GPIO1_IO05);
	iounmap(SW_MUX_GPIO1_IO06);
	iounmap(SW_PAD_GPIO1_IO06);
	iounmap(SW_MUX_GPIO1_IO07);
	iounmap(SW_PAD_GPIO1_IO07);
	iounmap(GPIO1_DR);
	iounmap(GPIO1_GDIR);
	
	del_timer(&tm); 
	/* 注销字符设备 */
	cdev_del(&stepmotor.cdev);/* 删除 cdev */
	unregister_chrdev_region(stepmotor.devid, stepmotor_CNT);

	device_destroy(stepmotor.class, stepmotor.devid);
	class_destroy(stepmotor.class);

	printk(" device rmmod ok\n");
}

module_init(stepmotor_init);
module_exit(stepmotor_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("guoguo");

Makefile

#内核源码树路径
KERN_DIR := /home/z/imax6ull/kernel
ARCH=arm
CROSS_COMPILE=arm-linux-gnueabihf-

export  ARCH  CROSS_COMPILE

#目标文件
obj-m += stepmotor.o

all:
	$(MAKE) -C $(KERN_DIR) M=$(CURDIR) modules

.PHONY:clean
clean:
	#$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
	rm -f *.o *.ko *.order *.symvers

应用测试程序stepmotortest.c

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"

static int fd = 0;

void stepmotor_init()
{
	/* 打开 stepmotor 驱动 */
	fd = open("/dev/mystepmotor", O_RDWR);
	if(fd < 0)
	{
		printf("file %s open faistepmotor!\r\n");
		return -1;
	}
}

int stepmotor_set(unsigned int x)
{
	int ret = 0;
	unsigned char buff[2];
	
	buff[0] = x/256;
	buff[1] = x&0xff;
	ret = write(fd, buff, sizeof(buff));
	if(ret < 0)
	{
		printf("stepmotor Control Faistepmotor!\r\n");
		close(fd);
		return -1;
	}
}

int main()
{
	stepmotor_init();
	while(1)
	{
		stepmotor_set(5);
		sleep(10);
		stepmotor_set(8);
		sleep(10);
	}
	close(fd); /* 关闭文件 */
	return 0;
}

编译驱动 make
会在当前目录生成stepmotor.ko

编译应用程序 arm-linux-gnueabihf-gcc stepmotortest.c-o stepmotortest

加载驱动 insmod stepmotor.ko
运行应用测试 ./stepmotortest
嵌入式linux字符设备驱动--步进电机驱动