From fcbc560ade254504ad2bea393ff685731e84a9bc Mon Sep 17 00:00:00 2001 From: Grigori Goronzy Date: Tue, 21 Aug 2018 12:42:51 +0200 Subject: [PATCH] stc8: print additional MCU info Print manufacturing date as well as the factory-measured internal voltage reference value. These values are displayed by STC-ISP, too. --- doc/stc8-protocol.txt | 3 ++- stcgal/protocols.py | 14 ++++++++++++++ stcgal/utils.py | 5 +++++ tests/test_utils.py | 8 ++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/doc/stc8-protocol.txt b/doc/stc8-protocol.txt index 436e33e..94ddb74 100644 --- a/doc/stc8-protocol.txt +++ b/doc/stc8-protocol.txt @@ -20,7 +20,8 @@ Status packet ------------- 46 B9 68 00 30 50 00 54 62 58 5D 00 04 FF FD 8B BF FF 27 4A F7 FE 73 55 00 F6 28 09 85 E3 5F 80 07 20 20 20 01 00 00 FE 05 3A 17 05 25 91 FF 10 AE 16 - ^^^^^ wakeup clock + ^^^^^ wakeup clock ^^^^^ reference voltage + ^^^^^^^^ mfg. date Clock set to 20 MHz by STC-ISP (encoding is different compared to STC15): diff --git a/stcgal/protocols.py b/stcgal/protocols.py index cec7bc5..d1515d8 100644 --- a/stcgal/protocols.py +++ b/stcgal/protocols.py @@ -1554,6 +1554,8 @@ class Stc8Protocol(Stc15Protocol): def __init__(self, port, handshake, baud, trim): Stc15Protocol.__init__(self, port, handshake, baud, trim) self.trim_divider = None + self.reference_voltage = None + self.mfg_date = () def initialize_options(self, status_packet): """Initialize options""" @@ -1580,6 +1582,12 @@ class Stc8Protocol(Stc15Protocol): # wakeup timer factory value self.wakeup_freq, = struct.unpack(">H", packet[23:25]) + self.reference_voltage, = struct.unpack(">H", packet[35:37]) + self.mfg_date = ( + 2000 + Utils.decode_packed_bcd(packet[37]), + Utils.decode_packed_bcd(packet[38]), + Utils.decode_packed_bcd(packet[39]) + ) bl_version, bl_stepping = struct.unpack("BB", packet[17:19]) bl_minor = packet[22] & 0x0f @@ -1587,6 +1595,12 @@ class Stc8Protocol(Stc15Protocol): bl_minor, chr(bl_stepping)) self.bsl_version = bl_version + def print_mcu_info(self): + """Print additional STC8 info""" + super().print_mcu_info() + print("Target ref. voltage: %d mV" % self.reference_voltage) + print("Target mfg. date: %04d-%02d-%02d" % self.mfg_date) + def calibrate(self): """Calibrate selected user frequency frequency and switch to selected baudrate.""" diff --git a/stcgal/utils.py b/stcgal/utils.py index 8a1723d..2fe9131 100644 --- a/stcgal/utils.py +++ b/stcgal/utils.py @@ -52,6 +52,11 @@ class Utils: return sep.join(["%02X" % x for x in bytes(bytestr)]) + @classmethod + def decode_packed_bcd(cls, byt): + """Decode two-digit packed BCD value""" + return (byt & 0x0f) + (10 * (byt >> 4)) + class BaudType: """Check baud rate for validity""" diff --git a/tests/test_utils.py b/tests/test_utils.py index 59809b7..0b91bb7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -64,6 +64,14 @@ class TestUtils(unittest.TestCase): with self.assertRaises(Exception): Utils.hexstr([400, 500]) + def test_decode_packed_bcd(self): + """Test packed BCD decoder""" + self.assertEqual(Utils.decode_packed_bcd(0x01), 1) + self.assertEqual(Utils.decode_packed_bcd(0x10), 10) + self.assertEqual(Utils.decode_packed_bcd(0x11), 11) + self.assertEqual(Utils.decode_packed_bcd(0x25), 25) + self.assertEqual(Utils.decode_packed_bcd(0x99), 99) + class TestBaudType(unittest.TestCase): """Test BaudType class"""