Step Motor Device Driver Embedded System Lab. II
Step Motor Step Motor Step Motor source Embedded System Lab. II 2
open loop, : : Pulse, 1 Pulse,, -, 1 +5%, step Step Motor (2),, Embedded System Lab. II 3
Step Motor Step Motor (1), 2~6 Embedded System Lab. II 4
STEP MOTOR Step Motor,,, Embedded System Lab. II 5
PXA255-Pro Step Motor Embedded System Lab. II 6
Step Motor Data bit 8bit PXA255 FPGA 1 Bit 7 6 5 4 3 2 1 Step Motor nb B na step2 A nb B na step1 A Embedded System Lab. II 7
Half Step Operation 1-2-1-2 1/2, Sequence A na Input B nb Output 1 1(H) (L) (L) (L) A 2 1 B 3 1 na 4 1 nb Embedded System Lab. II 8
(2) Full Step Operation 4 2. (Torque) 1Cm 1m () (1) 1Cm 1m ( : gfcm Kgfcm ) Sequence A na Input B nb Output 1 1(H) (L) 1(H) (L) AB 2 1 1 nab 3 1 1 nanb 4 1 1 AnB Embedded System Lab. II 9
Device driver source code #include <linux/module.h> #include <asm/hardware.h> #include <asm/uaccess.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/types.h> #include <asm/ioctl.h> #include <linux/ioport.h> #include <linux/delay.h> #include <asm/io.h> #include <linux/init.h> #include <linux/version.h> // ex)mod_inc_usr_count //copy_from_user() //udelay() //GPIO controller //init_module() cleanup_module() #define IOM_STEP_MAJOR 247 //define major number #define IOM_STEP_NAME "STEP" //define device name #define IOM_STEP_ADDRESS xcc Embedded System Lab. II 1
Device driver source code (2) #define A #define AB #define B #define _AB #define _A #define _A_B #define _B #define A_B (unsigned short)(x1) (unsigned short)(x5) (unsigned short)(x4) (unsigned short)(x6) (unsigned short)(x2) (unsigned short)(xa) (unsigned short)(x8) (unsigned short)(x9) #define iom_step_init init_module int iom_step_open(struct inode *, struct file *); int iom_step_release(struct inode *, struct file *); ssize_t iom_step_write(struct file *, const char *, size_t, loff_t *); int init iom_step_init(void); void cleanup_module(void); Embedded System Lab. II 11
Device driver source code (3) //Global variable static int step_usage = ; static int step_major = ; static char mode; static int check = ; static unsigned short *iom_step_addr; //file operation structure static struct file_operations iom_step_fops = { open: iom_step_open, write: iom_step_write, release: iom_step_release, }; Embedded System Lab. II 12
Device driver source code (4) into iom_step_open(struct inode *minode, struct file *mfile) { if(step_usage!= ) return -EBUSY; } MOD_INC_USE_COUNT; step_usage = 1; check = ; return ; int iom_step_release(struct inode *minode, struct file *mfile) { MOD_DEC_USE_COUNT; step_usage = ; outw(,iom_step_addr); return ; } Embedded System Lab. II 13
Device driver source code (5) size iom_step_write(struct file *inode, const char *gdata, size_t length, loff_t *off_what) { if(check == ) {// check const char *temp = gdata; copy_from_user(&mode, temp, 1); check = 1; } else if(check ==1) {// Step Motor const char *tmp = gdata; unsigned short speed; #if copy_from_user(&speed, tmp, 2); //Full Step Operation outw(ab,iom_step_addr); udelay(speed); Embedded System Lab. II 14
Device driver source code (6) #else outw(_ab,iom_step_addr); udelay(speed); outw(_a_b,iom_step_addr); udelay(speed); outw(a_b,iom_step_addr); udelay(speed); //Half Step Operation if(mode == 'a'){// outw(a,iom_step_addr); udelay(speed); outw(b,iom_step_addr); udelay(speed); outw(_a,iom_step_addr); udelay(speed); outw(_b,iom_step_addr); udelay(speed); } Embedded System Lab. II 15
Device driver source code (7) else if(mode == 'b'){// outw(_b,iom_step_addr); udelay(speed); outw(_a,iom_step_addr); udelay(speed); outw(b,iom_step_addr); udelay(speed); outw(a,iom_step_addr); udelay(speed); } #endif } return length; } Embedded System Lab. II 16
Device driver source code (8) into init iom_step_init(void) { int result; result = register_chrdev(iom_step_major,iom_step_name,&iom_step_fops); if(result < ) { printk(kern_warning"can't get any major\n"); return result; } step_major = IOM_STEP_MAJOR; iom_step_addr = ioremap(iom_step_address,x2); printk("init module, %s major number : %d\n",iom_step_name,step_major); return ; } void cleanup_module(void) { iounmap(iom_step_addr); if(unregister_chrdev(step_major,iom_step_name)) printk(kern_warning"%s DRIVER CLEANUP FALLED\n",IOM_STEP_NAME); } Embedded System Lab. II 17
Application source code #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/ioctl.h> #include <signal.h> static char mode; into main(void) { int fd; unsigned short c = xffff; printf("a. spin\n");//menu printf("b. back spin\n"); printf("select->"); scanf("%c", &mode); printf("\n"); Embedded System Lab. II 18
Application source code (2) } while(1){ fd = open("/dev/step",o_wronly);//divice open if (fd < ){ printf("device Open Error\n"); exit(1);} write(fd, &mode, 1);//write mode for(;;){ if(c < x1){ c = x1;// } else c -=x1;//increase speed write(fd,&c,2);//write speed } close(fd); } Embedded System Lab. II 19
Makefile INCLUDEDIR := /home/max233/linux-2.4.19-cd/include CFLAGS := -D KERNEL -I$(INCLUDEDIR) -Wall -O2 -DMODULE CROSS_COMPILE := arm-linux- CC=$(CROSS_COMPILE)gcc LD=$(CROSS_COMPILE)ld all: step_driver test_step step_driver: $(CC) $(CFLAGS) -c step_driver.c -o step_driver.o test_step: $(CC) -I$(INCLUDEDIR) -o test_step test_step.c clean: rm -f *.o rm -f test_step Embedded System Lab. II 2
Driver Step Motor Device Driver device driver file interface (node ) Device driver major number : int register_chrdev(unsigned int major, const char *name, struct file_operations *fops) Major : major number. Name : device Fops : device file : int unregister_chrdev(unsigned int major, const char *name) Embedded System Lab. II 21
Driver (2) kernel user program open close read write system call device driver file operations device_open device_close device_read device_write device Embedded System Lab. II 22
Driver (3) Major number minor number Major number : Minor number : major number register_chrdev() major number major number Major minor inode i_rdev 16bit. 8bit major, 8bit minor. Embedded System Lab. II 23
Driver (4) mknod mknod [device file name] [type] [major] [minor] Ex] %mknod /dev/step c 247 mdev_t : major, minor number MAJOR() : kdev_t major number Ex] MAJOR(inode->i_rdev); MINOR() : kdev_t minor number cat /proc/devices Embedded System Lab. II 24
Driver (5) insmod module (install) rmmod modules (unload) lsmod Load module depmod modprobe Module symbol Makefile dependency file depmod dependency module module load modinfo Embedded System Lab. II 25
Driver (6) Device Driver Embedded System Lab. II 26
Driver (7) step_driver.c, test application test_step.c, makefile Makefile make 2 files % make step_driver.o test_step target. minicom nfs. nfs Embedded System Lab. II 27
Driver (8) Step Motor Embedded System Lab. II 28