connectors/LPC/LPCProto.py
author Edouard TISSERANT <edouard.tisserant@gmail.com>
Sun, 06 Dec 2009 19:39:24 +0100
changeset 461 bcbc472c0ba8
parent 453 923d036dfa90
child 482 7c83eb6a55bd
permissions -rw-r--r--
Safer debug
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
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     7
LPC_STATUS=dict(STARTED = 0x01,
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     8
                STOPPED = 0x02,
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     9
                DEBUG = 0x03)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    10
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    11
class LPCError(exceptions.Exception):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    12
        """Exception class"""
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    13
        def __init__(self, msg):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    14
                self.msg = msg
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    15
                return
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    16
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    17
        def __str__(self):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    18
                return "LPC communication error ! " + str(self.msg)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    19
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    20
class LPCProto:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    21
    def __init__(self, port, rate, timeout):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    22
        # serialize access lock
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    23
        self.TransactionLock = Lock()
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    24
        # open serial port
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    25
#        self.serialPort = serial.Serial( port, rate, timeout = timeout )
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    26
        # Debugging serial stuff
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
        class myser:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    29
            def read(self_,cnt):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    30
                res = self._serialPort.read(cnt)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    31
                print "Recv :", map(hex,map(ord,res))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    32
                return res
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    33
            def write(self_, str):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    34
                print "Send :", map(hex,map(ord,str))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    35
                self._serialPort.write(str)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    36
            def flush(self_):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    37
                self._serialPort.flush()
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    38
        self.serialPort = myser()
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    39
        # start with empty
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    40
        self.serialPort.flush()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    41
        # handshake
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    42
        self.HandleTransaction(IDLETransaction())
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    43
    
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    44
    def HandleTransaction(self, transaction):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    45
        self.TransactionLock.acquire()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    46
        try:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    47
            transaction.SetPseudoFile(self.serialPort)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    48
            # send command, wait ack (timeout)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    49
            transaction.SendCommand()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    50
            current_plc_status = transaction.GetCommandAck()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    51
            if current_plc_status is not None:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    52
                res = transaction.ExchangeData()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    53
            else:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    54
                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
    55
        finally:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    56
            self.TransactionLock.release()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    57
        return current_plc_status, res
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    58
    
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    59
class LPCTransaction:
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    60
    def __init__(self, command, optdata = ""):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    61
        self.Command = command
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    62
        self.OptData = optdata
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    63
        self.pseudofile = None
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    64
        
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    65
    def SetPseudoFile(self, pseudofile):
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    66
        self.pseudofile = pseudofile
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    67
        
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    68
    def SendCommand(self):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    69
        # send command thread
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    70
        self.pseudofile.write(chr(self.Command))
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    71
        
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    72
    def GetCommandAck(self):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    73
        res = self.pseudofile.read(2)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    74
        if len(res) == 2:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    75
            comm_status, current_plc_status = map(ord, res)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    76
        else:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    77
            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
    78
        # 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
    79
        if(comm_status == self.Command):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    80
            return current_plc_status
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    81
        return None 
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    82
        
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    83
    def SendData(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    84
        length = len(self.OptData)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    85
        # transform length into a byte string
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    86
        # we presuppose endianess of LPC same as PC
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    87
        lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4) 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    88
        self.pseudofile.write(lengthstr + self.OptData)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    89
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    90
    def GetData(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    91
        lengthstr = self.pseudofile.read(4)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    92
        # transform a byte string into length 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    93
        length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    94
        return self.pseudofile.read(length)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    95
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    96
    def ExchangeData(self): 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    97
        pass
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    98
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    99
class IDLETransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   100
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   101
        LPCTransaction.__init__(self, 0x00)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   102
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   103
class STARTTransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   104
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   105
        LPCTransaction.__init__(self, 0x01)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   106
    
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   107
class STOPTransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   108
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   109
        LPCTransaction.__init__(self, 0x02)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   110
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   111
class SET_TRACE_VARIABLETransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   112
    def __init__(self, data):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   113
        LPCTransaction.__init__(self, 0x04, data)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   114
    ExchangeData = LPCTransaction.SendData
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   115
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   116
class GET_TRACE_VARIABLETransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   117
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   118
        LPCTransaction.__init__(self, 0x05)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   119
    ExchangeData = LPCTransaction.GetData
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   120
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   121
class SET_FORCED_VARIABLETransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   122
    def __init__(self, data):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   123
        LPCTransaction.__init__(self, 0x06, data)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   124
    ExchangeData = LPCTransaction.SendData
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   125
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   126
class GET_PLCIDTransaction(LPCTransaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   127
    def __init__(self):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   128
        LPCTransaction.__init__(self, 0x07)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   129
    ExchangeData = LPCTransaction.GetData
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   130
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   131
if __name__ == "__main__":
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   132
    TestConnection = LPCProto(6,115200,2)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   133
    #TestConnection.HandleTransaction(GET_PLCIDTransaction())
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   134
    TestConnection.HandleTransaction(STARTTransaction())
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   135
    TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   136
           "\x03\x00\x00\x00"))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   137
    TestConnection.HandleTransaction(STARTTransaction())
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   138
    while True:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   139
        time.sleep(0.5)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   140
        TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   141
           "\x01\x00\x00\x00"*31))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   142
   #print map(hex,map(ord,TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction())))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   143
    #TestConnection.HandleTransaction(STOPTransaction())