connectors/LPC/LPCProto.py
author laurent
Thu, 10 Dec 2009 09:34:55 +0100
changeset 494 9e4263099427
parent 482 7c83eb6a55bd
child 502 5343ae43f6d0
permissions -rw-r--r--
Bug when opening with not empty buildpath parameter fixed
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     1
import serial
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
     2
import exceptions
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
     3
import ctypes
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
     4
import time
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     5
from threading import Lock
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     6
482
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
     7
MAX_PACKET_SIZE=64
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
     8
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     9
LPC_STATUS=dict(STARTED = 0x01,
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    10
                STOPPED = 0x02,
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    11
                DEBUG = 0x03)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    12
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    13
class LPCError(exceptions.Exception):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    14
        """Exception class"""
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    15
        def __init__(self, msg):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    16
                self.msg = msg
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    17
                return
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    18
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    19
        def __str__(self):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    20
                return "LPC communication error ! " + str(self.msg)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    21
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    22
class LPCProto:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    23
    def __init__(self, port, rate, timeout):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    24
        # serialize access lock
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    25
        self.TransactionLock = Lock()
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    26
        # open serial port
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    27
#        self.serialPort = serial.Serial( port, rate, timeout = timeout )
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    28
        # Debugging serial stuff
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    29
        self._serialPort = serial.Serial( port, rate, timeout = timeout )
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    30
        class myser:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    31
            def read(self_,cnt):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    32
                res = self._serialPort.read(cnt)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    33
                print "Recv :", map(hex,map(ord,res))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    34
                return res
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    35
            def write(self_, str):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    36
                print "Send :", map(hex,map(ord,str))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    37
                self._serialPort.write(str)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    38
            def flush(self_):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    39
                self._serialPort.flush()
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    40
        self.serialPort = myser()
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    41
        # start with empty
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    42
        self.serialPort.flush()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    43
        # handshake
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    44
        self.HandleTransaction(IDLETransaction())
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    45
    
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    46
    def HandleTransaction(self, transaction):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    47
        self.TransactionLock.acquire()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    48
        try:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    49
            transaction.SetPseudoFile(self.serialPort)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    50
            # send command, wait ack (timeout)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    51
            transaction.SendCommand()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    52
            current_plc_status = transaction.GetCommandAck()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    53
            if current_plc_status is not None:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    54
                res = transaction.ExchangeData()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    55
            else:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    56
                raise LPCError("LPC transaction error - controller did not answer as expected")
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    57
        finally:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    58
            self.TransactionLock.release()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    59
        return current_plc_status, res
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    60
    
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    61
class LPCTransaction:
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    62
    def __init__(self, command, optdata = ""):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    63
        self.Command = command
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    64
        self.OptData = optdata
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    65
        self.pseudofile = None
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    66
        
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    67
    def SetPseudoFile(self, pseudofile):
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    68
        self.pseudofile = pseudofile
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    69
        
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    70
    def SendCommand(self):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    71
        # send command thread
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    72
        self.pseudofile.write(chr(self.Command))
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    73
        
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    74
    def GetCommandAck(self):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    75
        res = self.pseudofile.read(2)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    76
        if len(res) == 2:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    77
            comm_status, current_plc_status = map(ord, res)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    78
        else:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    79
            raise LPCError("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
    80
        # 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
    81
        if(comm_status == self.Command):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    82
            return current_plc_status
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    83
        return None 
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    84
        
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    85
    def SendData(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    86
        length = len(self.OptData)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    87
        # transform length into a byte string
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    88
        # 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
    89
        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
    90
        buffer = lengthstr + self.OptData
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
    91
        ###################################################################
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
    92
        # TO BE REMOVED AS SOON AS USB IS FIXED IN CONTROLLER
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
    93
        ###################################################################
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
    94
        length += 4
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
    95
        cursor = 0
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
    96
        while cursor < length:
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
    97
            next_cursor = cursor + MAX_PACKET_SIZE
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
    98
            # sent just enough bytes to not crash controller
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
    99
            self.pseudofile.write(buffer[cursor:next_cursor])
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   100
            # if sent quantity was 128
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   101
            if next_cursor <= length:
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   102
                self.GetCommandAck()
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   103
            cursor = next_cursor
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   104
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   105
    def GetData(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   106
        lengthstr = self.pseudofile.read(4)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   107
        # transform a byte string into length 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   108
        length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   109
        return self.pseudofile.read(length)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   110
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   111
    def ExchangeData(self): 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   112
        pass
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   113
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   114
class IDLETransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   115
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   116
        LPCTransaction.__init__(self, 0x00)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   117
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   118
class STARTTransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   119
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   120
        LPCTransaction.__init__(self, 0x01)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   121
    
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   122
class STOPTransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   123
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   124
        LPCTransaction.__init__(self, 0x02)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   125
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   126
class SET_TRACE_VARIABLETransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   127
    def __init__(self, data):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   128
        LPCTransaction.__init__(self, 0x04, data)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   129
    ExchangeData = LPCTransaction.SendData
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   130
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   131
class GET_TRACE_VARIABLETransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   132
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   133
        LPCTransaction.__init__(self, 0x05)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   134
    ExchangeData = LPCTransaction.GetData
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   135
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   136
class SET_FORCED_VARIABLETransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   137
    def __init__(self, data):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   138
        LPCTransaction.__init__(self, 0x06, data)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   139
    ExchangeData = LPCTransaction.SendData
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   140
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   141
class GET_PLCIDTransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   142
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   143
        LPCTransaction.__init__(self, 0x07)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   144
    ExchangeData = LPCTransaction.GetData
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   145
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   146
if __name__ == "__main__":
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   147
    TestConnection = LPCProto(6,115200,2)
482
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   148
#    TestConnection.HandleTransaction(GET_PLCIDTransaction())
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   149
    TestConnection.HandleTransaction(STARTTransaction())
482
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   150
#    TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   151
#           "\x03\x00\x00\x00"*200))
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   152
#    TestConnection.HandleTransaction(STARTTransaction())
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   153
    while True:
482
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   154
        #time.sleep(0.5)
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   155
        TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
482
7c83eb6a55bd fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 453
diff changeset
   156
           "\x01\x00\x00\x00"*200))
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   157
   #print map(hex,map(ord,TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction())))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   158
    #TestConnection.HandleTransaction(STOPTransaction())