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.

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. 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).

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值