You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					129 lines
				
				3.4 KiB
			
		
		
			
		
	
	
					129 lines
				
				3.4 KiB
			| 
								 
											2 years ago
										 
									 | 
							
								import os
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								import pytest
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								import time
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import cereal.messaging as messaging
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								from cereal import log
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.common.gpio import gpio_set, gpio_init
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from panda import Panda, PandaDFU, PandaProtocolMismatch
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								from openpilot.system.manager.process_config import managed_processes
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.system.hardware import HARDWARE
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.system.hardware.tici.pins import GPIO
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								HERE = os.path.dirname(os.path.realpath(__file__))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								@pytest.mark.tici
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								class TestPandad:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  def setup_method(self):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # ensure panda is up
							 | 
						||
| 
								 | 
							
								    if len(Panda.list()) == 0:
							 | 
						||
| 
								 | 
							
								      self._run_test(60)
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self.spi = HARDWARE.get_device_type() != 'tici'
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  def teardown_method(self):
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    managed_processes['pandad'].stop()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  def _run_test(self, timeout=30) -> float:
							 | 
						||
| 
								 | 
							
								    st = time.monotonic()
							 | 
						||
| 
								 | 
							
								    sm = messaging.SubMaster(['pandaStates'])
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    managed_processes['pandad'].start()
							 | 
						||
| 
								 | 
							
								    while (time.monotonic() - st) < timeout:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      sm.update(100)
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      if len(sm['pandaStates']) and sm['pandaStates'][0].pandaType != log.PandaState.PandaType.unknown:
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								        break
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    dt = time.monotonic() - st
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    managed_processes['pandad'].stop()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    if len(sm['pandaStates']) == 0 or sm['pandaStates'][0].pandaType == log.PandaState.PandaType.unknown:
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								      raise Exception("pandad failed to start")
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    return dt
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  def _go_to_dfu(self):
							 | 
						||
| 
								 | 
							
								    HARDWARE.recover_internal_panda()
							 | 
						||
| 
								 | 
							
								    assert Panda.wait_for_dfu(None, 10)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  def _assert_no_panda(self):
							 | 
						||
| 
								 | 
							
								    assert not Panda.wait_for_dfu(None, 3)
							 | 
						||
| 
								 | 
							
								    assert not Panda.wait_for_panda(None, 3)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def _flash_bootstub_and_test(self, fn, expect_mismatch=False):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self._go_to_dfu()
							 | 
						||
| 
								 | 
							
								    pd = PandaDFU(None)
							 | 
						||
| 
								 | 
							
								    if fn is None:
							 | 
						||
| 
								 | 
							
								      fn = os.path.join(HERE, pd.get_mcu_type().config.bootstub_fn)
							 | 
						||
| 
								 | 
							
								    with open(fn, "rb") as f:
							 | 
						||
| 
								 | 
							
								      pd.program_bootstub(f.read())
							 | 
						||
| 
								 | 
							
								    pd.reset()
							 | 
						||
| 
								 | 
							
								    HARDWARE.reset_internal_panda()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    assert Panda.wait_for_panda(None, 10)
							 | 
						||
| 
								 | 
							
								    if expect_mismatch:
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								      with pytest.raises(PandaProtocolMismatch):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								        Panda()
							 | 
						||
| 
								 | 
							
								    else:
							 | 
						||
| 
								 | 
							
								      with Panda() as p:
							 | 
						||
| 
								 | 
							
								        assert p.bootstub
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self._run_test(45)
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  def test_in_dfu(self):
							 | 
						||
| 
								 | 
							
								    HARDWARE.recover_internal_panda()
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self._run_test(60)
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  def test_in_bootstub(self):
							 | 
						||
| 
								 | 
							
								    with Panda() as p:
							 | 
						||
| 
								 | 
							
								      p.reset(enter_bootstub=True)
							 | 
						||
| 
								 | 
							
								      assert p.bootstub
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self._run_test()
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  def test_internal_panda_reset(self):
							 | 
						||
| 
								 | 
							
								    gpio_init(GPIO.STM_RST_N, True)
							 | 
						||
| 
								 | 
							
								    gpio_set(GPIO.STM_RST_N, 1)
							 | 
						||
| 
								 | 
							
								    time.sleep(0.5)
							 | 
						||
| 
								 | 
							
								    assert all(not Panda(s).is_internal() for s in Panda.list())
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self._run_test()
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    assert any(Panda(s).is_internal() for s in Panda.list())
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  def test_best_case_startup_time(self):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # run once so we're up to date
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self._run_test(60)
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    ts = []
							 | 
						||
| 
								 | 
							
								    for _ in range(10):
							 | 
						||
| 
								 | 
							
								      # should be nearly instant this time
							 | 
						||
| 
								 | 
							
								      dt = self._run_test(5)
							 | 
						||
| 
								 | 
							
								      ts.append(dt)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # 5s for USB (due to enumeration)
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								    # - 0.2s pandad -> pandad
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # - plus some buffer
							 | 
						||
| 
								 | 
							
								    assert 0.1 < (sum(ts)/len(ts)) < (0.5 if self.spi else 5.0)
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    print("startup times", ts, sum(ts) / len(ts))
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  def test_protocol_version_check(self):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    if not self.spi:
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								      pytest.skip("SPI test")
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # flash old fw
							 | 
						||
| 
								 | 
							
								    fn = os.path.join(HERE, "bootstub.panda_h7_spiv0.bin")
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self._flash_bootstub_and_test(fn, expect_mismatch=True)
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  def test_release_to_devel_bootstub(self):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self._flash_bootstub_and_test(None)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def test_recover_from_bad_bootstub(self):
							 | 
						||
| 
								 | 
							
								    self._go_to_dfu()
							 | 
						||
| 
								 | 
							
								    with PandaDFU(None) as pd:
							 | 
						||
| 
								 | 
							
								      pd.program_bootstub(b"\x00"*1024)
							 | 
						||
| 
								 | 
							
								      pd.reset()
							 | 
						||
| 
								 | 
							
								    HARDWARE.reset_internal_panda()
							 | 
						||
| 
								 | 
							
								    self._assert_no_panda()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    self._run_test(60)
							 |