Using National Instruments USB-6501 with Linux

The NI USB-6501 is a Full-Speed USB 2.0 (12 Mbit/s) device that provides 24 digital I/O lines channels and one 32-bit counter.

National Instruments provides a driver for Linux but it is huge (over 80 MB!) and buggy and, worst of all, it's not open-source!

There is neither an open-source driver in the official Linux kernel. But things are going to change! Support is planned to be integrated in release 3.17.

In the meantime, if you want to use this device with Linux, you can patch the kernel sources and have fun.

In this document I'm going to show you how to do this.

Install NI USB-6501 kernel module

For this step you need to download kernel sources and run your own kernel.

I presume you already did this and have kernel sources at hand. I'm not going to explain it, there are a lot of documentation on the web.

Next, you have to download this patch and apply it:

# cd /path/to/your/kernel/sources
# cp /path/to/0001-Add-support-for-NI-USB-6501.patch .
# patch -p1 < 0001-Add-support-for-NI-USB-6501.patch

Now you can enable NI USB-6501 support in your kernel:

# make menuconfig

Enable the following:

Device Drivers
        [*] Staging drivers
		<M> Data acquisition support (comedi)
			[*] Comedi USB drivers
				<M> NI USB-6501 support

Compile/install the kernel and modules:

# make && make modules
# make modules_install

If everything went fine, you should have a module called ni_usb6501.ko in this directory:

/lib/modules/$(uname -r)/kernel/drivers/staging/comedi/drivers

Check that the module loads correctly:

# modprobe ni_usb6501

If you don't get any errors your driver is ready to be used.

Install comedilib

Download comedilib from here

Extract, configure and compile:

$ tar xzf comedilib-0.10.2.tar.gz
$ cd comedilib-0.10.2
$ ./configure
$ make
$ sudo make install

If you get configure or compile errors, make sure you have the following packages installed in your system:

Digital I/O testing

Subdevice 0 is Digital I/O which is made up of 24 channels (three 8-bit ports)

When you first connect the device, all channels are set as input (default settings).

A simple testing can be made using the applications found in the demo directory of comedilib:

$ cd <path to comedilib installation>
$ cd demo

You can use inp for channel reading.

For example if you want to read channel 5 (-s means subdevice):

$ ./inp -s 0 -c 5

If you want to see the inputs change value, you can simply connect one of the pins present on the device marked GND or +5V to the selected input.

For testing output capabilities, you first need to configure the pin as output.

For this you can use the dio demo application. For example setting pin 7 as output:

$ ./dio -s 0 -c 7 1

and then for setting a value of +5V to the output you can use outp:

$ ./outp -s 0 -c 7 1

setting it to GND (0 V) can be done with this command:

$ ./outp -s 0 -c 7 0

Counter testing

Subdevice 1 is a 32 bit counter which gets incremented on falling edges on port 2.7.

You can read and write the counter as usual with inp and outp (this time using option "-s 1").

Get counter value:

$ ./inp -s 1 -c 0

Set counter value to 255:

$ ./outp -s 1 -c 0 255

There isn't however a suitable demo application for arming the device (start the counter) so I've written one which can be downloaded here.

Put arm_counter.c in the demo directory and compile it:

$ gcc arm_counter.c common.c -L/usr/local/lib -lcomedi -lm -o arm_counter

You can now reset and start the counter with this command:

$ ./arm_counter -s 1 -c 0

For simple testing you can connect with a wire a generic digital I/O of the device to port 2.7  (for example DIO 0.0).

Set it as output:

$ ./dio -s 0 -c 0 1

Toggle it:

$ ./outp -s 0 -c 0 1
$ ./outp -s 0 -c 0 0

and see the counter incrementing:

$ ./inp -s 1 -c 0

Have fun!