targets/LPC/__init__.py
author Edouard Tisserant
Wed, 09 May 2012 00:00:50 +0200
changeset 725 31dade089db5
parent 717 1c23952dbde1
permissions -rwxr-xr-x
refactoring
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
     1
import os
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
     2
from subprocess import Popen,PIPE
648
73295e742da2 Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents: 600
diff changeset
     3
from ..toolchain_makefile import toolchain_makefile
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
     4
import hashlib
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
     5
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
     6
class LPC_target(toolchain_makefile):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
     7
    #extension = ".ld"
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
     8
    #DebugEnabled = False
725
31dade089db5 refactoring
Edouard Tisserant
parents: 717
diff changeset
     9
    def __init__(self, CTRInstance):
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    10
        self.binmd5key = None
725
31dade089db5 refactoring
Edouard Tisserant
parents: 717
diff changeset
    11
        toolchain_makefile.__init__(self, CTRInstance)
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    12
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    13
    def _GetBinMD5FileName(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    14
        return os.path.join(self.buildpath, "lastbuildPLCbin.md5")
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    15
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    16
    def _get_md5_header(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    17
        """Returns signature header"""
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    18
        size = int(Popen(
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    19
             ['arm-elf-size','-B',os.path.join(self.buildpath,"ArmPLC_rom.elf")],
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    20
             stdout=PIPE).communicate()[0].splitlines()[1].split()[0])
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    21
        res = "&" + hashlib.md5(open(os.path.join(self.buildpath, "ArmPLC_rom.bin"), "rb").read(size)).hexdigest() + '\n' +\
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    22
              "$" + str(size) + '\n'
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    23
        return res
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    24
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    25
    def GetBinaryCode(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    26
        """Returns ready to send signed + sized intel formated hex program"""
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    27
        try:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    28
            res = self._get_md5_header() +\
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    29
                   open(os.path.join(self.buildpath, "ArmPLC_rom.hex"), "r").read()
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    30
            return res
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    31
        except Exception, e:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    32
            return None
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    33
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    34
    def _get_cached_md5_header(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    35
        if self.binmd5key is not None:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    36
            return self.binmd5key
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    37
        else:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    38
            try:
600
310455d73131 Print binary size only in LPCBeremiz
Edouard Tisserant
parents: 571
diff changeset
    39
                return open(self._GetBinMD5FileName(), "r").read()
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    40
            except IOError, e:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    41
                return None
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    42
691
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    43
    def ResetBinaryCodeMD5(self, mode):
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    44
        if mode == "BOOTLOADER":
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    45
            self.binmd5key = None
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    46
            try:
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    47
                os.remove(self._GetBinMD5FileName())
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    48
            except Exception, e:
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    49
                pass
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    50
        else:
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    51
            return toolchain_makefile.ResetBinaryCodeMD5(self)
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    52
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    53
    def GetBinaryCodeMD5(self, mode):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    54
        if mode == "BOOTLOADER":
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    55
            return self._get_cached_md5_header()
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    56
        else:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    57
            return toolchain_makefile.GetBinaryCodeMD5(self)
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    58
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    59
    def build(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    60
        res = toolchain_makefile.build(self)
691
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    61
        if res:
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    62
            self.binmd5key = self._get_md5_header()
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    63
            f = open(self._GetBinMD5FileName(), "w")
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    64
            f.write(self.binmd5key)
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    65
            f.close()
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    66
            try:
725
31dade089db5 refactoring
Edouard Tisserant
parents: 717
diff changeset
    67
                self.CTRInstance.logger.write(
691
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    68
                    _("Binary is %s bytes long\n")%
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    69
                        str(os.path.getsize(
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    70
                            os.path.join(self.buildpath, "ArmPLC_rom.bin"))))
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    71
            except Exception, e:
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 648
diff changeset
    72
                pass
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    73
        return res
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 540
diff changeset
    74