Project

General

Profile

Python tricks » History » Version 3

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