Wednesday, April 19, 2006

Stepper motor driver GNU/Linux 2.6 kernel

I started a work on some project related to Stepper Motor Driver in Linux. I found one tutorial at linux gazette online magazine. But to my surprise the driver written was for 2.4.x kernel's. I soon realised that I need to do pretty much stuff in order to make that driver work in 2.6.x kernel. Then again to my surprise I found a excellent tutorial at tldp explaining the 2.6.x and 2.4.x kernel module programming. I went through those tutorials and I ported the driver for 2.6.x kernel.
I used a debian 3.1 sarge with 2.6.8 kernel in it. I started off with some basic differences (between 2.4.x and 2.6.x). Code writing convention has change alot and these days module programmer prefer to use macro's more than anything. They even have macros for defining the init function and the exit function. I went on to learn those things and finally came up with a code which I suppose will work in most of the 2.6.x's series.
Here I present the code:

#include "linux/kernel.h"
#include "linux/fs.h"
#include "linux/module.h"
#include "linux/init.h"
#include "asm/io.h"
#include "asm/uaccess.h"

#define LPT_BASE 0x378
#define DEVICE_NAME "Stepper"
#define DRIVER_DESC "*Stepper Motor Driver*"

static int Major;
static int Device_Open=0;
static int i,j,k=0;
static int pattern[2][4][8] = {

void step(void)
printk(KERN_INFO "%d\n",pattern[i][j][k]);
printk(KERN_INFO "%d\n",pattern[i][j][k]);
static int stepper_open(struct inode *inode, struct file *file)
printk(KERN_INFO "Stepper Opened\n");
return -EBUSY;
return 0;
static int stepper_release(struct inode *inode, struct file *file)
printk(KERN_INFO "Stepper Released\n");
Device_Open --;
return 0;
static ssize_t stepper_write(struct file *file, const char *buffer, size_t len, loff_t *off)
char data;
switch (data)
case 'h': /* Half Step */
printk(KERN_INFO "Half-Step mode initialized\n");
case 'f': /* Full Step */
printk(KERN_INFO "Full-Step mode initialized\n");
case 'F': /* Forward */
case 'R': /* Reverse */
default: /* Invalid */
printk(KERN_INFO "Invalid argument\n");
return -EINVAL;
struct file_operations fops = {
static int __init entry(void)
if(Major < 0)
printk(KERN_INFO "Cannot get major number for the device %s\n",DEVICE_NAME);
return Major;
printk(KERN_INFO "'mknod /dev/%s c %d 0'\n",DEVICE_NAME,Major);
return 0;
static void __exit bye(void)
int ret = unregister_chrdev(Major, DEVICE_NAME);
if (ret < 0)
printk(KERN_INFO "Error in un-registering stepper: %d\n", ret);
printk(KERN_INFO "Stepper un-registered.\n");

The messages logged by this driver can be found in /var/log/messages file. Anytime we need to see what the program does or what it outputs we need to open the file and go down the file to see the recent outputs. This can be made easy by issuing the following command:

tail -f /var/log/messages

As a super user now we need to execute the below command on the terminal. Here the major number is the number allocated by the kernel when the module get's inserted into it. It's a dynamic number. Minor number is mostly 0 (in this case).
In my case I had to execute the following command:

mknod /dev/Stepper c 254 0

This create's a device file in the /dev directory and now if we want to communicate to the motor all we need to do is write data onto this file. That can be done by simply using:

echo "h" > /dev/Stepper
echo "F" > /dev/Stepper
(NOTE: What's happening when these commands execute can be see by tail -f /var/log/messages)

Here data to the stepper device is any valid character in the set {h,f,F,R}. First two elements of the set intializes the motor in half-step or full-step mode and the last two data set actually runs the motor in Forward or Reverse directions.

In the next article I will explain the user interface program I created to handle this motor. I used the power of GTK+ in creating that interface.

There maybe many points I have left in this article regarding the internal working of the program. For that I assume you have gone through the page .


Eric said...

Thanks for this post !!
I'm musician in france and going to use stepper motors to have light animation on stage.

I've read that you plan to make a GUI for this.

But i connot find it, have you already done this ? Do you know where can I find another one, if yours is not done ?


Anonymous said...

I don’t know If I said it already but …I’m so glad I found this site…Keep up the good work I read a lot of blogs on Viagra Online a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say great blog. Thanks!

Ron said...

Thank you
I have been trying to work this out for ages. I can now get on with my stepper project.


Isma said...

Hello .. firstly I would like to send greetings to all readers. After this, I recognize the content so interesting about this article. For me personally I liked all the information. I would like to know of cases like this more often. In my personal experience I might mention a book called Generic Viagra in this book that I mentioned have very interesting topics, and also you have much to do with the main theme of this article.

willy said...

To start with I would like to congratulate the cradores of this blog, mainly because when I read it I enjoyed it very much. A few years ago I attended a conference called guanacaste costa rica real estate, at that conference had many interesting topics. Perhaps readers may find no relationship between the blog and this conference, but if someday can attend, would realize that there is much to do with this blog.

Anonymous said...

Like you I experienced the same situation back then, due to a physical problem that I had by consuming by mistake generic viagra, I was able to solve the problem after I recovered form that episode.