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对应管脚上电平的变化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/286483.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

手动搭建koa+ts项目框架(swagger文档篇)

文章目录 一、安装依赖二、直接使用json文件生成三、根据对应api注释生成新建swagger.ts文件新建./routes/users.ts文件入口文件引入对应数据如有启发&#xff0c;可点赞收藏哟~ 一、安装依赖 swagger-jsdoc 读取您的JSDoc带注释的源代码并生成OpenAPI (Swagger) 规范koa2-swa…

意大利语文章翻译成中文怎么收费?

随着全球化的步伐加快&#xff0c;不同语言之间的交流日益频繁&#xff0c;其中意大利语翻译成中文的需求逐渐增多。那么&#xff0c;意大利语文章翻译成中文该如何收费呢&#xff1f;又在哪里能找到专业的意大利语翻译呢&#xff1f; 翻译是将一种语言文字转化为另一种语言文字…

Android:安卓学习笔记之OkHttp原理的简单理解和使用

Android OkHttp使用原理的简单理解和使用 OkHttp 0、前言1、请求与响应流程 1.1 请求的封装1.2 请求的发送1.3 请求的调度1.4 请求的处理2、拦截器 2.1 RetryAndFollowUpInterceptor2.2 BridgeInterceptor2.3 CacheInterceptor 2.3.1、HTTP缓存原理2.3.2、强制缓存2.3.3、协商…

Oracle迁移乾坤大挪移,用它轻松拿捏!

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 哈喽&#xff01;大家好&#xff0c;我是【IT邦德】&#xff0c;江湖人称jeames007&#xff0c;10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】&#xff01;&#x1f61c;&am…

智能 GPT 图书馆又重生了

智能 GPT 图书馆又重生了 作者&#xff1a;程序员小白条 1&#xff09;概述 自从大二寒假准备开始筹备这个项目&#xff0c;到现在已经一年了&#xff0c;这个项目能维护一年&#xff0c;不愧是我.jpg。本来这个项目只是想练练手&#xff0c;因为那时候刚学完 Spring Boot2 V…

macOS 安装 oh-my-zsh 后 node 报错 command not found : node

最近为了让终端中显示 git 分支的名称&#xff0c;安装了 oh-my-zsh &#xff0c;安装之后呢&#xff0c;我原先安装的 Volta、 node 都没法用了&#xff0c;报错如下&#xff1a; 这时候粗略判断应该是系统变量出了问题&#xff0c;oh-my-zsh 的变量文件是 ~/.zshrc&#xff0…

【数据结构】单链表的定义和操作

目录 1.单链表的定义 2.单链表的创建和初始化 3.单链表的插入节点操作 4.单链表的删除节点操作 5.单链表的查找节点操作 6.单链表的更新节点操作 7.完整代码 &#x1f308;嗨&#xff01;我是Filotimo__&#x1f308;。很高兴与大家相识&#xff0c;希望我的博客能对你有所帮助…

互式流程图|BPMN JointJS+ JavaScript 3.7.3 Crack

JointJS 是 JavaScript 图表库为卓越的 UI 提供支持 使用经过验证的库快速、自信地构建高级视觉和无代码/低代码应用程序。 赋能全球行业领导者 使用 JointJS 构建的图表 一个库&#xff0c;‍无限 UI 选项 直接在您的应用程序中享受交互式流程图、BPMN 和其他图表工作室。利用…

如何同时给每张PPT插入不同的图片?这2种方法可行!

有时候创作PPT&#xff0c;我们需要把几十张图片插入到PowerPoint中&#xff0c;每张图片作为一张幻灯片&#xff0c;如果一张张手动操作&#xff0c;那就未免太花时间了。今天小编来分享2种方法&#xff0c;可以让您快速给每张PPT插入不同图片。 方法一、使用“创建相册” 1.…

JVM-10-类加载

Java虚拟机把描述类的数据从Class文件加载到内存&#xff0c;并对数据进行校验、转换解析和初始化&#xff0c;最终形成可以被虚拟机直接使用的Java类型&#xff0c;这个过程被称作虚拟机的类加载机制。 一个类型从被加载到虚拟机内存中开始&#xff0c;到卸载出内存为止&#…

JavaScript基础函数+对象+继承

目录 1.创建函数 2.函数分类 2.1带参数函数 2.2匿名函数 2.3嵌套函数 2.4立即执行函数 ES6特有的箭头函数 2.5对象中的函数 3.this对象 4.有参构造函数创建对象 5.原型 prototype 6.函数应用&#xff08;继承&#xff09; 6.1原型链继承 6.2构造继承 6.3组合继承&…

Python Pandas 通过loc/iloc修改局部数据(第9讲)

Python Pandas 通过loc/iloc修改局部数据(第9讲)         🍹博主 侯小啾 感谢您的支持与信赖。☀️ 🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ…