author | laurent |
Fri, 04 Jun 2010 15:39:14 +0200 | |
changeset 561 | 4cc6eef4778f |
parent 554 | 6bd3f220b886 |
child 563 | c74a37d156df |
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: |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
18 |
raise LPCProtoError("LPC transaction error - controller did not answer as expected") |
448 | 19 |
finally: |
20 |
self.TransactionLock.release() |
|
508 | 21 |
return LPC_STATUS.get(current_plc_status,"Broken"), res |
448 | 22 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
23 |
class LPCAppTransaction: |
453 | 24 |
def __init__(self, command, optdata = ""): |
25 |
self.Command = command |
|
26 |
self.OptData = optdata |
|
27 |
self.pseudofile = None |
|
448 | 28 |
|
453 | 29 |
def SetPseudoFile(self, pseudofile): |
448 | 30 |
self.pseudofile = pseudofile |
31 |
||
32 |
def SendCommand(self): |
|
33 |
# send command thread |
|
34 |
self.pseudofile.write(chr(self.Command)) |
|
35 |
||
36 |
def GetCommandAck(self): |
|
453 | 37 |
res = self.pseudofile.read(2) |
38 |
if len(res) == 2: |
|
39 |
comm_status, current_plc_status = map(ord, res) |
|
40 |
else: |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
41 |
raise LPCProtoError("LPC transaction error - controller did not ack order") |
448 | 42 |
# LPC returns command itself as an ack for command |
43 |
if(comm_status == self.Command): |
|
44 |
return current_plc_status |
|
45 |
return None |
|
46 |
||
453 | 47 |
def SendData(self): |
48 |
length = len(self.OptData) |
|
49 |
# transform length into a byte string |
|
50 |
# 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
|
51 |
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
|
52 |
buffer = lengthstr + self.OptData |
536 | 53 |
return self.pseudofile.write(buffer) |
453 | 54 |
|
55 |
def GetData(self): |
|
56 |
lengthstr = self.pseudofile.read(4) |
|
57 |
# transform a byte string into length |
|
58 |
length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value |
|
59 |
return self.pseudofile.read(length) |
|
60 |
||
61 |
def ExchangeData(self): |
|
62 |
pass |
|
63 |
||
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
64 |
class IDLETransaction(LPCAppTransaction): |
453 | 65 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
66 |
LPCAppTransaction.__init__(self, 0x00) |
453 | 67 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
68 |
class STARTTransaction(LPCAppTransaction): |
453 | 69 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
70 |
LPCAppTransaction.__init__(self, 0x01) |
453 | 71 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
72 |
class STOPTransaction(LPCAppTransaction): |
453 | 73 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
74 |
LPCAppTransaction.__init__(self, 0x02) |
453 | 75 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
76 |
class RESETTransaction(LPCAppTransaction): |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
77 |
def __init__(self): |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
78 |
LPCAppTransaction.__init__(self, 0x03) |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
79 |
|
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
80 |
class SET_TRACE_VARIABLETransaction(LPCAppTransaction): |
453 | 81 |
def __init__(self, data): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
82 |
LPCAppTransaction.__init__(self, 0x04, data) |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
83 |
ExchangeData = LPCAppTransaction.SendData |
453 | 84 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
85 |
class GET_TRACE_VARIABLETransaction(LPCAppTransaction): |
453 | 86 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
87 |
LPCAppTransaction.__init__(self, 0x05) |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
88 |
ExchangeData = LPCAppTransaction.GetData |
453 | 89 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
90 |
class GET_PLCIDTransaction(LPCAppTransaction): |
453 | 91 |
def __init__(self): |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
92 |
LPCAppTransaction.__init__(self, 0x07) |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
93 |
ExchangeData = LPCAppTransaction.GetData |
453 | 94 |
|
448 | 95 |
if __name__ == "__main__": |
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
96 |
TestConnection = LPCAppProto(6,115200,2) |
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
97 |
# TestConnection.HandleTransaction(GET_PLCIDTransaction()) |
453 | 98 |
TestConnection.HandleTransaction(STARTTransaction()) |
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
99 |
# TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
100 |
# "\x03\x00\x00\x00"*200)) |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
101 |
# TestConnection.HandleTransaction(STARTTransaction()) |
508 | 102 |
while True: |
103 |
TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
|
104 |
"\x01\x00\x00\x00"+ |
|
105 |
"\x04"+ |
|
106 |
"\x01\x02\x02\x04"+ |
|
107 |
"\x01\x00\x00\x00"+ |
|
108 |
"\x08"+ |
|
109 |
"\x01\x02\x02\x04"+ |
|
110 |
"\x01\x02\x02\x04"+ |
|
111 |
"\x01\x00\x00\x00"+ |
|
112 |
"\x04"+ |
|
113 |
"\x01\x02\x02\x04")) |
|
502 | 114 |
#status,res = TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction()) |
115 |
#print len(res) |
|
116 |
#print "GOT : ", map(hex, map(ord, res)) |
|
453 | 117 |
#TestConnection.HandleTransaction(STOPTransaction()) |