Project

General

Profile

Python tricks » History » Version 2

Alexis Jeandet, 25/03/2015 11:30 AM

1 1 Alexis Jeandet
h1. Python tricks
2
3
{{>toc}}
4
5
h2. Basic SocExplorer interaction
6
7 2 Alexis Jeandet
An interesting feature in SocExplorer is that you can drag and drop a python script in the terminal, it will execute it. First you can get some updated examples in the doc folder of SocExplorer "source code":https://hephaistos.lpp.polytechnique.fr/rhodecode/HG_REPOSITORIES/LPP/INSTRUMENTATION/SocExplorer.
8 1 Alexis Jeandet
9
h3. Plugin related functions
10
11
* To load a plugin from python
12
13
 The key object to load plugins is the proxy object, in SocExplorer console you can type proxy and then with the "tab" key you will get all the available methods. The ones you need are loadSysDriver() and loadSysDriverToParent(), you can get their definition by invoking them without any arguments. In most cases with SocExplorer if you don't know how to use a python method just call it and you will get its definition.
14
15
 ** Load a root plugin
16
17
  <pre><code class="python">
18
19
#to load the AHBUART plugin
20
proxy.loadSysDriver("AHBUARTplugin")  
21
# You can also specify an instance name
22
proxy.loadSysDriver("AHBUARTplugin","InstanceName")
23
# SocExplorer can also resolve the plugin from it file name
24
proxy.loadSysDriver("/home/your-path/.SocExplorer/plugins/libahbuartplugin.so")
25
# Even without the path
26
proxy.loadSysDriver("libahbuartplugin.so")
27
28
  </code></pre>
29
30
31
 **  Load a child plugin
32
33
  A child plugin need a parent to be connected to.
34
35
  <pre><code class="python">
36
37
#to load the GenericRWplugin plugin and connect it to AHBUARTplugin0 instance
38
proxy.loadSysDriverToParent("GenericRWplugin", "AHBUARTplugin0")  
39
# You can also specify an instance name for the child plugin
40
proxy.loadSysDriverToParent("GenericRWplugin","InstanceName", "AHBUARTplugin0")  
41
#As for root plugin SocExplorer can resolve the plugin from it file name.
42
43
  </code></pre>
44
45
 ** Common plugin functions
46
47
 All the plugins will provide you two methods to read or write, with root plugin it means read or write in the SOC bus. With child driver it can have different meaning depending on the plugin. Usually to read or write on your SOC just do:
48
49
  <pre><code class="python">
50
51
#rootplugin is your root plugin instance name, address is the address from where you want to read.
52
#count is the number of words you want to read /!\ usually it is 32 bits words. 
53
#If count=2 you will read two 32 bits words. 
54
data=rootplugin.Read(address, count)  
55
56
#If you want to write just do
57
rootplugin.Write(address, [Word1,Word2,...])
58 2 Alexis Jeandet
59
  </code></pre>
60
61
 ** More advanced memory dump/load functions
62
63
  Since revision r71 of SocExplorer all plugins offer you some memory dump or load functions. You can easily load an elf, srec or binary file directly in the soc memory or dump any memory space in a binary or srec file.
64
65
  <pre><code class="python">
66
67
#rootplugin is your root plugin instance name, address is the address from where you want to read.
68
#count is the number of words you want to read /!\ usually it is 32 bits words. 
69
#If count=2 you will read two 32 bits words. 
70
#file is the source or destination file name "/yourpath/yourfilename" 
71
#format is the file format 'srec','binary'
72
data=rootplugin.Read(address, count)  
73
rootplugin.dumpMemory(address, count, file, format)
74
75
#If you want to load a file
76
#file is an abstractBinFile, you need to create one before 
77
78
#for an elf file
79
file = PySocExplorer.ElfFile("/yourpath/yourfilename")
80
81
#for an srec file
82
file = PySocExplorer.srecFile("/yourpath/yourfilename")
83
84
#for a binary file
85
file = PySocExplorer.binaryFile("/yourpath/yourfilename")
86
87
rootplugin.loadfile(file)
88 1 Alexis Jeandet
89
  </code></pre>
90
91
h3. SOC related functions
92
93
 * Find a peripheral address
94
95
With SOC design on FPGA, you can easily change the number of peripherals or their base address but you don't necessary want to update your python script each time you modify your SOC layout. One solution is to use plug and play feature such as the one provided by the Gaisler's GRLIB. SocExplorer is able to store and distribute the list of peripherals and their address, once a dedicated plugin did the scan for him such as the AMBA plugin. We suppose that the connection to the SOC is active, the scan is done and the root plugin instance name is "RootPlugin".
96
97
98
 <pre><code class="python">
99
100
#this function will return the base address of the first matching VID/PID device in the list
101
#by convention it is also the lower address one
102
baseAddress = SocExplorerEngine.getEnumDeviceBaseAddress("RootPlugin",VID,PID)
103
104
#you an also specify the index of the device, if you want the second
105
baseAddress = SocExplorerEngine.getEnumDeviceBaseAddress("RootPlugin",VID,PID,1)
106
107
  </code></pre>
108
109
h2. SocExplorer provided objects
110
111
h3. SocExplorer plot
112
113
SocExplorerPlot is a wrapper to the "QCustomPlot":http://www.qcustomplot.com/ class, a simple and efficient plot widget. The following example should give you this result, please note that you will also need numpy library to run it. 
114
115
p=. !{width: 80%}SocExplorerPlot.png(SocExplorerPlot example)!
116
117
  <pre><code class="python">
118
119
import numpy as np
120
freq1 = 30
121
freq2 = 300
122
time_step = 0.001
123
124
t_ini = -50 * 1.0/(max(freq1,freq2))
125
t_fin = -1 * t_ini
126
127
time_vec = np.arange(t_ini, t_fin, time_step)
128
129
#input signal
130
input_sig1 = np.sin(2 * np.pi * freq1 * time_vec)
131
input_sig2 = np.sin(2 * np.pi * freq2 * time_vec)
132
input_sig = input_sig1 + input_sig2
133
134
135
plot=PySocExplorer.SocExplorerPlot()
136
plot.setTitle("demo")
137
plot.setXaxisLabel("Time(s)")
138
plot.setYaxisLabel("Values")
139
140
Courbe1=plot.addGraph()
141
Courbe2=plot.addGraph()
142
Courbe3=plot.addGraph()
143
144
plot.setGraphData(Courbe1,time_vec.tolist(),input_sig1.tolist())
145
plot.setGraphData(Courbe2,time_vec.tolist(),input_sig2.tolist())
146
plot.setGraphData(Courbe3,time_vec.tolist(),input_sig.tolist())
147
148
pen=plot.getGraphPen(1)
149
pen.setWidth(1)
150
color=pen.color()
151
color.setRgb(0x00FF00)
152
pen.setColor(color)
153
plot.setGraphPen(1,pen)
154
155
pen=plot.getGraphPen(0)
156
pen.setWidth(1)
157
color=pen.color()
158
color.setRgb(0xFF0000)
159
pen.setColor(color)
160
plot.setGraphPen(2,pen)
161
162
plot.rescaleAxis()
163
164
  </code></pre>
165
166
h3. TCP_Terminal_Client
167
168
Sometime you need to print some information while your python script is running, unfortunately SocExplorer isn't multi-threaded so you won't get any output until your script execution is finished. To solve this problem with SocExplorer setup you will get a small tcp terminal program which will run in a separated process. From one Python object you will be able to start the terminal process and to send it some data to print. Note that an other utilization of this terminal should be to deport the print outputs on a distant computer. 
169
170
* Simple example 1
171
172
  <pre><code class="python">
173
174
term=PySocExplorer.TCP_Terminal_Client()
175
term.startServer()
176
term.connectToServer()
177
term.sendText("hello")
178
179
  </code></pre>
180
181
Should give you this result
182
183
p=. !{width: 40%}TCP_Terminal_Client1.png(SocExplorerPlot example)!
184
185
* Distant connection
186
187
  <pre><code class="python">
188
189
term=PySocExplorer.TCP_Terminal_Client()
190
#for a distant connection you can specify the distant address and port
191
#remember to check your firewall setings
192
term.connectToServer("192.168.1.10",2000)
193
term.sendText("hello")
194
195
  </code></pre>
196
197
* Multi-terminal and HTML print
198
199
  <pre><code class="python">
200
201
import time
202
terminal=PySocExplorer.TCP_Terminal_Client()
203
terminal.startServer()
204
terminal2=PySocExplorer.TCP_Terminal_Client()
205
terminal2.startServer(2200)
206
terminal2.connectToServer("127.0.0.1",2200)
207
208
terminal.connectToServer()
209
terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"Hello World")
210
terminal2.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"Hello World on terminal 2")
211
terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>")
212
terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"<ul>HTML Items List Example:<LI>Item1</LI><LI>Item2</LI></ul>")
213
terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"<p style=\"color:#0000ff\" style=\"background-color:#00ff00\">hello</p>") 
214
for i in range(0,100):
215
  terminal.sendText("<p><b>"+str(time.ctime())+": </b></p>"+"<p style=\"color:#0000ff\" style=\"background-color:#00ff00\">hello "+str(i)+"</p>") 
216
  time.sleep(0.05)
217
218
  </code></pre>
219
220
221
Should give you this result
222
223
p=. !{width: 70%}TCP_Terminal_Client2.png(SocExplorerPlot example)!
224
225
h3. QhexSpinBox
226
227
h3. QhexEdit