Skip to content

Avoid getting stuck in while loop during file write #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion adafruit_sdcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def _wait_for_ready(self, card: SPI, timeout: float = 0.3) -> None:

# pylint: disable-msg=too-many-arguments
# pylint: disable=no-member
# pylint: disable-msg=too-many-branches
# no-member disable should be reconsidered when it can be tested
def _cmd(
self,
Expand Down Expand Up @@ -248,6 +249,7 @@ def _cmd(

card.write(buf)

# pylint: disable-msg=too-many-nested-blocks
# wait for the response (response[7] == 0)
for _ in range(_CMD_TIMEOUT):
card.readinto(buf, end=1, write_value=0xFF)
Expand All @@ -256,15 +258,20 @@ def _cmd(
if data_block:
# Wait for the start block byte
buf[1] = 0xFF
while buf[1] != 0xFE:
for _ in range(_CMD_TIMEOUT):
card.readinto(buf, start=1, end=2, write_value=0xFF)
if buf[1] == 0xFE:
break
if buf[1] != 0xFE:
return -1
card.readinto(response_buf, write_value=0xFF)
if data_block:
# Read the checksum
card.readinto(buf, start=1, end=3, write_value=0xFF)
return buf[0]
return -1

# pylint: enable-msg=too-many-branches
# pylint: enable-msg=too-many-arguments

# pylint: disable-msg=too-many-arguments
Expand Down