connectors/LPC/LPCBootProto.py
author laurent
Wed, 21 Dec 2011 19:42:49 +0100
changeset 657 340c0b9caeca
parent 600 310455d73131
permissions -rw-r--r--
Adding support for integrating Python code Editor into Beremiz frame.
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     1
from LPCProto import *
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     2
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     3
class LPCBootProto(LPCProto):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     4
    def HandleTransaction(self, transaction):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     5
        self.TransactionLock.acquire()
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     6
        try:
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     7
            transaction.SetPseudoFile(self.serialPort)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     8
            res = transaction.ExchangeData()
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     9
        finally:
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    10
            self.TransactionLock.release()
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    11
        return "Stopped", res
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    12
    
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    13
class LPCBootTransaction:
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    14
    def __init__(self, optdata = ""):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    15
        self.OptData = optdata
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    16
        self.pseudofile = None
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    17
        
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    18
    def SetPseudoFile(self, pseudofile):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    19
        self.pseudofile = pseudofile
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    20
        
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    21
    def ExchangeData(self): 
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    22
        self.pseudofile.write(self.OptData)
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    23
        return map(lambda x:self.pseudofile.readline(), xrange(self.expectedlines))
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    24
567
72b51ec5be64 keep LPC in boot mode to give a chance to ask for a transfer
edouard
parents: 548
diff changeset
    25
class KEEPBOOTINGTransaction(LPCBootTransaction):
72b51ec5be64 keep LPC in boot mode to give a chance to ask for a transfer
edouard
parents: 548
diff changeset
    26
    def __init__(self):
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    27
        self.expectedlines = 2
567
72b51ec5be64 keep LPC in boot mode to give a chance to ask for a transfer
edouard
parents: 548
diff changeset
    28
        LPCBootTransaction.__init__(self, "md5\n")
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    29
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    30
class STARTTransaction(LPCBootTransaction):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    31
    def __init__(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    32
        self.expectedlines = 0
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    33
        LPCBootTransaction.__init__(self, "go\n")
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    34
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    35
class CHECKMD5Transaction(LPCBootTransaction):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    36
    def __init__(self, md5ref):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    37
        self.expectedlines = 5 
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    38
        LPCBootTransaction.__init__(self, md5ref+"md5\n")
567
72b51ec5be64 keep LPC in boot mode to give a chance to ask for a transfer
edouard
parents: 548
diff changeset
    39
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    40
class LOADTransaction(LPCBootTransaction):
569
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    41
    def __init__(self, data, PLCprint):
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    42
        self.PLCprint = PLCprint
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    43
        LPCBootTransaction.__init__(self, data)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    44
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 569
diff changeset
    45
    def ExchangeData(self):
569
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    46
        #file("fw.bin","w").write(self.OptData)
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    47
        data = self.OptData
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    48
        loptdata = len(self.OptData)
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    49
        count=0
600
310455d73131 Print binary size only in LPCBeremiz
Edouard Tisserant
parents: 571
diff changeset
    50
        #self.PLCprint("%dkB:" % (loptdata/1024))
569
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    51
        while len(data)>0:
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    52
            res = self.pseudofile.write(data[:loptdata/100])
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    53
            data = data[res:]
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    54
            count += 1
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    55
            if count % 10 == 0 :
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    56
                self.PLCprint("%d%%" % count)
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    57
            else :
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    58
                self.PLCprint(".")
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    59
        self.PLCprint("\n")
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    60
        return True
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    61
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    62
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: 569
diff changeset
    63
    __builtins__.BMZ_DBG = True
569
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    64
    TestConnection = LPCBootProto(2,115200,1200)
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    65
    mystr=file("fw.bin").read()
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    66
    def mylog(blah):
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    67
        print blah,
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    68
37af7286dd65 LPC program transfer more verbose, test code added in __main__ of LPCBootProto
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 567
diff changeset
    69
    TestConnection.HandleTransaction(LOADTransaction(mystr, mylog))