Commit 6d578024 authored by Claes's avatar Claes

Added support for Velleman K8055 USB experiment board

parent 7541534f
/*
* Proview $Id$
* Copyright (C) 2005 SSAB Oxelsund AB.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with the program, if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* rt_io_m_usb_agent.c -- I/O methods for class USB_Agent. */
#include "pwr.h"
#include "pwr_basecomponentclasses.h"
#include "pwr_otherioclasses.h"
#include "rt_io_base.h"
#include "rt_io_agent_init.h"
#include "rt_io_agent_close.h"
#include "rt_io_msg.h"
#if defined PWRE_CONF_LIBUSB
#include <libusb-1.0/libusb.h>
#include "rt_io_m_usb_agent.h"
static pwr_tStatus IoAgentInit( io_tCtx ctx,
io_sAgent *ap)
{
io_sLocalUSB_Agent *local;
int sts;
pwr_sClass_USB_Agent *op = (pwr_sClass_USB_Agent *)ap->op;
local = (io_sLocalUSB_Agent *) calloc( 1, sizeof(io_sLocalUSB_Agent));
ap->Local = local;
sts = libusb_init( &local->libusb_ctx);
if ( sts != 0) {
op->Status = sts;
local->libusb_ctx = 0;
errh_Error( "Init of USB agent failed '%s'", ap->Name);
}
else {
op->Status = IO__SUCCESS;
errh_Info( "Init of USBIO agent '%s'", ap->Name);
}
return IO__SUCCESS;
}
static pwr_tStatus IoAgentClose( io_tCtx ctx,
io_sAgent *ap)
{
io_sLocalUSB_Agent *local = ap->Local;
pwr_sClass_USB_Agent *op = (pwr_sClass_USB_Agent *)ap->op;
libusb_exit( local->libusb_ctx);
op->Status = 0;
free( local);
return IO__SUCCESS;
}
#else
static pwr_tStatus IoAgentInit( io_tCtx ctx, io_sAgent *ap) { return 0;}
static pwr_tStatus IoAgentClose( io_tCtx ctx, io_sAgent *ap) { return 0;}
#endif
/* Every method should be registred here. */
pwr_dExport pwr_BindIoMethods(USB_Agent) = {
pwr_BindIoMethod(IoAgentInit),
pwr_BindIoMethod(IoAgentClose),
pwr_NullMethod
};
/* rt_io_m_usb_agent.h -- I/O methods for class USB_Agent. */
#ifndef ra_io_m_usb_agent_h
#define ra_io_m_usb_agent_h
#if defined PWRE_CONF_LIBUSB
typedef struct {
libusb_context *libusb_ctx;
} io_sLocalUSB_Agent;
#endif
#endif
/*
* Proview $Id$
* Copyright (C) 2005 SSAB Oxelsund AB.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with the program, if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* rt_io_m_velleman_k8055_board.c -- I/O methods for class Velleman_K8055_Board. */
#include "pwr.h"
#include "pwr_basecomponentclasses.h"
#include "pwr_otherioclasses.h"
#include "co_time.h"
#include "rt_io_base.h"
#include "rt_io_card_init.h"
#include "rt_io_card_close.h"
#include "rt_io_card_read.h"
#include "rt_io_card_write.h"
#include "rt_io_msg.h"
#if defined PWRE_CONF_LIBUSB
#include <libusb-1.0/libusb.h>
#include "rt_io_m_usb_agent.h"
#include "rt_io_m_velleman_k8055_board.h"
static pwr_tStatus IoCardInit( io_tCtx ctx,
io_sAgent *ap,
io_sRack *rp,
io_sCard *cp)
{
ssize_t devnum;
libusb_device **list;
libusb_device *device = 0;
int found = 0;
int i;
io_sLocalUSB_Agent *local_agent = (io_sLocalUSB_Agent *)ap->Local;
io_sLocal_K8055 *local;
pwr_sClass_Velleman_K8055_Board *op = (pwr_sClass_Velleman_K8055_Board *)cp->op;
int sts;
if ( !local_agent->libusb_ctx)
return IO__INITFAIL;
devnum = libusb_get_device_list( local_agent->libusb_ctx, &list);
if ( devnum > 0) {
for ( i = 0; i < devnum; i++) {
struct libusb_device_descriptor desc;
if ( libusb_get_device_descriptor( list[i], &desc) != 0)
continue;
if ( desc.idVendor == 0x10cf &&
desc.idProduct == 0x5500 + op->Super.Address) {
device = list[i];
found = 1;
break;
}
}
}
if ( !found) {
errh_Error( "Init Velleman K8055, device not found '%s'", cp->Name);
op->Status = IO__NODEVICE;
((pwr_sClass_Velleman_K8055 *)rp->op)->Status = op->Status;
return IO__INITFAIL;
}
local = (io_sLocal_K8055 *) calloc( 1, sizeof(io_sLocal_K8055));
cp->Local = local;
sts = libusb_open( device, &local->libusb_device);
if ( sts != 0) {
op->Status = IO__INITFAIL;
((pwr_sClass_Velleman_K8055 *)rp->op)->Status = op->Status;
local->libusb_device = 0;
return IO__INITFAIL;
}
if ( libusb_kernel_driver_active( local->libusb_device, 0) != 0)
libusb_detach_kernel_driver( local->libusb_device, 0);
sts = libusb_claim_interface( local->libusb_device, 0);
if ( sts < 0) {
errh_Error( "K8055 Claim interface failed, sts %d, '%s'", sts, ap->Name);
op->Status = IO__INITFAIL;
((pwr_sClass_Velleman_K8055 *)rp->op)->Status = op->Status;
return IO__INITFAIL;
}
for ( i = 0; i < 2; i++) {
if ( cp->chanlist[i].sop)
io_AiRangeToCoef( &cp->chanlist[i]);
}
for ( i = 0; i < 2; i++) {
if ( cp->chanlist[i+7].sop)
io_AoRangeToCoef( &cp->chanlist[i+7]);
}
errh_Info( "Init of Velleman K8055 '%s'", cp->Name);
op->Status = IO__SUCCESS;
// Rack has no methods, set status
if ( ((pwr_sClass_Velleman_K8055 *)rp->op)->Status == 0)
((pwr_sClass_Velleman_K8055 *)rp->op)->Status = op->Status;
return IO__SUCCESS;
}
static pwr_tStatus IoCardClose( io_tCtx ctx,
io_sAgent *ap,
io_sRack *rp,
io_sCard *cp)
{
io_sLocalUSB_Agent *local_agent = (io_sLocalUSB_Agent *)ap->Local;
io_sLocal_K8055 *local = (io_sLocal_K8055 *)cp->Local;
if ( !local_agent->libusb_ctx)
return IO__SUCCESS;
if ( local->libusb_device)
libusb_close( local->libusb_device);
if ( cp->Local)
free( cp->Local);
return IO__SUCCESS;
}
static pwr_tStatus IoCardRead( io_tCtx ctx,
io_sAgent *ap,
io_sRack *rp,
io_sCard *cp)
{
io_sLocal_K8055 *local = (io_sLocal_K8055 *)cp->Local;
pwr_sClass_Velleman_K8055_Board *op = (pwr_sClass_Velleman_K8055_Board *)cp->op;
unsigned char data[9];
char endpoint = 0x81;
int size = 8;
int tsize;
unsigned char m;
int sts;
int i;
pwr_tUInt32 error_count = op->Super.ErrorCount;
// You have to read twice to get the latest ?????
sts = libusb_interrupt_transfer( local->libusb_device, endpoint, data, 8, &tsize, 20);
sts = libusb_interrupt_transfer( local->libusb_device, endpoint, data, 8, &tsize, 20);
if ( sts != 0 || tsize != size) {
op->Super.ErrorCount++;
return IO__SUCCESS;
}
else {
// Handle Ai
for ( i = 0; i < 2; i++) {
if ( cp->chanlist[i].sop) {
io_sChannel *chanp = &cp->chanlist[i];
pwr_sClass_ChanAi *cop = (pwr_sClass_ChanAi *)chanp->cop;
pwr_sClass_Ai *sop = (pwr_sClass_Ai *)chanp->sop;
pwr_tFloat32 actvalue;
int ivalue = data[i+2];
if ( cop->CalculateNewCoef)
// Request to calculate new coefficients
io_AiRangeToCoef( chanp);
io_ConvertAi( cop, ivalue, &actvalue);
// Filter
if ( sop->FilterType == 1 &&
sop->FilterAttribute[0] > 0 &&
sop->FilterAttribute[0] > ctx->ScanTime) {
actvalue = *(pwr_tFloat32 *)chanp->vbp + ctx->ScanTime / sop->FilterAttribute[0] *
(actvalue - *(pwr_tFloat32 *)chanp->vbp);
}
*(pwr_tFloat32 *)chanp->vbp = actvalue;
sop->SigValue = cop->SigValPolyCoef1 * ivalue + cop->SigValPolyCoef0;
sop->RawValue = ivalue;
}
}
// Handle Di
for ( i = 0; i < 5; i++) {
switch ( i) {
case 0: m = 16; break;
case 1: m = 32; break;
case 2: m = 1; break;
case 3: m = 64; break;
case 4: m = 128; break;
}
if ( cp->chanlist[i+2].sop)
*(pwr_tBoolean *)cp->chanlist[i+2].vbp = ((data[0] & m) != 0);
}
}
if ( op->Super.ErrorCount >= op->Super.ErrorSoftLimit &&
error_count < op->Super.ErrorSoftLimit) {
errh_Warning( "IO Card ErrorSoftLimit reached, '%s'", cp->Name);
}
if ( op->Super.ErrorCount >= op->Super.ErrorHardLimit) {
errh_Error( "IO Card ErrorHardLimit reached '%s', IO stopped", cp->Name);
ctx->Node->EmergBreakTrue = 1;
return IO__ERRDEVICE;
}
return IO__SUCCESS;
}
static pwr_tStatus IoCardWrite( io_tCtx ctx,
io_sAgent *ap,
io_sRack *rp,
io_sCard *cp)
{
io_sLocal_K8055 *local = (io_sLocal_K8055 *)cp->Local;
pwr_sClass_Velleman_K8055_Board *op = (pwr_sClass_Velleman_K8055_Board *)cp->op;
unsigned char data[9];
char endpoint = 0x1;
int size = 8;
int tsize;
unsigned char m;
int i;
int sts;
pwr_tUInt32 error_count = op->Super.ErrorCount;
memset( data, 0, sizeof(data));
data[0] = 0x5;
// Handle Do
m = 1;
unsigned char do_value = 0;
for ( i = 0; i < 8; i++) {
if ( cp->chanlist[i+9].sop) {
if ( *(pwr_tBoolean *)cp->chanlist[i+9].vbp)
do_value |= m;
}
m = m << 1;
}
data[1] = do_value;
// Handle Ao
for ( i = 0; i < 2; i++) {
if ( cp->chanlist[i+7].sop) {
io_sChannel *chanp = &cp->chanlist[i+7];
pwr_sClass_ChanAo *cop = (pwr_sClass_ChanAo *)chanp->cop;
if ( cop->CalculateNewCoef)
// Request to calculate new coefficients
io_AoRangeToCoef( chanp);
float fvalue = *(pwr_tFloat32 *)chanp->vbp * cop->OutPolyCoef1 + cop->OutPolyCoef0;
int ivalue = (int)fvalue;
if ( ivalue < 0)
ivalue = 0;
else if (ivalue > 255)
ivalue = 255;
data[i+2] = ivalue;
}
}
sts = libusb_interrupt_transfer( local->libusb_device, endpoint, data, size, &tsize, 20);
if ( sts != 0 || tsize != size) {
op->Super.ErrorCount++;
return IO__SUCCESS;
}
if ( op->Super.ErrorCount >= op->Super.ErrorSoftLimit &&
error_count < op->Super.ErrorSoftLimit) {
errh_Warning( "IO Card ErrorSoftLimit reached, '%s'", cp->Name);
}
if ( op->Super.ErrorCount >= op->Super.ErrorHardLimit) {
errh_Error( "IO Card ErrorHardLimit reached '%s', IO stopped", cp->Name);
ctx->Node->EmergBreakTrue = 1;
return IO__ERRDEVICE;
}
return IO__SUCCESS;
}
#else
static pwr_tStatus IoCardInit( io_tCtx ctx,io_sAgent *ap,io_sRack *rp,io_sCard *cp) {return 0;}
static pwr_tStatus IoCardClose( io_tCtx ctx,io_sAgent *ap,io_sRack *rp,io_sCard *cp) {return 0;}
static pwr_tStatus IoCardRead( io_tCtx ctx,io_sAgent *ap,io_sRack *rp,io_sCard *cp) {return 0;}
static pwr_tStatus IoCardWrite( io_tCtx ctx,io_sAgent *ap,io_sRack *rp,io_sCard *cp) {return 0;}
#endif
/* Every method should be registred here. */
pwr_dExport pwr_BindIoMethods(Velleman_K8055_Board) = {
pwr_BindIoMethod(IoCardInit),
pwr_BindIoMethod(IoCardClose),
pwr_BindIoMethod(IoCardRead),
pwr_BindIoMethod(IoCardWrite),
pwr_NullMethod
};
/* rt_io_m_velleman_k8055_board.h -- I/O methods for class velleman_k8055_board. */
#ifndef ra_io_m_velleman_k8055_board_h
#define ra_io_m_velleman_k8055_board_h
#if defined PWRE_CONF_LIBUSB
typedef struct {
libusb_device_handle *libusb_device;
} io_sLocal_K8055;
#endif
#endif
......@@ -7,3 +7,5 @@ Modbus_TCP_Server
Modbus_TCP_ServerModule
GPIO
GPIO_Module
USB_Agent
Velleman_K8055_Board
include $(pwre_dir_symbols)
-include $(pwre_kroot)/tools/bld/src/$(os_name)/$(hw_name)/$(type_name)_generic.mk
ifeq ($($(type_name)_generic_mk),)
-include $(pwre_kroot)/tools/bld/src/$(os_name)/$(type_name)_generic.mk
endif
ifeq ($($(type_name)_generic_mk),)
include $(pwre_kroot)/tools/bld/src/$(type_name)_generic.mk
endif
-include ../../special.mk
-include ../special.mk
-include special.mk
include $(pwre_dir_symbols)
-include $(pwre_kroot)/tools/bld/src/$(os_name)/$(hw_name)/$(type_name)_generic.mk
ifeq ($($(type_name)_generic_mk),)
-include $(pwre_kroot)/tools/bld/src/$(os_name)/$(type_name)_generic.mk
endif
ifeq ($($(type_name)_generic_mk),)
include $(pwre_kroot)/tools/bld/src/$(type_name)_generic.mk
endif
-include ../../special.mk
-include ../special.mk
-include special.mk
include $(pwre_dir_symbols)
-include $(pwre_kroot)/tools/bld/src/$(os_name)/$(hw_name)/$(type_name)_generic.mk
ifeq ($($(type_name)_generic_mk),)
-include $(pwre_kroot)/tools/bld/src/$(os_name)/$(type_name)_generic.mk
endif
ifeq ($($(type_name)_generic_mk),)
include $(pwre_kroot)/tools/bld/src/$(type_name)_generic.mk
endif
-include ../../special.mk
-include ../special.mk
-include special.mk
/*
Dummy for libusb-1.0
*/
#include <sys/types.h>
#include <stdint.h>
struct libusb_context;
struct libusb_device;
struct libusb_device_handle;
typedef struct libusb_context libusb_context;
typedef struct libusb_device libusb_device;
typedef struct libusb_device_handle libusb_device_handle;
struct libusb_device_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdUSB;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bMaxPacketSize0;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
};
int libusb_init(libusb_context **ctx) {return 0;}
void libusb_exit(libusb_context *ctx) {}
ssize_t libusb_get_device_list(libusb_context *ctx,
libusb_device ***list) {return 0;}
void libusb_free_device_list(libusb_device **list, int unref_devices) {}
int libusb_open(libusb_device *dev, libusb_device_handle **handle) {return 0;}
void libusb_close(libusb_device_handle *dev_handle) {}
int libusb_interrupt_transfer(libusb_device_handle *dev_handle,
unsigned char endpoint, unsigned char *data, int length,
int *actual_length, unsigned int timeout) {return 0;}
int libusb_kernel_driver_active(libusb_device_handle *dev, int interface) {return 0;}
int libusb_detach_kernel_driver(libusb_device_handle *dev, int interface) {return 0;}
int libusb_claim_interface(libusb_device_handle *dev, int iface) {return 0;}
int libusb_get_device_descriptor(libusb_device *dev,
struct libusb_device_descriptor *desc) {return 0;}
This diff is collapsed.
......@@ -5,11 +5,11 @@ ifeq ($(export_type),exp)
link = $(ld) $(explinkflags) $(domap) -o $(export_exe) \
$(export_obj) $(objects) $(rt_msg_objs) \
$(pwr_obj)/rt_io_user.o \
-lpwr_rt -lpwr_co -lpwr_msg_dummy -lpwr_usbio_dummy -lpthread -lm
-lpwr_rt -lpwr_co -lpwr_msg_dummy -lpwr_usbio_dummy -lpwr_usb_dummy -lpthread -lm
else
link = $(ld) $(elinkflags) $(domap) -o $(export_exe) \
$(export_obj) $(objects) $(rt_msg_objs) \
$(pwr_obj)/rt_io_user.o \
-lpwr_rt -lpwr_co -lpwr_msg_dummy -lpwr_usbio_dummy -lpthread -lm
-lpwr_rt -lpwr_co -lpwr_msg_dummy -lpwr_usbio_dummy -lpwr_usb_dummy -lpthread -lm
endif
endif
......@@ -100,6 +100,7 @@ connectedclass <Incorrect class of connected object> /error
replaced <Symbol is replaced> /info
normal <Normal operating state> /succ
initfail <Initialization failed> /error
nodevice <Device not found> /error
.end
......@@ -109,10 +109,18 @@ else
linkgtk :=
endif
ifeq ($(pwre_conf_libusb),1)
clibusb := -DPWRE_CONF_LIBUSB=1
linklibusb := -lusb-1.0
else
clibusb :=
linklibusb :=
endif
log_done =
csetos := -DOS_LINUX=1 -DOS=linux -D_LINUX -DHW_X86=1 -DHW=x86
cinc := -I$(inc_dir) -I$(einc_dir) -I$(hw_source) -I$(os_source) -I$(co_source) -I/usr/X11R6/include -I$(jdk)/include -I$(jdk)/include/linux \
`pkg-config --cflags gtk+-2.0` -DPREFIX=\"/usr/local\" -DSYSCONFDIR=\"/etc\" -DDATADIR=\"/usr/share\" -DLIBDIR=\"/usr/lib\" $(cmysql) $(cgtk)
`pkg-config --cflags gtk+-2.0` -DPREFIX=\"/usr/local\" -DSYSCONFDIR=\"/etc\" -DDATADIR=\"/usr/share\" -DLIBDIR=\"/usr/lib\" $(cmysql) $(cgtk) $(clibusb)
rm := rm
cp := cp
cpflags :=
......
......@@ -109,10 +109,18 @@ else
linkgtk :=
endif
ifeq ($(pwre_conf_libusb),1)
clibusb := -DPWRE_CONF_LIBUSB=1
linklibusb := -lusb-1.0
else
clibusb :=
linklibusb :=
endif
log_done =
csetos := -DOS_LINUX=1 -DOS=linux -D_LINUX -DHW_X86_64=1 -DHW=x86_64
cinc := -I$(inc_dir) -I$(einc_dir) -I$(hw_source) -I$(os_source) -I$(co_source) -I/usr/X11R6/include -I$(jdk)/include -I$(jdk)/include/linux \
`pkg-config --cflags gtk+-2.0` -DPREFIX=\"/usr/local\" -DSYSCONFDIR=\"/etc\" -DDATADIR=\"/usr/share\" -DLIBDIR=\"/usr/lib\" $(cmysql) $(cgtk)
`pkg-config --cflags gtk+-2.0` -DPREFIX=\"/usr/local\" -DSYSCONFDIR=\"/etc\" -DDATADIR=\"/usr/share\" -DLIBDIR=\"/usr/lib\" $(cmysql) $(cgtk) $(clibusb)
rm := rm
cp := cp
cpflags :=
......
......@@ -158,6 +158,13 @@ set_env()
export pwre_conf_gtk=0
fi
# libusb
if [ -e /usr/lib/libusb-1.0.so ]; then
export pwre_conf_libusb=1
else
export pwre_conf_libusb=0
fi
export pwre_conf_alsa=1
}
......
......@@ -167,7 +167,7 @@ CompileRtNode()
ld_opt_tmp="`cat $pwrp_exe/$FileName.opt`"
ld_opt="`eval echo $ld_opt_tmp`"
else
ld_opt="`eval echo $pwr_obj/rt_io_user.o -lpwr_rt -lpwr_usbio_dummy`"
ld_opt="`eval echo $pwr_obj/rt_io_user.o -lpwr_rt -lpwr_usbio_dummy -lpwr_usb_dummy`"
fi
if $ldxx $link_debug -L/lib/thread -L$pwrp_lib -L$pwrp_cmn/x86_linux/lib -L$pwr_lib \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment