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