Skip to content

Commit

Permalink
adding pre-commit and running reformater
Browse files Browse the repository at this point in the history
  • Loading branch information
snhobbs committed May 14, 2024
1 parent 216e12d commit 6b6bddf
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 271 deletions.
49 changes: 49 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
exclude: '^docs/|/migrations/|devcontainer.json'
default_stages: [commit]

default_language_version:
python: python3.11

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-json
- id: check-toml
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: detect-private-key

- repo: https://github.com/adamchainz/django-upgrade
rev: '1.16.0'
hooks:
- id: django-upgrade
args: ['--target-version', '4.2']

# Run the Ruff linter.
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.2
hooks:
# Linter
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
# Formatter
- id: ruff-format

- repo: https://github.com/Riverside-Healthcare/djLint
rev: v1.34.1
hooks:
- id: djlint-reformat-django
- id: djlint-django

# sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date
ci:
autoupdate_schedule: weekly
skip: []
submodules: false
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fails the chip will boot back into the ISP mode.
The image is then written from the top most page down to the first page.
The first sector contains the valid image checksum so a failed write will
keep the device in ISP mode instead of just being bricked.


## Chip Families Supported:
LPC84x
Expand All @@ -22,7 +22,7 @@ keep the device in ISP mode instead of just being bricked.
NXP chips with 1kB sector sizes should work by adding their information to the
lpctools_parts.def configuration file.

The configuration file is identical to that used by the lpctools project
The configuration file is identical to that used by the lpctools project
<http://git.techno-innov.fr/?p=lpctools>

## Usage
Expand All @@ -38,7 +38,7 @@ The configuration file is identical to that used by the lpctools project
NXPISP is a python3 package and can be installed using pip.
Clone the repository, enter the directory with setup.py in it and run
pip install .
The default location for the configuration file is at /etc/lpctools_parts.def.
The default location for the configuration file is at /etc/lpctools_parts.def.
The file can either be copied there or the path passed in when calling the tool
with the --config_file/-f flag.

Expand Down
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
theme: jekyll-theme-hacker
theme: jekyll-theme-hacker
42 changes: 29 additions & 13 deletions examples/test.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
import logging
from time import sleep
import timeout_decorator
from isp_programmer import tools, UartDevice, GetPartDescriptor, MassErase, \
InitConnection, ISPConnection, ReadImage, WriteBinaryToFlash, \
ChipDescription, ReadSector
from isp_programmer import (
tools,
UartDevice,
GetPartDescriptor,
MassErase,
InitConnection,
ISPConnection,
ReadImage,
WriteBinaryToFlash,
ChipDescription,
ReadSector,
)

retry = tools.retry
calc_crc = tools.calc_crc


def SetupChip(baudrate: int, crystal_frequency: int, chip_file: str, sleep_time: float = 1):
def SetupChip(
baudrate: int, crystal_frequency: int, chip_file: str, sleep_time: float = 1
):
device = "/dev/ttyUSB0"
kStartingBaudRate = baudrate
iodevice = UartDevice(device, baudrate=kStartingBaudRate)
isp = ISPConnection(iodevice)
#print(baudrate, device, crystal_frequency, chip_file)
# print(baudrate, device, crystal_frequency, chip_file)

InitConnection(isp)
part_id = retry(isp.ReadPartID, count=100, exception=timeout_decorator.TimeoutError)()
part_id = retry(
isp.ReadPartID, count=100, exception=timeout_decorator.TimeoutError
)()

descriptor = GetPartDescriptor(chip_file, part_id)
logging.info(f"{part_id}, {descriptor}")
chip = ChipDescription(descriptor)
chip.CrystalFrequency = crystal_frequency#12000#khz == 30MHz
chip.CrystalFrequency = crystal_frequency # 12000#khz == 30MHz

print("Setting new baudrate %d"%baudrate)
print("Setting new baudrate %d" % baudrate)
isp.baud_rate = baudrate
return isp, chip


def main(imagein):
isp, chip = SetupChip(9600, 12000, "./lpctools_parts.def")
# Clear chip, write, read
Expand All @@ -38,17 +52,19 @@ def main(imagein):
logging.info(f"image length: {len(image)} {image}")
assert len(image) == 0 # Test for blank chip
logging.info("Checking Sectors are blank")
assert isp.CheckSectorsBlank(0, chip.SectorCount-1)
assert isp.CheckSectorsBlank(0, chip.SectorCount - 1)

expected_data = bytes([0xff]*chip.sector_bytes)
expected_data = bytes([0xFF] * chip.sector_bytes)
crc_expected = calc_crc(expected_data)

# Read first sector
sleep(0.1)
isp.WriteToRam(chip.RAMStartWrite, expected_data)
sleep(0.1)
isp.reset()
first_sector = retry(isp.ReadMemory, count=2, exception=(UserWarning, timeout_decorator.TimeoutError))(chip.RAMStartWrite, chip.sector_bytes)
first_sector = retry(
isp.ReadMemory, count=2, exception=(UserWarning, timeout_decorator.TimeoutError)
)(chip.RAMStartWrite, chip.sector_bytes)
# first_sector = chip.ReadSector(0)
assert first_sector == expected_data
# crc_calculated = chip.ReadCRC(chip.FlashRange[0], chip.sector_bytes)
Expand All @@ -59,7 +75,7 @@ def main(imagein):
logging.info("RAM CRC check passed")

data = b"hello world"
data += bytes([0xff] *(chip.sector_bytes - len(data)))
data += bytes([0xFF] * (chip.sector_bytes - len(data)))
assert len(data) == chip.sector_bytes
crc_expected = calc_crc(data)
WriteBinaryToFlash(isp, chip, data, 0)
Expand All @@ -80,7 +96,7 @@ def main(imagein):

if __name__ == "__main__":
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG-2)
logging.getLogger().setLevel(logging.DEBUG - 2)
# imagein = "../blinky845.hex"
imagein = "../blinky845MAX.hex"
main(imagein)
17 changes: 12 additions & 5 deletions src/isp_programmer/IODevices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@


class IODevice:
''' Generic for a byte IO device'''
"""Generic for a byte IO device"""

def read_byte(self):
pass

Expand All @@ -28,7 +29,8 @@ def ReadLine(self):


class MockUart(IODevice):
'''Mock IO device for testing'''
"""Mock IO device for testing"""

def __init__(self, port: str = "/dev/ttyUSB0", baudrate: int = 9600):
self.baudrate = baudrate
self.port = port
Expand All @@ -47,8 +49,14 @@ def GetBaudrate(self):


class UartDevice(IODevice):
'''Serial IO device wrapper around pyserial'''
def __init__(self, port: str = "/dev/ttyUSB0", baudrate: int = 9600, timeout: float = kTimeout):
"""Serial IO device wrapper around pyserial"""

def __init__(
self,
port: str = "/dev/ttyUSB0",
baudrate: int = 9600,
timeout: float = kTimeout,
):
self.uart = Serial(port, baudrate, xonxoff=False, timeout=timeout)
self.read = self.uart.read
self.read_all = self.uart.read_all
Expand All @@ -71,4 +79,3 @@ def ReadLine(self):
return bytes(line).decode("utf-8")
except UnicodeDecodeError:
raise TimeoutError

Loading

0 comments on commit 6b6bddf

Please sign in to comment.