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.