Step Motor. Step Motor Device Driver. Step Motor. Step Motor (2) Step Motor. Step Motor. source. open loop,

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Step Motor. Step Motor Device Driver. Step Motor. Step Motor (2) Step Motor. Step Motor. source. open loop,"

Transcription

1 Step Motor Device Driver Step Motor Step Motor Step Motor source Embedded System Lab. II Embedded System Lab. II 2 Step Motor (2) open loop, : : Pulse, Pulse,, -, +5%, step,, Step Motor Step Motor ( ), 2 ~6 Embedded System Lab. II 3 Embedded System Lab. II 4

2 STEP MOTOR PXA255-Pro Step Motor Step Motor,,, Embedded System Lab. II 5 Embedded System Lab. II 6 Step Motor Data bit 8bit PXA255 FPGA it Half Step Operation /2, Step Motor n step2 A n step A Sequence A Input n Output (H) (L) (L) (L) A n Embedded System Lab. II 7 Embedded System Lab. II 8

3 (2) Device driver source code Full Step Operation 4 2. (Torque) Cm m ( ) ( ) Cm m ( : gfcm Kgfcm ) Sequence A (H) (L) Input (H) n (L) Output A n An #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 9 Embedded System Lab. II Device driver source code (2) Device driver source code (3) #define A #define A #define #define _A #define _A #define _A_ #define _ #define A_ #define iom_step_init (unsigned short)(x) (unsigned short)(x5) (unsigned short)(x4) (unsigned short)(x6) (unsigned short)(x2) (unsigned short)(xa) (unsigned short)(x8) (unsigned short)(x9) 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); //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 Embedded System Lab. II 2

4 Device driver source code (4) Device driver source code (5) into iom_step_open(struct inode *minode, struct file *mfile) if(step_usage!= ) return -EUSY; MOD_INC_USE_COUNT; step_usage = ; 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 3 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, ); check = ; else if(check ==) // Step Motor const char *tmp = gdata; unsigned short speed; #if copy_from_user(&speed, tmp, 2); //Full Step Operation outw(a,iom_step_addr); Embedded System Lab. II 4 Device driver source code (6) Device driver source code (7) #else outw(_a,iom_step_addr); outw(_a_,iom_step_addr); outw(a_,iom_step_addr); //Half Step Operation if(mode == 'a')// outw(a,iom_step_addr); outw(,iom_step_addr); outw(_a,iom_step_addr); outw(_,iom_step_addr); else if(mode == 'b')// outw(_,iom_step_addr); outw(_a,iom_step_addr); outw(,iom_step_addr); outw(a,iom_step_addr); #endif return length; Embedded System Lab. II 5 Embedded System Lab. II 6

5 Device driver source code (8) Application source code 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 7 #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 8 Application source code (2) Makefile while() fd = open("/dev/step",o_wronly);//divice open if (fd < ) printf("device Open Error\n"); exit(); write(fd, &mode, );//write mode for(;;) if(c < x) c = x;// else c -=x;//increase speed write(fd,&c,2);//write speed close(fd); INCLUDEDIR := /home/max233/linux 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 9 Embedded System Lab. II 2

6 Driver Driver (2) 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 user program open close read write system call kernel device driver file operations device_open device_close device_read device_write : int unregister_chrdev(unsigned int major, const char *name) device Embedded System Lab. II 2 Embedded System Lab. II 22 Driver (3) Driver (4) Major number minor number Major number : Minor number : major number register_chrdev() major number major number Major minor inode i_rdev 6bit. 8bit major, 8bit minor. 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 23 Embedded System Lab. II 24

7 Driver (5) Driver (6) Device Driver insmod module (install) rmmod modules (unload) lsmod depmod modprobe modinfo Load module Module symbol Makefile dependency file depmod dependency module module load Embedded System Lab. II 25 Embedded System Lab. II 26 Driver (7) Driver (8) step_driver.c, test application test_step.c, makefile Makefile make 2 files % make step_driver.o test_step target. minicom nfs. nfs Step Motor Embedded System Lab. II 27 Embedded System Lab. II 28