简体   繁体   English

使用python读取硬件中的寄存器

[英]reading registers in hw using python

My aim is to read some registers on the FPGA using python script. 我的目的是使用python脚本读取FPGA上的一些寄存器。 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. 我已经在硬件(FPGA)上实现了一些寄存器,并且试图读取寄存器.C中有一些程序能够读取寄存器。 But I have to write the read/write program in python so that I can integrate it with my verifcation environment (written in python). 但是我必须用python编写读/写程序,以便可以将其与我的验证环境(用python编写)集成。 I am new to python (beginner level) so I wish you can guide me through your suggestions and comments. 我是python(初级)的新手,所以希望您能指导我提出建议和意见。 Below is the code which I implemented. 下面是我实现的代码。

This is my code. 这是我的代码。

#!/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)

You won't be able to do that in pure Python. 您将无法在纯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. 如果您拥有的C代码已经在共享库中(Windows中为.dll,在Linux中为.so),则您可以使用ctypes模块访问它。 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). 如果不是这样,则必须将该C代码包装在共享库或Python扩展模块中(在C端更复杂,在Python端更简单)。

For some good examples of this kind of thing, I recommend the O'Reilly book "Real World Instrumentation with Python" , by JM Hughes. 对于这类事情的一些很好的例子,我推荐由JM Hughes撰写的O'Reilly书“ Python的真实世界”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM