2013-05-04 259 views
0

我的目标是读取使用python脚本在FPGA上一些寄存器。 我已经实现一些寄存器的硬件(FPGA),我想读的registers.There在C一些程序,其能够读取寄存器。但我必须在Python中编写读/写程序,以便可以将它与我的验证环境(用python编写)集成。我是python新手(初学者级别),所以我希望你能指导我通过你的建议和意见。以下是我实施的代码。寄存器读取

这是我的代码。

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

请问你的代码的工作?你没有真正说出问题所在。 – 2013-05-04 11:26:43

回答

1

您将无法在纯Python中做到这一点。如果你有C代码已经在共享库文件(.dll在Windows中,。所以在Linux中),那么您可以使用ctypes的模块来访问它。如果没有,你将不得不将这些C代码包装在共享库或Python扩展模块中(C端更复杂,Python端更简单)。

对于这种事情的一些很好的例子,我推荐O'Reilly的书"Real World Instrumentation with Python",由J.M.休斯。

+0

非常感谢李。我感谢你的帮助。我会尝试ctypes .. – user2349460 2013-05-04 12:00:52

+0

我删除第一个链接的原因是,它是一个网站托管盗版电子书。我们不允许在这里,所以我已经指出O'Reilly为本书提供的实际网站。 – 2013-07-13 22:32:25

+0

对我来说看起来很合理,但我会为此付诸流水。 – 2013-07-13 22:54:14