Commit ce791134 authored by Matthieu Cattin's avatar Matthieu Cattin

Merge branch 'fmctdc1ns5cha_calib'

Conflicts:
	common/cp210x_gpio.py
	common/find_usb_tty.py
parents e6213411 60cdea41
# Copyright INCAA Computers, 2012
# Author: Bert Gooijer <bert.gooijer@incaacomputers.com>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
import os
class usbtmc:
def __init__(self, device):
print("Opening: %s" % device)
self.device = device
self.FILE = os.open(device, os.O_RDWR)
def write(self, command):
os.write(self.FILE, command)
def read(self):
return os.read(self.FILE, 4000)
def getName(self):
self.write("*IDN?")
return self.read()
def sendReset(self):
self.write("*RST")
def close(self):
os.close(self.FILE)
class PendulumCNT91:
def __init__(self, device):
self.meas = usbtmc(device)
self.name = self.meas.getName()
print self.name
def write(self, command):
self.meas.write(command)
def read(self):
return self.meas.read()
def reset(self):
self.meas.sendReset()
def close(self):
self.meas.close()
\ No newline at end of file
#! /usr/bin/env python
# coding: utf8
import fcntl, struct, termios, os
import time
import array
class CCP210x:
def __init__(self, device):
self.fd = open(device, 'wb')
def gpio_set(self, mask):
f = array.array('I', [mask])
f[0] = (f[0] << 8) | 0xFF
fcntl.ioctl(self.fd.fileno(), 0x8001, f, 1)
def gpio_get(self):
f = array.array('I', [0])
fcntl.ioctl(self.fd.fileno(), 0x8000, f, 1)
return f[0]
# Create an object (the 0 is used to generate the name, eg. /dev/ttyUSB0
#gpio = cp210x_gpio(0)
# Infinite test loop
#while 1:
# # Pass the mask of the 4 bits as a hex number
# gpio.gpio_set(0xf)
# # Returns the states of the 4 bits as hex number
# print gpio.gpio_get()
# time.sleep(1)
# gpio.gpio_set(0x0)
# print gpio.gpio_get()
# time.sleep(1)
#! /usr/bin/env python
# coding: utf8
import fcntl, struct, termios, os
import time
import array
class CCP210x:
def __init__(self, device):
self.fd = open(device, 'wb')
def gpio_set(self, mask):
f = array.array('I', [mask])
f[0] = (f[0] << 8) | 0xFF
fcntl.ioctl(self.fd.fileno(), 0x8001, f, 1)
def gpio_get(self):
f = array.array('I', [0])
fcntl.ioctl(self.fd.fileno(), 0x8000, f, 1)
return f[0]
# Create an object (the 0 is used to generate the name, eg. /dev/ttyUSB0
#gpio = cp210x_gpio(0)
# Infinite test loop
#while 1:
# # Pass the mask of the 4 bits as a hex number
# gpio.gpio_set(0xf)
# # Returns the states of the 4 bits as hex number
# print gpio.gpio_get()
# time.sleep(1)
# gpio.gpio_set(0x0)
# print gpio.gpio_get()
# time.sleep(1)
#!/usr/bin/python
import sys
import rr
import time
import csr
class CGN4124:
# Host registers (BAR12), for DMA items storage
HOST_BAR = 0xC
HOST_DMA_CARRIER_START_ADDR = 0x00
HOST_DMA_HOST_START_ADDR_L = 0x04
HOST_DMA_HOST_START_ADDR_H = 0x08
HOST_DMA_LENGTH = 0x0C
HOST_DMA_NEXT_ITEM_ADDR_L = 0x10
HOST_DMA_NEXT_ITEM_ADDR_H = 0x14
HOST_DMA_ATTRIB = 0x18
# GN4124 chip registers (BAR4)
GN4124_BAR = 0x4
R_PCI_SYS_CFG = 0x800
R_CLK_CSR = 0x808
R_INT_CFG0 = 0x820
R_GPIO_DIR_MODE = 0xA04
R_GPIO_INT_MASK_CLR = 0xA18
R_GPIO_INT_MASK_SET = 0xA1C
R_GPIO_INT_STATUS = 0xA20
R_GPIO_INT_VALUE = 0xA28
CLK_CSR_DIVOT_MASK = 0x3F0
INT_CFG0_GPIO = 15
GPIO_INT_SRC = 8
# GN4124 core registers (BAR0)
R_DMA_CTL = 0x00
R_DMA_STA = 0x04
R_DMA_CARRIER_START_ADDR = 0x08
R_DMA_HOST_START_ADDR_L = 0x0C
R_DMA_HOST_START_ADDR_H = 0x10
R_DMA_LENGTH = 0x14
R_DMA_NEXT_ITEM_ADDR_L = 0x18
R_DMA_NEXT_ITEM_ADDR_H = 0x1C
R_DMA_ATTRIB = 0x20
DMA_CTL_START = 0
DMA_CTL_ABORT = 1
DMA_CTL_SWAP = 2
DMA_STA = ['Idle','Done','Busy','Error','Aborted']
DMA_ATTRIB_LAST = 0
DMA_ATTRIB_DIR = 1
def rd_reg(self, bar, addr):
return self.bus.iread(bar, addr, 4)
def wr_reg(self, bar, addr, value):
self.bus.iwrite(bar, addr, 4, value)
def __init__(self, bus, csr_addr):
self.bus = bus
self.dma_csr = csr.CCSR(bus, csr_addr)
self.dma_item_cnt = 0
# Get page list
self.pages = self.bus.getplist()
# Shift by 12 to get the 32-bit physical addresses
self.pages = [addr << 12 for addr in self.pages]
self.set_interrupt_config()
# Enable interrupt from gn4124
self.bus.irqena()
# Set local bus frequency
def set_local_bus_freq(self, freq):
# freq in MHz
# LCLK = (25MHz*(DIVFB+1))/(DIVOT+1)
# DIVFB = 31
# DIVOT = (800/LCLK)-1
divot = int(round((800/freq)-1,0))
#print '%d' % divot
data = 0xe001f00c + (divot << 4)
#print '%.8X' % data
#print 'Set local bus freq to %dMHz' % int(round(800/(divot+1),0))
self.wr_reg(self.GN4124_BAR, self.R_CLK_CSR, data)
# Get local bus frequency
# return: frequency in MHz
def get_local_bus_freq(self):
reg = self.rd_reg(self.GN4124_BAR, self.R_CLK_CSR)
divot = ((reg & self.CLK_CSR_DIVOT_MASK)>>4)
return (800/(divot + 1))
# Get physical addresses of the pages allocated to GN4124
def get_physical_addr(self):
return self.pages
# Enable interrupt handling in the driver
def irq_en(self):
self.bus.irqena()
# Wait for interrupt
def wait_irq(self):
# Add here reading of the interrupt source (once the irq core will be present)
return self.bus.irqwait()
# GN4124 RSTOUT33 assert/de-assert cycle
def rstout33_cycle(self):
# assert RSTOUT33 pin
self.wr_reg(self.GN4124_BAR, self.R_PCI_SYS_CFG, 0x00021040)
# de-assert RSTOUT33 pin
self.wr_reg(self.GN4124_BAR, self.R_PCI_SYS_CFG, 0x00025000)
# GN4124 interrupt configuration
def set_interrupt_config(self):
# Set interrupt line from FPGA (GPIO8) as input
self.wr_reg(self.GN4124_BAR, self.R_GPIO_DIR_MODE, (1<<self.GPIO_INT_SRC))
# Set interrupt mask for all GPIO except for GPIO8
self.wr_reg(self.GN4124_BAR, self.R_GPIO_INT_MASK_SET, ~(1<<self.GPIO_INT_SRC))
# Make sure the interrupt mask is cleared for GPIO8
self.wr_reg(self.GN4124_BAR, self.R_GPIO_INT_MASK_CLR, (1<<self.GPIO_INT_SRC))
# Interrupt on rising edge of GPIO8
self.wr_reg(self.GN4124_BAR, self.R_GPIO_INT_VALUE, (1<<self.GPIO_INT_SRC))
# GPIO as interrupt 0 source
self.wr_reg(self.GN4124_BAR, self.R_INT_CFG0, (1<<self.INT_CFG0_GPIO))
# Get DMA controller status
def get_dma_status(self):
reg = self.dma_csr.rd_reg(self.R_DMA_STA)
if(reg > len(self.DMA_STA)):
print("DMA status register : %.8X") % reg
raise Exception('Invalid DMA status')
else:
return self.DMA_STA[reg]
# Configure DMA byte swapping
# 0 = A1 B2 C3 D4 (straight)
# 1 = B2 A1 D4 C3 (swap bytes in words)
# 2 = C3 D4 A1 B2 (swap words)
# 3 = D4 C3 B2 A1 (invert bytes)
def set_dma_swap(self, swap):
if(swap > 3):
raise Exception('Invalid swapping configuration : %d') % swap
else:
self.dma_csr.wr_reg(self.R_CTL, (swap << self.DMA_CTL_SWAP))
# Add DMA item (first item is on the board, the following in the host memory)
# carrier_addr, host_addr, length and next_item_addr are in bytes
# dma_dir = 1 -> PCIe to carrier
# dma_dir = 0 -> carrier to PCIe
# dma_last = 0 -> last item in the transfer
# dma_last = 1 -> more item in the transfer
# Only supports 32-bit host address
def add_dma_item(self, carrier_addr, host_addr, length, dma_dir, last_item):
if(0 == self.dma_item_cnt):
# write the first DMA item in the carrier
self.dma_csr.wr_reg(self.R_DMA_CARRIER_START_ADDR, carrier_addr)
self.dma_csr.wr_reg(self.R_DMA_HOST_START_ADDR_L, (host_addr & 0xFFFFFFFF))
self.dma_csr.wr_reg(self.R_DMA_HOST_START_ADDR_H, (host_addr >> 32))
self.dma_csr.wr_reg(self.R_DMA_LENGTH, length)
self.dma_csr.wr_reg(self.R_DMA_NEXT_ITEM_ADDR_L, (self.pages[0] & 0xFFFFFFFF))
self.dma_csr.wr_reg(self.R_DMA_NEXT_ITEM_ADDR_H, 0x0)
attrib = (dma_dir << self.DMA_ATTRIB_DIR) + (last_item << self.DMA_ATTRIB_LAST)
self.dma_csr.wr_reg(self.R_DMA_ATTRIB, attrib)
else:
# write nexy DMA item(s) in host memory
# uses page 0 to store DMA items
# current and next item addresses are automatically set
current_item_addr = (self.dma_item_cnt-1)*0x20
next_item_addr = (self.dma_item_cnt)*0x20
self.wr_reg(self.HOST_BAR, self.HOST_DMA_CARRIER_START_ADDR + current_item_addr, carrier_addr)
self.wr_reg(self.HOST_BAR, self.HOST_DMA_HOST_START_ADDR_L + current_item_addr, host_addr)
self.wr_reg(self.HOST_BAR, self.HOST_DMA_HOST_START_ADDR_H + current_item_addr, 0x0)
self.wr_reg(self.HOST_BAR, self.HOST_DMA_LENGTH + current_item_addr, length)
self.wr_reg(self.HOST_BAR, self.HOST_DMA_NEXT_ITEM_ADDR_L + current_item_addr,
self.pages[0] + next_item_addr)
self.wr_reg(self.HOST_BAR, self.HOST_DMA_NEXT_ITEM_ADDR_H + current_item_addr, 0x0)
attrib = (dma_dir << self.DMA_ATTRIB_DIR) + (last_item << self.DMA_ATTRIB_LAST)
self.wr_reg(self.HOST_BAR, self.HOST_DMA_ATTRIB + current_item_addr, attrib)
self.dma_item_cnt += 1
# Start DMA transfer
def start_dma(self):
self.dma_item_cnt = 0
self.dma_csr.wr_bit(self.R_DMA_CTL, self.DMA_CTL_START, 1)
# The following two lines should be removed
# when the GN4124 vhdl core will implement auto clear of start bit
#while(('Idle' == self.get_dma_status()) or
# ('Busy' == self.get_dma_status())):
# pass
self.dma_csr.wr_bit(self.R_DMA_CTL, self.DMA_CTL_START, 0)
# Abort DMA transfer
def abort_dma(self):
self.dma_item_cnt = 0
self.dma_csr.wr_bit(self.R_DMA_CTL, self.DMA_CTL_ABORT, 1)
# The following two lines should be removed
# when the GN4124 vhdl core will implement auto clear of start bit
while('Aborted' != self.get_dma_status()):
pass
self.dma_csr.wr_bit(self.R_DMA_CTL, self.DMA_CTL_ABORT, 0)
# Get memory page
def get_memory_page(self, page_nb):
data = []
for i in range(2**10):
data.append(self.rd_reg(self.HOST_BAR, (page_nb<<12)+(i<<2)))
return data
# Set memory page
def set_memory_page(self, page_nb, pattern):
for i in range(2**10):
self.wr_reg(self.HOST_BAR, (page_nb<<12)+(i<<2), pattern)
......@@ -37,6 +37,12 @@ default_test_syntax = r'(test)?(\d\d)'
original_raw_input = raw_input
sys.path.append('gnurabbit/python/')
sys.path.append('common/')
sys.path.append('common/usb_box')
sys.path.append('common/cp210x')
sys.path.append('test/fmceeprom/python')
def pts_raw_input(msg, default='y'):
try:
ret = original_raw_input(msg)
......
ifneq ($(KERNELRELEASE),)
obj-m := usbtmc.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
endif
This diff is collapsed.
// usbtmc.h
// This file is part of a Linux kernel module for USBTMC (USB Test and
// Measurement Class) devices
// Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
// See usbtmc.c source file for license details
#include <linux/ioctl.h> // For _IO macro
// Driver parameters that you might want to tune...
// Maximum number of USBTMC devices to be concurrently serviced by this module
#define USBTMC_MINOR_NUMBERS 16
// Size of driver internal IO buffer. Must be multiple of 4 and at least as
// large as wMaxPacketSize (which is usually 512 bytes).
#define USBTMC_SIZE_IOBUFFER 4096
// Default USB timeout (in jiffies)
#define USBTMC_DEFAULT_TIMEOUT 10*HZ
// Maximum number of read cycles to empty bulk in endpoint during CLEAR and
// ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
// packet is never read.
#define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
// Other definitions
// Request values for USBTMC driver's ioctl entry point
#define USBTMC_IOC_NR 91
#define USBTMC_IOCTL_GET_CAPABILITIES _IO(USBTMC_IOC_NR,0)
#define USBTMC_IOCTL_INDICATOR_PULSE _IO(USBTMC_IOC_NR,1)
#define USBTMC_IOCTL_CLEAR _IO(USBTMC_IOC_NR,2)
#define USBTMC_IOCTL_ABORT_BULK_OUT _IO(USBTMC_IOC_NR,3)
#define USBTMC_IOCTL_ABORT_BULK_IN _IO(USBTMC_IOC_NR,4)
#define USBTMC_IOCTL_SET_ATTRIBUTE _IO(USBTMC_IOC_NR,5)
#define USBTMC_IOCTL_CLEAR_OUT_HALT _IO(USBTMC_IOC_NR,6)
#define USBTMC_IOCTL_CLEAR_IN_HALT _IO(USBTMC_IOC_NR,7)
//#define USBTMC_IOCTL_TIMEOUT _IO(USBTMC_IOC_NR,8)
#define USBTMC_IOCTL_GET_ATTRIBUTE _IO(USBTMC_IOC_NR,9)
#define USBTMC_IOCTL_INSTRUMENT_DATA _IO(USBTMC_IOC_NR,10)
#define USBTMC_IOCTL_RESET_CONF _IO(USBTMC_IOC_NR,11)
// Request names for usbtmc_ioctl command line utility
#define USBTMC_IOCTL_NAME_GET_CAPABILITIES "getcaps"
#define USBTMC_IOCTL_NAME_INDICATOR_PULSE "indpulse"
#define USBTMC_IOCTL_NAME_CLEAR "clear"
#define USBTMC_IOCTL_NAME_ABORT_BULK_OUT "abortout"
#define USBTMC_IOCTL_NAME_ABORT_BULK_IN "abortin"
#define USBTMC_IOCTL_NAME_SET_ATTRIBUTE "setattr"
#define USBTMC_IOCTL_NAME_CLEAR_OUT_HALT "clearouthalt"
#define USBTMC_IOCTL_NAME_CLEAR_IN_HALT "clearinhalt"
#define USBTMC_IOCTL_NAME_GET_ATTRIBUTE "getattr"
#define USBTMC_IOCTL_NAME_RESET_CONF "reset"
// This structure is used with USBTMC_IOCTL_GET_CAPABILITIES.
// See section 4.2.1.8 of the USBTMC specification for details.
struct usbtmc_dev_capabilities {
char interface_capabilities;
char device_capabilities;
char usb488_interface_capabilities;
char usb488_device_capabilities;
};
// This structure is used with USBTMC_IOCTL_SET_ATTRIBUTE and
// USBTMC_IOCTL_GET_ATTRIBUTE.
struct usbtmc_attribute {
int attribute;
int value;
};
// Defines for attributes and their values
#define USBTMC_ATTRIB_AUTO_ABORT_ON_ERROR 0
#define USBTMC_ATTRIB_NAME_AUTO_ABORT_ON_ERROR "autoabort"
#define USBTMC_ATTRIB_READ_MODE 1
#define USBTMC_ATTRIB_NAME_READ_MODE "readmode"
#define USBTMC_ATTRIB_TIMEOUT 2
#define USBTMC_ATTRIB_NAME_TIMEOUT "timeout"
#define USBTMC_ATTRIB_NUM_INSTRUMENTS 3
#define USBTMC_ATTRIB_NAME_NUM_INSTRUMENTS "numinst"
#define USBTMC_ATTRIB_MINOR_NUMBERS 4
#define USBTMC_ATTRIB_NAME_MINOR_NUMBERS "numminor"
#define USBTMC_ATTRIB_SIZE_IO_BUFFER 5
#define USBTMC_ATTRIB_NAME_SIZE_IO_BUFFER "buffsize"
#define USBTMC_ATTRIB_DEFAULT_TIMEOUT 6
#define USBTMC_ATTRIB_NAME_DEFAULT_TIMEOUT "deftimeout"
#define USBTMC_ATTRIB_DEBUG_MODE 7
#define USBTMC_ATTRIB_NAME_DEBUG_MODE "debug"
#define USBTMC_ATTRIB_VERSION 8
#define USBTMC_ATTRIB_NAME_VERSION "version"
#define USBTMC_ATTRIB_TERM_CHAR_ENABLED 9
#define USBTMC_ATTRIB_NAME_TERM_CHAR_ENABLED "termcharenab"
#define USBTMC_ATTRIB_TERM_CHAR 10
#define USBTMC_ATTRIB_NAME_TERM_CHAR "termchar"
#define USBTMC_ATTRIB_ADD_NL_ON_READ 11
#define USBTMC_ATTRIB_NAME_ADD_NL_ON_READ "addnlread"
#define USBTMC_ATTRIB_REM_NL_ON_WRITE 12
#define USBTMC_ATTRIB_NAME_REM_NL_ON_WRITE "remnlwrite"
#define USBTMC_ATTRIB_VAL_OFF 0
#define USBTMC_ATTRIB_NAME_VAL_OFF "off"
#define USBTMC_ATTRIB_VAL_ON 1
#define USBTMC_ATTRIB_NAME_VAL_ON "on"
#define USBTMC_ATTRIB_VAL_FREAD 0
#define USBTMC_ATTRIB_NAME_VAL_FREAD "fread"
#define USBTMC_ATTRIB_VAL_READ 1
#define USBTMC_ATTRIB_NAME_VAL_READ "read"
// This structure is used with USBTMC_IOCTL_INSTRUMENT_DATA.
struct usbtmc_instrument {
int minor_number;
char manufacturer[200];
char product[200];
char serial_number[200];
};
// USBTMC status values
#define USBTMC_STATUS_SUCCESS 0x01
#define USBTMC_STATUS_PENDING 0x02
#define USBTMC_STATUS_FAILED 0x80
#define USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS 0x81
#define USBTMC_STATUS_SPLIT_NOT_IN_PROGRESS 0x82
#define USBTMC_STATUS_SPLIT_IN_PROGRESS 0x83
// USBTMC requests values
#define USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT 1
#define USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS 2
#define USBTMC_REQUEST_INITIATE_ABORT_BULK_IN 3
#define USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS 4
#define USBTMC_REQUEST_INITIATE_CLEAR 5
#define USBTMC_REQUEST_CHECK_CLEAR_STATUS 6
#define USBTMC_REQUEST_GET_CAPABILITIES 7
#define USBTMC_REQUEST_INDICATOR_PULSE 64
// usbtmc_ioctl.c
// This file is part of a Linux kernel module for USBTMC (USB Test and
// Measurement Class) devices. It allows access to the driver's ioctl
// entry point.
// Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
// See revision history at the end of this file
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// The GNU General Public License is available at
// http://www.gnu.org/copyleft/gpl.html.
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include "usbtmc.h"
int minor_number=0;
char devfile[100];
int request=-1;
int myfile;
int rv;
struct usbtmc_dev_capabilities devcaps;
struct usbtmc_attribute attr;
int main(int argc,char *argv[])
{
if(argc<3) goto print_usage;
// Convert parameter #1 (minor number)
sscanf(argv[1],"%d",&minor_number);
if((minor_number<1)||(minor_number>USBTMC_MINOR_NUMBERS)) {
printf("Error: Bad minor number.\n");
goto print_usage;
}
sprintf(devfile,"/dev/usbtmc%d",minor_number);
// Convert parameter #2 (request name)
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_GET_CAPABILITIES))
request=USBTMC_IOCTL_GET_CAPABILITIES;
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_INDICATOR_PULSE))
request=USBTMC_IOCTL_INDICATOR_PULSE;
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_CLEAR))
request=USBTMC_IOCTL_CLEAR;
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_ABORT_BULK_OUT))
request=USBTMC_IOCTL_ABORT_BULK_OUT;
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_ABORT_BULK_IN))
request=USBTMC_IOCTL_ABORT_BULK_IN;
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_SET_ATTRIBUTE))
request=USBTMC_IOCTL_SET_ATTRIBUTE;
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_CLEAR_OUT_HALT))
request=USBTMC_IOCTL_CLEAR_OUT_HALT;
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_CLEAR_IN_HALT))
request=USBTMC_IOCTL_CLEAR_IN_HALT;
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_GET_ATTRIBUTE))
request=USBTMC_IOCTL_GET_ATTRIBUTE;
if(!strcmp(argv[2],USBTMC_IOCTL_NAME_RESET_CONF))
request=USBTMC_IOCTL_RESET_CONF;
// Open device file
myfile=open(devfile,O_RDWR);
if(myfile==-1) {
printf("Error: Can't open device file %s.\n",devfile);
exit(-1);
}
switch(request) {
case USBTMC_IOCTL_INDICATOR_PULSE:
case USBTMC_IOCTL_CLEAR:
case USBTMC_IOCTL_ABORT_BULK_OUT:
case USBTMC_IOCTL_ABORT_BULK_IN:
case USBTMC_IOCTL_CLEAR_OUT_HALT:
case USBTMC_IOCTL_CLEAR_IN_HALT:
case USBTMC_IOCTL_RESET_CONF:
rv=ioctl(myfile,request,0);
if(rv==-1) {
printf("Error: ioctl returned %d.\n",rv);
}
break;
case USBTMC_IOCTL_GET_CAPABILITIES:
rv=ioctl(myfile,request,&devcaps);
printf("Interface capabilities: %u\n",
devcaps.interface_capabilities);
printf("Device capabilities: %u\n",
devcaps.device_capabilities);
printf("USB488 interface capabilities: %u\n",
devcaps.usb488_interface_capabilities);
printf("USB488 device capabilities: %u\n",
devcaps.usb488_device_capabilities);
break;
case USBTMC_IOCTL_SET_ATTRIBUTE:
case USBTMC_IOCTL_GET_ATTRIBUTE:
// Convert parameter #3 (attribute name)
attr.attribute=-1;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_AUTO_ABORT_ON_ERROR))
attr.attribute=USBTMC_ATTRIB_AUTO_ABORT_ON_ERROR;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_READ_MODE))
attr.attribute=USBTMC_ATTRIB_READ_MODE;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_TIMEOUT))
attr.attribute=USBTMC_ATTRIB_TIMEOUT;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_NUM_INSTRUMENTS))
attr.attribute=USBTMC_ATTRIB_NUM_INSTRUMENTS;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_MINOR_NUMBERS))
attr.attribute=USBTMC_ATTRIB_MINOR_NUMBERS;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_SIZE_IO_BUFFER))
attr.attribute=USBTMC_ATTRIB_SIZE_IO_BUFFER;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_DEFAULT_TIMEOUT))
attr.attribute=USBTMC_ATTRIB_DEFAULT_TIMEOUT;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_DEBUG_MODE))
attr.attribute=USBTMC_ATTRIB_DEBUG_MODE;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_VERSION))
attr.attribute=USBTMC_ATTRIB_VERSION;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_TERM_CHAR_ENABLED))
attr.attribute=USBTMC_ATTRIB_TERM_CHAR_ENABLED;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_TERM_CHAR))
attr.attribute=USBTMC_ATTRIB_TERM_CHAR;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_ADD_NL_ON_READ))
attr.attribute=USBTMC_ATTRIB_ADD_NL_ON_READ;
if(!strcmp(argv[3],USBTMC_ATTRIB_NAME_REM_NL_ON_WRITE))
attr.attribute=USBTMC_ATTRIB_REM_NL_ON_WRITE;
if(attr.attribute==-1) {
printf("Error: Bad attribute name\n");
close(myfile);
goto print_usage;
}
if(request==USBTMC_IOCTL_SET_ATTRIBUTE) {
attr.value=-1;
if(!strcmp(argv[4],USBTMC_ATTRIB_NAME_VAL_OFF))
attr.value=USBTMC_ATTRIB_VAL_OFF;
if(!strcmp(argv[4],USBTMC_ATTRIB_NAME_VAL_ON))
attr.value=USBTMC_ATTRIB_VAL_ON;
if(!strcmp(argv[4],USBTMC_ATTRIB_NAME_VAL_FREAD))
attr.value=USBTMC_ATTRIB_VAL_FREAD;
if(!strcmp(argv[4],USBTMC_ATTRIB_NAME_VAL_READ))
attr.value=USBTMC_ATTRIB_VAL_READ;
if(attr.value==-1) sscanf(argv[4],"%d",&attr.value);
}
rv=ioctl(myfile,request,&attr);
if(request==USBTMC_IOCTL_GET_ATTRIBUTE) {
if((attr.attribute==USBTMC_ATTRIB_AUTO_ABORT_ON_ERROR)||
(attr.attribute==USBTMC_ATTRIB_DEBUG_MODE)||
(attr.attribute==USBTMC_ATTRIB_TERM_CHAR_ENABLED)||
(attr.attribute==USBTMC_ATTRIB_ADD_NL_ON_READ)||
(attr.attribute==USBTMC_ATTRIB_REM_NL_ON_WRITE))
if(attr.value==USBTMC_ATTRIB_VAL_OFF)
printf("Value: %s\n",USBTMC_ATTRIB_NAME_VAL_OFF);
else
printf("Value: %s\n",USBTMC_ATTRIB_NAME_VAL_ON);
if(attr.attribute==USBTMC_ATTRIB_READ_MODE)
if(attr.value==USBTMC_ATTRIB_VAL_FREAD)
printf("Value: %s\n",USBTMC_ATTRIB_NAME_VAL_FREAD);
else
printf("Value: %s\n",USBTMC_ATTRIB_NAME_VAL_READ);
if(attr.attribute==USBTMC_ATTRIB_TIMEOUT)
printf("Value: %d\n",attr.value);
if(attr.attribute==USBTMC_ATTRIB_NUM_INSTRUMENTS)
printf("Value: %d\n",attr.value);
if(attr.attribute==USBTMC_ATTRIB_MINOR_NUMBERS)
printf("Value: %d\n",attr.value);
if(attr.attribute==USBTMC_ATTRIB_SIZE_IO_BUFFER)
printf("Value: %d\n",attr.value);
if(attr.attribute==USBTMC_ATTRIB_DEFAULT_TIMEOUT)
printf("Value: %d\n",attr.value);
if(attr.attribute==USBTMC_ATTRIB_VERSION)
printf("Value: %d\n",attr.value);
if(attr.attribute==USBTMC_ATTRIB_TERM_CHAR)
printf("Value: %d\n",attr.value);
}
break;
default:
printf("Error: Bad request name.\n");
close(myfile);
goto print_usage;
}
// Close device file
close(myfile);
exit(0);
print_usage:
printf("Usage:\n");
printf("usbtmc_ioctl n request [ attribute [ value ] ]\n");
printf("where\n");
printf("m = minor number, e. g. 1 for /dev/usbtmc1\n");
printf("request = { clear , setattr , getattr , reset etc}\n");
printf("attribute = { autoabort , readmode , timeout etc }\n");
printf("See html documentation for details!\n");
printf("Example:\n");
printf("usbtmc_ioctl 1 clear\n");
printf("Clears input and output buffer of device /dev/usbtmc1\n");
exit(-1);
}
// Revision history
//
// 1.0 05.11.2007 Initial version.
// 1.0.1 07.11.2007 Set cdev struct to zero prior to calling cdev_init().
// 1.0.2 09.11.2007 Bug fixes related to control requests.
// 1.0.3 13.11.2007 Automatic ABORT on error in FREAD (shell) mode.
// 1.0.4 02.12.2007 Added a whole bunch of attributes.
// 1.1 08.12.2007 Clean-up.
#!/bin/bash
module="usbtmc"
# Remove module from kernel (just in case it is still running)
/sbin/rmmod $module 2> /dev/null
# Install module
#/sbin/insmod /home/user/pts_fmcfd/pts/usbdriver/$module.ko
/sbin/insmod /home/user/pts/usbdriver/$module.ko 2> /dev/null
# Find major number used
major=$(cat /proc/devices | grep USBTMCCHR | awk '{print $1}')
#echo Using major number $major
# Remove old device files
rm -f /dev/${module}[0-9]
# Ceate new device files
mknod /dev/${module}0 c $major 0
mknod /dev/${module}1 c $major 1