connectors/LPC/LPCAppProto.py
author edouard
Wed, 23 Feb 2011 16:54:29 +0100
changeset 578 6f5795bdee49
parent 576 7fcdc0d3d8d9
permissions -rw-r--r--
Making debugger more stable with LPC
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
     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
6bd3f220b886 Fixed PLC status bytes values
Lolitech
parents: 545
diff changeset
     4
LPC_STATUS={0xaa : "Started",
6bd3f220b886 Fixed PLC status bytes values
Lolitech
parents: 545
diff changeset
     5
            0x55 : "Stopped"}
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     6
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
     7
class LPCAppProto(LPCProto):
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     8
    def HandleTransaction(self, transaction):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     9
        self.TransactionLock.acquire()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    10
        try:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    11
            transaction.SetPseudoFile(self.serialPort)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    12
            # send command, wait ack (timeout)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    13
            transaction.SendCommand()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    14
            current_plc_status = transaction.GetCommandAck()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    15
            if current_plc_status is not None:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    16
                res = transaction.ExchangeData()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    17
            else:
563
c74a37d156df Better serial comm handling in LPC connector
Lolitech
parents: 554
diff changeset
    18
                raise LPCProtoError("controller did not answer as expected")
c74a37d156df Better serial comm handling in LPC connector
Lolitech
parents: 554
diff changeset
    19
        except Exception, e:
576
7fcdc0d3d8d9 Some typo fixes to make debug related methods in LPCAppOject stop throwing exceptions, less agressive error message when unplugging LPC
edouard
parents: 571
diff changeset
    20
            raise LPCProtoError("application mode transaction error : "+str(e))
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    21
        finally:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    22
            self.TransactionLock.release()
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
    23
        return LPC_STATUS.get(current_plc_status,"Broken"), res
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    24
    
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    25
class LPCAppTransaction:
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    26
    def __init__(self, command, optdata = ""):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    27
        self.Command = command
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    28
        self.OptData = optdata
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    29
        self.pseudofile = None
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    30
        
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    31
    def SetPseudoFile(self, pseudofile):
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    32
        self.pseudofile = pseudofile
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    33
        
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    34
    def SendCommand(self):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    35
        # send command thread
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    36
        self.pseudofile.write(chr(self.Command))
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    37
        
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    38
    def GetCommandAck(self):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    39
        res = self.pseudofile.read(2)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    40
        if len(res) == 2:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    41
            comm_status, current_plc_status = map(ord, res)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    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
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    44
        # LPC returns command itself as an ack for command
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    45
        if(comm_status == self.Command):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    46
            return current_plc_status
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    47
        return None 
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    48
        
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    49
    def SendData(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    50
        length = len(self.OptData)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    51
        # transform length into a byte string
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    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
9b77aabf3d36 Cosmetix fixes for LPC tests/build
Lolitech
parents: 508
diff changeset
    55
        return self.pseudofile.write(buffer)
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    56
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    57
    def GetData(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    58
        lengthstr = self.pseudofile.read(4)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    59
        # transform a byte string into length 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    60
        length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    61
        return self.pseudofile.read(length)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    62
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    63
    def ExchangeData(self): 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    64
        pass
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    65
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    66
class IDLETransaction(LPCAppTransaction):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    67
    def __init__(self):
570
46abd6b2f639 LPC application mode IDLE comamnd is now the same as PLCID (0x07)
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 563
diff changeset
    68
        LPCAppTransaction.__init__(self, 0x07)
46abd6b2f639 LPC application mode IDLE comamnd is now the same as PLCID (0x07)
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 563
diff changeset
    69
    ExchangeData = LPCAppTransaction.GetData
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    70
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    71
class STARTTransaction(LPCAppTransaction):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    72
    def __init__(self):
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    73
        LPCAppTransaction.__init__(self, 0x01)
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    74
    
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    75
class STOPTransaction(LPCAppTransaction):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    76
    def __init__(self):
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    77
        LPCAppTransaction.__init__(self, 0x02)
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    78
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    79
class RESETTransaction(LPCAppTransaction):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    80
    def __init__(self):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    81
        LPCAppTransaction.__init__(self, 0x03)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    82
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    83
class SET_TRACE_VARIABLETransaction(LPCAppTransaction):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    84
    def __init__(self, data):
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    85
        LPCAppTransaction.__init__(self, 0x04, data)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    86
    ExchangeData = LPCAppTransaction.SendData
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    87
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    88
class GET_TRACE_VARIABLETransaction(LPCAppTransaction):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    89
    def __init__(self):
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    90
        LPCAppTransaction.__init__(self, 0x05)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    91
    ExchangeData = LPCAppTransaction.GetData
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    92
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    93
class GET_PLCIDTransaction(LPCAppTransaction):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    94
    def __init__(self):
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    95
        LPCAppTransaction.__init__(self, 0x07)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
    96
    ExchangeData = LPCAppTransaction.GetData
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    97
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    98
if __name__ == "__main__":
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 570
diff changeset
    99
    __builtins__.BMZ_DBG = True
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
   100
    TestConnection = LPCAppProto(6,115200,2)
482
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   101
#    TestConnection.HandleTransaction(GET_PLCIDTransaction())
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   102
    TestConnection.HandleTransaction(STARTTransaction())
482
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   103
#    TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   104
#           "\x03\x00\x00\x00"*200))
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   105
#    TestConnection.HandleTransaction(STARTTransaction())
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   106
    while True:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   107
        TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   108
           "\x01\x00\x00\x00"+
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   109
           "\x04"+
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   110
           "\x01\x02\x02\x04"+
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   111
           "\x01\x00\x00\x00"+
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   112
           "\x08"+
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   113
           "\x01\x02\x02\x04"+
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   114
           "\x01\x02\x02\x04"+
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   115
           "\x01\x00\x00\x00"+
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   116
           "\x04"+
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 503
diff changeset
   117
           "\x01\x02\x02\x04"))
502
5343ae43f6d0 LPC connector - one step further
edouard
parents: 482
diff changeset
   118
    #status,res = TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction())
5343ae43f6d0 LPC connector - one step further
edouard
parents: 482
diff changeset
   119
    #print len(res)
5343ae43f6d0 LPC connector - one step further
edouard
parents: 482
diff changeset
   120
    #print "GOT : ", map(hex, map(ord, res))
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   121
    #TestConnection.HandleTransaction(STOPTransaction())