5 Commits

Author SHA1 Message Date
1a5cf18590 debian: Update Build-Depends and Depends
This closes #32

Signed-off-by: Andrew Andrianov <andrew@ncrmnt.org>
2017-10-25 21:31:14 +03:00
a5e1cc26ee Merge pull request #31 from nekromant/progressbar
Implement progress callback and tqdm progressbar
2017-10-22 15:55:45 +02:00
b77157bc40 .travis.yml: Install tqdm to make ci happy
Signed-off-by: Andrew Andrianov <andrew@ncrmnt.org>
2017-10-19 11:26:42 +03:00
092fbdc842 protocols.py: Implement progress callback and tqdm progressbar
Signed-off-by: Andrew Andrianov <andrew@ncrmnt.org>
2017-10-19 11:26:27 +03:00
e0bda73fed Merge pull request #29 from grigorig/advanced-tests
Advanced tests
2017-10-18 23:22:40 +02:00
3 changed files with 28 additions and 23 deletions

View File

@ -11,7 +11,7 @@ python:
before_install: before_install:
- sudo apt install rpm dpkg-dev debhelper dh-python python3-setuptools fakeroot python3-serial python3-yaml - sudo apt install rpm dpkg-dev debhelper dh-python python3-setuptools fakeroot python3-serial python3-yaml
install: install:
- pip install pyserial pyusb - pip install pyserial pyusb tqdm
script: script:
- python setup.py build - python setup.py build
- python setup.py test - python setup.py test

4
debian/control vendored
View File

@ -2,14 +2,14 @@ Source: stcgal
Section: electronics Section: electronics
Priority: optional Priority: optional
Maintainer: Andrew Andrianov <andrew@ncrmnt.org> Maintainer: Andrew Andrianov <andrew@ncrmnt.org>
Build-Depends: debhelper (>= 9), python3, python3-setuptools, dh-python Build-Depends: debhelper (>= 9), python3, python3-setuptools, dh-python, python3-serial, python3-tqdm, python3-yaml
Standards-Version: 3.9.5 Standards-Version: 3.9.5
Homepage: https://github.com/grigorig/stcgal Homepage: https://github.com/grigorig/stcgal
X-Python3-Version: >= 3.2 X-Python3-Version: >= 3.2
Package: stcgal Package: stcgal
Architecture: all Architecture: all
Depends: ${misc:Depends}, python3, python3-serial Depends: ${misc:Depends}, python3, python3-serial, python3-tqdm
Recommends: python3-usb (>= 1.0.0~b2) Recommends: python3-usb (>= 1.0.0~b2)
Description: STC MCU ISP flash tool Description: STC MCU ISP flash tool
stcgal is a command line flash programming tool for STC MCU Ltd. stcgal is a command line flash programming tool for STC MCU Ltd.

View File

@ -34,6 +34,7 @@ from stcgal.utils import Utils
from stcgal.options import Stc89Option, Stc12Option, Stc12AOption, Stc15Option, Stc15AOption from stcgal.options import Stc89Option, Stc12Option, Stc12AOption, Stc15Option, Stc15AOption
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
import functools import functools
import tqdm
try: try:
import usb.core, usb.util import usb.core, usb.util
@ -83,6 +84,22 @@ class StcBaseProtocol(ABC):
self.debug = False self.debug = False
self.status_packet = None self.status_packet = None
self.protocol_name = None self.protocol_name = None
self.bar = None
self.progress_cb = self.progress_bar_cb
def progress_text_cb(self, current, written, maximum):
print(current, written, maximum)
def progress_bar_cb(self, current, written, maximum):
if not self.bar:
self.bar = tqdm.tqdm(
total = maximum,
unit = " Bytes",
desc = "Writing flash"
)
self.bar.update(written)
if current == maximum:
self.bar.close()
def dump_packet(self, data, receive=True): def dump_packet(self, data, receive=True):
if self.debug: if self.debug:
@ -553,8 +570,6 @@ class Stc89Protocol(StcBaseProtocol):
as the block size (depends on MCU's RAM size). as the block size (depends on MCU's RAM size).
""" """
print("Writing %d bytes: " % len(data), end="")
sys.stdout.flush()
for i in range(0, len(data), self.PROGRAM_BLOCKSIZE): for i in range(0, len(data), self.PROGRAM_BLOCKSIZE):
packet = bytes(3) packet = bytes(3)
packet += struct.pack(">H", i) packet += struct.pack(">H", i)
@ -568,9 +583,8 @@ class Stc89Protocol(StcBaseProtocol):
raise StcProtocolException("incorrect magic in write packet") raise StcProtocolException("incorrect magic in write packet")
elif len(response) < 2 or response[1] != csum: elif len(response) < 2 or response[1] != csum:
raise StcProtocolException("verification checksum mismatch") raise StcProtocolException("verification checksum mismatch")
print(".", end="") self.progress_cb(i, self.PROGRAM_BLOCKSIZE, len(data))
sys.stdout.flush() self.progress_cb(len(data), self.PROGRAM_BLOCKSIZE, len(data))
print(" done")
def program_options(self): def program_options(self):
"""Program option byte into flash""" """Program option byte into flash"""
@ -940,8 +954,6 @@ class Stc12BaseProtocol(StcBaseProtocol):
as the block size (depends on MCU's RAM size). as the block size (depends on MCU's RAM size).
""" """
print("Writing %d bytes: " % len(data), end="")
sys.stdout.flush()
for i in range(0, len(data), self.PROGRAM_BLOCKSIZE): for i in range(0, len(data), self.PROGRAM_BLOCKSIZE):
packet = bytes(3) packet = bytes(3)
packet += struct.pack(">H", i) packet += struct.pack(">H", i)
@ -952,9 +964,8 @@ class Stc12BaseProtocol(StcBaseProtocol):
response = self.read_packet() response = self.read_packet()
if response[0] != 0x00: if response[0] != 0x00:
raise StcProtocolException("incorrect magic in write packet") raise StcProtocolException("incorrect magic in write packet")
print(".", end="") self.progress_cb(i, self.PROGRAM_BLOCKSIZE, len(data))
sys.stdout.flush() self.progress_cb(len(data), self.PROGRAM_BLOCKSIZE, len(data))
print(" done")
print("Finishing write: ", end="") print("Finishing write: ", end="")
sys.stdout.flush() sys.stdout.flush()
@ -1463,8 +1474,6 @@ class Stc15Protocol(Stc15AProtocol):
def program_flash(self, data): def program_flash(self, data):
"""Program the MCU's flash memory.""" """Program the MCU's flash memory."""
print("Writing %d bytes: " % len(data), end="")
sys.stdout.flush()
for i in range(0, len(data), self.PROGRAM_BLOCKSIZE): for i in range(0, len(data), self.PROGRAM_BLOCKSIZE):
packet = bytes([0x22]) if i == 0 else bytes([0x02]) packet = bytes([0x22]) if i == 0 else bytes([0x02])
packet += struct.pack(">H", i) packet += struct.pack(">H", i)
@ -1476,9 +1485,8 @@ class Stc15Protocol(Stc15AProtocol):
response = self.read_packet() response = self.read_packet()
if len(response) < 2 or response[0] != 0x02 or response[1] != 0x54: if len(response) < 2 or response[0] != 0x02 or response[1] != 0x54:
raise StcProtocolException("incorrect magic in write packet") raise StcProtocolException("incorrect magic in write packet")
print(".", end="") self.progress_cb(i, self.PROGRAM_BLOCKSIZE, len(data))
sys.stdout.flush() self.progress_cb(len(data), self.PROGRAM_BLOCKSIZE, len(data))
print(" done")
# BSL 7.2+ needs a write finish packet according to dumps # BSL 7.2+ needs a write finish packet according to dumps
if self.bsl_version >= 0x72: if self.bsl_version >= 0x72:
@ -1661,8 +1669,6 @@ class StcUsb15Protocol(Stc15Protocol):
def program_flash(self, data): def program_flash(self, data):
"""Program the MCU's flash memory.""" """Program the MCU's flash memory."""
print("Writing %d bytes: " % len(data), end="")
sys.stdout.flush()
for i in range(0, len(data), self.PROGRAM_BLOCKSIZE): for i in range(0, len(data), self.PROGRAM_BLOCKSIZE):
packet = data[i:i+self.PROGRAM_BLOCKSIZE] packet = data[i:i+self.PROGRAM_BLOCKSIZE]
while len(packet) < self.PROGRAM_BLOCKSIZE: packet += b"\x00" while len(packet) < self.PROGRAM_BLOCKSIZE: packet += b"\x00"
@ -1672,9 +1678,8 @@ class StcUsb15Protocol(Stc15Protocol):
response = self.read_packet() response = self.read_packet()
if response[0] != 0x02 or response[1] != 0x54: if response[0] != 0x02 or response[1] != 0x54:
raise StcProtocolException("incorrect magic in write packet") raise StcProtocolException("incorrect magic in write packet")
print(".", end="") self.progress_cb(i, self.PROGRAM_BLOCKSIZE, len(data))
sys.stdout.flush() self.progress_cb(len(data), self.PROGRAM_BLOCKSIZE, len(data))
print(" done")
def program_options(self): def program_options(self):
print("Setting options: ", end="") print("Setting options: ", end="")