Developing Device Support kazuro furukawa kek jp Writing

  • Slides: 29
Download presentation
Developing Device Support < kazuro. furukawa @ kek. jp > Writing Device Support Kazuro

Developing Device Support < kazuro. furukawa @ kek. jp > Writing Device Support Kazuro Furukawa <kazuro. furukawa@kek. jp> for EPICS 2009 at RRCAT January 30, 2009 Based on presentations by Eric Norum, 2004 Ralph Lange, 2006 EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Writing Device Support – Scope u. An overview of the concepts

Developing Device Support Writing Device Support – Scope u. An overview of the concepts associated with writing EPICS Device Support routines. u. Examples show the “stone knives and bearskins” approach. u. The ASYN package provides a framework which makes writing device support much easier. v. The concepts presented here still apply. EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Writing Device Support – Outline u. What is ‘Device Support’? u.

Developing Device Support Writing Device Support – Outline u. What is ‘Device Support’? u. The. dbd file entry u. The driver DSET u. Device addresses u. Support routines u. Using interrupts u. Asynchronous input/output u. Callbacks EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support What is ‘Device Support’? u. Interface between record and hardware u.

Developing Device Support What is ‘Device Support’? u. Interface between record and hardware u. A set of routines for record support to call v. The record type determines the required set of routines v. These routines have full read/write access to any record field u. Determines synchronous/asynchronous nature of record u. Performs record I/O v. Provides interrupt handling mechanism EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Why use Device Support? u. Could instead make a different record

Developing Device Support Why use Device Support? u. Could instead make a different record type for each hardware interface, with fields to allow full control over the provided facilities. u. A separate device support level provides several advantages: v. Users need not learn a new record type for each type of device v. Increases modularity ³I/O hardware changes are less disruptive ³Device support is simpler than record support ³Hardware interface code is isolated from record API u. Custom records are available if really needed. v. By which I mean “really, really needed!” v. Existing record types are sufficient for most applications. EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support How Does a Record Find Its Device Support? ��� Through. dbd

Developing Device Support How Does a Record Find Its Device Support? ��� Through. dbd ‘device’ statements: EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support The. dbd File Entry u. The IOC discovers device support from

Developing Device Support The. dbd File Entry u. The IOC discovers device support from entries in. dbd files device(rec. Type, addr. Type, dset. Name, ”dtype. Name”) uaddr. Type is one of AB_IO INST_IO BITBUS_IO CAMAC_IO GPIB_IO RF_IO VME_IO VXI_IO udset. Name is the name of the C Device Support Entry Table (DSET) • By convention name indicates record and hardware type: device(ai, GPIB_IO, dev. Aidg 535, "dg 535") device(bi, VME_IO, dev. Bi. Xy 240, "XYCOM-240") EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support The DSET u. A C structure containing pointers to functions u.

Developing Device Support The DSET u. A C structure containing pointers to functions u. Content dependent upon record type u. Each device support layer defines a DSET with pointers to its own functions u. A DSET structure declaration looks like: struct dset { long number; long (*report)(int level); long (*initialize)(int pass); long (*init. Record)(struct … *precord); long (*get. Io. Int. Info)(…); … read/write and other routines as required }; unumber specifies number of pointers (often 5 or 6) u. A NULL is given when an optional routine is not implemented u. DSET structures and functions are usually declared static EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support The DSET – initialize long initialize(int pass); u. Initializes the device

Developing Device Support The DSET – initialize long initialize(int pass); u. Initializes the device support layer u. Optional routine, not always needed u. Used for one-time startup operations: v. Start background tasks v. Create shared tables u. Called twice by ioc. Init() vpass=0 – Before any record initialization ³Doesn’t usually access hardware since device address information is not yet known vpass=1 – After all record initialization ³Can be used as a final startup step. All device address information is now known EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support The DSET – init. Record long init. Record(struct … *precord); u.

Developing Device Support The DSET – init. Record long init. Record(struct … *precord); u. Called by ioc. Init() once for each record with matching DTYP u. Optional routine, but usually supplied u. Routines often v. Validate the INP or OUTP field v. Verify that addressed hardware is present v. Allocate device-specific storage for the record ³Each record contains a void *dpvt pointer for this purpose v. Program device registers v. Set record-specific fields needed for conversion to/from engineering units EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support The DSET – init. Record – Device Addresses u. Device support.

Developing Device Support The DSET – init. Record – Device Addresses u. Device support. dbd entry was device(rec. Type, addr. Type, dset, "name") uaddr. Type specifies the type to use for the address link, e. g. device(bo, VME_IO, dev. Bo. Xy 240, "Xycom XY 240") sets pbo->out: vpbo->out. type = VME_IO v. Device support uses pbo->out. value. vmeio which is a struct vmeio { short card; short signal; char *parm; }; u. IOC Application Developer’s Guide describes all types EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support The DSET – report long report(int level); u. Called by dbior

Developing Device Support The DSET – report long report(int level); u. Called by dbior shell command u. Prints information about current state, hardware status, I/O statistics, etc. u. Amount of output is controlled by the level argument vlevel=0 – list hardware connected, one device per line vlevel>0 – provide different type or more detailed information EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support The DSET – read/write long read(struct … *precord); long write(struct …

Developing Device Support The DSET – read/write long read(struct … *precord); long write(struct … *precord); u. Called when record is processed u. Perform (or initiate) the I/O operation: v. Synchronous input ³Copy value from hardware into precord->rval ³Return 0 (to indicate success) v. Synchronous output ³Copy value from precord->rval to hardware ³Return 0 (to indicate success) EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support A Simple Example (VME / vx. Works or RTEMS) #include <rec.

Developing Device Support A Simple Example (VME / vx. Works or RTEMS) #include <rec. Gbl. h> #include <dev. Sup. h> #include <dev. Lib. h> #include <bi. Record. h> #include <epics. Export. h> static long init. Record(struct bi. Record *prec){ char *pbyte, dummy; if ((prec->inp. type != VME_IO) || (prec->inp. value. vmeio. signal < 0) || (prec->inp. value. vmeio. signal > 7)) { rec. Gbl. Record. Error(S_dev_bad. Inp. Type, (void *)prec, "dev. Bi. First: Bad INP"); return -1; } if (dev. Register. Address("dev. Bi. First", at. VMEA 16, prec->inp. value. vmeio. card, 0 x 1, &pbyte) != 0) { rec. Gbl. Record. Error(S_dev_bad. Card, (void *)prec, "dev. Bi. First: Bad VME address"); return -1; } if (dev. Read. Probe(1, pbyte, &dummy) < 0) { rec. Gbl. Record. Error(S_dev_bad. Card, (void *)prec, "dev. Bi. First: Nothing there!"); return -1; } prec->dpvt = pbyte; prec->mask = 1 << prec->inp. value. vmeio. signal; return 0; } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support A Simple Example (VME / vx. Works or RTEMS) static long

Developing Device Support A Simple Example (VME / vx. Works or RTEMS) static long read(struct bi. Record *prec) { volatile char *pbyte = (volatile char *)prec->dpvt; prec->rval = *pbyte; return 0; } static struct { long number; long (*report)(int); long (*initialize)(int); long (*init. Record)(struct bi. Record *); long (*get. Io. Int. Info)(); long (*read)(struct bi. Record *); } dev. Bi. First = { 5, NULL, init. Record, NULL, read }; epics. Export. Address(dset, dev. Bi. First); EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support A Simple Example – Device Support. dbd File The. dbd file

Developing Device Support A Simple Example – Device Support. dbd File The. dbd file for the device support routines shown on the preceding pages might be device(bi, VME_IO, dev. Bi. First, “simple. Input”) EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support A Simple Example – Application. db File An application. db file

Developing Device Support A Simple Example – Application. db File An application. db file using the device support routines shown on the preceding pages might contain record(bi, "$(P): status. Bit") { field(DESC, "Simple example binary input") field(DTYP, "simple. Input") field(INP, "#C$(C) S$(S)") } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support A Simple Example – Application Startup Script An application startup script

Developing Device Support A Simple Example – Application Startup Script An application startup script (st. cmd) using the device support routines shown on the preceding pages might contain db. Load. Records("db/example. db", "P=test, C=0 x 1 E 0, S=0") which would expand the. db file into record(bi, "test: status. Bit") { field(DESC, "Simple example binary input") field(DTYP, "simple. Input") field(INP, "#C 0 x 1 E 0 S 0") } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Useful Facilities u. ANSI C routines (EPICS headers fill in vendor

Developing Device Support Useful Facilities u. ANSI C routines (EPICS headers fill in vendor holes) vepics. Stdio. h – printf, sscanf, epics. Snprintf vepics. String. h – strcpy, memcpy, epics. Str. Dup vepics. Stdlib. h – getenv, abs, epics. Scan. Double u. OS-independent hardware access (dev. Lib. h) v. Bus address Local address conversion v. Interrupt control v. Bus probing u. EPICS routines vepics. Event. h – process synchronization semaphore vepics. Mutex. h – mutual-exclusion semaphore vepics. Thread. h – multithreading support vrec. Gbl. h – record error and alarm reporting EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Device Interrupts u vx. Works/RTEMS interrupt handlers can be written in

Developing Device Support Device Interrupts u vx. Works/RTEMS interrupt handlers can be written in C u VME interrupts have two parameters v v Interrupt level (1 -7, but don’t use level 7) – often set with on-board jumpers or DIP switches Interrupt vector (0 -255, <64 reserved on MC 680 x 0) – often set by writing to an on-board register u OS initialization takes two calls 1. Connect interrupt handler to vector dev. Connect. Interrupt. VME(unsigned vector. Number, void (*p. Function)(void *), void *parameter); 2. Enable interrupt from VME to CPU dev. Enable. Interrupt. Level. VME (unsigned level); EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support I/O Interrupt Record Processing u. Record is processed when hardware interrupt

Developing Device Support I/O Interrupt Record Processing u. Record is processed when hardware interrupt occurs u. Granularity depends on device support and hardware v. Interrupt per-channel vs. interrupt per-card u#include <db. Scan. h> to get additional declarations u. Call scan. Io. Init once for each interrupt source to initialize a local value: scan. Io. Init(&ioscanpvt); u. DSET must provide a get. Io. Int. Info routine to specify the interrupt source associated with a record – a single interrupt source can be associated with more than one record u. Interrupt handler calls scan. Io. Request with the ‘ioscanpvt’ value for that source – this is one of the very few routines which may be called from an interrupt handler EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support The DSET – get. Io. Int. Info long get. Io. Int.

Developing Device Support The DSET – get. Io. Int. Info long get. Io. Int. Info(int cmd, struct … *precord, IOSCANPVT *ppvt); u. Set *ppvt to the value of the IOSCANPVT variable for the interrupt source to be associated with this record u. Must have already called scan. Io. Init to initialize the IOSCANPVT variable u. Return 0 to indicate success or non-zero to indicate failure – in which case the record SCAN field will be set to Passive u. Routine is called with v(cmd=0) when record is set to SCAN=I/O Intr v(cmd=1) when record SCAN field is set to any other value EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support The DSET – special. Linconv long special. Linconv(struct … *precord, int

Developing Device Support The DSET – special. Linconv long special. Linconv(struct … *precord, int after); u. Analog input (ai) and output (ao) record DSETs include this sixth routine u. Called just before (after=0) and just after (after=1) the value of the LINR, EGUL or EGUF fields changes u“Before” usually does nothing u“After” recalculates ESLO from EGUL/EGUF and the hardware range u. If record LINR field is Linear ai record processing will compute val as val = ((rval + roff) * aslo + aoff) * eslo + eoff Ao record processing is similar, but in reverse EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Asynchronous I/O u. Device support must not wait for slow I/O

Developing Device Support Asynchronous I/O u. Device support must not wait for slow I/O u. Hardware read/write operations which take “a long time” to complete must use asynchronous record processing v. TI/O 100 s – definitely “a long time” v. TI/O 10 s – definitely “not a long time” v 10 s < TI/O < 100 s – ? ? ? u. If device does not provide a completion interrupt a “worker” thread can be created to perform the I/O vthis technique is used for Ethernet-attached devices EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Asynchronous I/O – Read/Write Operation u. Check value of precord->pact and

Developing Device Support Asynchronous I/O – Read/Write Operation u. Check value of precord->pact and if zero: v. Set precord->pact to 1 v. Start the I/O operation ³write hardware or send message to worker thread v. Return 0 u. When operation completes run the following code from a thread (i. e. NOT from an interrupt handler) struct rset *prset = (struct rset *)precord->rset; db. Scan. Lock(precord); (*prset->process)(precord); db. Scan. Unlock(precord); u. The record’s process routine will call the device support read/write routine – with precord->pact=1 -Complete the I/O, set rval, etc. EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Asynchronous I/O – Callbacks u. An interrupt handler must not call

Developing Device Support Asynchronous I/O – Callbacks u. An interrupt handler must not call a record’s process routine directly u. Use the callback system (callback. h) to do this u. Declare a callback variable CALLBACK my. Callback; u. Issue the following from the interrupt handler callback. Request. Process. Callback(&my. Call. Back, priority. Low, precord); u. This queues a request to a callback handler thread which will perform the lock/process/unlock operations shown on the previous page u. There are three callback handler threads v. With priorities Low, Medium and High EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Asynchronous I/O – ASYN u. This should be your first consideration

Developing Device Support Asynchronous I/O – ASYN u. This should be your first consideration for new device support u. It provides a powerful, flexible framework for writing device support for v. Message-based asynchronous devices v. Register-based synchronous devices u. Will be completely described in a subsequent lecture ASYN will be covered in the next session. You will find the package and documentation on the EPICS web site. Caveat – there is a learning curve for ASYN … my rule of thumb: ASYN makes it easy to do the hard stuff, but hard to do the easy stuff. EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Hands on # Commands to build a very simple device support

Developing Device Support Hands on # Commands to build a very simple device support # k. furukawa, jan. 2009. # get rrcat. tar. gz from # http: //www-linac. kek. jp/epics/second/rrcat. tar. gz # open terminal # from another terminal mkdir second camonitor {your-username}: SEC 10 cd second # example: camonitor user 7: SEC 10 make. Base. App. pl -t ioc Clock 1 make. Base. App. pl -t ioc -i -p Clock 1 tar xzf ~/Desktop/rrcat. tar. gz perl -i -pe s/CLOCK/{your-username}/ Clock 1 App/Db/ai. Second. db # example: perl -i -pe s/CLOCK/user 7/ Clock 1 App/Db/ai. Second. db make clean install 2>&1 | tee make. log # (if your shell is csh : make clean install |& tee make. log) cd ioc. Boot/ioc. Clock 1 chmod +x st. cmd. /st. cmd EPICS Workshop 2009, RRCAT, India # Please look in the files Clock 1 App/src/dev. Ai. Second. c Clock 1 App/src/ai. Second. dbd Clock 1 App/Db/ai. Second. db Kazuro Furukawa, KEK, Jan. 2009.

Developing Device Support Thank you EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan.

Developing Device Support Thank you EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009.