Beispiel #1
0
    def test_flash_write_fast_data_packet_failure(self):
        argv = ['pictools', 'flash_write', 'test_flash_write.s19']

        chunk = bytes(range(256))
        serial.Serial.read.side_effect = [
            *programmer_ping_read(), *connect_read(), *ping_read(),
            b'\xff\xff', b'\x00\x04', b'\xff\xff\xfc\x10', b'\x49\x5b'
        ]

        with open('test_flash_write.s19', 'w') as fout:
            binfile = bincopy.BinFile()
            binfile.add_binary(chunk, 0x1d000000)
            fout.write(binfile.as_srec())

        with patch('sys.argv', argv):
            with self.assertRaises(SystemExit) as cm:
                pictools.main()

            self.assertEqual(str(cm.exception),
                             'error: 1008: flash write failed')

        expected_writes = [
            programmer_ping_write(),
            connect_write(),
            ping_write(),
            flash_write_fast_write(0x1d000000, 256, 0x3fbd, 0x54b7),
            flash_write_fast_data_write(chunk)
        ]

        self.assert_calls(serial.Serial.write.call_args_list, expected_writes)
Beispiel #2
0
    def test_execute_command_failure_response(self):
        argv = ['pictools', 'device_status_print']

        serial.Serial.read.side_effect = [
            *programmer_ping_read(), *failure_read()
        ]

        with patch('sys.argv', argv):
            with self.assertRaises(SystemExit) as cm:
                pictools.main()

            self.assertEqual(str(cm.exception),
                             'error: 1008: flash write failed')
Beispiel #3
0
    def test_execute_command_bad_crc_response(self):
        argv = ['pictools', 'device_status_print']

        serial.Serial.read.side_effect = [
            *programmer_ping_read(), b'\x00\x68\x00\x01', b'\xba', b'\xf8\xbc'
        ]

        with patch('sys.argv', argv):
            with self.assertRaises(SystemExit) as cm:
                pictools.main()

            self.assertEqual(
                str(cm.exception),
                'error: expected response packet crc 0xf8bd, but got 0xf8bc')
Beispiel #4
0
    def test_execute_command_short_crc_response(self):
        argv = ['pictools', 'device_status_print']

        serial.Serial.read.side_effect = [
            *programmer_ping_read(), b'\x00\x00\x00\x00', b'\x55'
        ]

        with patch('sys.argv', argv):
            with self.assertRaises(SystemExit) as cm:
                pictools.main()

            self.assertEqual(
                str(cm.exception),
                'error: timeout reading the response packet crc from the '
                'programmer')
Beispiel #5
0
    def test_execute_command_wrong_response(self):
        argv = ['pictools', 'device_status_print']

        serial.Serial.read.side_effect = [
            *programmer_ping_read(),
            # Unexpected erase response.
            *flash_erase_read()
        ]

        with patch('sys.argv', argv):
            with self.assertRaises(SystemExit) as cm:
                pictools.main()

            self.assertEqual(
                str(cm.exception),
                'error: expected programmer command response type '
                'PROGRAMMER_DEVICE_STATUS, but got ERASE')
Beispiel #6
0
    def test_programmer_upload(self):
        argv = ['pictools', 'programmer_upload']

        check_call = Mock()
        find_executable = Mock(return_value='found')
        sleep = Mock()

        with patch('subprocess.check_call', check_call):
            with patch('pictools.find_executable', find_executable):
                with patch('time.sleep', sleep):
                    with patch('sys.argv', argv):
                        pictools.main()

        serial.Serial.__init__.assert_called_once_with('/dev/ttyUSB1', 1200)
        sleep.assert_called_once_with(2)
        find_executable.assert_called_once_with('bossac')
        self.assertEqual(len(check_call.call_args_list), 1)
        self.assertEqual(
            check_call.call_args_list[0][0][0][:-1],
            ['bossac', '--port', 'ttyUSB1', '-e', '-w', '-b', '-R'])
Beispiel #7
0
    def assert_command(self,
                       argv,
                       expected_reads,
                       expected_writes,
                       expected_output_lines=None):
        stdout = StringIO()
        serial.Serial.read.side_effect = expected_reads

        if expected_output_lines is not None:
            with patch('sys.argv', argv):
                with patch('sys.stdout', stdout):
                    pictools.main()

            actual_output = stdout.getvalue()
            expected_output = '\n'.join(expected_output_lines)
            self.assertEqual(actual_output, expected_output)
        else:
            with patch('sys.argv', argv):
                pictools.main()

        self.assert_calls(serial.Serial.write.call_args_list, expected_writes)
Beispiel #8
0
    def test_generate_ramapp_upload_instructions(self):
        argv = [
            'pictools', 'generate_ramapp_upload_instructions',
            'tests/files/ramapp.out',
            'test_generate_ramapp_upload_instructions.i'
        ]

        with open('tests/files/ramapp.dis', 'rb') as fin:
            check_output = Mock(return_value=fin.read())

        with patch('subprocess.check_output', check_output):
            with patch('sys.argv', argv):
                pictools.main()

        with open('tests/files/ramapp.i', 'r') as fin:
            expected = fin.read()
            expected = expected.format(pictools.__version__)

        with open('test_generate_ramapp_upload_instructions.i', 'r') as fin:
            actual = fin.read()

        self.assertEqual(actual, expected)