1
1
mirror of https://github.com/OpenVoiceOS/OpenVoiceOS synced 2025-06-05 22:19:21 +02:00

[RPI] Refactor SPI and GPIO handling for XVF3510 initialization

This commit significantly overhauls the script for setting the XVF3510 board to boot from SPI slave mode and for loading a binary file. The refactor not only aims at improving code readability, maintainability, and robustness but also addresses compatibility issues with the latest Raspberry Pi models, specifically the Raspberry Pi 5, which does not support the RPi.GPIO library.

Changes made:
- Switched from `smbus` to `smbus2` for I2C communication, offering a more modern and robust interface.
- Replaced `RPi.GPIO` and `spidev` with `digitalio` and `busio` from the `adafruit_blinka` library, enhancing cross-platform compatibility and providing a more Pythonic API for GPIO and SPI operations.
- Introduced type annotations for function signatures, improving code readability and type safety.
- Added exception handling around I2C operations and file reading, increasing the script's robustness by gracefully handling potential errors.
- Defined global variables for GPIO pin configurations, making the code cleaner and easier to modify for different setups.
- Encapsulated GPIO setup and reset logic into dedicated functions (`setup_direct_gpio`), streamlining the main logic flow and separating concerns.
- Modularized SPI setup (`setup_spi`) and data transmission logic (`send_data_over_spi` and `handle_block_transfer`), enhancing code organization and maintainability.

Benefits:
- The use of `smbus2` and `adafruit_blinka` libraries modernizes the script and may improve compatibility with a wider range of devices and future Python versions.
- Type annotations and structured exception handling make the script more understandable and safer to execute, reducing the risk of runtime errors.
- The refactoring into more granular functions and the introduction of global variables for configuration parameters make the script easier to read, modify, and extend.
- Improved error handling ensures that the script fails gracefully, providing clear error messages and avoiding potential resource leaks.
This commit is contained in:
Bohdan Buinich
2024-02-04 01:24:43 +02:00
parent 379e94dad3
commit c0442cc0e6
21 changed files with 290 additions and 185 deletions

View File

@@ -1,11 +1,14 @@
config BR2_PACKAGE_PYTHON_ADAFRUIT_BLINKA
bool "python-adafruit-blinka"
select BR2_PACKAGE_PYTHON_ADAFRUIT_CIRCUITPYTHON_TYPING # runtime
select BR2_PACKAGE_PYTHON_ADAFRUIT_PLATFORMDETECT # runtime
select BR2_PACKAGE_PYTHON_ADAFRUIT_PUREIO # runtime
select BR2_PACKAGE_PYTHON_PYFTDI # runtime
select BR2_PACKAGE_PYTHON_ADAFRUIT_CIRCUITPYTHON_TYPING
select BR2_PACKAGE_PYTHON_ADAFRUIT_PLATFORMDETECT
select BR2_PACKAGE_PYTHON_ADAFRUIT_PUREIO
select BR2_PACKAGE_PYTHON_PYFTDI
select BR2_PACKAGE_PYTHON_RPI_GPIO
select BR2_PACKAGE_PYTHON_SYSV_IPC
select BR2_PACKAGE_PYTHON_GPIOD
help
CircuitPython APIs for non-CircuitPython versions of Python
such as CPython on Linux and MicroPython.
CircuitPython APIs for non-CircuitPython versions of Python such as
CPython on Linux and MicroPython.
https://github.com/adafruit/Adafruit_Blinka
https://pypi.org/project/Adafruit-Blinka

View File

@@ -1,5 +1,5 @@
# md5, sha256 from https://pypi.org/pypi/adafruit-blinka/json
md5 b15a11eb7e0910af0d5825ed386b71d6 Adafruit-Blinka-8.6.0.tar.gz
sha256 b05f03468edc898aa07af59bafa88ca78e1b50f7d263a53e6573ace0050f64eb Adafruit-Blinka-8.6.0.tar.gz
md5 835cabd601befbe78dc64720f7efacd6 Adafruit-Blinka-8.31.0.tar.gz
sha256 720edca821066022e912dffcb364ea04bd3e3f208132b975876a21b2a2daa850 Adafruit-Blinka-8.31.0.tar.gz
# Locally computed sha256 checksums
sha256 50e0c3b5b4486be0ed420639f8a1f6f115f29101feee6bcd954a4b81db04d0f3 LICENSE

View File

@@ -4,11 +4,13 @@
#
################################################################################
PYTHON_ADAFRUIT_BLINKA_VERSION = 8.6.0
PYTHON_ADAFRUIT_BLINKA_VERSION = 8.31.0
PYTHON_ADAFRUIT_BLINKA_SOURCE = Adafruit-Blinka-$(PYTHON_ADAFRUIT_BLINKA_VERSION).tar.gz
PYTHON_ADAFRUIT_BLINKA_SITE = https://files.pythonhosted.org/packages/76/29/541a6a22d923fbe0d4cf52f953a2c737370cb717f8df1cbd95ab97fe2fba
PYTHON_ADAFRUIT_BLINKA_SITE = https://files.pythonhosted.org/packages/74/ed/e51689be726338ba16f2a88373ad4690702c5ddf8668be1b0289cb279840
PYTHON_ADAFRUIT_BLINKA_SETUP_TYPE = setuptools
PYTHON_ADAFRUIT_BLINKA_LICENSE = MIT
PYTHON_ADAFRUIT_BLINKA_LICENSE_FILES = LICENSE
PYTHON_ADAFRUIT_BLINKA_BIN_ARCH_EXCLUDE = usr/lib/python$(PYTHON3_VERSION_MAJOR)/site-packages/adafruit_blinka/microcontroller/bcm283x/pulseio
PYTHON_ADAFRUIT_BLINKA_BIN_ARCH_EXCLUDE += usr/lib/python$(PYTHON3_VERSION_MAJOR)/site-packages/adafruit_blinka/microcontroller/amlogic/a311d/pulseio
$(eval $(python-package))