1 Commits

Author SHA1 Message Date
3ce6b565ef Possible fix/workaround for OSX frequency trimming 2017-06-16 09:47:12 +02:00
18 changed files with 103 additions and 499 deletions

1
.gitignore vendored
View File

@ -5,4 +5,3 @@ __pycache__
/build
/dist
/deb_dist
/.vscode

View File

@ -1,33 +0,0 @@
sudo: required
dist: trusty
language: python
cache:
- pip
python:
- "3.4"
- "3.5"
- "3.6"
- "pypy3"
before_install:
- sudo apt install rpm dpkg-dev debhelper dh-python python3-setuptools fakeroot python3-serial python3-yaml
install:
- pip install pyserial pyusb
script:
- python setup.py build
- python setup.py test
before_deploy:
- deactivate
- python3 setup.py bdist_rpm
- dpkg-buildpackage -uc -us
- cp ../*.deb dist/
deploy:
provider: releases
api_key: $GH_TOKEN
file_glob: true
file:
- dist/stcgal*_all.deb
- dist/stcgal*.noarch.rpm
skip_cleanup: true
on:
tags: true
python: "3.4"

View File

@ -1,5 +1,3 @@
[![Build Status](https://travis-ci.org/grigorig/stcgal.svg)](https://travis-ci.org/grigorig/stcgal)
stcgal - STC MCU ISP flash tool
===============================

View File

@ -27,7 +27,7 @@ from setuptools import setup, find_packages
setup(
name = "stcgal",
version = stcgal.__version__,
packages = find_packages(exclude=["doc", "test"]),
packages = find_packages(exclude=["doc"]),
install_requires = ["pyserial"],
extras_require = {
"usb": ["pyusb>=1.0.0"]
@ -55,6 +55,4 @@ setup(
"Topic :: Software Development :: Embedded Systems",
"Topic :: Software Development",
],
test_suite = "test",
tests_require = ["PyYAML"],
)

View File

@ -20,10 +20,10 @@
# SOFTWARE.
#
import sys
import sys, os, time, struct
import argparse
import stcgal
from stcgal.utils import BaudType
from stcgal.utils import Utils, BaudType
from stcgal.protocols import *
from stcgal.ihex import IHex
@ -55,16 +55,13 @@ class StcGal:
self.protocol.debug = opts.debug
def emit_options(self, options):
"""Set options from command line to protocol handler."""
for opt in options:
for o in options:
try:
kv = opt.split("=", 1)
if len(kv) < 2:
raise ValueError("incorrect format")
kv = o.split("=", 1)
if len(kv) < 2: raise ValueError("incorrect format")
self.protocol.set_option(kv[0], kv[1])
except ValueError as ex:
raise NameError("invalid option '%s' (%s)" % (kv[0], ex))
except ValueError as e:
raise NameError("invalid option '%s' (%s)" % (kv[0], e))
def load_file_auto(self, fileobj):
"""Load file with Intel Hex autodetection."""
@ -77,16 +74,14 @@ class StcGal:
binary = hexfile.extract_data()
print("%d bytes (Intel HEX)" %len(binary))
return binary
except ValueError as ex:
raise IOError("invalid Intel HEX file (%s)" %ex)
except ValueError as e:
raise IOError("invalid Intel HEX file (%s)" %e)
else:
binary = fileobj.read()
print("%d bytes (Binary)" %len(binary))
return binary
def program_mcu(self):
"""Execute the standard programming flow."""
code_size = self.protocol.model.code
ee_size = self.protocol.model.eeprom
@ -129,8 +124,6 @@ class StcGal:
self.protocol.disconnect()
def run(self):
"""Run programmer, main entry point."""
try:
self.protocol.connect(autoreset=self.opts.autoreset)
@ -147,21 +140,21 @@ class StcGal:
self.protocol.initialize(base_protocol)
except KeyboardInterrupt:
sys.stdout.flush()
sys.stdout.flush();
print("interrupted")
return 2
except (StcFramingException, StcProtocolException) as ex:
sys.stdout.flush()
print("Protocol error: %s" % ex, file=sys.stderr)
except (StcFramingException, StcProtocolException) as e:
sys.stdout.flush();
print("Protocol error: %s" % e, file=sys.stderr)
self.protocol.disconnect()
return 1
except serial.SerialException as ex:
sys.stdout.flush()
print("Serial port error: %s" % ex, file=sys.stderr)
except serial.SerialException as e:
sys.stdout.flush();
print("Serial port error: %s" % e, file=sys.stderr)
return 1
except IOError as ex:
sys.stdout.flush()
print("I/O error: %s" % ex, file=sys.stderr)
except IOError as e:
sys.stdout.flush();
print("I/O error: %s" % e, file=sys.stderr)
return 1
try:
@ -171,27 +164,27 @@ class StcGal:
else:
self.protocol.disconnect()
return 0
except NameError as ex:
sys.stdout.flush()
print("Option error: %s" % ex, file=sys.stderr)
except NameError as e:
sys.stdout.flush();
print("Option error: %s" % e, file=sys.stderr)
self.protocol.disconnect()
return 1
except (StcFramingException, StcProtocolException) as ex:
sys.stdout.flush()
print("Protocol error: %s" % ex, file=sys.stderr)
except (StcFramingException, StcProtocolException) as e:
sys.stdout.flush();
print("Protocol error: %s" % e, file=sys.stderr)
self.protocol.disconnect()
return 1
except KeyboardInterrupt:
sys.stdout.flush()
sys.stdout.flush();
print("interrupted", file=sys.stderr)
self.protocol.disconnect()
return 2
except serial.SerialException as ex:
print("Serial port error: %s" % ex, file=sys.stderr)
except serial.SerialException as e:
print("Serial port error: %s" % e, file=sys.stderr)
return 1
except IOError as ex:
sys.stdout.flush()
print("I/O error: %s" % ex, file=sys.stderr)
except IOError as e:
sys.stdout.flush();
print("I/O error: %s" % e, file=sys.stderr)
self.protocol.disconnect()
return 1

View File

@ -69,7 +69,7 @@ class Stc89Option(BaseOption):
return not bool(self.msr & 1)
def set_t6(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr &= 0xfe
self.msr |= 0x01 if not bool(val) else 0x00
@ -77,7 +77,7 @@ class Stc89Option(BaseOption):
return not bool(self.msr & 4)
def set_pindetect(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr &= 0xfb
self.msr |= 0x04 if not bool(val) else 0x00
@ -85,7 +85,7 @@ class Stc89Option(BaseOption):
return not bool(self.msr & 8)
def set_ee_erase(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr &= 0xf7
self.msr |= 0x08 if not bool(val) else 0x00
@ -104,7 +104,7 @@ class Stc89Option(BaseOption):
return bool(self.msr & 32)
def set_ale(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr &= 0xdf
self.msr |= 0x20 if bool(val) else 0x00
@ -112,7 +112,7 @@ class Stc89Option(BaseOption):
return bool(self.msr & 64)
def set_xram(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr &= 0xbf
self.msr |= 0x40 if bool(val) else 0x00
@ -120,7 +120,7 @@ class Stc89Option(BaseOption):
return not bool(self.msr & 128)
def set_watchdog(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr &= 0x7f
self.msr |= 0x80 if not bool(val) else 0x00
@ -150,7 +150,7 @@ class Stc12AOption(BaseOption):
def set_low_voltage_detect(self, val):
lvds = {"low": 1, "high": 0}
if val not in lvds.keys():
raise ValueError("must be one of %s" % list(lvds.keys()))
raise ValueError("must be one of %s" % list(sources.keys()))
self.msr[3] &= 0xbf
self.msr[3] |= lvds[val] << 6
@ -169,7 +169,7 @@ class Stc12AOption(BaseOption):
return not bool(self.msr[1] & 32)
def set_watchdog(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[1] &= 0xdf
self.msr[1] |= 0x20 if not val else 0x00
@ -177,7 +177,7 @@ class Stc12AOption(BaseOption):
return not bool(self.msr[1] & 8)
def set_watchdog_idle(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[1] &= 0xf7
self.msr[1] |= 0x08 if not val else 0x00
@ -196,7 +196,7 @@ class Stc12AOption(BaseOption):
return not bool(self.msr[2] & 2)
def set_ee_erase(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[2] &= 0xfd
self.msr[2] |= 0x02 if not val else 0x00
@ -204,7 +204,7 @@ class Stc12AOption(BaseOption):
return not bool(self.msr[2] & 1)
def set_pindetect(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[2] &= 0xfe
self.msr[2] |= 0x01 if not val else 0x00
@ -235,7 +235,7 @@ class Stc12Option(BaseOption):
return bool(self.msr[0] & 1)
def set_reset_pin_enabled(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[0] &= 0xfe
self.msr[0] |= 0x01 if bool(val) else 0x00
@ -243,7 +243,7 @@ class Stc12Option(BaseOption):
return not bool(self.msr[0] & 64)
def set_low_voltage_detect(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[0] &= 0xbf
self.msr[0] |= 0x40 if not val else 0x00
@ -295,7 +295,7 @@ class Stc12Option(BaseOption):
return not bool(self.msr[2] & 32)
def set_watchdog(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[2] &= 0xdf
self.msr[2] |= 0x20 if not val else 0x00
@ -303,7 +303,7 @@ class Stc12Option(BaseOption):
return not bool(self.msr[2] & 8)
def set_watchdog_idle(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[2] &= 0xf7
self.msr[2] |= 0x08 if not val else 0x00
@ -322,7 +322,7 @@ class Stc12Option(BaseOption):
return not bool(self.msr[3] & 2)
def set_ee_erase(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[3] &= 0xfd
self.msr[3] |= 0x02 if not val else 0x00
@ -330,7 +330,7 @@ class Stc12Option(BaseOption):
return not bool(self.msr[3] & 1)
def set_pindetect(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[3] &= 0xfe
self.msr[3] |= 0x01 if not val else 0x00
@ -359,7 +359,7 @@ class Stc15AOption(BaseOption):
return bool(self.msr[0] & 16)
def set_reset_pin_enabled(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[0] &= 0xef
self.msr[0] |= 0x10 if bool(val) else 0x00
@ -367,7 +367,7 @@ class Stc15AOption(BaseOption):
return not bool(self.msr[2] & 32)
def set_watchdog(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[2] &= 0xdf
self.msr[2] |= 0x20 if not val else 0x00
@ -375,7 +375,7 @@ class Stc15AOption(BaseOption):
return not bool(self.msr[2] & 8)
def set_watchdog_idle(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[2] &= 0xf7
self.msr[2] |= 0x08 if not val else 0x00
@ -394,7 +394,7 @@ class Stc15AOption(BaseOption):
return bool(self.msr[1] & 64)
def set_lvrs(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[1] &= 0xbf
self.msr[1] |= 0x40 if val else 0x00
@ -402,7 +402,7 @@ class Stc15AOption(BaseOption):
return bool(self.msr[1] & 128)
def set_eeprom_lvd(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[1] &= 0x7f
self.msr[1] |= 0x80 if val else 0x00
@ -420,7 +420,7 @@ class Stc15AOption(BaseOption):
return not bool(self.msr[12] & 2)
def set_ee_erase(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[12] &= 0xfd
self.msr[12] |= 0x02 if not val else 0x00
@ -428,7 +428,7 @@ class Stc15AOption(BaseOption):
return not bool(self.msr[12] & 1)
def set_pindetect(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[12] &= 0xfe
self.msr[12] |= 0x01 if not val else 0x00
@ -463,7 +463,7 @@ class Stc15Option(BaseOption):
return not bool(self.msr[2] & 16)
def set_reset_pin_enabled(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[2] &= 0xef
self.msr[2] |= 0x10 if not bool(val) else 0x00
@ -493,7 +493,7 @@ class Stc15Option(BaseOption):
return not bool(self.msr[0] & 32)
def set_watchdog(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[0] &= 0xdf
self.msr[0] |= 0x20 if not val else 0x00
@ -501,7 +501,7 @@ class Stc15Option(BaseOption):
return not bool(self.msr[0] & 8)
def set_watchdog_idle(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[0] &= 0xf7
self.msr[0] |= 0x08 if not val else 0x00
@ -520,7 +520,7 @@ class Stc15Option(BaseOption):
return not bool(self.msr[1] & 64)
def set_lvrs(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[1] &= 0xbf
self.msr[1] |= 0x40 if not val else 0x00
@ -528,7 +528,7 @@ class Stc15Option(BaseOption):
return bool(self.msr[1] & 128)
def set_eeprom_lvd(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[1] &= 0x7f
self.msr[1] |= 0x80 if val else 0x00
@ -546,7 +546,7 @@ class Stc15Option(BaseOption):
return bool(self.msr[3] & 2)
def set_ee_erase(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[3] &= 0xfd
self.msr[3] |= 0x02 if val else 0x00
@ -554,7 +554,7 @@ class Stc15Option(BaseOption):
return not bool(self.msr[3] & 1)
def set_pindetect(self, val):
val = Utils.to_bool(val)
val = Utils.to_bool(val);
self.msr[3] &= 0xfe
self.msr[3] |= 0x01 if not val else 0x00

View File

@ -114,7 +114,11 @@ class StcBaseProtocol:
# read and check frame start magic
packet = bytes()
packet += self.read_bytes_safe(1)
# XXX: skip extraneous 0xFE byte?
leading = self.read_bytes_safe(1)
if leading == 0xfe:
leading = self.read_bytes_safe(1)
packet += leading
# Some (?) BSL versions don't send a frame start with the status
# packet. Let's be liberal and accept that always, just in case.
if packet[0] == self.PACKET_MCU[0]:
@ -142,7 +146,7 @@ class StcBaseProtocol:
packet += self.read_bytes_safe(packet_len - 3)
# verify checksum and extract payload
payload = self.extract_payload(packet)
payload = self.extract_payload(packet);
self.dump_packet(packet, receive=True)
@ -192,12 +196,12 @@ class StcBaseProtocol:
mcu_name += "E" if self.status_packet[17] < 0x70 else "W"
self.model = self.model._replace(name = mcu_name)
protocol_database = [("stc89", r"STC(89|90)(C|LE)\d"),
("stc12a", r"STC12(C|LE)\d052"),
("stc12b", r"STC12(C|LE)(52|56)"),
("stc12", r"(STC|IAP)(10|11|12)\D"),
("stc15a", r"(STC|IAP)15[FL][01]0\d(E|EA|)$"),
("stc15", r"(STC|IAP|IRC)15\D")]
protocol_database = [("stc89", "STC(89|90)(C|LE)\d"),
("stc12a", "STC12(C|LE)\d052"),
("stc12b", "STC12(C|LE)(52|56)"),
("stc12", "(STC|IAP)(10|11|12)\D"),
("stc15a", "(STC|IAP)15[FL][01]0\d(E|EA|)$"),
("stc15", "(STC|IAP|IRC)15\D")]
for protocol_name, pattern in protocol_database:
if re.match(pattern, self.model.name):
@ -422,7 +426,7 @@ class Stc89Protocol(StcBaseProtocol):
bl_version, bl_stepping = struct.unpack("BB", packet[17:19])
self.mcu_bsl_version = "%d.%d%s" % (bl_version >> 4, bl_version & 0x0f,
chr(bl_stepping))
chr(bl_stepping))
def handshake(self):
"""Switch to transfer baudrate
@ -579,7 +583,7 @@ class Stc12AProtocol(Stc12AOptionsMixIn, Stc89Protocol):
bl_version, bl_stepping = struct.unpack("BB", packet[17:19])
self.mcu_bsl_version = "%d.%d%s" % (bl_version >> 4, bl_version & 0x0f,
chr(bl_stepping))
chr(bl_stepping))
self.bsl_version = bl_version
@ -768,7 +772,7 @@ class Stc12BaseProtocol(StcBaseProtocol):
bl_version, bl_stepping = struct.unpack("BB", packet[17:19])
self.mcu_bsl_version = "%d.%d%s" % (bl_version >> 4, bl_version & 0x0f,
chr(bl_stepping))
chr(bl_stepping))
self.bsl_version = bl_version
@ -886,6 +890,7 @@ class Stc12BaseProtocol(StcBaseProtocol):
packet += struct.pack(">H", self.PROGRAM_BLOCKSIZE)
packet += data[i:i+self.PROGRAM_BLOCKSIZE]
while len(packet) < self.PROGRAM_BLOCKSIZE + 7: packet += b"\x00"
csum = sum(packet[7:]) & 0xff
self.write_packet(packet)
response = self.read_packet()
if response[0] != 0x00:
@ -964,7 +969,7 @@ class Stc15AProtocol(Stc12Protocol):
bl_version, bl_stepping = struct.unpack("BB", packet[17:19])
self.mcu_bsl_version = "%d.%d%s" % (bl_version >> 4, bl_version & 0x0f,
chr(bl_stepping))
chr(bl_stepping))
self.trim_data = packet[51:58]
self.freq_counter = freq_counter
@ -1018,8 +1023,7 @@ class Stc15AProtocol(Stc12Protocol):
"""
user_speed = self.trim_frequency
if user_speed <= 0:
user_speed = self.mcu_clock_hz
if user_speed <= 0: user_speed = self.mcu_clock_hz
program_speed = 22118400
user_count = int(self.freq_counter * (user_speed / self.mcu_clock_hz))
@ -1177,7 +1181,8 @@ class Stc15Protocol(Stc15AProtocol):
bl_version, bl_stepping = struct.unpack("BB", packet[17:19])
bl_minor = packet[22] & 0x0f
self.mcu_bsl_version = "%d.%d.%d%s" % (bl_version >> 4, bl_version & 0x0f,
bl_minor, chr(bl_stepping))
bl_minor,
chr(bl_stepping))
self.bsl_version = bl_version
def print_mcu_info(self):
@ -1406,7 +1411,7 @@ class Stc15Protocol(Stc15AProtocol):
configuration."""
msr = self.options.get_msr()
packet = bytes([0xff] * 23)
packet = bytes([0xff] * 23)
packet += bytes([(self.trim_frequency >> 24) & 0xff,
0xff,
(self.trim_frequency >> 16) & 0xff,
@ -1460,9 +1465,8 @@ class StcUsb15Protocol(Stc15Protocol):
def dump_packet(self, data, request=0, value=0, index=0, receive=True):
if self.debug:
print("%s bRequest=%02X wValue=%04X wIndex=%04X data: %s" %
(("<-" if receive else "->"), request, value, index,
Utils.hexstr(data, " ")), file=sys.stderr)
print("%s bRequest=%02X wValue=%04X wIndex=%04X data: %s" % (("<-" if receive else "->"),
request, value, index, Utils.hexstr(data, " ")), file=sys.stderr)
def read_packet(self):
"""Read a packet from the MCU"""
@ -1502,16 +1506,15 @@ class StcUsb15Protocol(Stc15Protocol):
self.dump_packet(chunks, request, value, index, receive=False)
host2dev = usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_RECIPIENT_DEVICE | usb.util.CTRL_OUT
self.dev.ctrl_transfer(host2dev, request, value, index, chunks)
self.dev.ctrl_transfer(host2dev, request, value, index, chunks);
def connect(self, autoreset=False):
"""Connect to USB device and read info packet"""
# USB support is optional. Provide an error if pyusb is not available.
if not _usb_available:
raise StcProtocolException(
"USB support not available. " +
"pyusb is not installed or not working correctly.")
if _usb_available == False:
raise StcProtocolException("USB support not available. "
+ "pyusb is not installed or not working correctly.")
print("Waiting for MCU, please cycle power: ", end="")
sys.stdout.flush()

View File

@ -19,41 +19,31 @@
# SOFTWARE.
#
import argparse
import serial
import argparse
class Utils:
"""Common utility functions"""
@classmethod
def to_bool(cls, val):
def to_bool(self, val):
"""make sensible boolean from string or other type value"""
if val is None:
return False
if isinstance(val, bool):
return val
elif isinstance(val, int):
return bool(val)
elif len(val) == 0:
return False
else:
return True if val[0].lower() == "t" or val[0] == "1" else False
if isinstance(val, bool): return val
if isinstance(val, int): return bool(val)
if len(val) == 0: return False
return True if val[0].lower() == "t" or val[0] == "1" else False
@classmethod
def to_int(cls, val):
def to_int(self, val):
"""make int from any value, nice error message if not possible"""
try:
return int(val, 0)
except:
raise ValueError("invalid integer")
try: return int(val, 0)
except: raise ValueError("invalid integer")
@classmethod
def hexstr(cls, bytestr, sep=""):
def hexstr(self, bytestr, sep=""):
"""make formatted hex string output from byte sequence"""
return sep.join(["%02X" % x for x in bytes(bytestr)])
return sep.join(["%02X" % x for x in bytestr])
class BaudType:
@ -65,5 +55,5 @@ class BaudType:
raise argparse.ArgumentTypeError("illegal baudrate")
return baud
def __repr__(self):
return "baudrate"
def __repr__(self): return "baudrate"

View File

View File

@ -1,19 +0,0 @@
name: IAP15F2K61S2 programming test
protocol: stc15
code_data: [49, 50, 51, 52, 53, 54, 55, 56, 57]
responses:
- [0x50, 0x87, 0xD3, 0x75, 0x9C, 0xF5, 0x3B, 0x17, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x09, 0x81, 0x00, 0x00, 0x71, 0x53, 0x00, 0xF4, 0x49, 0x04, 0x06, 0x58, 0x9C, 0x02, 0x0E, 0x14, 0x17, 0x19, 0x19, 0x00, 0xF4, 0xF4, 0x04, 0xD2]
- [0x00, 0x0B, 0x03, 0x37, 0x04, 0x9A, 0x06, 0x02, 0x06, 0x6B, 0x09, 0x27, 0x0B, 0xE8, 0x0D, 0x0A, 0x12, 0x5A, 0x17, 0x9B, 0x14, 0x8F, 0x1C, 0x96, 0x00, 0x00]
- [0x00, 0x0C, 0x09, 0x04, 0x09, 0x09, 0x09, 0x0E, 0x09, 0x0E, 0x09, 0x18, 0x09, 0x1D, 0x12, 0x00, 0x12, 0x0F, 0x12, 0x19, 0x12, 0x23, 0x12, 0x2D, 0x12, 0x37]
- [0x01]
- [0x05]
- [0x03, 0x0D, 0x00, 0x00, 0x21, 0x02, 0x26, 0x32]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x04, 0x54]

View File

@ -1,19 +0,0 @@
name: STC12C2052AD programming test
protocol: stc12a
code_data: [49, 50, 51, 52, 53, 54, 55, 56, 57]
responses:
- [0x00, 0x04, 0xEC, 0x04, 0xEC, 0x04, 0xEC, 0x04, 0xEC, 0x04, 0xEC, 0x04, 0xEC, 0x04, 0xEB, 0x04, 0xEB, 0x58, 0x44, 0x00, 0xF2, 0x12, 0x83, 0xFD, 0xF7, 0xF7, 0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0xFD, 0xF7, 0xF7, 0xFF]
- [0x8F, 0xC0, 0x79, 0x3F, 0xFE, 0x28, 0x85]
- [0x8E, 0xC0, 0x79, 0x3F, 0xFE, 0x28]
- [0x80]
- [0x80]
- [0x80]
- [0x80]
- [0x80]
- [0x80, 0x66]
- [0x80, 0x80]
- [0x80, 0x80]
- [0x80, 0x80]
- [0x80, 0xEE]
- [0x10, 0xC0, 0x16, 0xF7, 0xFF, 0xBF, 0x03, 0xFF, 0x58, 0x44, 0xFD, 0xF7, 0xF7, 0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0xFD, 0xF7, 0xF7, 0xFF]
- [0x80]

View File

@ -1,15 +0,0 @@
name: STC12C5A60S2 programming test
protocol: stc12
code_data: [49, 50, 51, 52, 53, 54, 55, 56, 57]
responses:
- [0x50, 0x04, 0xBD, 0x04, 0xBC, 0x04, 0xBC, 0x04, 0xBD, 0x04, 0xBC, 0x04, 0xBC, 0x04, 0xBC, 0x04, 0xBC, 0x62, 0x49, 0x00, 0xD1, 0x7E, 0x8C, 0xFF, 0x7F, 0xF7, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x03, 0x00, 0xB0, 0x02, 0x2E, 0x6B, 0x00, 0xCD, 0x80, 0x00, 0x00]
- [0x8F]
- [0x8F, 0xC0, 0x7E, 0x3F, 0xFE, 0xA0, 0x83, 0x04]
- [0x84, 0xC0, 0x7E, 0x3F, 0xFE, 0xA0, 0x04]
- [0x00]
- [0x00, 0x03]
- [0x00, 0x00]
- [0x00, 0x00]
- [0x00, 0x00]
- [0x8D]
- [0x50, 0xFF, 0x7F, 0xF7, 0xFF, 0xFF, 0x03, 0xFF, 0x62, 0x49, 0xFF, 0x7F, 0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x00, 0xB0, 0x02, 0x2E, 0x6B, 0x00, 0xCD, 0x80, 0x00, 0x00]

View File

@ -1,20 +0,0 @@
name: STC15F104E programming test
protocol: stc15a
code_data: [49, 50, 51, 52, 53, 54, 55, 56, 57]
responses:
- [0x50, 0x02, 0xB0, 0x02, 0xB0, 0x02, 0xAF, 0x02, 0xB0, 0x02, 0xE6, 0x02, 0xE7, 0x00, 0x00, 0x00, 0x00, 0x67, 0x51, 0xFF, 0xF2, 0x94, 0x8C, 0xEF, 0x3B, 0xF5, 0x58, 0x34, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x58, 0x50, 0x0C, 0x94, 0x21, 0xFF, 0x29]
- [0x8f]
- [0x65, 0x58, 0x50, 0x0C, 0x95, 0x21, 0xFF, 0x2B, 0xFF, 0xFF, 0x06, 0x06, 0x58, 0x00, 0x02, 0x00, 0x58, 0x80, 0x02, 0x00, 0x58, 0x80, 0x02, 0x00, 0x58, 0xFF, 0x02, 0x00, 0x58, 0x00, 0x02, 0x00, 0x58, 0x80, 0x02, 0x00]
- [0x65, 0x58, 0x50, 0x0C, 0x95, 0x21, 0xFF, 0x2B, 0xFF, 0xFF, 0x06, 0x0B, 0x58, 0x24, 0x02, 0x00, 0x58, 0x25, 0x02, 0x00, 0x58, 0x26, 0x02, 0x00, 0x58, 0x27, 0x02, 0x00, 0x58, 0x28, 0x02, 0x00, 0x58, 0x29, 0x02, 0x00, 0x58, 0x2A, 0x02, 0x00, 0x58, 0x2B, 0x02, 0x00, 0x58, 0x2C, 0x02, 0x00, 0x58, 0x2D, 0x02, 0x00, 0x58, 0x2E, 0x02, 0x00]
- [0x01]
- [0x05]
- [0x03, 0x0C, 0x00, 0x00, 0x17, 0x01, 0xA0, 0xE0]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x04, 0x54]

View File

@ -1,19 +0,0 @@
name: STC15L104W programming test
protocol: stc15
code_data: [49, 50, 51, 52, 53, 54, 55, 56, 57]
responses:
- [0x50, 0x66, 0x3C, 0x93, 0xBA, 0xF7, 0xBB, 0x9F, 0x00, 0x5B, 0x68, 0x00, 0xFD, 0x00, 0x00, 0x00, 0x00, 0x71, 0x51, 0x03, 0xF2, 0xD4, 0x04, 0x06, 0x58, 0xBA, 0x02, 0x2A, 0x31, 0x32, 0x38, 0x30, 0x80, 0x14, 0x10, 0x04, 0xD9]
- [0x00, 0x0B, 0x03, 0x0A, 0x04, 0x4F, 0x05, 0x9E, 0x06, 0x20, 0x08, 0xB9, 0x0B, 0x5C, 0x0C, 0x6A, 0x11, 0x7E, 0x16, 0x79, 0x13, 0x77, 0x1A, 0xB1, 0x00, 0x00]
- [0x00, 0x0C, 0x04, 0xD6, 0x04, 0xDB, 0x04, 0xE0, 0x04, 0xE0, 0x04, 0xE0, 0x04, 0xE5, 0x11, 0xE2, 0x11, 0xF1, 0x11, 0xFB, 0x12, 0x05, 0x12, 0x0A, 0x12, 0x19]
- [0x01]
- [0x05]
- [0x03, 0x0C, 0x00, 0x00, 0x17, 0x01, 0xA0, 0xE0]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x04, 0x54]

View File

@ -1,20 +0,0 @@
name: STC15W4K56S4 programming test
protocol: stc15
code_data: [49, 50, 51, 52, 53, 54, 55, 56, 57]
responses:
- [0x50, 0x8D, 0xFF, 0x73, 0x96, 0xF5, 0x7B, 0x9F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x27, 0xED, 0x00, 0x00, 0x73, 0x54, 0x00, 0xF5, 0x28, 0x04, 0x06, 0x70, 0x96, 0x02, 0x15, 0x19, 0x1C, 0x1E, 0x23, 0x00, 0xEC, 0xE0, 0x04, 0xD7, 0xF8, 0x73, 0xBF, 0xFF, 0xFF, 0x15, 0x09, 0x25, 0x60]
- [0x00, 0x0B, 0x0D, 0x21, 0x12, 0xBC, 0x18, 0x3E, 0x1A, 0x05, 0x24, 0xFA, 0x2F, 0xB3, 0x34, 0xD1, 0x4A, 0x52, 0x5E, 0xC0, 0x52, 0xDB, 0x73, 0x1A, 0x00, 0x00]
- [0x00, 0x0C, 0x23, 0xBF, 0x23, 0xD3, 0x23, 0xE7, 0x23, 0xF6, 0x24, 0x0F, 0x24, 0x23, 0x47, 0x73, 0x47, 0xB9, 0x47, 0xE1, 0x48, 0x09, 0x48, 0x36, 0x48, 0x59]
- [0x01]
- [0x05]
- [0x03, 0xF5, 0x28, 0x00, 0xA5, 0x03, 0x27, 0x49]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x02, 0x54]
- [0x07, 0x54]
- [0x04, 0x54]

View File

@ -1,19 +0,0 @@
name: STC89C52RC programming test
protocol: stc89
code_data: [49, 50, 51, 52, 53, 54, 55, 56, 57]
responses:
- [0x00, 0x25, 0xE6, 0x25, 0xE6, 0x25, 0xE6, 0x25, 0xE6, 0x25, 0xE6, 0x25, 0xE6, 0x25, 0xE2, 0x25, 0xE6, 0x43, 0x43, 0xFC, 0xF0, 0x02, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
- [0x8F, 0xFD, 0xF8, 0x02, 0x10, 0x28, 0x81]
- [0x8E, 0xFD, 0xF8, 0x02, 0x10, 0x28]
- [0x80]
- [0x80]
- [0x80]
- [0x80]
- [0x80]
- [0x80, 0x66]
- [0x80, 0x80]
- [0x80, 0x80]
- [0x80, 0x80]
- [0x8D, 0xFC, 0xFF, 0xF6, 0xFF]
- [0x10, 0xC0, 0x16, 0xF6, 0xFF, 0xF1, 0x03, 0xFF, 0x43, 0x43, 0xFC]
- [0x80]

View File

@ -1,136 +0,0 @@
#
# Copyright (c) 2017 Grigori Goronzy <greg@chown.ath.cx>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
"""Tests that simulate a whole programming cycle"""
import unittest
from unittest.mock import patch
import yaml
import stcgal.frontend
import stcgal.protocols
def convert_to_bytes(list_of_lists):
"""Convert lists of integer lists to list of byte lists"""
return [bytes(x) for x in list_of_lists]
def get_default_opts():
"""Get a default preconfigured option object"""
opts = unittest.mock.MagicMock()
opts.protocol = "stc89"
opts.autoreset = False
opts.port = ""
opts.baud = 19200
opts.handshake = 9600
opts.trim = 22118
opts.eeprom_image = None
opts.debug = False
opts.code_image.name = "test.bin"
opts.code_image.read.return_value = b"123456789"
return opts
class ProgramTests(unittest.TestCase):
"""Test MCU programming cycles for different families, based on traces"""
@patch("stcgal.protocols.StcBaseProtocol.read_packet")
@patch("stcgal.protocols.Stc89Protocol.write_packet")
@patch("stcgal.protocols.serial.Serial", autospec=True)
@patch("stcgal.protocols.time.sleep")
@patch("sys.stdout")
def test_program_stc89(self, out, sleep_mock, serial_mock, write_mock, read_mock):
"""Test a programming cycle with STC89 protocol"""
self._program_yml("./test/stc89c52rc.yml", serial_mock, read_mock)
@patch("stcgal.protocols.StcBaseProtocol.read_packet")
@patch("stcgal.protocols.Stc89Protocol.write_packet")
@patch("stcgal.protocols.serial.Serial", autospec=True)
@patch("stcgal.protocols.time.sleep")
@patch("sys.stdout")
def test_program_stc12(self, out, sleep_mock, serial_mock, write_mock, read_mock):
"""Test a programming cycle with STC12 protocol"""
self._program_yml("./test/stc12c5a60s2.yml", serial_mock, read_mock)
@patch("stcgal.protocols.StcBaseProtocol.read_packet")
@patch("stcgal.protocols.Stc89Protocol.write_packet")
@patch("stcgal.protocols.serial.Serial", autospec=True)
@patch("stcgal.protocols.time.sleep")
@patch("sys.stdout")
def test_program_stc12a(self, out, sleep_mock, serial_mock, write_mock, read_mock):
"""Test a programming cycle with STC12A protocol"""
self._program_yml("./test/stc12c2052ad.yml", serial_mock, read_mock)
def test_program_stc12b(self):
"""Test a programming cycle with STC12B protocol"""
self.skipTest("trace missing")
@patch("stcgal.protocols.StcBaseProtocol.read_packet")
@patch("stcgal.protocols.Stc89Protocol.write_packet")
@patch("stcgal.protocols.serial.Serial", autospec=True)
@patch("stcgal.protocols.time.sleep")
@patch("sys.stdout")
def test_program_stc15f2(self, out, sleep_mock, serial_mock, write_mock, read_mock):
"""Test a programming cycle with STC15 protocol, F2 series"""
self._program_yml("./test/iap15f2k61s2.yml", serial_mock, read_mock)
@patch("stcgal.protocols.StcBaseProtocol.read_packet")
@patch("stcgal.protocols.Stc89Protocol.write_packet")
@patch("stcgal.protocols.serial.Serial", autospec=True)
@patch("stcgal.protocols.time.sleep")
@patch("sys.stdout")
def test_program_stc15w4(self, out, sleep_mock, serial_mock, write_mock, read_mock):
"""Test a programming cycle with STC15 protocol, W4 series"""
self._program_yml("./test/stc15w4k56s4.yml", serial_mock, read_mock)
@unittest.skip("trace is broken")
@patch("stcgal.protocols.StcBaseProtocol.read_packet")
@patch("stcgal.protocols.Stc89Protocol.write_packet")
@patch("stcgal.protocols.serial.Serial", autospec=True)
@patch("stcgal.protocols.time.sleep")
@patch("sys.stdout")
def test_program_stc15a(self, out, sleep_mock, serial_mock, write_mock, read_mock):
"""Test a programming cycle with STC15A protocol"""
self._program_yml("./test/stc15f104e.yml", serial_mock, read_mock)
@patch("stcgal.protocols.StcBaseProtocol.read_packet")
@patch("stcgal.protocols.Stc89Protocol.write_packet")
@patch("stcgal.protocols.serial.Serial", autospec=True)
@patch("stcgal.protocols.time.sleep")
@patch("sys.stdout")
def test_program_stc15l1(self, out, sleep_mock, serial_mock, write_mock, read_mock):
"""Test a programming cycle with STC15 protocol, L1 series"""
self._program_yml("./test/stc15l104w.yml", serial_mock, read_mock)
def test_program_stc15w4_usb(self):
"""Test a programming cycle with STC15W4 USB protocol"""
self.skipTest("USB not supported yet, trace missing")
def _program_yml(self, yml, serial_mock, read_mock):
"""Program MCU with data from YAML file"""
with open(yml) as test_file:
test_data = yaml.load(test_file.read())
opts = get_default_opts()
opts.protocol = test_data["protocol"]
opts.code_image.read.return_value = bytes(test_data["code_data"])
serial_mock.return_value.inWaiting.return_value = 1
read_mock.side_effect = convert_to_bytes(test_data["responses"])
gal = stcgal.frontend.StcGal(opts)
self.assertEqual(gal.run(), 0)

View File

@ -1,77 +0,0 @@
#
# Copyright (c) 2017 Grigori Goronzy <greg@chown.ath.cx>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
"""Tests for utility functions and other misc parts"""
import argparse
import unittest
from unittest.mock import patch
from stcgal.utils import Utils, BaudType
class TestUtils(unittest.TestCase):
"""Test for utility functions in the Utils class"""
def test_to_bool(self):
"""Test special utility function for bool conversion"""
self.assertTrue(Utils.to_bool(True))
self.assertTrue(Utils.to_bool("true"))
self.assertTrue(Utils.to_bool("True"))
self.assertTrue(Utils.to_bool("t"))
self.assertTrue(Utils.to_bool("T"))
self.assertTrue(Utils.to_bool(1))
self.assertTrue(Utils.to_bool(-1))
self.assertFalse(Utils.to_bool(0))
self.assertFalse(Utils.to_bool(None))
self.assertFalse(Utils.to_bool("false"))
self.assertFalse(Utils.to_bool("False"))
self.assertFalse(Utils.to_bool("f"))
self.assertFalse(Utils.to_bool("F"))
self.assertFalse(Utils.to_bool(""))
def test_to_int(self):
"""Test wrapped integer conversion"""
self.assertEqual(Utils.to_int("2"), 2)
self.assertEqual(Utils.to_int("0x10"), 16)
with self.assertRaises(ValueError):
Utils.to_int("a")
with self.assertRaises(ValueError):
Utils.to_int("")
with self.assertRaises(ValueError):
Utils.to_int(None)
def test_hexstr(self):
"""Test byte array formatter"""
self.assertEqual(Utils.hexstr([10]), "0A")
self.assertEqual(Utils.hexstr([1, 2, 3]), "010203")
with self.assertRaises(Exception):
Utils.hexstr([400, 500])
class TestBaudType(unittest.TestCase):
"""Test BaudType class"""
def test_create_baud_type(self):
"""Test creation of BaudType instances"""
baud_type = BaudType()
self.assertEqual(baud_type("2400"), 2400)
self.assertEqual(baud_type("115200"), 115200)
with self.assertRaises(argparse.ArgumentTypeError):
baud_type("2374882")