author | edouard |
Tue, 01 Feb 2011 11:05:07 +0100 | |
changeset 567 | 72b51ec5be64 |
parent 563 | c74a37d156df |
child 570 | 46abd6b2f639 |
permissions | -rw-r--r-- |
453 | 1 |
import ctypes |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
2 |
from LPCProto import * |
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
3 |
|
554 | 4 |
LPC_STATUS={0xaa : "Started", |
5 |
0x55 : "Stopped"} |
|
448 | 6 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
7 |
class LPCAppProto(LPCProto): |
448 | 8 |
def HandleTransaction(self, transaction): |
9 |
self.TransactionLock.acquire() |
|
10 |
try: |
|
11 |
transaction.SetPseudoFile(self.serialPort) |
|
12 |
# send command, wait ack (timeout) |
|
13 |
transaction.SendCommand() |
|
14 |
current_plc_status = transaction.GetCommandAck() |
|
15 |
if current_plc_status is not None: |
|
16 |
res = transaction.ExchangeData() |
|
17 |
else: |
|
563 | 18 |
raise LPCProtoError("controller did not answer as expected") |
19 |
except Exception, e: |
|
20 |
raise LPCProtoError("LPC transaction error : "+str(e)) |
|
448 | 21 |
finally: |
22 |
self.TransactionLock.release() |
|
508 | 23 |
return LPC_STATUS.get(current_plc_status,"Broken"), res |
448 | 24 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
25 |
class LPCAppTransaction: |
453 | 26 |
def __init__(self, command, optdata = ""): |
27 |
self.Command = command |
|
28 |
self.OptData = optdata |
|
29 |
self.pseudofile = None |
|
448 | 30 |
|
453 | 31 |
def SetPseudoFile(self, pseudofile): |
448 | 32 |
self.pseudofile = pseudofile |
33 |
||
34 |
def SendCommand(self): |
|
35 |
# send command thread |
|
36 |
self.pseudofile.write(chr(self.Command)) |
|
37 |
||
38 |
def GetCommandAck(self): |
|
453 | 39 |
res = self.pseudofile.read(2) |
40 |
if len(res) == 2: |
|
41 |
comm_status, current_plc_status = map(ord, res) |
|
42 |
else: |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
43 |
raise LPCProtoError("LPC transaction error - controller did not ack order") |
448 | 44 |
# LPC returns command itself as an ack for command |
45 |
if(comm_status == self.Command): |
|
46 |
return current_plc_status |
|
47 |
return None |
|
48 |
||
453 | 49 |
def SendData(self): |
50 |
length = len(self.OptData) |
|
51 |
# transform length into a byte string |
|
52 |
# we presuppose endianess of LPC same as PC |
|
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
53 |
lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4) |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
54 |
buffer = lengthstr + self.OptData |
536 | 55 |
return self.pseudofile.write(buffer) |
453 | 56 |
|
57 |
def GetData(self): |
|
58 |
lengthstr = self.pseudofile.read(4) |
|
59 |
# transform a byte string into length |
|
60 |
length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value |
|
61 |
return self.pseudofile.read(length) |
|
62 |
||
63 |
def ExchangeData(self): |
|
64 |
pass |
|
65 |
||
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
66 |
class IDLETransaction(LPCAppTransaction): |
453 | 67 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
68 |
LPCAppTransaction.__init__(self, 0x00) |
453 | 69 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
70 |
class STARTTransaction(LPCAppTransaction): |
453 | 71 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
72 |
LPCAppTransaction.__init__(self, 0x01) |
453 | 73 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
74 |
class STOPTransaction(LPCAppTransaction): |
453 | 75 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
76 |
LPCAppTransaction.__init__(self, 0x02) |
453 | 77 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
78 |
class RESETTransaction(LPCAppTransaction): |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
79 |
def __init__(self): |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
80 |
LPCAppTransaction.__init__(self, 0x03) |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
81 |
|
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
82 |
class SET_TRACE_VARIABLETransaction(LPCAppTransaction): |
453 | 83 |
def __init__(self, data): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
84 |
LPCAppTransaction.__init__(self, 0x04, data) |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
85 |
ExchangeData = LPCAppTransaction.SendData |
453 | 86 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
87 |
class GET_TRACE_VARIABLETransaction(LPCAppTransaction): |
453 | 88 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
89 |
LPCAppTransaction.__init__(self, 0x05) |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
90 |
ExchangeData = LPCAppTransaction.GetData |
453 | 91 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
92 |
class GET_PLCIDTransaction(LPCAppTransaction): |
453 | 93 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
94 |
LPCAppTransaction.__init__(self, 0x07) |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
95 |
ExchangeData = LPCAppTransaction.GetData |
453 | 96 |
|
448 | 97 |
if __name__ == "__main__": |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
98 |
TestConnection = LPCAppProto(6,115200,2) |
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
99 |
# TestConnection.HandleTransaction(GET_PLCIDTransaction()) |
453 | 100 |
TestConnection.HandleTransaction(STARTTransaction()) |
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
101 |
# TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
102 |
# "\x03\x00\x00\x00"*200)) |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
103 |
# TestConnection.HandleTransaction(STARTTransaction()) |
508 | 104 |
while True: |
105 |
TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
|
106 |
"\x01\x00\x00\x00"+ |
|
107 |
"\x04"+ |
|
108 |
"\x01\x02\x02\x04"+ |
|
109 |
"\x01\x00\x00\x00"+ |
|
110 |
"\x08"+ |
|
111 |
"\x01\x02\x02\x04"+ |
|
112 |
"\x01\x02\x02\x04"+ |
|
113 |
"\x01\x00\x00\x00"+ |
|
114 |
"\x04"+ |
|
115 |
"\x01\x02\x02\x04")) |
|
502 | 116 |
#status,res = TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction()) |
117 |
#print len(res) |
|
118 |
#print "GOT : ", map(hex, map(ord, res)) |
|
453 | 119 |
#TestConnection.HandleTransaction(STOPTransaction()) |