| 56 |
benedikt93 |
1 |
#!/usr/bin/env python
|
|
|
2 |
#
|
|
|
3 |
#
|
| 171 |
farthen |
4 |
# Copyright 2010 TheSeven, benedikt93, Farthen
|
| 56 |
benedikt93 |
5 |
#
|
|
|
6 |
#
|
| 427 |
farthen |
7 |
# This file is part of emCORE.
|
| 56 |
benedikt93 |
8 |
#
|
| 427 |
farthen |
9 |
# emCORE is free software: you can redistribute it and/or
|
| 56 |
benedikt93 |
10 |
# modify it under the terms of the GNU General Public License as
|
|
|
11 |
# published by the Free Software Foundation, either version 2 of the
|
|
|
12 |
# License, or (at your option) any later version.
|
|
|
13 |
#
|
| 427 |
farthen |
14 |
# emCORE is distributed in the hope that it will be useful,
|
| 56 |
benedikt93 |
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
17 |
# See the GNU General Public License for more details.
|
|
|
18 |
#
|
|
|
19 |
# You should have received a copy of the GNU General Public License
|
| 427 |
farthen |
20 |
# along with emCORE. If not, see <http://www.gnu.org/licenses/>.
|
| 56 |
benedikt93 |
21 |
#
|
|
|
22 |
#
|
|
|
23 |
|
| 402 |
farthen |
24 |
"""
|
| 427 |
farthen |
25 |
emCORE client library.
|
|
|
26 |
Provides functions to communicate with emCORE devices via the USB bus.
|
| 402 |
farthen |
27 |
"""
|
|
|
28 |
|
| 56 |
benedikt93 |
29 |
import sys
|
|
|
30 |
import struct
|
| 506 |
farthen |
31 |
import ctypes
|
| 171 |
farthen |
32 |
import usb.core
|
| 582 |
farthen |
33 |
import base64
|
| 56 |
benedikt93 |
34 |
|
| 506 |
farthen |
35 |
from libemcoredata import *
|
| 532 |
farthen |
36 |
from misc import Logger, Bunch, Error, ArgumentError, gethwname
|
| 394 |
farthen |
37 |
from functools import wraps
|
|
|
38 |
|
| 171 |
farthen |
39 |
class DeviceNotFoundError(Error):
|
|
|
40 |
pass
|
| 56 |
benedikt93 |
41 |
|
| 171 |
farthen |
42 |
class DeviceError(Error):
|
|
|
43 |
pass
|
| 56 |
benedikt93 |
44 |
|
| 171 |
farthen |
45 |
class SendError(Error):
|
|
|
46 |
pass
|
| 56 |
benedikt93 |
47 |
|
| 171 |
farthen |
48 |
class ReceiveError(Error):
|
|
|
49 |
pass
|
| 56 |
benedikt93 |
50 |
|
|
|
51 |
|
| 398 |
farthen |
52 |
def command(timeout = None, target = None):
|
| 394 |
farthen |
53 |
"""
|
|
|
54 |
Decorator for all commands.
|
|
|
55 |
It adds the "timeout" variable to all commands.
|
|
|
56 |
It also provides the possibility to set the timeout directly in the decorator.
|
|
|
57 |
It also includes some dirty hacks to not learn from.
|
|
|
58 |
"""
|
|
|
59 |
time = timeout # dirty hack because otherwise it would raise a scoping problem.
|
|
|
60 |
# The reason is probably because I suck but I can't find any good explanation of this.
|
|
|
61 |
def decorator(func):
|
|
|
62 |
@wraps(func)
|
|
|
63 |
def wrapper(*args, **kwargs):
|
| 398 |
farthen |
64 |
self = args[0] # little cheat as it expects self being always the first argument
|
| 394 |
farthen |
65 |
# precommand stuff
|
| 398 |
farthen |
66 |
if target is not None:
|
|
|
67 |
if self.lib.dev.hwtypeid != target:
|
| 518 |
farthen |
68 |
raise DeviceError("Wrong device for target-specific command. Expected \'%s\' but got \'%s\'" %
|
|
|
69 |
(gethwname(target), gethwname(self.lib.dev.hwtypeid)))
|
| 394 |
farthen |
70 |
timeout = None
|
|
|
71 |
if "timeout" in kwargs.keys():
|
|
|
72 |
timeout = kwargs['timeout']
|
|
|
73 |
elif time is not None:
|
|
|
74 |
timeout = time
|
|
|
75 |
if timeout is not None:
|
|
|
76 |
oldtimeout = self.lib.dev.timeout
|
|
|
77 |
self.lib.dev.timeout = timeout
|
|
|
78 |
# function call
|
|
|
79 |
ret = func(*args)
|
|
|
80 |
# postcommand stuff
|
|
|
81 |
if timeout is not None:
|
|
|
82 |
self.lib.dev.timeout = oldtimeout
|
|
|
83 |
return ret
|
| 396 |
farthen |
84 |
func._command = True
|
|
|
85 |
wrapper.func = func
|
| 394 |
farthen |
86 |
return wrapper
|
|
|
87 |
return decorator
|
|
|
88 |
|
|
|
89 |
|
| 427 |
farthen |
90 |
class Emcore(object):
|
| 176 |
farthen |
91 |
"""
|
| 427 |
farthen |
92 |
Class for all emcore functions.
|
| 394 |
farthen |
93 |
They all get the "@command()" decorator.
|
|
|
94 |
This decorator has a timeout variable that can be set to change the
|
|
|
95 |
device timeout for the duration of the function.
|
|
|
96 |
It also adds a "timeout" argument to every function to access this
|
|
|
97 |
feature from external. So DON'T EVER use a parameter called 'timeout'
|
|
|
98 |
in your commands. Variables are ok.
|
| 499 |
farthen |
99 |
|
|
|
100 |
If you want to enable logging please pass a misc.Logger object to the
|
|
|
101 |
constructor.
|
| 176 |
farthen |
102 |
"""
|
| 499 |
farthen |
103 |
def __init__(self, logger = Logger(loglevel = -1)):
|
|
|
104 |
self.logger = logger
|
| 427 |
farthen |
105 |
self.logger.debug("Initializing Emcore object\n")
|
| 401 |
farthen |
106 |
self.lib = Lib(self.logger)
|
| 343 |
farthen |
107 |
|
|
|
108 |
self.getversioninfo()
|
| 440 |
farthen |
109 |
if self.lib.dev.swtypeid != 2:
|
|
|
110 |
if self.lib.dev.swtypeid == 1:
|
|
|
111 |
raise DeviceError("Connected to emBIOS. emBIOS is not supported by libemcore")
|
|
|
112 |
else:
|
|
|
113 |
raise DeviceError("Connected to unknown software type. Exiting")
|
|
|
114 |
|
| 176 |
farthen |
115 |
self.getpacketsizeinfo()
|
| 343 |
farthen |
116 |
self.getusermemrange()
|
| 56 |
benedikt93 |
117 |
|
| 171 |
farthen |
118 |
@staticmethod
|
|
|
119 |
def _alignsplit(addr, size, blksize, align):
|
| 177 |
farthen |
120 |
if size <= blksize: return (size, 0, 0)
|
| 171 |
farthen |
121 |
end = addr + size
|
|
|
122 |
if addr & (align - 1):
|
|
|
123 |
bodyaddr = (addr + min(size, blksize)) & ~(align - 1)
|
|
|
124 |
else: bodyaddr = addr
|
|
|
125 |
headsize = bodyaddr - addr
|
|
|
126 |
if (size - headsize) & (align - 1):
|
|
|
127 |
tailaddr = (end - min(end - bodyaddr, blksize) + align - 1) & ~(align - 1)
|
|
|
128 |
else: tailaddr = end
|
|
|
129 |
tailsize = end - tailaddr
|
|
|
130 |
return (headsize, tailaddr - bodyaddr, tailsize)
|
| 56 |
benedikt93 |
131 |
|
| 394 |
farthen |
132 |
@command()
|
| 178 |
farthen |
133 |
def _readmem(self, addr, size):
|
|
|
134 |
""" Reads the memory from location 'addr' with size 'size'
|
|
|
135 |
from the device.
|
|
|
136 |
"""
|
| 548 |
farthen |
137 |
resp = self.lib.monitorcommand(struct.pack("<IIII", 4, addr, size, 0), "III%ds" % size, (None, None, None, "data"))
|
| 178 |
farthen |
138 |
return resp.data
|
| 394 |
farthen |
139 |
|
|
|
140 |
@command()
|
| 178 |
farthen |
141 |
def _writemem(self, addr, data):
|
|
|
142 |
""" Writes the data in 'data' to the location 'addr'
|
|
|
143 |
in the memory of the device.
|
|
|
144 |
"""
|
| 548 |
farthen |
145 |
return self.lib.monitorcommand(struct.pack("<IIII%ds" % len(data), 5, addr, len(data), 0, data), "III", (None, None, None))
|
| 178 |
farthen |
146 |
|
| 394 |
farthen |
147 |
@command()
|
| 178 |
farthen |
148 |
def _readdma(self, addr, size):
|
|
|
149 |
""" Reads the memory from location 'addr' with size 'size'
|
|
|
150 |
from the device. This uses DMA and the data in endpoint.
|
|
|
151 |
"""
|
| 548 |
farthen |
152 |
self.lib.monitorcommand(struct.pack("<IIII", 6, addr, size, 0), "III", (None, None, None))
|
|
|
153 |
return struct.unpack("<%ds" % size, self.lib.dev.din(size))[0]
|
| 178 |
farthen |
154 |
|
| 394 |
farthen |
155 |
@command()
|
| 178 |
farthen |
156 |
def _writedma(self, addr, data):
|
|
|
157 |
""" Writes the data in 'data' to the location 'addr'
|
|
|
158 |
in the memory of the device. This uses DMA and the data out endpoint.
|
|
|
159 |
"""
|
| 548 |
farthen |
160 |
self.lib.monitorcommand(struct.pack("<IIII", 7, addr, len(data), 0), "III", (None, None, None))
|
| 178 |
farthen |
161 |
return self.lib.dev.dout(data)
|
|
|
162 |
|
| 394 |
farthen |
163 |
@command()
|
| 171 |
farthen |
164 |
def getversioninfo(self):
|
| 427 |
farthen |
165 |
""" This returns the emCORE version and device information. """
|
| 548 |
farthen |
166 |
resp = self.lib.monitorcommand(struct.pack("<IIII", 1, 0, 0, 0), "IBBBBI", ("revision", "majorv", "minorv", "patchv", "swtypeid", "hwtypeid"))
|
| 342 |
farthen |
167 |
self.lib.dev.version.revision = resp.revision
|
|
|
168 |
self.lib.dev.version.majorv = resp.majorv
|
|
|
169 |
self.lib.dev.version.minorv = resp.minorv
|
|
|
170 |
self.lib.dev.version.patchv = resp.patchv
|
| 518 |
farthen |
171 |
self.logger.debug("Device Software Type ID = 0x%X\n" % resp.swtypeid)
|
| 342 |
farthen |
172 |
self.lib.dev.swtypeid = resp.swtypeid
|
| 518 |
farthen |
173 |
self.logger.debug("Device Hardware Type ID = 0x%X\n" % resp.hwtypeid)
|
| 342 |
farthen |
174 |
self.lib.dev.hwtypeid = resp.hwtypeid
|
|
|
175 |
return resp
|
| 56 |
benedikt93 |
176 |
|
| 394 |
farthen |
177 |
@command()
|
| 171 |
farthen |
178 |
def getpacketsizeinfo(self):
|
| 427 |
farthen |
179 |
""" This returns the emCORE max packet size information.
|
| 171 |
farthen |
180 |
It also sets the properties of the device object accordingly.
|
|
|
181 |
"""
|
| 548 |
farthen |
182 |
resp = self.lib.monitorcommand(struct.pack("<IIII", 1, 1, 0, 0), "HHII", ("coutmax", "cinmax", "doutmax", "dinmax"))
|
| 518 |
farthen |
183 |
self.logger.debug("Device cout packet size limit = %d\n" % resp.coutmax)
|
| 343 |
farthen |
184 |
self.lib.dev.packetsizelimit.cout = resp.coutmax
|
| 518 |
farthen |
185 |
self.logger.debug("Device cin packet size limit = %d\n" % resp.cinmax)
|
| 343 |
farthen |
186 |
self.lib.dev.packetsizelimit.cin = resp.cinmax
|
| 518 |
farthen |
187 |
self.logger.debug("Device din packet size limit = %d\n" % resp.doutmax)
|
| 343 |
farthen |
188 |
self.lib.dev.packetsizelimit.din = resp.dinmax
|
| 518 |
farthen |
189 |
self.logger.debug("Device dout packet size limit = %d\n" % resp.dinmax)
|
| 343 |
farthen |
190 |
self.lib.dev.packetsizelimit.dout = resp.doutmax
|
| 171 |
farthen |
191 |
return resp
|
| 56 |
benedikt93 |
192 |
|
| 394 |
farthen |
193 |
@command()
|
| 171 |
farthen |
194 |
def getusermemrange(self):
|
|
|
195 |
""" This returns the memory range the user has access to. """
|
| 548 |
farthen |
196 |
resp = self.lib.monitorcommand(struct.pack("<IIII", 1, 2, 0, 0), "III", ("lower", "upper", None))
|
| 518 |
farthen |
197 |
self.logger.debug("Device user memory = 0x%X - 0x%X\n" % (resp.lower, resp.upper))
|
| 342 |
farthen |
198 |
self.lib.dev.usermem.lower = resp.lower
|
|
|
199 |
self.lib.dev.usermem.upper = resp.upper
|
|
|
200 |
return resp
|
| 56 |
benedikt93 |
201 |
|
| 394 |
farthen |
202 |
@command()
|
| 171 |
farthen |
203 |
def reset(self, force=False):
|
|
|
204 |
""" Reboot the device """
|
|
|
205 |
if force:
|
| 548 |
farthen |
206 |
return self.lib.monitorcommand(struct.pack("<IIII", 2, 0, 0, 0))
|
| 171 |
farthen |
207 |
else:
|
| 548 |
farthen |
208 |
return self.lib.monitorcommand(struct.pack("<IIII", 2, 1, 0, 0), "III", (None, None, None))
|
| 56 |
benedikt93 |
209 |
|
| 394 |
farthen |
210 |
@command()
|
| 171 |
farthen |
211 |
def poweroff(self, force=False):
|
|
|
212 |
""" Powers the device off. """
|
|
|
213 |
if force:
|
| 548 |
farthen |
214 |
return self.lib.monitorcommand(struct.pack("<IIII", 3, 0, 0, 0))
|
| 171 |
farthen |
215 |
else:
|
| 548 |
farthen |
216 |
return self.lib.monitorcommand(struct.pack("<IIII", 3, 1, 0, 0), "III", (None, None, None))
|
| 56 |
benedikt93 |
217 |
|
| 394 |
farthen |
218 |
@command()
|
| 171 |
farthen |
219 |
def read(self, addr, size):
|
|
|
220 |
""" Reads the memory from location 'addr' with size 'size'
|
|
|
221 |
from the device. This cares about too long packages
|
|
|
222 |
and decides whether to use DMA or not.
|
|
|
223 |
"""
|
| 343 |
farthen |
224 |
cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
|
|
|
225 |
din_maxsize = self.lib.dev.packetsizelimit.din
|
| 582 |
farthen |
226 |
data = b""
|
| 171 |
farthen |
227 |
(headsize, bodysize, tailsize) = self._alignsplit(addr, size, cin_maxsize, 16)
|
| 518 |
farthen |
228 |
self.logger.debug("Downloading %d bytes from 0x%X, split as (%d/%d/%d)\n" % (size, addr, headsize, bodysize, tailsize))
|
| 171 |
farthen |
229 |
if headsize != 0:
|
| 178 |
farthen |
230 |
data += self._readmem(addr, headsize)
|
| 171 |
farthen |
231 |
addr += headsize
|
|
|
232 |
while bodysize > 0:
|
|
|
233 |
if bodysize >= 2 * cin_maxsize:
|
|
|
234 |
readsize = min(bodysize, din_maxsize)
|
| 178 |
farthen |
235 |
data += self._readdma(addr, readsize)
|
| 171 |
farthen |
236 |
else:
|
|
|
237 |
readsize = min(bodysize, cin_maxsize)
|
| 178 |
farthen |
238 |
data += self._readmem(addr, readsize)
|
| 171 |
farthen |
239 |
addr += readsize
|
|
|
240 |
bodysize -= readsize
|
|
|
241 |
if tailsize != 0:
|
| 178 |
farthen |
242 |
data += self._readmem(addr, tailsize)
|
| 171 |
farthen |
243 |
return data
|
| 56 |
benedikt93 |
244 |
|
| 394 |
farthen |
245 |
@command()
|
| 171 |
farthen |
246 |
def write(self, addr, data):
|
|
|
247 |
""" Writes the data in 'data' to the location 'addr'
|
|
|
248 |
in the memory of the device. This cares about too long packages
|
|
|
249 |
and decides whether to use DMA or not.
|
|
|
250 |
"""
|
| 343 |
farthen |
251 |
cout_maxsize = self.lib.dev.packetsizelimit.cout - self.lib.headersize
|
|
|
252 |
dout_maxsize = self.lib.dev.packetsizelimit.dout
|
| 171 |
farthen |
253 |
(headsize, bodysize, tailsize) = self._alignsplit(addr, len(data), cout_maxsize, 16)
|
| 518 |
farthen |
254 |
self.logger.debug("Uploading %d bytes to 0x%X, split as (%d/%d/%d)\n" % (len(data), addr, headsize, bodysize, tailsize))
|
| 171 |
farthen |
255 |
offset = 0
|
|
|
256 |
if headsize != 0:
|
| 178 |
farthen |
257 |
self._writemem(addr, data[offset:offset+headsize])
|
| 171 |
farthen |
258 |
offset += headsize
|
|
|
259 |
addr += headsize
|
|
|
260 |
while bodysize > 0:
|
|
|
261 |
if bodysize >= 2 * cout_maxsize:
|
|
|
262 |
writesize = min(bodysize, dout_maxsize)
|
| 178 |
farthen |
263 |
self._writedma(addr, data[offset:offset+writesize])
|
| 171 |
farthen |
264 |
else:
|
|
|
265 |
writesize = min(bodysize, cout_maxsize)
|
| 178 |
farthen |
266 |
self._writemem(addr, data[offset:offset+writesize])
|
| 171 |
farthen |
267 |
offset += writesize
|
|
|
268 |
addr += writesize
|
|
|
269 |
bodysize -= writesize
|
|
|
270 |
if tailsize != 0:
|
| 178 |
farthen |
271 |
self._writemem(addr, data[offset:offset+tailsize])
|
| 171 |
farthen |
272 |
return data
|
| 56 |
benedikt93 |
273 |
|
| 394 |
farthen |
274 |
@command()
|
| 442 |
farthen |
275 |
def upload(self, data):
|
|
|
276 |
""" Allocates memory of the size of 'data' and uploads 'data' to that memory region.
|
|
|
277 |
Returns the address where 'data' is stored
|
|
|
278 |
"""
|
|
|
279 |
addr = self.malloc(len(data))
|
|
|
280 |
self.write(addr, data)
|
|
|
281 |
return addr
|
|
|
282 |
|
|
|
283 |
@command()
|
| 588 |
theseven |
284 |
def readstring(self, addr, maxlength = 256, replacement = "."):
|
| 173 |
farthen |
285 |
""" Reads a zero terminated string from memory
|
|
|
286 |
Reads only a maximum of 'maxlength' chars.
|
|
|
287 |
"""
|
| 592 |
farthen |
288 |
if addr == 0: return "<NULL>"
|
| 343 |
farthen |
289 |
cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
|
| 173 |
farthen |
290 |
string = ""
|
| 592 |
farthen |
291 |
done = False
|
| 588 |
theseven |
292 |
while not done and (len(string) < maxlength or maxlength < 0):
|
| 178 |
farthen |
293 |
data = self._readmem(addr, min(maxlength - len(string), cin_maxsize))
|
| 582 |
farthen |
294 |
length = data.find(b"\0")
|
| 173 |
farthen |
295 |
if length >= 0:
|
| 592 |
farthen |
296 |
data = data[:length]
|
|
|
297 |
done = True
|
|
|
298 |
for i in range(len(data)):
|
|
|
299 |
byte = ord(data[i : i + 1])
|
|
|
300 |
if byte < 0x20: string = string + replacement
|
| 588 |
theseven |
301 |
else: string = string + chr(byte)
|
| 173 |
farthen |
302 |
addr += cin_maxsize
|
|
|
303 |
return string
|
|
|
304 |
|
| 394 |
farthen |
305 |
@command()
|
| 171 |
farthen |
306 |
def i2cread(self, index, slaveaddr, startaddr, size):
|
|
|
307 |
""" Reads data from an i2c slave """
|
| 582 |
farthen |
308 |
data = b""
|
| 236 |
farthen |
309 |
for i in range(size):
|
| 548 |
farthen |
310 |
resp = self.lib.monitorcommand(struct.pack("<IBBBBII", 8, index, slaveaddr, startaddr + i, 1, 0, 0), "III1s", (None, None, None, "data"))
|
| 236 |
farthen |
311 |
data += resp.data
|
|
|
312 |
return data
|
| 56 |
benedikt93 |
313 |
|
| 394 |
farthen |
314 |
@command()
|
| 171 |
farthen |
315 |
def i2cwrite(self, index, slaveaddr, startaddr, data):
|
|
|
316 |
""" Writes data to an i2c slave """
|
| 176 |
farthen |
317 |
size = len(data)
|
|
|
318 |
if size > 256 or size < 1:
|
| 341 |
farthen |
319 |
raise ArgumentError("Size must be a number between 1 and 256")
|
| 176 |
farthen |
320 |
if size == 256:
|
|
|
321 |
size = 0
|
| 548 |
farthen |
322 |
return self.lib.monitorcommand(struct.pack("<IBBBBII%ds" % size, 9, index, slaveaddr, startaddr, size, 0, 0, data), "III", (None, None, None))
|
| 56 |
benedikt93 |
323 |
|
| 394 |
farthen |
324 |
@command()
|
| 176 |
farthen |
325 |
def usbcread(self):
|
| 582 |
farthen |
326 |
""" Reads one packet with the maximal cin size from the console """
|
| 343 |
farthen |
327 |
cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
|
| 548 |
farthen |
328 |
resp = self.lib.monitorcommand(struct.pack("<IIII", 10, cin_maxsize, 0, 0), "III%ds" % cin_maxsize, ("validsize", "buffersize", "queuesize", "data"))
|
| 582 |
farthen |
329 |
resp.data = resp.data[:resp.validsize].decode("latin_1")
|
| 176 |
farthen |
330 |
resp.maxsize = cin_maxsize
|
|
|
331 |
return resp
|
| 56 |
benedikt93 |
332 |
|
| 394 |
farthen |
333 |
@command()
|
| 171 |
farthen |
334 |
def usbcwrite(self, data):
|
|
|
335 |
""" Writes data to the USB console """
|
| 343 |
farthen |
336 |
cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
|
| 176 |
farthen |
337 |
size = len(data)
|
|
|
338 |
while len(data) > 0:
|
|
|
339 |
writesize = min(cin_maxsize, len(data))
|
| 548 |
farthen |
340 |
resp = self.lib.monitorcommand(struct.pack("<IIII%ds" % writesize, 11, writesize, 0, 0, data[:writesize]), "III", ("validsize", "buffersize", "freesize"))
|
| 176 |
farthen |
341 |
data = data[resp.validsize:]
|
|
|
342 |
return size
|
| 56 |
benedikt93 |
343 |
|
| 394 |
farthen |
344 |
@command()
|
| 176 |
farthen |
345 |
def cread(self, bitmask=0x1):
|
|
|
346 |
""" Reads one packet with the maximal cin size from the device consoles
|
| 171 |
farthen |
347 |
identified with the specified bitmask
|
|
|
348 |
"""
|
| 343 |
farthen |
349 |
cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
|
| 548 |
farthen |
350 |
resp = self.lib.monitorcommand(struct.pack("<IIII", 13, bitmask, cin_maxsize, 0), "III%ds" % cin_maxsize, ("size", None, None))
|
| 176 |
farthen |
351 |
resp.data = resp.data[size:]
|
|
|
352 |
resp.maxsize = cin_maxsize
|
|
|
353 |
return resp
|
| 394 |
farthen |
354 |
|
|
|
355 |
@command()
|
| 176 |
farthen |
356 |
def cwrite(self, data, bitmask=0x1):
|
| 171 |
farthen |
357 |
""" Writes data to the device consoles
|
|
|
358 |
identified with the specified bitmask.
|
|
|
359 |
"""
|
| 343 |
farthen |
360 |
cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
|
| 176 |
farthen |
361 |
size = len(data)
|
|
|
362 |
while len(data) > 0:
|
|
|
363 |
writesize = min(cin_maxsize, len(data))
|
| 548 |
farthen |
364 |
resp = self.lib.monitorcommand(struct.pack("<IIII%ds" % writesize, 12, bitmask, writesize, 0, data[:writesize]), "III", (None, None, None))
|
| 176 |
farthen |
365 |
data = data[writesize:]
|
|
|
366 |
return size
|
| 56 |
benedikt93 |
367 |
|
| 394 |
farthen |
368 |
@command()
|
| 171 |
farthen |
369 |
def cflush(self, bitmask):
|
|
|
370 |
""" Flushes the consoles specified with 'bitmask' """
|
| 548 |
farthen |
371 |
return self.lib.monitorcommand(struct.pack("<IIII", 14, bitmask, 0, 0), "III", (None, None, None))
|
| 56 |
benedikt93 |
372 |
|
| 394 |
farthen |
373 |
@command()
|
| 173 |
farthen |
374 |
def getprocinfo(self):
|
| 171 |
farthen |
375 |
""" Gets current state of the scheduler """
|
| 173 |
farthen |
376 |
schedulerstate = self.lockscheduler()
|
| 548 |
farthen |
377 |
resp = self.lib.monitorcommand(struct.pack("<IIII", 15, 0, 0, 0), "III", ("structver", "structptr", None))
|
| 506 |
farthen |
378 |
if resp.structver != 2:
|
|
|
379 |
raise DeviceError("Unsupported thread struct version!")
|
|
|
380 |
|
| 173 |
farthen |
381 |
threads = []
|
| 506 |
farthen |
382 |
structptr = resp.structptr
|
| 173 |
farthen |
383 |
id = 0
|
| 506 |
farthen |
384 |
while structptr != 0:
|
|
|
385 |
threadstruct = scheduler_thread()
|
| 518 |
farthen |
386 |
self.logger.debug("Reading thread struct of thread at 0x%X\n" % structptr)
|
| 506 |
farthen |
387 |
threaddata = self.read(structptr, ctypes.sizeof(scheduler_thread))
|
|
|
388 |
threadstruct._from_string(threaddata)
|
|
|
389 |
threadstruct = threadstruct._to_bunch()
|
|
|
390 |
threadstruct.id = id # only for the purpose of detecting the idle thread as it is always the first one
|
|
|
391 |
threadstruct.addr = structptr
|
| 592 |
farthen |
392 |
if threadstruct.name != 0:
|
|
|
393 |
threadstruct.name = self.readstring(threadstruct.name)
|
| 587 |
theseven |
394 |
else: threadstruct.name = "[Thread %08X]" % structptr
|
| 506 |
farthen |
395 |
threadstruct.state = thread_state(threadstruct.state)
|
|
|
396 |
threads.append(threadstruct)
|
| 173 |
farthen |
397 |
id += 1
|
| 506 |
farthen |
398 |
structptr = threadstruct.thread_next
|
| 508 |
farthen |
399 |
self.lockscheduler(schedulerstate)
|
| 173 |
farthen |
400 |
return threads
|
| 56 |
benedikt93 |
401 |
|
| 394 |
farthen |
402 |
@command()
|
| 173 |
farthen |
403 |
def lockscheduler(self, freeze=True):
|
| 171 |
farthen |
404 |
""" Freezes/Unfreezes the scheduler """
|
| 548 |
farthen |
405 |
resp = self.lib.monitorcommand(struct.pack("<IIII", 16, 1 if freeze else 0, 0, 0), "III", ("before", None, None))
|
| 173 |
farthen |
406 |
return True if resp.before == 1 else False
|
| 67 |
benedikt93 |
407 |
|
| 394 |
farthen |
408 |
@command()
|
| 173 |
farthen |
409 |
def unlockscheduler(self):
|
| 171 |
farthen |
410 |
""" Unfreezes the scheduler """
|
| 548 |
farthen |
411 |
return self.lib.monitorcommand(struct.pack("<IIII", 16, 0, 0, 0), "III", ("before", None, None))
|
| 56 |
benedikt93 |
412 |
|
| 394 |
farthen |
413 |
@command()
|
| 171 |
farthen |
414 |
def suspendthread(self, id, suspend=True):
|
|
|
415 |
""" Suspends the thread with the specified id """
|
| 548 |
farthen |
416 |
resp = self.lib.monitorcommand(struct.pack("<IIII", 17, 1 if suspend else 0, id, 0), "III", ("before", None, None))
|
| 173 |
farthen |
417 |
return True if resp.before == 1 else False
|
| 56 |
benedikt93 |
418 |
|
| 394 |
farthen |
419 |
@command()
|
| 173 |
farthen |
420 |
def resumethread(self, id):
|
|
|
421 |
""" Resumes the thread with the specified id """
|
| 548 |
farthen |
422 |
return self.lib.monitorcommand(struct.pack("<IIII", 17, 0, id, 0), "III", ("before", None, None))
|
| 56 |
benedikt93 |
423 |
|
| 394 |
farthen |
424 |
@command()
|
| 171 |
farthen |
425 |
def killthread(self, id):
|
|
|
426 |
""" Kills the thread with the specified id """
|
| 548 |
farthen |
427 |
return self.lib.monitorcommand(struct.pack("<IIII", 18, id, 0, 0), "III", ("before", None, None))
|
| 56 |
benedikt93 |
428 |
|
| 394 |
farthen |
429 |
@command()
|
| 171 |
farthen |
430 |
def createthread(self, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state):
|
|
|
431 |
""" Creates a thread with the specified attributes """
|
|
|
432 |
if threadtype == "user":
|
|
|
433 |
threadtype = 0
|
|
|
434 |
elif threadtype == "system":
|
|
|
435 |
threadtype = 1
|
|
|
436 |
else:
|
| 341 |
farthen |
437 |
raise ArgumentError("Threadtype must be either 'system' or 'user'")
|
| 171 |
farthen |
438 |
if priority > 256 or priority < 0:
|
| 341 |
farthen |
439 |
raise ArgumentError("Priority must be a number between 0 and 256")
|
| 171 |
farthen |
440 |
if state == "ready":
|
|
|
441 |
state = 0
|
|
|
442 |
elif state == "suspended":
|
|
|
443 |
state = 1
|
|
|
444 |
else:
|
| 341 |
farthen |
445 |
raise ArgumentError("State must be either 'ready' or 'suspended'")
|
| 548 |
farthen |
446 |
resp = self.lib.monitorcommand(struct.pack("<IIIIIIII", 19, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state), "III", ("threadptr", None, None))
|
| 516 |
farthen |
447 |
if resp.threadptr < 0:
|
| 518 |
farthen |
448 |
raise DeviceError("The device returned the error code %d" % resp.threadptr)
|
| 171 |
farthen |
449 |
return resp
|
| 56 |
benedikt93 |
450 |
|
| 394 |
farthen |
451 |
@command()
|
| 172 |
farthen |
452 |
def flushcaches(self):
|
| 171 |
farthen |
453 |
""" Flushes the CPU instruction and data cache """
|
| 548 |
farthen |
454 |
return self.lib.monitorcommand(struct.pack("<IIII", 20, 0, 0, 0), "III", (None, None, None))
|
| 56 |
benedikt93 |
455 |
|
| 394 |
farthen |
456 |
@command()
|
| 172 |
farthen |
457 |
def execimage(self, addr):
|
| 427 |
farthen |
458 |
""" Runs the emCORE app at 'addr' """
|
| 548 |
farthen |
459 |
return self.lib.monitorcommand(struct.pack("<IIII", 21, addr, 0, 0), "III", ("thread", None, None))
|
| 56 |
benedikt93 |
460 |
|
| 394 |
farthen |
461 |
@command()
|
| 238 |
farthen |
462 |
def run(self, app):
|
| 427 |
farthen |
463 |
""" Uploads and runs the emCORE app in the string 'app' """
|
| 589 |
farthen |
464 |
if app[:8].decode("latin_1") != "emCOexec":
|
| 427 |
farthen |
465 |
raise ArgumentError("The specified app is not an emCORE application")
|
| 452 |
theseven |
466 |
baseaddr = self.malloc(len(app))
|
| 238 |
farthen |
467 |
self.write(baseaddr, app)
|
| 452 |
theseven |
468 |
result = self.execimage(baseaddr)
|
|
|
469 |
return Bunch(thread=result.thread)
|
| 238 |
farthen |
470 |
|
| 395 |
farthen |
471 |
@command(timeout = 5000)
|
| 171 |
farthen |
472 |
def bootflashread(self, memaddr, flashaddr, size):
|
|
|
473 |
""" Copies the data in the bootflash at 'flashaddr' of the specified size
|
|
|
474 |
to the memory at addr 'memaddr'
|
|
|
475 |
"""
|
| 548 |
farthen |
476 |
return self.lib.monitorcommand(struct.pack("<IIII", 22, memaddr, flashaddr, size), "III", (None, None, None))
|
| 82 |
benedikt93 |
477 |
|
| 395 |
farthen |
478 |
@command(timeout = 30000)
|
| 171 |
farthen |
479 |
def bootflashwrite(self, memaddr, flashaddr, size):
|
|
|
480 |
""" Copies the data in the memory at 'memaddr' of the specified size
|
|
|
481 |
to the boot flash at addr 'flashaddr'
|
|
|
482 |
"""
|
| 548 |
farthen |
483 |
return self.lib.monitorcommand(struct.pack("<IIII", 23, memaddr, flashaddr, size), "III", (None, None, None))
|
| 56 |
benedikt93 |
484 |
|
| 394 |
farthen |
485 |
@command()
|
| 442 |
farthen |
486 |
def execfirmware(self, targetaddr, addr, size):
|
|
|
487 |
""" Moves the firmware at 'addr' with size 'size' to 'targetaddr' and passes all control to it. """
|
| 518 |
farthen |
488 |
self.logger.debug("Moving firmware at 0x%X with the size %d to 0x%X and executing it\n" % (addr, size, targetaddr))
|
| 548 |
farthen |
489 |
return self.lib.monitorcommand(struct.pack("<IIII", 24, targetaddr, addr, size))
|
| 56 |
benedikt93 |
490 |
|
| 395 |
farthen |
491 |
@command(timeout = 30000)
|
| 171 |
farthen |
492 |
def aesencrypt(self, addr, size, keyindex):
|
|
|
493 |
""" Encrypts the buffer at 'addr' with the specified size
|
|
|
494 |
with the hardware AES key index 'keyindex'
|
|
|
495 |
"""
|
| 548 |
farthen |
496 |
return self.lib.monitorcommand(struct.pack("<IBBHII", 25, 1, 0, keyindex, addr, size), "III", (None, None, None))
|
| 82 |
benedikt93 |
497 |
|
| 395 |
farthen |
498 |
@command(timeout = 30000)
|
| 171 |
farthen |
499 |
def aesdecrypt(self, addr, size, keyindex):
|
|
|
500 |
""" Decrypts the buffer at 'addr' with the specified size
|
|
|
501 |
with the hardware AES key index 'keyindex'
|
|
|
502 |
"""
|
| 548 |
farthen |
503 |
return self.lib.monitorcommand(struct.pack("<IBBHII", 25, 0, 0, keyindex, addr, size), "III", (None, None, None))
|
| 82 |
benedikt93 |
504 |
|
| 395 |
farthen |
505 |
@command(timeout = 30000)
|
| 171 |
farthen |
506 |
def hmac_sha1(self, addr, size, destination):
|
|
|
507 |
""" Generates a HMAC-SHA1 hash of the buffer and saves it to 'destination' """
|
| 548 |
farthen |
508 |
return self.lib.monitorcommand(struct.pack("<IIII", 26, addr, size, destination), "III", (None, None, None))
|
| 56 |
benedikt93 |
509 |
|
| 398 |
farthen |
510 |
@command(target = 0x47324e49)
|
| 227 |
theseven |
511 |
def ipodnano2g_getnandinfo(self):
|
|
|
512 |
""" Target-specific function: ipodnano2g
|
|
|
513 |
Gathers some information about the NAND chip used
|
|
|
514 |
"""
|
| 548 |
farthen |
515 |
return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0001, 0, 0, 0), "IHHHH", ("type", "pagesperblock", "banks", "userblocks", "blocks"))
|
| 227 |
theseven |
516 |
|
| 398 |
farthen |
517 |
@command(timeout = 30000, target = 0x47324e49)
|
| 227 |
theseven |
518 |
def ipodnano2g_nandread(self, addr, start, count, doecc, checkempty):
|
|
|
519 |
""" Target-specific function: ipodnano2g
|
|
|
520 |
Reads data from the NAND chip into memory
|
|
|
521 |
"""
|
| 548 |
farthen |
522 |
return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0002, addr | (0x80000000 if doecc else 0) | (0x40000000 if checkempty else 0), start, count), "III", (None, None, None))
|
| 227 |
theseven |
523 |
|
| 398 |
farthen |
524 |
@command(timeout = 30000, target = 0x47324e49)
|
| 227 |
theseven |
525 |
def ipodnano2g_nandwrite(self, addr, start, count, doecc):
|
|
|
526 |
""" Target-specific function: ipodnano2g
|
|
|
527 |
Writes data to the NAND chip
|
|
|
528 |
"""
|
| 548 |
farthen |
529 |
return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0003, addr | (0x80000000 if doecc else 0), start, count), "III", (None, None, None))
|
| 227 |
theseven |
530 |
|
| 398 |
farthen |
531 |
@command(timeout = 30000, target = 0x47324e49)
|
| 227 |
theseven |
532 |
def ipodnano2g_nanderase(self, addr, start, count):
|
|
|
533 |
""" Target-specific function: ipodnano2g
|
|
|
534 |
Erases blocks on the NAND chip and stores the results to memory
|
|
|
535 |
"""
|
| 548 |
farthen |
536 |
return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0004, addr, start, count), "III", (None, None, None))
|
| 227 |
theseven |
537 |
|
| 398 |
farthen |
538 |
@command(target = 0x4c435049)
|
| 346 |
theseven |
539 |
def ipodclassic_gethddinfo(self):
|
|
|
540 |
""" Target-specific function: ipodclassic
|
|
|
541 |
Gather information about the hard disk drive
|
|
|
542 |
"""
|
| 548 |
farthen |
543 |
return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0001, 0, 0, 0), "IQQII", ("identifyptr", "totalsectors", "virtualsectors", "bbtptr", "bbtsize"))
|
| 346 |
theseven |
544 |
|
| 398 |
farthen |
545 |
@command(timeout = 30000, target = 0x4c435049)
|
| 346 |
theseven |
546 |
def ipodclassic_hddaccess(self, type, sector, count, addr):
|
|
|
547 |
""" Target-specific function: ipodclassic
|
|
|
548 |
Access the hard disk, type = 0 (read) / 1 (write)
|
|
|
549 |
"""
|
| 548 |
farthen |
550 |
rc = self.lib.monitorcommand(struct.pack("<IIQIIII", 0xffff0002, type, sector, count, addr, 0, 0), "III", ("rc", None, None))
|
| 585 |
theseven |
551 |
if (rc.rc > 0x80000000):
|
|
|
552 |
raise DeviceError("HDD access (type=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (type, sector, count, addr, rc.rc))
|
| 346 |
theseven |
553 |
|
| 398 |
farthen |
554 |
@command(target = 0x4c435049)
|
| 585 |
theseven |
555 |
def ipodclassic_writebbt(self, bbt, tempaddr = None):
|
| 346 |
theseven |
556 |
""" Target-specific function: ipodclassic
|
|
|
557 |
Write hard drive bad block table
|
|
|
558 |
"""
|
|
|
559 |
try:
|
|
|
560 |
bbtheader = struct.unpack("<8s2024sQII512I", bbt[:4096])
|
|
|
561 |
except struct.error:
|
| 427 |
farthen |
562 |
raise ArgumentError("The specified file is not an emCORE hard disk BBT")
|
| 346 |
theseven |
563 |
if bbtheader[0] != "emBIbbth":
|
| 427 |
farthen |
564 |
raise ArgumentError("The specified file is not an emCORE hard disk BBT")
|
| 346 |
theseven |
565 |
virtualsectors = bbtheader[2]
|
| 602 |
theseven |
566 |
bbtsectors = bbtheader[4]
|
| 585 |
theseven |
567 |
if tempaddr is None:
|
|
|
568 |
tempaddr = self.malloc(len(bbt))
|
|
|
569 |
malloc = True
|
|
|
570 |
else:
|
|
|
571 |
malloc = False
|
|
|
572 |
try:
|
|
|
573 |
self.write(tempaddr, bbt)
|
|
|
574 |
sector = 0
|
|
|
575 |
count = 1
|
|
|
576 |
offset = 0
|
|
|
577 |
for i in range(bbtsectors):
|
| 602 |
theseven |
578 |
if bbtheader[5 + i] == sector + count:
|
| 585 |
theseven |
579 |
count = count + 1
|
|
|
580 |
else:
|
|
|
581 |
self.ipodclassic_hddaccess(1, sector, count, tempaddr + offset)
|
|
|
582 |
offset = offset + count * 4096
|
| 602 |
theseven |
583 |
sector = bbtheader[5 +i]
|
| 585 |
theseven |
584 |
count = 1
|
|
|
585 |
self.ipodclassic_hddaccess(1, sector, count, tempaddr + offset)
|
| 602 |
theseven |
586 |
finally:
|
| 585 |
theseven |
587 |
if malloc == True:
|
|
|
588 |
self.free(tempaddr)
|
| 346 |
theseven |
589 |
|
| 394 |
farthen |
590 |
@command()
|
| 379 |
theseven |
591 |
def storage_get_info(self, volume):
|
| 346 |
theseven |
592 |
""" Get information about a storage device """
|
| 484 |
theseven |
593 |
self.logger.debug("Getting storage information\n")
|
| 548 |
farthen |
594 |
result = self.lib.monitorcommand(struct.pack("<IIII", 27, volume, 0, 0), "IIIIIIII", ("version", None, None, "sectorsize", "numsectors", "vendorptr", "productptr", "revisionptr"))
|
| 346 |
theseven |
595 |
if result.version != 1:
|
|
|
596 |
raise ValueError("Unknown version of storage_info struct: %d" % result.version)
|
| 379 |
theseven |
597 |
result.vendor = self.readstring(result.vendorptr)
|
|
|
598 |
result.product = self.readstring(result.productptr)
|
|
|
599 |
result.revision = self.readstring(result.revisionptr)
|
| 484 |
theseven |
600 |
self.logger.debug("Got storage information:\n")
|
|
|
601 |
self.logger.debug("Vendor: %s\n" % result.vendor)
|
|
|
602 |
self.logger.debug("Product: %s\n" % result.product)
|
|
|
603 |
self.logger.debug("Revision: %s\n" % result.revision)
|
|
|
604 |
self.logger.debug("Sector size: %d\n" % result.sectorsize)
|
|
|
605 |
self.logger.debug("Number of sectors: %d\n" % result.numsectors)
|
| 346 |
theseven |
606 |
return result
|
|
|
607 |
|
| 395 |
farthen |
608 |
@command(timeout = 50000)
|
| 479 |
theseven |
609 |
def storage_read_sectors_md(self, volume, sector, count, addr):
|
|
|
610 |
""" Read sectors from as storage device """
|
| 518 |
farthen |
611 |
self.logger.debug("Reading %d sectors from disk at volume %d, sector %d to memory at 0x%X\n" % (count, volume, sector, addr))
|
| 548 |
farthen |
612 |
result = self.lib.monitorcommand(struct.pack("<IIQIIII", 28, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
|
| 518 |
farthen |
613 |
self.logger.debug("Read sectors, result: 0x%X\n" % result.rc)
|
| 346 |
theseven |
614 |
if result.rc > 0x80000000:
|
|
|
615 |
raise DeviceError("storage_read_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
|
| 479 |
theseven |
616 |
|
|
|
617 |
@command(timeout = 50000)
|
|
|
618 |
def storage_write_sectors_md(self, volume, sector, count, addr):
|
|
|
619 |
""" Read sectors from as storage device """
|
| 518 |
farthen |
620 |
self.logger.debug("Writing %d sectors from memory at 0x%X to disk at volume %d, sector %d\n" % (count, addr, volume, sector))
|
| 548 |
farthen |
621 |
result = self.lib.monitorcommand(struct.pack("<IIQIIII", 29, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
|
| 518 |
farthen |
622 |
self.logger.debug("Wrote sectors, result: 0x%X\n" % result.rc)
|
| 346 |
theseven |
623 |
if result.rc > 0x80000000:
|
| 479 |
theseven |
624 |
raise DeviceError("storage_write_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
|
| 394 |
farthen |
625 |
|
| 395 |
farthen |
626 |
@command(timeout = 30000)
|
| 479 |
theseven |
627 |
def fat_enable_flushing(self, state):
|
|
|
628 |
""" Enables/disables flushing the FAT cache after every transaction """
|
| 484 |
theseven |
629 |
if state != 0: self.logger.debug("Enabling FAT flushing\n")
|
|
|
630 |
else: self.logger.debug("Disabling FAT flushing\n")
|
| 548 |
farthen |
631 |
self.lib.monitorcommand(struct.pack("<IIII", 58, state, 0, 0), "III", (None, None, None))
|
| 484 |
theseven |
632 |
if state != 0: self.logger.debug("Enabled FAT flushing\n")
|
|
|
633 |
else: self.logger.debug("Disabled FAT flushing\n")
|
| 517 |
farthen |
634 |
|
| 479 |
theseven |
635 |
@command(timeout = 30000)
|
| 346 |
theseven |
636 |
def file_open(self, filename, mode):
|
|
|
637 |
""" Opens a file and returns the handle """
|
| 487 |
theseven |
638 |
self.logger.debug("Opening remote file %s with mode %d\n" % (filename, mode))
|
| 548 |
farthen |
639 |
result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(filename), 30, mode, 0, 0, filename, 0), "III", ("fd", None, None))
|
| 346 |
theseven |
640 |
if result.fd > 0x80000000:
|
|
|
641 |
raise DeviceError("file_open(filename=\"%s\", mode=0x%X) failed with RC=0x%08X, errno=%d" % (filename, mode, result.fd, self.errno()))
|
| 518 |
farthen |
642 |
self.logger.debug("Opened file as handle 0x%X\n" % result.fd)
|
| 346 |
theseven |
643 |
return result.fd
|
|
|
644 |
|
| 395 |
farthen |
645 |
@command(timeout = 30000)
|
| 346 |
theseven |
646 |
def file_size(self, fd):
|
|
|
647 |
""" Gets the size of a file referenced by a handle """
|
| 518 |
farthen |
648 |
self.logger.debug("Getting file size of handle 0x%X\n" % fd)
|
| 548 |
farthen |
649 |
result = self.lib.monitorcommand(struct.pack("<IIII", 31, fd, 0, 0), "III", ("size", None, None))
|
| 346 |
theseven |
650 |
if result.size > 0x80000000:
|
|
|
651 |
raise DeviceError("file_size(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.size, self.errno()))
|
| 484 |
theseven |
652 |
self.logger.debug("Got file size: %d bytes\n" % result.size)
|
| 346 |
theseven |
653 |
return result.size
|
| 394 |
farthen |
654 |
|
| 395 |
farthen |
655 |
@command(timeout = 30000)
|
| 480 |
theseven |
656 |
def file_read(self, fd, size, addr = None):
|
| 472 |
farthen |
657 |
""" Reads data from a file referenced by a handle. If addr is not given it allocates a buffer itself. """
|
|
|
658 |
if addr is None:
|
|
|
659 |
addr = self.malloc(size)
|
|
|
660 |
malloc = True
|
|
|
661 |
else:
|
|
|
662 |
malloc = False
|
| 518 |
farthen |
663 |
self.logger.debug("Reading %d bytes from file handle 0x%X to 0x%X\n" % (size, fd, addr))
|
| 472 |
farthen |
664 |
try:
|
| 548 |
farthen |
665 |
result = self.lib.monitorcommand(struct.pack("<IIII", 32, fd, addr, size), "III", ("rc", None, None))
|
| 479 |
theseven |
666 |
if result.rc > 0x80000000:
|
|
|
667 |
raise DeviceError("file_read(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
|
|
|
668 |
except:
|
| 472 |
farthen |
669 |
if malloc == True:
|
|
|
670 |
self.free(addr)
|
| 479 |
theseven |
671 |
raise
|
| 518 |
farthen |
672 |
self.logger.debug("File read result: 0x%X\n" % result.rc)
|
| 479 |
theseven |
673 |
return Bunch(rc = result.rc, addr = addr)
|
| 394 |
farthen |
674 |
|
| 395 |
farthen |
675 |
@command(timeout = 30000)
|
| 479 |
theseven |
676 |
def file_write(self, fd, size, addr):
|
|
|
677 |
""" Writes data from a file referenced by a handle. """
|
| 518 |
farthen |
678 |
self.logger.debug("Writing %d bytes from 0x%X to file handle 0x%X\n" % (size, addr, fd))
|
| 548 |
farthen |
679 |
result = self.lib.monitorcommand(struct.pack("<IIII", 33, fd, addr, size), "III", ("rc", None, None))
|
| 346 |
theseven |
680 |
if result.rc > 0x80000000:
|
|
|
681 |
raise DeviceError("file_write(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
|
| 518 |
farthen |
682 |
self.logger.debug("File write result: 0x%X\n" % result.rc)
|
| 346 |
theseven |
683 |
return result.rc
|
|
|
684 |
|
| 395 |
farthen |
685 |
@command(timeout = 30000)
|
| 346 |
theseven |
686 |
def file_seek(self, fd, offset, whence):
|
|
|
687 |
""" Seeks the file handle to the specified position in the file """
|
| 518 |
farthen |
688 |
self.logger.debug("Seeking file handle 0x%X to whence=%d, offset=0x%X\n" % (fd, whence, offset))
|
| 548 |
farthen |
689 |
result = self.lib.monitorcommand(struct.pack("<IIII", 34, fd, offset, whence), "III", ("rc", None, None))
|
| 346 |
theseven |
690 |
if result.rc > 0x80000000:
|
|
|
691 |
raise DeviceError("file_seek(fd=%d, offset=0x%08X, whence=%d) failed with RC=0x%08X, errno=%d" % (fd, offset, whence, result.rc, self.errno()))
|
| 518 |
farthen |
692 |
self.logger.debug("File seek result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
693 |
return result.rc
|
|
|
694 |
|
| 395 |
farthen |
695 |
@command(timeout = 30000)
|
| 346 |
theseven |
696 |
def file_truncate(self, fd, length):
|
|
|
697 |
""" Truncates a file referenced by a handle to a specified length """
|
| 518 |
farthen |
698 |
self.logger.debug("Truncating file with handle 0x%X to 0x%X bytes\n" % (fd, length))
|
| 548 |
farthen |
699 |
result = self.lib.monitorcommand(struct.pack("<IIII", 35, fd, offset, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
700 |
if result.rc > 0x80000000:
|
|
|
701 |
raise DeviceError("file_truncate(fd=%d, length=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, length, result.rc, self.errno()))
|
| 518 |
farthen |
702 |
self.logger.debug("File truncate result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
703 |
return result.rc
|
|
|
704 |
|
| 395 |
farthen |
705 |
@command(timeout = 30000)
|
| 346 |
theseven |
706 |
def file_sync(self, fd):
|
|
|
707 |
""" Flushes a file handles' buffers """
|
| 518 |
farthen |
708 |
self.logger.debug("Flushing buffers of file with handle 0x%X\n" % (fd))
|
| 548 |
farthen |
709 |
result = self.lib.monitorcommand(struct.pack("<IIII", 36, fd, 0, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
710 |
if result.rc > 0x80000000:
|
|
|
711 |
raise DeviceError("file_sync(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.rc, self.errno()))
|
| 518 |
farthen |
712 |
self.logger.debug("File flush result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
713 |
return result.rc
|
|
|
714 |
|
| 395 |
farthen |
715 |
@command(timeout = 30000)
|
| 346 |
theseven |
716 |
def file_close(self, fd):
|
|
|
717 |
""" Closes a file handle """
|
| 518 |
farthen |
718 |
self.logger.debug("Closing file handle 0x%X\n" % (fd))
|
| 548 |
farthen |
719 |
result = self.lib.monitorcommand(struct.pack("<IIII", 37, fd, 0, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
720 |
if result.rc > 0x80000000:
|
|
|
721 |
raise DeviceError("file_close(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.rc, self.errno()))
|
| 518 |
farthen |
722 |
self.logger.debug("File close result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
723 |
return result.rc
|
|
|
724 |
|
| 395 |
farthen |
725 |
@command(timeout = 30000)
|
| 346 |
theseven |
726 |
def file_close_all(self):
|
|
|
727 |
""" Closes all file handles opened through the debugger """
|
| 484 |
theseven |
728 |
self.logger.debug("Closing all files that were opened via USB\n")
|
| 548 |
farthen |
729 |
result = self.lib.monitorcommand(struct.pack("<IIII", 38, 0, 0, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
730 |
if result.rc > 0x80000000:
|
|
|
731 |
raise DeviceError("file_close_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
|
| 484 |
theseven |
732 |
self.logger.debug("Closed %d files\n" % (result.rc))
|
| 346 |
theseven |
733 |
return result.rc
|
|
|
734 |
|
| 395 |
farthen |
735 |
@command(timeout = 30000)
|
| 484 |
theseven |
736 |
def file_kill_all(self, volume):
|
|
|
737 |
""" Kills all file handles of a volume (in the whole system) """
|
|
|
738 |
self.logger.debug("Killing all file handles of volume %d\n" % (volume))
|
| 548 |
farthen |
739 |
result = self.lib.monitorcommand(struct.pack("<IIII", 39, volume, 0, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
740 |
if result.rc > 0x80000000:
|
|
|
741 |
raise DeviceError("file_kill_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
|
| 484 |
theseven |
742 |
self.logger.debug("Closed %d files\n" % (result.rc))
|
| 346 |
theseven |
743 |
return result.rc
|
|
|
744 |
|
| 395 |
farthen |
745 |
@command(timeout = 30000)
|
| 346 |
theseven |
746 |
def file_unlink(self, filename):
|
|
|
747 |
""" Removes a file """
|
| 484 |
theseven |
748 |
self.logger.debug("Deleting file %s\n" % (filename))
|
| 548 |
farthen |
749 |
result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(filename), 40, 0, 0, 0, filename, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
750 |
if result.rc > 0x80000000:
|
|
|
751 |
raise DeviceError("file_unlink(filename=\"%s\") failed with RC=0x%08X, errno=%d" % (filename, result.rc, self.errno()))
|
| 518 |
farthen |
752 |
self.logger.debug("Delete file result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
753 |
return result.rc
|
|
|
754 |
|
| 395 |
farthen |
755 |
@command(timeout = 30000)
|
| 346 |
theseven |
756 |
def file_rename(self, oldname, newname):
|
|
|
757 |
""" Renames a file """
|
| 484 |
theseven |
758 |
self.logger.debug("Renaming file %s to %s\n" % (oldname, newname))
|
| 548 |
farthen |
759 |
result = self.lib.monitorcommand(struct.pack("<IIII248s%dsB" % min(247, len(newname)), 41, 0, 0, 0, oldname, newname, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
760 |
if result.rc > 0x80000000:
|
|
|
761 |
raise DeviceError("file_rename(oldname=\"%s\", newname=\"%s\") failed with RC=0x%08X, errno=%d" % (oldname, newname, result.rc, self.errno()))
|
| 518 |
farthen |
762 |
self.logger.debug("Rename file result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
763 |
return result.rc
|
|
|
764 |
|
| 395 |
farthen |
765 |
@command(timeout = 30000)
|
| 346 |
theseven |
766 |
def dir_open(self, dirname):
|
|
|
767 |
""" Opens a directory and returns the handle """
|
| 484 |
theseven |
768 |
self.logger.debug("Opening directory %s\n" % (dirname))
|
| 548 |
farthen |
769 |
result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(dirname), 42, 0, 0, 0, dirname, 0), "III", ("handle", None, None))
|
| 346 |
theseven |
770 |
if result.handle == 0:
|
|
|
771 |
raise DeviceError("dir_open(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.handle, self.errno()))
|
| 518 |
farthen |
772 |
self.logger.debug("Opened directory as handle 0x%X\n" % (result.handle))
|
| 346 |
theseven |
773 |
return result.handle
|
|
|
774 |
|
| 395 |
farthen |
775 |
@command(timeout = 30000)
|
| 346 |
theseven |
776 |
def dir_read(self, handle):
|
|
|
777 |
""" Reads the next entry from a directory """
|
| 518 |
farthen |
778 |
self.logger.debug("Reading next entry of directory handle 0x%X\n" % (handle))
|
| 548 |
farthen |
779 |
result = self.lib.monitorcommand(struct.pack("<IIII", 43, handle, 0, 0), "III", ("version", "maxpath", "ptr"))
|
| 346 |
theseven |
780 |
if result.ptr == 0:
|
|
|
781 |
raise DeviceError("dir_read(handle=0x%08X) failed with RC=0x%08X, errno=%d" % (handle, result.ptr, self.errno()))
|
|
|
782 |
if result.version != 1:
|
|
|
783 |
raise ValueError("Unknown version of dirent struct: %d" % result.version)
|
|
|
784 |
dirent = self.read(result.ptr, result.maxpath + 16)
|
|
|
785 |
ret = Bunch()
|
| 548 |
farthen |
786 |
(ret.name, ret.attributes, ret.size, ret.startcluster, ret.wrtdate, ret.wrttime) = struct.unpack("<%dsIIIHH" % result.maxpath, dirent)
|
| 346 |
theseven |
787 |
ret.name = ret.name[:ret.name.index('\x00')]
|
| 484 |
theseven |
788 |
self.logger.debug("Read directory entry:\n")
|
|
|
789 |
self.logger.debug("Name: %s\n" % ret.name)
|
| 518 |
farthen |
790 |
self.logger.debug("Attributes: 0x%X\n" % ret.attributes)
|
| 484 |
theseven |
791 |
self.logger.debug("Size: %d\n" % ret.size)
|
|
|
792 |
self.logger.debug("Start cluster: %d\n" % ret.startcluster)
|
| 518 |
farthen |
793 |
self.logger.debug("Last written date: 0x%X\n" % ret.wrtdate)
|
|
|
794 |
self.logger.debug("Last written time: 0x%X\n" % ret.wrttime)
|
| 346 |
theseven |
795 |
return ret
|
|
|
796 |
|
| 395 |
farthen |
797 |
@command(timeout = 30000)
|
| 346 |
theseven |
798 |
def dir_close(self, handle):
|
|
|
799 |
""" Closes a directory handle """
|
| 518 |
farthen |
800 |
self.logger.debug("Closing directory handle 0x%X\n" % (handle))
|
| 548 |
farthen |
801 |
result = self.lib.monitorcommand(struct.pack("<IIII", 44, handle, 0, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
802 |
if result.rc > 0x80000000:
|
|
|
803 |
raise DeviceError("dir_close(handle=0x%08X) failed with RC=0x%08X, errno=%d" % (handle, result.rc, self.errno()))
|
| 518 |
farthen |
804 |
self.logger.debug("Close directory result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
805 |
return result.rc
|
|
|
806 |
|
| 395 |
farthen |
807 |
@command(timeout = 30000)
|
| 346 |
theseven |
808 |
def dir_close_all(self):
|
|
|
809 |
""" Closes all directory handles opened through the debugger """
|
| 484 |
theseven |
810 |
self.logger.debug("Closing all directories that were opened via USB\n")
|
| 548 |
farthen |
811 |
result = self.lib.monitorcommand(struct.pack("<IIII", 45, 0, 0, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
812 |
if result.rc > 0x80000000:
|
|
|
813 |
raise DeviceError("dir_close_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
|
| 484 |
theseven |
814 |
self.logger.debug("Closed %d directories\n" % (result.rc))
|
| 346 |
theseven |
815 |
return result.rc
|
|
|
816 |
|
| 395 |
farthen |
817 |
@command(timeout = 30000)
|
| 484 |
theseven |
818 |
def dir_kill_all(self, volume):
|
|
|
819 |
""" Kills all directory handles of a volume (in the whole system) """
|
|
|
820 |
self.logger.debug("Closing all directories of volume %d\n" % (volume))
|
| 548 |
farthen |
821 |
result = self.lib.monitorcommand(struct.pack("<IIII", 46, volume, 0, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
822 |
if result.rc > 0x80000000:
|
|
|
823 |
raise DeviceError("dir_kill_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
|
| 484 |
theseven |
824 |
self.logger.debug("Closed %d directories\n" % (result.rc))
|
| 346 |
theseven |
825 |
return result.rc
|
|
|
826 |
|
| 395 |
farthen |
827 |
@command(timeout = 30000)
|
| 346 |
theseven |
828 |
def dir_create(self, dirname):
|
|
|
829 |
""" Creates a directory """
|
| 484 |
theseven |
830 |
self.logger.debug("Creating directory %s\n" % (dirname))
|
| 548 |
farthen |
831 |
result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(dirname), 47, 0, 0, 0, dirname, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
832 |
if result.rc > 0x80000000:
|
|
|
833 |
raise DeviceError("dir_create(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.rc, self.errno()))
|
| 518 |
farthen |
834 |
self.logger.debug("Create directory result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
835 |
return result.rc
|
|
|
836 |
|
| 395 |
farthen |
837 |
@command(timeout = 30000)
|
| 346 |
theseven |
838 |
def dir_remove(self, dirname):
|
|
|
839 |
""" Removes an (empty) directory """
|
| 484 |
theseven |
840 |
self.logger.debug("Removing directory %s\n" % (dirname))
|
| 548 |
farthen |
841 |
result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(dirname), 48, 0, 0, 0, dirname, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
842 |
if result.rc > 0x80000000:
|
|
|
843 |
raise DeviceError("dir_remove(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.rc, self.errno()))
|
| 518 |
farthen |
844 |
self.logger.debug("Remove directory result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
845 |
return result.rc
|
|
|
846 |
|
| 394 |
farthen |
847 |
@command()
|
| 346 |
theseven |
848 |
def errno(self):
|
|
|
849 |
""" Returns the number of the last error that happened """
|
| 484 |
theseven |
850 |
self.logger.debug("Getting last error number\n")
|
| 548 |
farthen |
851 |
result = self.lib.monitorcommand(struct.pack("<IIII", 49, 0, 0, 0), "III", ("errno", None, None))
|
| 518 |
farthen |
852 |
self.logger.debug("Last error: 0x%X\n" % (result.errno))
|
| 346 |
theseven |
853 |
return result.errno
|
|
|
854 |
|
| 394 |
farthen |
855 |
@command()
|
| 346 |
theseven |
856 |
def disk_mount(self, volume):
|
|
|
857 |
""" Mounts a volume """
|
| 484 |
theseven |
858 |
self.logger.debug("Mounting volume %d\n" % (volume))
|
| 548 |
farthen |
859 |
result = self.lib.monitorcommand(struct.pack("<IIII", 50, volume, 0, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
860 |
if result.rc > 0x80000000:
|
|
|
861 |
raise DeviceError("disk_mount(volume=%d) failed with RC=0x%08X, errno=%d" % (volume, result.rc, self.errno()))
|
| 518 |
farthen |
862 |
self.logger.debug("Mount volume result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
863 |
return result.rc
|
|
|
864 |
|
| 394 |
farthen |
865 |
@command()
|
| 346 |
theseven |
866 |
def disk_unmount(self, volume):
|
|
|
867 |
""" Unmounts a volume """
|
| 484 |
theseven |
868 |
self.logger.debug("Unmounting volume %d\n" % (volume))
|
| 548 |
farthen |
869 |
result = self.lib.monitorcommand(struct.pack("<IIII", 51, volume, 0, 0), "III", ("rc", None, None))
|
| 346 |
theseven |
870 |
if result.rc > 0x80000000:
|
|
|
871 |
raise DeviceError("disk_unmount(volume=%d) failed with RC=0x%08X, errno=%d" % (volume, result.rc, self.errno()))
|
| 518 |
farthen |
872 |
self.logger.debug("Unmount volume result: 0x%X\n" % (result.rc))
|
| 346 |
theseven |
873 |
return result.rc
|
|
|
874 |
|
| 441 |
farthen |
875 |
@command()
|
|
|
876 |
def malloc(self, size):
|
|
|
877 |
""" Allocates 'size' bytes and returns a pointer to the allocated memory """
|
| 442 |
farthen |
878 |
self.logger.debug("Allocating %d bytes of memory\n" % size)
|
| 548 |
farthen |
879 |
result = self.lib.monitorcommand(struct.pack("<IIII", 52, size, 0, 0), "III", ("ptr", None, None))
|
| 518 |
farthen |
880 |
self.logger.debug("Allocated %d bytes of memory at 0x%X\n" % (size, result.ptr))
|
| 441 |
farthen |
881 |
return result.ptr
|
|
|
882 |
|
|
|
883 |
@command()
|
|
|
884 |
def memalign(self, align, size):
|
|
|
885 |
""" Allocates 'size' bytes aligned to 'align' and returns a pointer to the allocated memory """
|
| 518 |
farthen |
886 |
self.logger.debug("Allocating %d bytes of memory aligned to 0x%X\n" % (size, align))
|
| 548 |
farthen |
887 |
result = self.lib.monitorcommand(struct.pack("<IIII", 53, align, size, 0), "III", ("ptr", None, None))
|
| 518 |
farthen |
888 |
self.logger.debug("Allocated %d bytes of memory at 0x%X\n" % (size, result.ptr))
|
| 441 |
farthen |
889 |
return result.ptr
|
|
|
890 |
|
|
|
891 |
@command()
|
|
|
892 |
def realloc(self, ptr, size):
|
|
|
893 |
""" The size of the memory block pointed to by 'ptr' is changed to the 'size' bytes,
|
|
|
894 |
expanding or reducing the amount of memory available in the block.
|
|
|
895 |
Returns a pointer to the reallocated memory.
|
|
|
896 |
"""
|
| 518 |
farthen |
897 |
self.logger.debug("Reallocating 0x%X to have the new size %d\n" % (ptr, size))
|
| 548 |
farthen |
898 |
result = self.lib.monitorcommand(struct.pack("<IIII", 54, ptr, size, 0), "III", ("ptr", None, None))
|
| 518 |
farthen |
899 |
self.logger.debug("Reallocated memory at 0x%X to 0x%X with the new size %d\n" % (ptr, result.ptr, size))
|
| 441 |
farthen |
900 |
return result.ptr
|
|
|
901 |
|
|
|
902 |
@command()
|
|
|
903 |
def reownalloc(self, ptr, owner):
|
|
|
904 |
""" Changes the owner of the memory allocation 'ptr' to the thread struct at addr 'owner' """
|
| 518 |
farthen |
905 |
self.logger.debug("Changing owner of the memory region 0x%X to 0x%X\n" % (ptr, owner))
|
| 548 |
farthen |
906 |
return self.lib.monitorcommand(struct.pack("<IIII", 55, ptr, owner, 0), "III", (None, None, None))
|
| 441 |
farthen |
907 |
|
|
|
908 |
@command()
|
|
|
909 |
def free(self, ptr):
|
|
|
910 |
""" Frees the memory space pointed to by 'ptr' """
|
| 518 |
farthen |
911 |
self.logger.debug("Freeing the memory region at 0x%X\n" % ptr)
|
| 548 |
farthen |
912 |
return self.lib.monitorcommand(struct.pack("<IIII", 56, ptr, 0, 0), "III", (None, None, None))
|
| 441 |
farthen |
913 |
|
| 473 |
farthen |
914 |
@command()
|
|
|
915 |
def free_all(self):
|
|
|
916 |
""" Frees all memory allocations created by the monitor thread """
|
|
|
917 |
self.logger.debug("Freeing all memory allocations created by the monitor thread\n")
|
| 548 |
farthen |
918 |
return self.lib.monitorcommand(struct.pack("<IIII", 57, 0, 0, 0), "III", (None, None, None))
|
| 346 |
theseven |
919 |
|
| 517 |
farthen |
920 |
|
| 171 |
farthen |
921 |
class Lib(object):
|
| 401 |
farthen |
922 |
def __init__(self, logger):
|
|
|
923 |
self.logger = logger
|
|
|
924 |
self.logger.debug("Initializing Lib object\n")
|
| 171 |
farthen |
925 |
self.idVendor = 0xFFFF
|
|
|
926 |
self.idProduct = 0xE000
|
| 176 |
farthen |
927 |
|
|
|
928 |
self.headersize = 0x10
|
|
|
929 |
|
|
|
930 |
self.connect()
|
| 56 |
benedikt93 |
931 |
|
| 171 |
farthen |
932 |
def connect(self):
|
| 401 |
farthen |
933 |
self.dev = Dev(self.idVendor, self.idProduct, self.logger)
|
| 171 |
farthen |
934 |
self.connected = True
|
| 56 |
benedikt93 |
935 |
|
| 171 |
farthen |
936 |
def monitorcommand(self, cmd, rcvdatatypes=None, rcvstruct=None):
|
| 582 |
farthen |
937 |
self.logger.debug("Sending monitorcommand [0x%s]\n" % base64.b16encode(cmd[3::-1]).decode("ascii"))
|
| 269 |
farthen |
938 |
writelen = self.dev.cout(cmd)
|
| 171 |
farthen |
939 |
if rcvdatatypes:
|
|
|
940 |
rcvdatatypes = "I" + rcvdatatypes # add the response
|
|
|
941 |
data = self.dev.cin(struct.calcsize(rcvdatatypes))
|
|
|
942 |
data = struct.unpack(rcvdatatypes, data)
|
| 506 |
farthen |
943 |
try:
|
|
|
944 |
response = responsecode(data[0])
|
|
|
945 |
except IndexError:
|
|
|
946 |
self.logger.debug("Response: UNKOWN\n")
|
|
|
947 |
raise DeviceError("Invalid response! This should NOT happen!")
|
|
|
948 |
if response == "OK":
|
| 401 |
farthen |
949 |
self.logger.debug("Response: OK\n")
|
| 171 |
farthen |
950 |
if rcvstruct:
|
|
|
951 |
datadict = Bunch()
|
|
|
952 |
counter = 1 # start with 1, 0 is the id
|
|
|
953 |
for item in rcvstruct:
|
|
|
954 |
if item != None: # else the data is undefined
|
|
|
955 |
datadict[item] = data[counter]
|
|
|
956 |
counter += 1
|
|
|
957 |
return datadict
|
|
|
958 |
else:
|
|
|
959 |
return data
|
| 506 |
farthen |
960 |
elif response == "UNSUPPORTED":
|
| 401 |
farthen |
961 |
self.logger.debug("Response: UNSUPPORTED\n")
|
| 171 |
farthen |
962 |
raise DeviceError("The device does not support this command.")
|
| 506 |
farthen |
963 |
elif response == "INVALID":
|
| 401 |
farthen |
964 |
self.logger.debug("Response: INVALID\n")
|
| 171 |
farthen |
965 |
raise DeviceError("Invalid command! This should NOT happen!")
|
| 506 |
farthen |
966 |
elif response == "BUSY":
|
| 401 |
farthen |
967 |
self.logger.debug("Response: BUSY\n")
|
| 171 |
farthen |
968 |
raise DeviceError("Device busy")
|
| 269 |
farthen |
969 |
else:
|
|
|
970 |
return writelen
|
| 56 |
benedikt93 |
971 |
|
|
|
972 |
|
| 171 |
farthen |
973 |
class Dev(object):
|
| 401 |
farthen |
974 |
def __init__(self, idVendor, idProduct, logger):
|
| 171 |
farthen |
975 |
self.idVendor = idVendor
|
|
|
976 |
self.idProduct = idProduct
|
| 67 |
benedikt93 |
977 |
|
| 401 |
farthen |
978 |
self.logger = logger
|
|
|
979 |
self.logger.debug("Initializing Dev object\n")
|
|
|
980 |
|
| 171 |
farthen |
981 |
self.interface = 0
|
|
|
982 |
self.timeout = 100
|
| 517 |
farthen |
983 |
|
| 171 |
farthen |
984 |
self.connect()
|
|
|
985 |
self.findEndpoints()
|
|
|
986 |
|
| 401 |
farthen |
987 |
self.logger.debug("Successfully connected to device\n")
|
| 342 |
farthen |
988 |
|
|
|
989 |
# Device properties
|
| 343 |
farthen |
990 |
self.packetsizelimit = Bunch()
|
|
|
991 |
self.packetsizelimit.cout = None
|
|
|
992 |
self.packetsizelimit.cin = None
|
|
|
993 |
self.packetsizelimit.dout = None
|
|
|
994 |
self.packetsizelimit.din = None
|
| 342 |
farthen |
995 |
|
| 343 |
farthen |
996 |
self.version = Bunch()
|
| 342 |
farthen |
997 |
self.version.revision = None
|
|
|
998 |
self.version.majorv = None
|
|
|
999 |
self.version.minorv = None
|
|
|
1000 |
self.version.patchv = None
|
|
|
1001 |
self.swtypeid = None
|
|
|
1002 |
self.hwtypeid = None
|
|
|
1003 |
|
| 343 |
farthen |
1004 |
self.usermem = Bunch()
|
| 342 |
farthen |
1005 |
self.usermem.lower = None
|
|
|
1006 |
self.usermem.upper = None
|
| 56 |
benedikt93 |
1007 |
|
| 171 |
farthen |
1008 |
def __del__(self):
|
|
|
1009 |
self.disconnect()
|
| 56 |
benedikt93 |
1010 |
|
| 171 |
farthen |
1011 |
def findEndpoints(self):
|
| 401 |
farthen |
1012 |
self.logger.debug("Searching for device endpoints:\n")
|
| 171 |
farthen |
1013 |
epcounter = 0
|
| 343 |
farthen |
1014 |
self.endpoint = Bunch()
|
| 171 |
farthen |
1015 |
for cfg in self.dev:
|
|
|
1016 |
for intf in cfg:
|
|
|
1017 |
for ep in intf:
|
|
|
1018 |
if epcounter == 0:
|
| 518 |
farthen |
1019 |
self.logger.debug("Found cout endpoint at 0x%X\n" % ep.bEndpointAddress)
|
| 343 |
farthen |
1020 |
self.endpoint.cout = ep.bEndpointAddress
|
| 171 |
farthen |
1021 |
elif epcounter == 1:
|
| 518 |
farthen |
1022 |
self.logger.debug("Found cin endpoint at 0x%X\n" % ep.bEndpointAddress)
|
| 343 |
farthen |
1023 |
self.endpoint.cin = ep.bEndpointAddress
|
| 171 |
farthen |
1024 |
elif epcounter == 2:
|
| 518 |
farthen |
1025 |
self.logger.debug("Found dout endpoint at 0x%X\n" % ep.bEndpointAddress)
|
| 343 |
farthen |
1026 |
self.endpoint.dout = ep.bEndpointAddress
|
| 171 |
farthen |
1027 |
elif epcounter == 3:
|
| 518 |
farthen |
1028 |
self.logger.debug("Found din endpoint at 0x%X\n" % ep.bEndpointAddress)
|
| 343 |
farthen |
1029 |
self.endpoint.din = ep.bEndpointAddress
|
| 171 |
farthen |
1030 |
epcounter += 1
|
|
|
1031 |
if epcounter <= 3:
|
| 518 |
farthen |
1032 |
raise DeviceError("Not all endpoints found in the descriptor. Only %d endpoints found, at least 4 endpoints were expeceted" % epcounter)
|
| 56 |
benedikt93 |
1033 |
|
| 171 |
farthen |
1034 |
def connect(self):
|
| 427 |
farthen |
1035 |
self.logger.debug("Looking for emCORE device\n")
|
| 171 |
farthen |
1036 |
self.dev = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
|
|
|
1037 |
if self.dev is None:
|
|
|
1038 |
raise DeviceNotFoundError()
|
| 401 |
farthen |
1039 |
self.logger.debug("Device Found!\n")
|
|
|
1040 |
self.logger.debug("Setting first configuration\n")
|
| 171 |
farthen |
1041 |
self.dev.set_configuration()
|
| 56 |
benedikt93 |
1042 |
|
| 171 |
farthen |
1043 |
def disconnect(self):
|
|
|
1044 |
pass
|
| 102 |
benedikt93 |
1045 |
|
| 171 |
farthen |
1046 |
def send(self, endpoint, data):
|
|
|
1047 |
size = self.dev.write(endpoint, data, self.interface, self.timeout)
|
|
|
1048 |
if size != len(data):
|
| 176 |
farthen |
1049 |
raise SendError("Not all data was written!")
|
| 171 |
farthen |
1050 |
return len
|
| 102 |
benedikt93 |
1051 |
|
| 171 |
farthen |
1052 |
def receive(self, endpoint, size):
|
|
|
1053 |
read = self.dev.read(endpoint, size, self.interface, self.timeout)
|
|
|
1054 |
if len(read) != size:
|
| 176 |
farthen |
1055 |
raise ReceiveError("Requested size and read size don't match!")
|
| 171 |
farthen |
1056 |
return read
|
| 56 |
benedikt93 |
1057 |
|
| 171 |
farthen |
1058 |
def cout(self, data):
|
| 518 |
farthen |
1059 |
self.logger.debug("Sending data to cout endpoint with the size %d\n" % len(data))
|
| 343 |
farthen |
1060 |
if self.packetsizelimit.cout and len(data) > self.packetsizelimit.cout:
|
| 171 |
farthen |
1061 |
raise SendError("Packet too big")
|
| 343 |
farthen |
1062 |
return self.send(self.endpoint.cout, data)
|
| 94 |
benedikt93 |
1063 |
|
| 171 |
farthen |
1064 |
def cin(self, size):
|
| 518 |
farthen |
1065 |
self.logger.debug("Receiving data on the cin endpoint with the size %d\n" % size)
|
| 343 |
farthen |
1066 |
if self.packetsizelimit.cin and size > self.packetsizelimit.cin:
|
| 171 |
farthen |
1067 |
raise ReceiveError("Packet too big")
|
| 343 |
farthen |
1068 |
return self.receive(self.endpoint.cin, size)
|
| 94 |
benedikt93 |
1069 |
|
| 171 |
farthen |
1070 |
def dout(self, data):
|
| 518 |
farthen |
1071 |
self.logger.debug("Sending data to cout endpoint with the size %d\n" % len(data))
|
| 343 |
farthen |
1072 |
if self.packetsizelimit.dout and len(data) > self.packetsizelimit.dout:
|
| 171 |
farthen |
1073 |
raise SendError("Packet too big")
|
| 343 |
farthen |
1074 |
return self.send(self.endpoint.dout, data)
|
| 94 |
benedikt93 |
1075 |
|
| 171 |
farthen |
1076 |
def din(self, size):
|
| 518 |
farthen |
1077 |
self.logger.debug("Receiving data on the din endpoint with the size %d\n" % size)
|
| 343 |
farthen |
1078 |
if self.packetsizelimit.din and size > self.packetsizelimit.din:
|
| 171 |
farthen |
1079 |
raise ReceiveError("Packet too big")
|
| 343 |
farthen |
1080 |
return self.receive(self.endpoint.din, size)
|
| 56 |
benedikt93 |
1081 |
|
| 96 |
benedikt93 |
1082 |
|
| 171 |
farthen |
1083 |
if __name__ == "__main__":
|
| 396 |
farthen |
1084 |
from misc import Logger
|
|
|
1085 |
logger = Logger()
|
|
|
1086 |
|
| 500 |
farthen |
1087 |
if sys.argv[1] == "gendoc":
|
| 396 |
farthen |
1088 |
# Generates Documentation
|
|
|
1089 |
from misc import gendoc
|
| 501 |
farthen |
1090 |
logger.write("Generating documentation\n")
|
| 396 |
farthen |
1091 |
cmddict = {}
|
| 582 |
farthen |
1092 |
for attr, value in Emcore.__dict__.items():
|
| 396 |
farthen |
1093 |
if getattr(value, 'func', False):
|
|
|
1094 |
if getattr(value.func, '_command', False):
|
|
|
1095 |
cmddict[value.func.__name__] = value
|
| 501 |
farthen |
1096 |
logger.write(gendoc(cmddict))
|
| 441 |
farthen |
1097 |
|
|
|
1098 |
elif sys.argv[1] == "malloctest":
|
|
|
1099 |
emcore = Emcore()
|
| 501 |
farthen |
1100 |
logger.write("Allocating 200 bytes of memory: ")
|
| 441 |
farthen |
1101 |
addr = emcore.malloc(200)
|
| 518 |
farthen |
1102 |
logger.write("0x%X\n" % addr)
|
| 501 |
farthen |
1103 |
logger.write("Reallocating to 2000 bytes: ")
|
| 441 |
farthen |
1104 |
addr = emcore.realloc(addr, 2000)
|
| 518 |
farthen |
1105 |
logger.write("0x%X\n" % addr)
|
|
|
1106 |
logger.write("Freeing 0x%X\n" % addr)
|
| 441 |
farthen |
1107 |
emcore.free(addr)
|
| 501 |
farthen |
1108 |
logger.write("Allocating 1000 bytes of memory aligned to 100 bytes: ")
|
| 441 |
farthen |
1109 |
addr = emcore.memalign(100, 1000)
|
| 518 |
farthen |
1110 |
logger.write("0x%X\n" % addr)
|
|
|
1111 |
logger.write("Freeing 0x%X\n" % addr)
|
| 441 |
farthen |
1112 |
emcore.free(addr)
|