pytest_sdspi_card_example.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. import logging
  4. import re
  5. import pytest
  6. from pytest_embedded import Dut
  7. @pytest.mark.esp32
  8. @pytest.mark.esp32c3 # no runner available at the moment
  9. @pytest.mark.sdcard_spimode
  10. def test_examples_sd_card_sdspi(dut: Dut) -> None:
  11. dut.expect('example: Initializing SD card', timeout=20)
  12. dut.expect('example: Using SPI peripheral', timeout=20)
  13. # Provide enough time for possible SD card formatting
  14. dut.expect('Filesystem mounted', timeout=60)
  15. # These lines are matched separately because of ASCII color codes in the output
  16. name = dut.expect(re.compile(rb'Name: (\w+)\r'), timeout=20).group(1).decode()
  17. _type = dut.expect(re.compile(rb'Type: (\S+)'), timeout=20).group(1).decode()
  18. speed = dut.expect(re.compile(rb'Speed: (\S+)'), timeout=20).group(1).decode()
  19. size = dut.expect(re.compile(rb'Size: (\S+)'), timeout=20).group(1).decode()
  20. logging.info('Card {} {} {}MHz {} found'.format(name, _type, speed, size))
  21. message_list1 = ('Opening file /sdcard/hello.txt',
  22. 'File written',
  23. 'Renaming file /sdcard/hello.txt to /sdcard/foo.txt',
  24. 'Reading file /sdcard/foo.txt',
  25. "Read from file: 'Hello {}!'".format(name))
  26. sd_card_format = re.compile(str.encode('Formatting card, allocation unit size=\\S+'))
  27. message_list2 = ('file doesnt exist, format done',
  28. 'Opening file /sdcard/nihao.txt',
  29. 'File written',
  30. 'Reading file /sdcard/nihao.txt',
  31. "Read from file: 'Nihao {}!'".format(name),
  32. 'Card unmounted')
  33. for msg in message_list1:
  34. dut.expect_exact(msg, timeout=30)
  35. dut.expect(sd_card_format, timeout=180) # Provide enough time for SD card FATFS format operation
  36. for msg in message_list2:
  37. dut.expect_exact(msg, timeout=30)