Skip to content

Commit

Permalink
Merge e61a36b into 2f02a37
Browse files Browse the repository at this point in the history
  • Loading branch information
brainelectronics authored Jun 14, 2023
2 parents 2f02a37 + e61a36b commit e58e0a1
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 30 deletions.
1 change: 0 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ exclude =
# example testing folder before going live
thinking
# custom scripts, not being part of the distribution
libs_external
sdist_upip.py
setup.py

Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ on:
branches-ignore:
- 'main'
- 'develop'
pull_request:
branches:
- 'main'
- 'develop'

permissions:
contents: read
Expand Down Expand Up @@ -52,3 +56,11 @@ jobs:
- name: Test built package
run: |
twine check dist/*
- name: Validate mip package file
run: |
upy-package \
--setup_file setup.py \
--package_changelog_file changelog.md \
--package_file package.json \
--validate \
--ignore-version
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ MicroPython library to interact with Winbond W25Q Flash chips
<!-- MarkdownTOC -->

- [Installation](#installation)
- [Install required tools](#install-required-tools)
- [Install required tools](#install-required-tools)
- [Stetup](#stetup)
- [Install package with upip](#install-package-with-upip)
- [General](#general)
- [Specific version](#specific-version)
- [Test version](#test-version)
- [Manually](#manually)
- [Upload files to board](#upload-files-to-board)
- [Install package](#install-package)
- [General](#general)
- [Specific version](#specific-version)
- [Test version](#test-version)
- [Manually](#manually)
- [Upload files to board](#upload-files-to-board)
- [Usage](#usage)
- [Credits](#credits)

Expand Down Expand Up @@ -62,13 +62,14 @@ rshell --help

## Stetup

### Install package with upip
### Install package

Connect your MicroPython board to a network
Connect the MicroPython device to a network (if possible)

```python
import network
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect('SSID', 'PASSWORD')
station.isconnected()
```
Expand Down Expand Up @@ -101,7 +102,8 @@ mip.install("github:brainelectronics/micropython-winbond", version="feature/add-
mip.install("github:brainelectronics/micropython-winbond", version="0.4.0")
```

For MicroPython versions below 1.19.1 use the `upip` package instead of `mip`
For MicroPython versions below 1.19.1 use the `upip` package instead of `mip`.
With `upip` always the latest available version will be installed.

```python
import upip
Expand All @@ -120,7 +122,8 @@ import mip
mip.install("github:brainelectronics/micropython-winbond", version="0.4.0-rc2.dev4")
```

For MicroPython versions below 1.19.1 use the `upip` package instead of `mip`
For MicroPython versions below 1.19.1 use the `upip` package instead of `mip`.
With `upip` always the latest available version will be installed.

```python
import upip
Expand Down Expand Up @@ -153,7 +156,7 @@ folders to the device
mkdir /pyboard/lib
mkdir /pyboard/lib/winbond

cp winbond.py /pyboard/lib/winbond
cp winbond/* /pyboard/lib/winbond
cp main.py /pyboard/lib/winbond
cp boot.py /pyboard/lib/winbond
```
Expand All @@ -166,6 +169,7 @@ import os
from winbond import W25QFlash

# the used SPI and CS pin is setup specific, change accordingly
# check the boot.py file of this repo for further boards
flash = W25QFlash(spi=SPI(2), cs=Pin(5), baud=2000000, software_reset=True)

flash_mount_point = '/external'
Expand Down Expand Up @@ -207,10 +211,5 @@ his [post to use Winbond flash chips][ref-upy-forum-winbond-driver]
[ref-rtd-micropython-winbond]: https://micropython-winbond.readthedocs.io/en/latest/
[ref-remote-upy-shell]: https://github.com/dhylands/rshell
[ref-brainelectronics-test-pypiserver]: https://github.com/brainelectronics/test-pypiserver
[ref-be32]: https://github.com/brainelectronics/BE32-01/
[ref-pfalcon-picoweb-sdist-upip]: https://github.com/pfalcon/picoweb/blob/b74428ebdde97ed1795338c13a3bdf05d71366a0/sdist_upip.py
[ref-test-pypi]: https://test.pypi.org/
[ref-pypi]: https://pypi.org/
[ref-invalid-auth-test-pypi]: https://test.pypi.org/help/#invalid-auth
[ref-crizeo]: https://forum.micropython.org/memberlist.php?mode=viewprofile&u=3067
[ref-upy-forum-winbond-driver]: https://forum.micropython.org/viewtopic.php?f=16&t=3899&start=10
14 changes: 12 additions & 2 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
# mosi 13 23
# miso 12 19
# cs x, here 5 x, here 5
CS_PIN = Pin(5)
spi = SPI(2)
if 'esp32s3' in os_info.machine.lower():
CS_PIN = Pin(10)
spi = SPI(2, 2000000, sck=Pin(12), mosi=Pin(11), miso=Pin(13))
else:
CS_PIN = Pin(5)
spi = SPI(2)
elif 'rp2' in os_info:
# https://docs.micropython.org/en/latest/rp2/quickref.html#hardware-spi-bus
# Pin id=0 id=1
Expand All @@ -57,6 +61,12 @@

flash = W25QFlash(spi=spi, cs=CS_PIN, baud=2000000, software_reset=True)

# get Flash infos/properties
print("Flash manufacturer ID: 0x{0:02x}".format(flash.manufacturer))
print("Flash Memory Type: {}".format(flash.mem_type))
print("Flash Device Type: 0x{0:02x}".format(flash.device))
print("Flash size: {} bytes".format(flash.capacity))

flash_mount_point = '/external'

try:
Expand Down
12 changes: 11 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
### Fixed
-->
## [0.5.0] - 2023-05-14
### Added
- Properties for `manufacturer`, `mem_type`, `device`, `capacity`
- BE-ESP32-01 specific pin and SPI definition in `boot.py`
- Validate `package.json` file with every test workflow run but without version validation

### Removed
- Verbose print statements

## [0.4.0] - 2023-03-24
### Added
- `package.json` for `mip` installation with MicroPython v1.19.1 or newer
Expand Down Expand Up @@ -72,8 +81,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
without hardware reset pins, extended documentation and PEP8 fixes

<!-- Links -->
[Unreleased]: https://github.com/brainelectronics/micropython-winbond/compare/0.4.0...main
[Unreleased]: https://github.com/brainelectronics/micropython-winbond/compare/0.5.0...main

[0.5.0]: https://github.com/brainelectronics/micropython-winbond/tree/0.5.0
[0.4.0]: https://github.com/brainelectronics/micropython-winbond/tree/0.4.0
[0.3.0]: https://github.com/brainelectronics/micropython-winbond/tree/0.3.0
[0.2.0]: https://github.com/brainelectronics/micropython-winbond/tree/0.2.0
Expand Down
9 changes: 9 additions & 0 deletions docs/EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ from winbond import W25QFlash
# check the docs of your device for further details and pin infos
CS_PIN = Pin(5)
spi = SPI(0)
# for the BE-ESP32-01 use
# CS_PIN = Pin(10)
# spi = SPI(2, 2000000, sck=Pin(12), mosi=Pin(11), miso=Pin(13))

flash = W25QFlash(spi=spi, cs=CS_PIN, baud=2000000, software_reset=True)

# get Flash infos/properties
print("Flash manufacturer ID: 0x{0:02x}".format(flash.manufacturer))
print("Flash Memory Type: {}".format(flash.mem_type))
print("Flash Device Type: 0x{0:02x}".format(flash.device))
print("Flash size: {} bytes".format(flash.capacity))
# manufacturer: 0xef
# mem_type: 64
# device: 0x4016
Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ flake8>=5.0.0,<6
coverage>=6.4.2,<7
nose2>=0.12.0,<1
yamllint>=1.29,<2
setup2upypackage>=0.2.0,<1
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: Implementation :: MicroPython',
],
keywords='micropython, winbond, library',
project_urls={
Expand Down
59 changes: 50 additions & 9 deletions winbond/winbond.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def __init__(self,
:param software_reset: Flag to use software reset
:type software_reset: bool
"""
self._manufacturer = 0x0
self._mem_type = 0
self._device_type = 0x0
self._capacity = 0

self.cs = cs
self.spi = spi
self.cs.init(self.cs.OUT, value=1)
Expand All @@ -58,12 +63,51 @@ def __init__(self,
# setup address mode:
if self._ADR_LEN == 4:
if not self._read_status_reg(nr=16): # not in 4-byte mode
print("entering 4-byte address mode")
self._await()
self.cs(0)
self.spi.write(b'\xB7') # 'Enter 4-Byte Address Mode'
self.cs(1)

@property
def capacity(self) -> int:
"""
Get the storage capacity of the flash
:returns: Capacity of the flash in bytes
:rtype: int
"""
return self._capacity

@property
def device(self) -> int:
"""
Get the flash device type
:returns: Flash device type
:rtype: int
"""
return self._device_type

@property
def manufacturer(self) -> int:
"""
Get the manufacturer ID of the flash
:returns: Manufacturer ID of the flash
:rtype: int
"""
return self._manufacturer

@property
def mem_type(self) -> int:
"""
Get the memory type of the flash
:returns: Memory type of the flash
:rtype: int
"""
return self._mem_type

def reset(self) -> None:
"""
Reset the Winbond flash if the device has no hardware reset pin.
Expand Down Expand Up @@ -103,7 +147,6 @@ def reset(self) -> None:
self.cs(1)
time.sleep_us(30)
self._busy = False
# print('Reset performed')

def identify(self) -> None:
"""
Expand Down Expand Up @@ -132,10 +175,11 @@ def identify(self) -> None:
raise OSError("manufacturer ({}) or memory type ({}) unsupported".
format(hex(mf), hex(mem_type)))

print("manufacturer: {}".format(hex(mf))) # 0xef
print("mem_type: {}".format(mem_type))
print("device: {}".format(hex(mem_type << 8 | cap))) # 0x4016
print("capacity: {} bytes".format(self._CAPACITY)) # 4194304 bytes
self._manufacturer = hex(mf)
self._mem_type = mem_type
self._device_type = hex(mem_type << 8 | cap)
self._capacity = self._CAPACITY

# return self._CAPACITY # calculate number of bytes

def get_size(self) -> int:
Expand Down Expand Up @@ -224,7 +268,6 @@ def _read(self, buf: list, addr: int) -> None:
assert addr + len(buf) <= self._CAPACITY, \
"memory not addressable at %s with range %d (max.: %s)" % \
(hex(addr), len(buf), hex(self._CAPACITY - 1))
# print("read {} bytes starting at {}".format(len(buf), hex(addr)))

self._await()
self.cs(0)
Expand Down Expand Up @@ -268,7 +311,6 @@ def _write(self, buf: list, addr: int) -> None:
assert addr + len(buf) <= self._CAPACITY, \
("memory not addressable at {} with range {} (max.: {})".
format(hex(addr), len(buf), hex(self._CAPACITY - 1)))
# print("write buf[{}] to {} ({})".format(len(buf), hex(addr), addr))

for i in range(0, len(buf), self.PAGE_SIZE):
self._wren()
Expand Down Expand Up @@ -296,7 +338,6 @@ def _writeblock(self, blocknum: int, buf: list) -> None:
"""
assert len(buf) == self.BLOCK_SIZE, \
"invalid block length: {}".format(len(buf))
# print("writeblock({}, buf[{}])".format(blocknum, len(buf)))

sector_nr = blocknum // 8
sector_addr = sector_nr * self.SECTOR_SIZE
Expand Down

0 comments on commit e58e0a1

Please sign in to comment.