Exemplo n.º 1
0
    def flash_target(self):
        # create new instance of TI-TXT class
        ti_txt = TiTxtParser(self.verbose_mode)

        # parse the TI-TXT
        content = ti_txt.parse(self.file_name)
        if(content == {}):
            if(self.verbose_mode == True):
                print "Failed to parse TI-TXT file:", self.file_name
            return False
        #ti_txt.debug_print_content()

        # try to fill the data
        full_content = ti_txt.fill(content, self.start_addr, 0xFFFF, 0xFF)
        if(full_content == {}):
            if(self.verbose_mode == True):
                print "Failed to fill TI-TXT file"
            return False
        #ti_txt.debug_print_full_content(full_content)

        if(self.verbose_mode == True):
            print "\n== Flashing Target Device =="

        # try to open the serial-port
        try:
            if(self.verbose_mode == True):
                print "Opening Serial Port:", self.serial_port
            ser = serial.Serial(self.serial_port, timeout=8)
        except:
            if(self.verbose_mode == True):
                print "Failed to open serial port"
            return False

        # flush input output
        ser.flushInput()
        ser.flushOutput()

        # send CMD_SYNC byte
        if(self.verbose_mode == True):
            print "Sending SYNC byte (", hex(CMD_SYNC),")"
        ser.write(('' + chr(CMD_SYNC)))
        time.sleep(SLEEP_1MS * 100) # device needs time to rewrite int.vector

        # send the data bytes and calculate checksum while sending
        if(self.verbose_mode == True):
            data_len = len(range(self.start_addr, 0xFFFE))
            print "Sending binary data - length:", data_len, "(",
            print hex(data_len), ") bytes"
        chksum = 0
        for addr in range(self.start_addr, 0xFFFE):
            byte = full_content[self.start_addr][addr-self.start_addr]
            ser.write(('' + chr(byte)))
            time.sleep(SLEEP_1MS * 5) # sleep 5 ms between sending bytes
            chksum ^= byte

        # send checksum
        if(self.verbose_mode == True):
            print "Sending checksum byte (", hex(chksum),")"
        ser.write(('' + chr(chksum)))
        time.sleep(SLEEP_1MS * 5) # sleep 5 ms between sending bytes

        # wait for reply
        if(self.verbose_mode == True):
        	print "Reading reply from target"
        byte = ser.read()
        ser.close()
        if(byte == chr(ACK)):
            if(self.verbose_mode == True):
                print "received ACK - SUCCESS"
            return True
        else:
            if(self.verbose_mode == True):
            	print "received NACK - ERROR"
            return False