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

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时