Skip to content

Commit

Permalink
Merge pull request #9 from hstarmans/main
Browse files Browse the repository at this point in the history
Await function timeout, block size adjustment, print instead of errors
  • Loading branch information
brainelectronics authored Nov 16, 2023
2 parents d3a3c1e + 77de11e commit 1838d2e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Install the pre-commit hooks below with
# 'pre-commit install'

# Run the hooks on all files with
# 'pre-commit run --all'

repos:
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear, flake8-print]
args: [--ignore=T201]
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
### Fixed
-->
## [0.5.2] - 2023-11-15
### Fixed
- await function no longer caught in loop and has timeout of 2 seconds
- writeblocks no longer fails for invalid block lengths
- packages no longer raises exception but prints warning if memory is untested
## [0.5.1] - 2023-05-16
### Fixed
- Return type of `manufacturer` and `device` property fixed to be integers instead of strings, see #6
Expand Down
2 changes: 1 addition & 1 deletion winbond/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

__version_info__ = ("0", "0", "0")
__version_info__ = ("0", "5", "2")
__version__ = '.'.join(__version_info__)
22 changes: 14 additions & 8 deletions winbond/winbond.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ def identify(self) -> None:
if not (mf and mem_type and cap): # something is 0x00
raise OSError("device not responding, check wiring. ({}, {}, {})".
format(hex(mf), hex(mem_type), hex(cap)))
if mf != 0xEF or mem_type not in [0x40, 0x60]:
if mf != 0xEF or mem_type not in [0x40, 0x60, 0x70]:
# Winbond manufacturer, Q25 series memory (tested 0x40 only)
raise OSError("manufacturer ({}) or memory type ({}) unsupported".
format(hex(mf), hex(mem_type)))
print(f"Warning manufacturer ({hex(mf)}) or memory type"
f"({hex(mem_type)}) not tested.")

self._manufacturer = mf
self._mem_type = mem_type
Expand Down Expand Up @@ -231,9 +231,12 @@ def _await(self) -> None:
self.spi.write(b'\x05') # 'Read Status Register-1' command

# last bit (1) is BUSY bit in stat. reg. byte (0 = not busy, 1 = busy)
trials = 0
while 0x1 & self.spi.read(1, 0xFF)[0]:
pass

if trials > 20:
raise Exception("Device keeps busy, aborting.")
time.sleep(0.1)
trials += 1
self.cs(1)
self._busy = False

Expand Down Expand Up @@ -380,10 +383,11 @@ def writeblocks(self, blocknum: int, buf: list) -> None:
:param buf: The data buffer
:type buf: list
"""
assert len(buf) % self.BLOCK_SIZE == 0, \
'invalid buffer length: {}'.format(len(buf))

buf_len = len(buf)
if buf_len % self.BLOCK_SIZE != 0:
# appends xFF dummy bytes
buf += bytearray((self.BLOCK_SIZE - buf_len) * [255])

if buf_len == self.BLOCK_SIZE:
self._writeblock(blocknum=blocknum, buf=buf)
else:
Expand All @@ -394,6 +398,8 @@ def writeblocks(self, blocknum: int, buf: list) -> None:
buf=buf_mv[offset:offset + self.BLOCK_SIZE])
offset += self.BLOCK_SIZE
blocknum += 1
# remove appended bytes
buf = buf[:buf_len]

def count(self) -> int:
"""
Expand Down

0 comments on commit 1838d2e

Please sign in to comment.