Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 2013: The ASYN Software Module EPICS The ASYN Software Module Andy Foster Observatory Sciences Limited.

Similar presentations


Presentation on theme: "1 2013: The ASYN Software Module EPICS The ASYN Software Module Andy Foster Observatory Sciences Limited."— Presentation transcript:

1 1 2013: The ASYN Software Module EPICS The ASYN Software Module Andy Foster Observatory Sciences Limited

2 2 2013: The ASYN Software Module EPICS Outline u Where is the ASYN layer? u What is ASYN? u Key concepts of ASYN u ASYN Architecture u Vocabulary u Asyn Port u Asyn Interfaces u Asyn Command u Asyn User u AsynManager u Synchronous control flow u Asynchronous control flow u Writing ASYN device support u Writing ASYN driver support u Porting existing drivers to ASYN u Asyn Trace u asynRecord u Summary u Where to find more information

3 3 2013: The ASYN Software Module EPICS Where is the ASYN layer? u Traditionally, the interface between device and driver support u Historical name: supports synchronous as well as asynchronous devices RECORD SUPPORT DEVICE SUPPORT ASYN LAYER DATABASE ACCESS CHANNEL ACCESS DRIVER SUPPORT

4 4 2013: The ASYN Software Module EPICS What is ASYN? u The EPICS interface between device support and driver support was only loosely defined. u Device support has direct access to the record structure and can manipulate all fields u A driver support library could have almost any API. It can even be an externally provided general purpose library u ASYN is a software module which provide facilities for interfacing device and driver support u read(), write() interface with parameter and address passing u Handles some of the generic housekeeping tasks in device support

5 5 2013: The ASYN Software Module EPICS What is ASYN? u Asyn provides implementation of device support for standard records u ai,ao,bi,bo,mbbi,mbbo,waveform and so on u Handles the details of the interaction with the record fields u Asyn drivers only need to read/write data to/from hardware and the driver support layer u No need to write support specifically for every record u But not limited to use with EPICS device/driver support u Only depends on libCom from EPICS base u Other C code can talk directly to an Asyn support module (e.g. SNL, genSub) u Some support for generic communication protocols u Serial interfaces RS232/485 u Ethernet TCP/IP, UDP/IP

6 6 2013: The ASYN Software Module EPICS Key concepts of ASYN u The ASYN layer has two interfaces: u Upwards to the device support layer. u This has been written for all the standard EPICS records. u Downwards to a device driver u Here a set of standard interfaces have been defined for both message passing (e.g. serial) and register reading/writing (e.g. DAC based devices). u Therefore, for a new piece of hardware, we only need to write a driver which implements one or more of the standard interfaces to the ASYN layer.

7 7 2013: The ASYN Software Module EPICS ASYN Architecture Device support (or SNL code, another driver, or non-EPICS software) device Port (named object) Port driver addr=0addr=1 Interfaces (named; Pure Virtual Functions) asynCommon (connect, report, …) asynOctet (write, read, setInputEos,…)

8 8 2013: The ASYN Software Module EPICS Vocabulary: ASYN Port u Provides access to a device u portName (string) provides the reference to the hardware u Drivers register a port, device support connects to it u One or many devices can be connected, addresses identify individual devices u May be blocking or non-blocking u Depends on speed of device u Is configured in startup script: drvAsynSerialPortConfigure ("COM2", "/dev/sttyS1) drvAsynIPPortConfigure ("fooServer", " :40000) myDeviceDriverConfigure ("portname", parameters)

9 9 2013: The ASYN Software Module EPICS Vocabulary: ASYN Interfaces u Device support (and other ASYN clients) communicate with driver support through defined interfaces u Each interface defines a table of driver functions or methods u Examples are: u asynOctet (read, write, setInputEOS, etc) u Used for message based I/O: serial, TCP/IP u asynUInt32Digital (read, write, u Used for bit field registers: status word, switches, etc u asynInt32, asynInt32Array (read, write, getBounds, …) u Integer registers: ADC, DAC, encoder, … u Integer arrays: spectrum analyzer, oscilloscope u asynFloat64, asynFloat64Array (read, write,…) u Floating point registers and arrays

10 : The ASYN Software Module EPICS Vocabulary: ASYN Interfaces u asynCommon (report, connect, disconnect) u report: Generates a report about the hardware device u connect/disconnect: connect/disconnect to the hardware device u Every driver must implement these! u Every port has one or many interfaces

11 : The ASYN Software Module EPICS Vocabulary: ASYN Command u An ASYN driver defines a set of commands it supports e.g. enum FINS_COMMANDS { FINS_MODEL, …, … } (Factory Intelligent Network Service - PLC) u In an EPICS database the ASYN command is the argument at the end of the INP or OUT field. The DTYP field is set to the type of ASYN interface being used e.g. record(waveform, "$(device):MODEL:CPU") { field(DTYP, "asynOctetRead") field(INP, addr, timeout) FINS_MODEL") }

12 : The ASYN Software Module EPICS Vocabulary: ASYN Command u ASYN commands are supported through a special interface. u asynDrvUser ( create, getType, destroy) u create: maps the ENUM command to the string used in INP/OUT in the database u getType: Looks up ENUM command based on the database string. u Destroys the resources created by create. asynStatus drvUserCreate(void *pvt, asynUser *pasynUser, const char *drvInfo, const char **pptypeName, size_t *psize) { if (drvInfo) { if (strcmp("FINS_MODEL", drvInfo) == 0) { pasynUser->reason = FINS_MODEL; } else { } }

13 : The ASYN Software Module EPICS Vocabulary: asynUser u Identifies the client e.g. EPICS record. u Each client needs one asynUser u An asynUser must not be shared between parts of code that can simultaneously access a driver. For example, device support for EPICS records should create a separate asynUser for each record instance. By creation of an asynUser we obtain a handle for accessing ports and for calling interfaces implemented by drivers. In writing ASYN device support, before doing anything you must obtain a pointer to an asynUser pasynUser=pasynManager->createAsynUser( processCallback, timeoutCallback); u Provide 2 callbacks: u processCallback is called when you are scheduled to access the port u timeoutCallback is called if port times out

14 : The ASYN Software Module EPICS Vocabulary: asynManager u Core of ASYN. u Creates threads for blocking ports. u Registers and finds ports and interfaces. u Schedules access to ports. u Device support and driver support do not need to implement queues or semaphores, this is handled by asynManager. There is exactly one global instance: pasynManager u Clients ask asynManager for services pasynManager->connectDevice(pasynUser, "portname", address) pasynManager->findInterface(pasynUser, interfaceType,...) pasynManager->queueRequest(pasynUser, priority, timeout)

15 : The ASYN Software Module EPICS Control flow for non- blocking port 1. Record processing calls device support. 2. Device support calls queueRequest. 3. Since the port is synchronous, queueRequest calls lockPort and then processCallback. 4. processCallback calls the driver. The driver returns the results of the I/O operation to processCallback. 5. processCallback returns to queueRequest, which calls unlockPort and returns to device support, which returns to record support to complete processing. Used for devices that provide fast responses, typically VME or register based devices

16 : The ASYN Software Module EPICS Control flow for blocking port 1. Record processing calls device support with PACT=0. 2. Device support calls queueRequest. 3. queueRequest places the request on the driver queue (the application thread can now continue). 4. The portThread removes the request from the queue. 5. The portThread calls the users processCallback located in device support. 6. processCallback calls the driver. The driver blocks until the operation is complete and returns the results of the I/O operation to processCallback. 7. processCallback calls the EPICS routine callbackRequestProcess Callback to make the record process again. 8. Record support calls device support again, with PACT=1. Device support updates fields in the record and returns to record support to complete the processing. Used for slow devices like serial or ethernet

17 : The ASYN Software Module EPICS Writing ASYN Device Support Step 1: Connect to the port Before doing anything you must obtain a pointer to an asynUser pasynUser=pasynManager->createAsynUser( processCallback, timeoutCallback); u Connect to the device (port, address) status=pasynManager->connectDevice(pasynUser, port, addr); u Find the interface (e.g. asynOctet) pasynInterface=pasynManager->findInterface (pasynUser, asynOctetType, 1); u We can now find the address of the asynOctet interface and of the private driver structure: pasynOctet =(asynOctet *)pasynInterface->pinterface; drvPvt = pasynInterface->pdrvPvt; u The following call is made from processCallback when we have access to the port (next slide): pasynOctet->read ( drvPvt, pasynUser,...

18 : The ASYN Software Module EPICS Step 2: Request access to the port u Ask asynManager to put your request to the queue status=pasynManager->queueRequest(pasynUser, priority, timeout); Priorities: asynQueuePriority { Low | Medium | High } queueRequest never blocks. Blocking port: AsynManager will call your processCallback when port is free. The callback runs in port thread. Non blocking port: queueRequest calls processCallback. If port is not free for timeout seconds, asynManager calls timeoutCallback. In processCallback, you have exclusive access to the port.

19 : The ASYN Software Module EPICS Step 3: processCallback (asynOctet methods) u Flush (discard old input) status=pasynOctet->flush(drvPvt, pasynUser); Write: Status = pasynOctet->write( drvPvt,pasynUser,data,size,&bytesWritten); Actual number of written bytes is returned in bytesWritten. u Read: status=pasynOctet->read(drvPvt, pasynUser,buffer,maxsize,&bytesReceived,&eomReason); Actual number of written bytes is returned in bytesReceived. End of message reason is returned in eomReason.

20 : The ASYN Software Module EPICS Step 3: processCallback (asynInt32 methods) u Get bounds status=pasynInt32->getBounds( drvPvt, pasynUser, &low, &high); Limits for valid register values are returned in low and high. u Write status=pasynInt32->write( drvPvt, pasynUser, value); u Read status=pasynInt32->read( drvPvt, pasynUser, &value); Current register value is returned in value.

21 : The ASYN Software Module EPICS Rules for using driver methods Never use I/O methods outside processCallback. u Only talk to the port that has called you back. u You can do as many I/O as you like. You must always use the interface method table pasyn { Octet | Int32 |…} to access the driver. You always need pasynUser as an argument. u All other clients of the same port (even with other addresses) have to wait until you are finished. So, remember, its not nice of you if your device blocks for a long time!

22 : The ASYN Software Module EPICS Writing ASYN Driver Support u Since the interface to the device support layer has been written for most common EPICS records, we are more likely to need to write an ASYN driver. u Starting design of a new ASYN based driver u Decide whether your device is synchronous or asynchronous (timing) u Choose a number of commands which your driver will implement u Identify which standard interfaces are appropriate for communicating with the device u A driver must maintain a data structure for all its internal storage u Common practice for most drivers – not just asyn u A pointer to this structure will be passed around as an argument to the various callbacks to the driver code

23 : The ASYN Software Module EPICS Writing ASYN Driver Support u asynCommon and asynDrvUser must be registered with asynManager u For each data interface, you must supply a number of methods u read, write are the main ones u Each data interface must be initialised

24 : The ASYN Software Module EPICS Example ASYN driver: foo #include... Define functions here... static struct asynCommon foo_Common = { fooReport, fooConnect, fooDisconnect }; static asynDrvUser foo_DrvUser = { fooCreate, fooGetType, fooDestroy}; static asynInt32 foo_Int32 = { fooInt32Read, fooInt32Write, NULL, NULL, NULL}; typedef struct drvPvt {... char *portName; asynInterface common; asynInterface drvUser; asynInterface int32; } drvPvt; /* Add asynInterface lines to a non-asyn driver */

25 : The ASYN Software Module EPICS ASYN driver initialisation routine fooInit( char *portName, char *address ) /* startup script */ { drvPvt *pFoo; pFoo = callocMustSucceed(1, sizeof(drvPvt), FUNCNAME); pdrvPvt->portName = epicsStrDup(portName); pFoo->common.interfaceType = asynCommonType; pFoo->common.pinterface = (void *)&foo_Common; pFoo->common.drvPvt = pFoo; pFoo->drvUser.interfaceType = asynDrvUserType; pFoo->drvUser.pinterface = (void *)&foo_DrvUser; pFoo->drvUser.drvPvt = pFoo; pFoo->int32.interfaceType = asynInt32Type; pFoo->int32.pinterface = (void *)&foo_Int32; pFoo->int32.drvPvt = pFoo; pasynManager->registerPort( pFoo->portName, ASYN_MULTIDEVICE | ASYN_CANBLOCK, /* ASYN_MULTIDEVICE set: port supports > 1 device */ /* ASYN_CANBLOCK set: separate thread for the port */ 1, /* autoconnect: asynmanager connects automatically */ 0, /* medium priority for thread */ 0 ) /* default stack size for thread */ pasynManager->registerInterface (pFoo->portName, &pFoo->common); pasynManager->registerInterface (pFoo->portName, &pFoo->drvUser); pasynInt32Base->initialize (pFoo->portName, &pFoo->int32); }

26 : The ASYN Software Module EPICS Implementation of driver read function static asynStatus fooInt32Read( void *drvPvt, asynUser *pasynUser, epicsInt32 *value) { drvPvt *pdrvPvt = (drvPvt *)drvPvt; epicsInt32 addr; /* Find out which address on the device we are reading */ pasynManager->getAddr(pasynUser,&addr); /* pasynUser->reason is set in the asynDrvUser create method – see earlier */ switch ( pasynUser->reason ) { case AMP_GAIN: *value = readAmpGainFromDevice(); break; case AMP_CLOCK_READ: *value = readAmpClock(); break; } return asynSuccess; }

27 : The ASYN Software Module EPICS Porting existing drivers to ASYN u Depends on the existing design u Does it have both device and driver support? u Device support is supplied by asyn so remove or untangle existing device support u Identify and name specific commands to communicate with the device u Implement an enum type with the commands and a lookup table for string representations u Implement driver read + write functions u Typically with big switch/case structure to handle different reasons or commands u Implement driver structure u A driver structure should already exist (common in way of keeping track of a device) u Add the asyn interface pointers to the structure u Initialisation routine must register and initialize the relevant interfaces

28 : The ASYN Software Module EPICS Vocabulary: asynTrace u Diagnostic facility u Provides routines to call for diagnostic messages: asynPrint(), asynPrintIO() u Several masks or levels can be selected for debugging purposes u Provides consistent debugging mechanism for drivers

29 : The ASYN Software Module EPICS asynRecord u Special record type that can use all asyn interfaces. u Can connect to different ports at run-time. u Is a good debug tool. u Access to options, including tracing. u Comes with set of medm screens for different interfaces. u Can handle simple devices: u e.g. asynOctet: write one string, read one string u If a new instrument arrives that has a serial, GPIB or ethernet port, then it is often possible to communicate with it just by attaching an asynRecord i.e. a database containing one record!

30 : The ASYN Software Module EPICS asynRecord medm screens

31 : The ASYN Software Module EPICS Summary Advantages of ASYN u Drivers implement standard interfaces that can be accessed from: u Multiple record types u SNL programs u Other drivers u Generic device support eliminates the need for separate device support in 90% of cases u Consistent trace/debugging at ( port, addr ) level u asynRecord can be used for testing, debugging, and actual I/O applications u Easy to add ASYN interfaces to existing drivers: u Register port, implement interface write(), read() and change debugging output u Preserve 90% of driver code

32 : The ASYN Software Module EPICS More information u AsynDriver u u StreamDevice u epics.web.psi.ch/software/streamdevice/ epics.web.psi.ch/software/streamdevice/ u linuxGpib u linux-gpib.sourceforge.net/ linux-gpib.sourceforge.net/ u Drivers/device supports using asynDriver u u Talks about asynDriver u cs/ASYN.html cs/ASYN.html u


Download ppt "1 2013: The ASYN Software Module EPICS The ASYN Software Module Andy Foster Observatory Sciences Limited."

Similar presentations


Ads by Google