本文介绍了使用python读取硬件中的寄存器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用python脚本读取FPGA上的一些寄存器。
我已经在硬件(FPGA)上实现了一些寄存器,我试图读取这些寄存器。C中有一些程序能够读取这些寄存器。但是我必须用python编写读/写程序,以便可以将其与我的验证环境(用python编写)集成。我是python(初级)的新手,所以希望您能指导我提出建议和意见。下面是我实现的代码。

My aim is to read some registers on the FPGA using python script.I have implemented some registers on the hardware (FPGA) and I am trying to read the registers.There are some programs in C which are able to read the registers. But I have to write the read/write program in python so that I can integrate it with my verifcation environment (written in python). I am new to python (beginner level) so I wish you can guide me through your suggestions and comments. Below is the code which I implemented.

这是我的代码。

#!/usr/bin/env python


import array
import fcntl
import re
import socket
import struct
import os

#connectedSockets = {}

# IOCTL Commands
SIOCREGREAD = 0x89F0
SIOCREGWRITE = 0x89F1

reg = 0x58000008

# open the NF descriptor
# Open a file
nf = os.open( "/dev/nf10", os.O_RDWR )
print "Opened NF descriptor"

# Now get a file object for the above file.
nf0 = os.fdopen(nf, "w+")

#print "opened nf0 file object"
inner_struct = struct.pack("II", reg, 0x0)
inner_struct_pinned = array.array('c', inner_struct)
print inner_struct_pinned
fcntl.ioctl(nf0, SIOCREGREAD,)
retval = struct.unpack("II", inner_struct_pinned)[0]
print retval

os.fdclose(nf)


推荐答案

您将无法在纯Python中做到这一点。如果您拥有的C代码已经在共享库中(Windows中为.dll,在Linux中为.so),则您可以使用ctypes模块访问它。如果不是这样,则必须将该C代码包装在共享库或Python扩展模块中(C端更复杂,Python端更简单)。

You won't be able to do that in pure Python. If the C code you have is already in a shared library (.dll in Windows, .so in Linux), then you may be able to access it using the ctypes module. If not, you will have to wrap that C code in either a shared library or a Python extension module (more complex on the C side, simpler on the Python side).

对于这类事情的一些很好的例子,我推荐O'Reilly的书,作者:JM Hughes。

For some good examples of this kind of thing, I recommend the O'Reilly book "Real World Instrumentation with Python", by J.M. Hughes.

这篇关于使用python读取硬件中的寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 16:33