EPICS asynPortDriver使用示例

2023-12-16 12:44

本文主要是介绍EPICS asynPortDriver使用示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在文本中,将展示如何将EPICS asyn模块和其他库联用,从而实现对arm单板机上GPIO口的控制。

在本例中使用到的硬件是:

在程序中需要厂家提供的wringPi库,才能通过C语言库函数调用实现对其GPIO的控制。

以下是这个单板机GPIO的管脚对应关系,本程序中用到的wPi编号是9和10:

本IOC应用程序,仅需要需要base和asyn模块,在configure/RELEASSE文件中指定:

SUPPORT = /usr/local/EPICS/synApps/support
ASYN = ${SUPPORT}/asynEPICS_BASE = /usr/local/EPICS/base

 以下是cpp源代码,它继承了asynPortDriver类,并且重写了其writeUInt32Digital方法,用于在指定GPIO管脚上,设置电平状态:

# drvgpioasyn.cpp
#include <iocsh.h>
#include <epicsThread.h>
#include <asynPortDriver.h>
#include <wiringPi.h>#include <epicsExport.h>static const char *driverName = "GPIOAsynDriver";#define digitalOutputString       "DIGITAL_OUTPUT"#define NUM_IO_BITS     2   // Number of digital I/O bits to USEclass GPIOAsynDriverOut : public asynPortDriver {
public:GPIOAsynDriverOut(const char *portName);/* These are the methods that we override from asynPortDriver */virtual asynStatus writeUInt32Digital(asynUser *pasynUser, epicsUInt32 value, epicsUInt32 mask);virtual void report(FILE *fp, int details);protected:// Analog output parametersint digitalOutput_;#define FIRST_GPIOOUT_PARAM  digitalOutput_#define LAST_GPIOOUT_PARAM digitalOutput_
private:int gpio[2];void init_GPIO_Out();asynStatus setGPIO(int index, int val);
};#define NUM_PARAMS ((int)(&LAST_GPIOOUT_PARAM - &FIRST_GPIOOUT_PARAM + 1))GPIOAsynDriverOut::GPIOAsynDriverOut(const char *portName): asynPortDriver(portName, 1 , NUM_PARAMS,asynUInt32DigitalMask | asynDrvUserMask,  // Interfaces that we implementasynUInt32DigitalMask,                    // Interfaces that do callbacksASYN_MULTIDEVICE | ASYN_CANBLOCK, 1, /* ASYN_CANBLOCK=1, ASYN_MULTIDEVICE=1, autoConnect=1 */0, 0) /* Default priority and stack size */
{// Digital I/O parametersinit_GPIO_Out();createParam(digitalOutputString,     asynParamUInt32Digital, &digitalOutput_);
}asynStatus GPIOAsynDriverOut::writeUInt32Digital(asynUser *pasynUser, epicsUInt32 value, epicsUInt32 mask)
{int function = pasynUser->reason;int status=0;int i;epicsUInt32 outValue=0, outMask;static const char *functionName = "writeUInt32Digital";setUIntDigitalParam(function, value, mask);if (function == digitalOutput_) {for (i=0, outMask=1; i<NUM_IO_BITS; i++, outMask = (outMask<<1)) {// Only write the value if the mask has this bit setoutValue = ((value &outMask) == 0) ? 0 : 1;if ((mask & outMask ) != 0) {status = setGPIO(i, outValue);}}}if (status == 0) {asynPrint(pasynUser, ASYN_TRACEIO_DRIVER,"%s:%s, port %s, wrote outValue=0x%x, value=0x%x, mask=0x%x\n",driverName, functionName, this->portName, outValue, value, mask);} else {asynPrint(pasynUser, ASYN_TRACE_ERROR,"%s:%s, port %s, ERROR writing outValue=0x%x, value=0x%x, mask=0x%x,  status=%d\n",driverName, functionName, this->portName, outValue, value, mask, status);}return (status==0) ? asynSuccess : asynError;
}void GPIOAsynDriverOut::init_GPIO_Out()
{int i;this->gpio[0] = 9;this->gpio[1] = 10;wiringPiSetup();for (i = 0; i < NUM_IO_BITS ; i++){pinMode(this->gpio[i], OUTPUT);digitalWrite(this->gpio[i], 0);}
}asynStatus GPIOAsynDriverOut::setGPIO(int index, int val)
{digitalWrite(this->gpio[index], val);return asynSuccess;
}void GPIOAsynDriverOut::report(FILE *fp, int details)
{fprintf(fp, "  Port: %s,GPIO %d - %d\n", this->portName, this->gpio[0], this->gpio[1]);asynPortDriver::report(fp, details);
}extern "C" int GPIOAsynDriverOutConfig(const char *portName)
{GPIOAsynDriverOut *pGPIOAsynDriverOut = new GPIOAsynDriverOut(portName);pGPIOAsynDriverOut = NULL;return(asynSuccess);
}static const iocshArg configArg0 = { "Port name",      iocshArgString};
static const iocshArg * const configArgs[] = {&configArg0,};
static const iocshFuncDef configFuncDef = {"GPIOAsynDriverOutConfig", 1, configArgs};
static void configCallFunc(const iocshArgBuf *args)
{GPIOAsynDriverOutConfig(args[0].sval);
}void drvGPIOAsynDriverOutRegister(void)
{iocshRegister(&configFuncDef,configCallFunc);
}extern "C" {epicsExportRegistrar(drvGPIOAsynDriverOutRegister);
}

编写一个数据库定义文件:

# drvgpioasyn.dbd
registrar(drvGPIOAsynDriverOutRegister)

在同级目录下的Makefile中指定需要的数据库定义文件,库文件以及源文件:

TOP=../..include $(TOP)/configure/CONFIG
#----------------------------------------
#  ADD MACRO DEFINITIONS AFTER THIS LINE
#=============================#=============================
# Build the IOC applicationPROD_IOC = gpioasyn
# gpioasyn.dbd will be created and installed
DBD += gpioasyn.dbd# gpioasyn.dbd will be made up from these files:
gpioasyn_DBD += base.dbd
gpioasyn_DBD += asyn.dbd
gpioasyn_DBD += drvgpioasyn.dbd# Include dbd files from all support applications:
#gpioasyn_DBD += xxx.dbd# Add all the support libraries needed by this IOC
gpioasyn_LIBS += asyn
gpioasyn_LIBS +=  wiringPi
wiringPi_DIR = /usr/libgpioasyn_SRCS += drvgpioasyn.cpp
# gpioasyn_registerRecordDeviceDriver.cpp derives from gpioasyn.dbd
gpioasyn_SRCS += gpioasyn_registerRecordDeviceDriver.cpp# Build the main IOC entry point on workstation OSs.
gpioasyn_SRCS_DEFAULT += gpioasynMain.cpp
gpioasyn_SRCS_vxWorks += -nil-# Add support from base/src/vxWorks if needed
#gpioasyn_OBJS_vxWorks += $(EPICS_BASE_BIN)/vxComLibrary# Finally link to the EPICS Base libraries
gpioasyn_LIBS += $(EPICS_BASE_IOC_LIBS)#===========================include $(TOP)/configure/RULES

在Db/目录下,定义数据库模板文件digitalout.template,在此仅定义了一个数字量输出记录bo,用于写GPIO管脚:

record(bo, "$(P)$(R)")
{field(PINI, "$(PINI=YES)")field(DESC, "$(DESC=)")field(DTYP, "asynUInt32Digital")field(OUT,  "@asynMask($(PORT),$(ADDR),$(MASK))DIGITAL_OUTPUT")field(ZNAM, "$(ZNAM=Low)")field(ZSV,  "$(ZSV=NO_ALARM)")field(ONAM, "$(ONAM=High)")field(OSV,  "$(OSV=NO_ALARM)")
}

将这个模板文件添加到同级的Makefile文件中。

返回这个IOC的顶层目录,执行make,编译整个项目。

进入启动目录cd iocBoot/iocgpioasyn/,编辑以下文件:

1) digitalout.substitutions

file "$(TOP)/db/digitalout.template"
{
pattern
{ R,   MASK,  ADDR}
{Bo1,  0x01,     0}
{Bo2,  0x02,     0}
}

2) st.cmd:

orangepi@orangepi4-lts:/usr/local/EPICS/program/gpioasyn/iocBoot/iocgpioasyn$ cat st.cmd
#!../../bin/linux-aarch64/gpioasyn#- You may have to change gpioasyn to something else
#- everywhere it appears in this file< envPathscd "${TOP}"## Register all support components
dbLoadDatabase "dbd/gpioasyn.dbd"
gpioasyn_registerRecordDeviceDriver pdbbaseGPIOAsynDriverOutConfig("GPIO")
cd "${TOP}/iocBoot/${IOC}"
dbLoadTemplate("digitalout.substitutions","P=TEST:,PORT=GPIO,ADDR=0,TIMEOUT=1")
iocInit

启动程序,进行测试:

root@orangepi4-lts:/usr/local/EPICS/program/gpioasyn/iocBoot/iocgpioasyn# ../../bin/linux-aarch64/gpioasyn st.cmd
#!../../bin/linux-aarch64/gpioasyn
< envPaths
epicsEnvSet("IOC","iocgpioasyn")
epicsEnvSet("TOP","/usr/local/EPICS/program/gpioasyn")
epicsEnvSet("SUPPORT","/usr/local/EPICS/synApps/support")
epicsEnvSet("EPICS_BASE","/usr/local/EPICS/base")
cd "/usr/local/EPICS/program/gpioasyn"
## Register all support components
dbLoadDatabase "dbd/gpioasyn.dbd"
gpioasyn_registerRecordDeviceDriver pdbbase
## Load record instances
#dbLoadRecords("db/gpioasyn.db","user=orangepi")
GPIOAsynDriverOutConfig("GPIO")
cd "/usr/local/EPICS/program/gpioasyn/iocBoot/iocgpioasyn"
dbLoadTemplate("digitalout.substitutions","P=TEST:,PORT=GPIO,ADDR=0,TIMEOUT=1")
iocInit
Starting iocInit
############################################################################
## EPICS R7.0.7
## Rev. 2023-06-25T15:48+0000
## Rev. Date build date/time:
############################################################################
iocRun: All initialization complete
## Start any sequence programs
#seq sncxxx,"user=orangepi"
epics> dbl
TEST:Bo1
TEST:Bo2

用caput对IOC中加载的两个bo记录进行写1和写0,可以测量到GPIO对应管脚上电平的变化。

这篇关于EPICS asynPortDriver使用示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/500521

相关文章

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU