【问题标题】:reading registers in hw using python使用python读取硬件中的寄存器
【发布时间】:2013-04-28 16:12:10
【问题描述】:

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

这是我的代码。

#!/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 cpu-registers


【解决方案1】:

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

对于这类事情的一些很好的例子,我推荐 O'Reilly 的书 "Real World Instrumentation with Python",作者是 J.M. Hughes。

【讨论】:

  • 我删除第一个链接的原因是它指向一个托管盗版电子书的网站。我们不允许这样做,所以我指向了 O'Reilly 的实际网站。
  • 在我看来是合法的,但我会相信你的话。
猜你喜欢
  • 2010-10-13
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多