Esempio n. 1
0
    def __init__(self, file_name='', file=None):
        if (file == None):
            file = open(file_name, 'U')

        ext_address = 0
        self.records = []
        eof = False
        for line in file:
            hex_record_regexp = re.compile(r'^:[0-9a-fA-F]+$')
            hex_record_chopper = re.compile(r'^:(..)(....)(..)(.*)(..)$')
            if (hex_record_regexp.match(line)):
                if (eof):
                    raise Invalid_Record_Error(
                        'extra record after EOF record.')
                chop = hex_record_chopper.match(line)
                (length_str, address_str, type_str, data_str,
                 checksum_str) = chop.groups()
                length = int(length_str, 16)
                address = int(address_str, 16)
                type = int(type_str, 16)
                data = data_string.data_string(''.join([
                    chr(int(data_str[x:x + 2], 16))
                    for x in range(0, len(data_str), 2)
                ]))
                checksum = int(checksum_str, 16)

                if (length != len(data)):
                    raise Invalid_Record_Error('Incorrect data length: ' +
                                               str(length) + ' != ' +
                                               str(len(data)))

                checksum_test = 0
                for x in range(1, len(line) - 4, 2):
                    checksum_test = (checksum_test +
                                     int(line[x:x + 2], 16)) % 256
                checksum_test = (256 - checksum_test) % 256
                if (checksum_test != checksum):
                    raise self.Invalid_Checksum_Error(
                        str(checksum_test) + ' != ' + str(checksum))

                if (type == 0):
                    # data record
                    self.records.append(((address | ext_address), data))
                elif (type == 1):
                    # EOF record
                    eof = True
                elif (type == 2):
                    # Ext. linear address record
                    ext_address = struct.unpack('>H', data)[0] << 16
                elif (type == 4):
                    ext_address = struct.unpack('>H', data)[0] << 4
                else:
                    raise self.Invalid_Record_Error('Unknown record type (' +
                                                    str(type) + ')')
            elif (len(line) != 0):
                raise self.Invalid_Record_Error(
                    'Record does not start with colon:  ' + line)
Esempio n. 2
0
    def merge(self, data_str):
        data_list = list(data_str)

        for record in self.records:
            (address, data) = record

            if ((address + len(data)) > len(data_list)):
                raise IndexError, 'Data record out of range.'

            data_list[address:address + len(data)] = data

        return data_string.data_string(''.join(data_list))
Esempio n. 3
0
    def merge(self, data_str):
        data_list = list(data_str)

        for record in self.records:
            (address, data) = record

            if ((address + len(data)) > len(data_list)):
                raise IndexError, 'Data record out of range.'

            data_list[address:address + len(data)] = data

        return data_string.data_string(''.join(data_list))
Esempio n. 4
0
    def __init__(self, file_name = '', file = None):
        if (file == None):
            file = open(file_name, 'U')

        ext_address = 0
        self.records = []
        eof = False
        for line in file:
            hex_record_regexp = re.compile(r'^:[0-9a-fA-F]+$')
            hex_record_chopper = re.compile(r'^:(..)(....)(..)(.*)(..)$')
            if (hex_record_regexp.match(line)):
                if (eof):
                    raise Invalid_Record_Error, 'extra record after EOF record.'
                chop = hex_record_chopper.match(line)
                (length_str,
                 address_str,
                 type_str,
                 data_str,
                 checksum_str) = chop.groups()
                length = int(length_str, 16)
                address = int(address_str, 16)
                type = int(type_str, 16)
                data = data_string.data_string(''.join([chr(int(data_str[x:x+2], 16)) for x in xrange(0, len(data_str), 2)]))
                checksum = int(checksum_str, 16)

                if (length != len(data)):
                    raise Invalid_Record_Error, 'Incorrect data length: ' + str(length) + ' != ' + str(len(data))

                checksum_test = 0
                for x in xrange(1, len(line) - 4, 2):
                    checksum_test = (checksum_test + int(line[x:x+2], 16)) % 256
                checksum_test = (256 - checksum_test) % 256
                if (checksum_test != checksum):
                    raise self.Invalid_Checksum_Error, str(checksum_test) + ' != ' + str(checksum)

                if (type == 0):
                    # data record
                    self.records.append(((address | ext_address),
                                         data))
                elif (type == 1):
                    # EOF record
                    eof = True
                elif (type == 2):
                    # Ext. linear address record
                    ext_address = struct.unpack('>H', data)[0] << 16
                elif (type == 4):
                    ext_address = struct.unpack('>H', data)[0] << 4
                else:
                    raise self.Invalid_Record_Error, 'Unknown record type (' + str(type) + ')'
            elif (len(line) != 0):
                raise self.Invalid_Record_Error, 'Record does not start with colon:  ' + line
Esempio n. 5
0
    def read_eeprom(self):
        "Returns data stored in PIC EEPROM."
        cmd = 12
        self._need_vars()

        eeprom_size = self.vars['eeprom_size']

        self._command_start()
        self._set_programming_voltages_command(True)
        self.port.write(chr(cmd))
        response = self._read(eeprom_size)
        self._set_programming_voltages_command(False)
        self._command_end()
        return data_string.data_string(response)
Esempio n. 6
0
    def read_eeprom(self):
        "Returns data stored in PIC EEPROM."
        cmd = 12
        self._need_vars()

        eeprom_size = self.vars['eeprom_size']

        self._command_start()
        self._set_programming_voltages_command(True)
        self.port.write(chr(cmd))
        response = self._read(eeprom_size)
        self._set_programming_voltages_command(False)
        self._command_end()
        return data_string.data_string(response)
Esempio n. 7
0
    def read_rom(self):
        "Returns contents of PIC ROM as a string of big-endian values."
        cmd = 11
        self._need_vars()

        # vars['rom_size'] is in words.  So multiply by two to get bytes.
        rom_size = self.vars['rom_size'] * 2

        self._command_start()
        self._set_programming_voltages_command(True)
        self.port.write(chr(cmd))

        response = self._read(rom_size)

        self._set_programming_voltages_command(False)
        self._command_end()
        return data_string.data_string(response)
Esempio n. 8
0
    def read_rom(self):
        "Returns contents of PIC ROM as a string of big-endian values."
        cmd = 11
        self._need_vars()

        # vars['rom_size'] is in words.  So multiply by two to get bytes.
        rom_size = self.vars['rom_size'] * 2

        self._command_start()
        self._set_programming_voltages_command(True)
        self.port.write(chr(cmd))

        response = self._read(rom_size)

        self._set_programming_voltages_command(False)
        self._command_end()
        return data_string.data_string(response)