Project

General

Profile

Python tricks » History » Version 4

Alexis Jeandet, 25/03/2015 02:56 PM

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 4 Alexis Jeandet
  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 1 Alexis Jeandet
15
 ** Load a root plugin
16
17 4 Alexis Jeandet
 <pre><code class="python">
18 1 Alexis Jeandet
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 4 Alexis Jeandet
 </code></pre>
29 1 Alexis Jeandet
30
31
 **  Load a child plugin
32
33
  A child plugin need a parent to be connected to.
34
35 4 Alexis Jeandet
 <pre><code class="python">
36 1 Alexis Jeandet
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 4 Alexis Jeandet
 </code></pre>
44 1 Alexis Jeandet
45
46 4 Alexis Jeandet
47 1 Alexis Jeandet
 ** Common plugin functions
48
49 4 Alexis Jeandet
  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:
50 1 Alexis Jeandet
51 4 Alexis Jeandet
 <pre><code class="python">
52 1 Alexis Jeandet
53
#rootplugin is your root plugin instance name, address is the address from where you want to read.
54
#count is the number of words you want to read /!\ usually it is 32 bits words. 
55
#If count=2 you will read two 32 bits words. 
56
data=rootplugin.Read(address, count)  
57
58
#If you want to write just do
59 2 Alexis Jeandet
rootplugin.Write(address, [Word1,Word2,...])
60
61 4 Alexis Jeandet
 </code></pre>
62 3 Alexis Jeandet
63
64
65
 ** More advanced memory dump or load functions
66 1 Alexis Jeandet
67 3 Alexis Jeandet
  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.
68 2 Alexis Jeandet
69 4 Alexis Jeandet
 <pre><code class="python">
70 2 Alexis Jeandet
71
#rootplugin is your root plugin instance name, address is the address from where you want to read.
72
#count is the number of words you want to read /!\ usually it is 32 bits words. 
73
#If count=2 you will read two 32 bits words. 
74
#file is the source or destination file name "/yourpath/yourfilename" 
75
#format is the file format 'srec','binary'
76
data=rootplugin.Read(address, count)  
77
rootplugin.dumpMemory(address, count, file, format)
78
79
#If you want to load a file
80
#file is an abstractBinFile, you need to create one before 
81
82
#for an elf file
83
file = PySocExplorer.ElfFile("/yourpath/yourfilename")
84
85
#for an srec file
86
file = PySocExplorer.srecFile("/yourpath/yourfilename")
87
88
#for a binary file
89
file = PySocExplorer.binaryFile("/yourpath/yourfilename")
90
91
rootplugin.loadfile(file)
92 1 Alexis Jeandet
93
  </code></pre>
94
95
h3. SOC related functions
96
97
 * Find a peripheral address
98
99
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".
100
101
102
 <pre><code class="python">
103
104
#this function will return the base address of the first matching VID/PID device in the list
105
#by convention it is also the lower address one
106
baseAddress = SocExplorerEngine.getEnumDeviceBaseAddress("RootPlugin",VID,PID)
107
108
#you an also specify the index of the device, if you want the second
109
baseAddress = SocExplorerEngine.getEnumDeviceBaseAddress("RootPlugin",VID,PID,1)
110
111
  </code></pre>
112
113
h2. SocExplorer provided objects
114
115
h3. SocExplorer plot
116
117
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. 
118
119
p=. !{width: 80%}SocExplorerPlot.png(SocExplorerPlot example)!
120
121
  <pre><code class="python">
122
123
import numpy as np
124
freq1 = 30
125
freq2 = 300
126
time_step = 0.001
127
128
t_ini = -50 * 1.0/(max(freq1,freq2))
129
t_fin = -1 * t_ini
130
131
time_vec = np.arange(t_ini, t_fin, time_step)
132
133
#input signal
134
input_sig1 = np.sin(2 * np.pi * freq1 * time_vec)
135
input_sig2 = np.sin(2 * np.pi * freq2 * time_vec)
136
input_sig = input_sig1 + input_sig2
137
138
139
plot=PySocExplorer.SocExplorerPlot()
140
plot.setTitle("demo")
141
plot.setXaxisLabel("Time(s)")
142
plot.setYaxisLabel("Values")
143
144
Courbe1=plot.addGraph()
145
Courbe2=plot.addGraph()
146
Courbe3=plot.addGraph()
147
148
plot.setGraphData(Courbe1,time_vec.tolist(),input_sig1.tolist())
149
plot.setGraphData(Courbe2,time_vec.tolist(),input_sig2.tolist())
150
plot.setGraphData(Courbe3,time_vec.tolist(),input_sig.tolist())
151
152
pen=plot.getGraphPen(1)
153
pen.setWidth(1)
154
color=pen.color()
155
color.setRgb(0x00FF00)
156
pen.setColor(color)
157
plot.setGraphPen(1,pen)
158
159
pen=plot.getGraphPen(0)
160
pen.setWidth(1)
161
color=pen.color()
162
color.setRgb(0xFF0000)
163
pen.setColor(color)
164
plot.setGraphPen(2,pen)
165
166
plot.rescaleAxis()
167
168
  </code></pre>
169
170
h3. TCP_Terminal_Client
171
172
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. 
173
174
* Simple example 1
175
176
  <pre><code class="python">
177
178
term=PySocExplorer.TCP_Terminal_Client()
179
term.startServer()
180
term.connectToServer()
181
term.sendText("hello")
182
183
  </code></pre>
184
185
Should give you this result
186
187
p=. !{width: 40%}TCP_Terminal_Client1.png(SocExplorerPlot example)!
188
189
* Distant connection
190
191
  <pre><code class="python">
192
193
term=PySocExplorer.TCP_Terminal_Client()
194
#for a distant connection you can specify the distant address and port
195
#remember to check your firewall setings
196
term.connectToServer("192.168.1.10",2000)
197
term.sendText("hello")
198
199
  </code></pre>
200
201
* Multi-terminal and HTML print
202
203
  <pre><code class="python">
204
205
import time
206
terminal=PySocExplorer.TCP_Terminal_Client()
207
terminal.startServer()
208
terminal2=PySocExplorer.TCP_Terminal_Client()
209
terminal2.startServer(2200)
210
terminal2.connectToServer("127.0.0.1",2200)
211
212
terminal.connectToServer()
213
terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"Hello World")
214
terminal2.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"Hello World on terminal 2")
215
terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>")
216
terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"<ul>HTML Items List Example:<LI>Item1</LI><LI>Item2</LI></ul>")
217
terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"<p style=\"color:#0000ff\" style=\"background-color:#00ff00\">hello</p>") 
218
for i in range(0,100):
219
  terminal.sendText("<p><b>"+str(time.ctime())+": </b></p>"+"<p style=\"color:#0000ff\" style=\"background-color:#00ff00\">hello "+str(i)+"</p>") 
220
  time.sleep(0.05)
221
222
  </code></pre>
223
224
225
Should give you this result
226
227
p=. !{width: 70%}TCP_Terminal_Client2.png(SocExplorerPlot example)!
228
229
h3. QhexSpinBox
230
231
h3. QhexEdit