Commit Graph

60 Commits

Author SHA1 Message Date
Grigori Goronzy
384471f765 stc15: fix fuzzing test failure 2021-01-06 20:08:26 +01:00
Grigori Goronzy
df2fbc23cd stc15: fix baudrate switching
Baudrate switching fixes for both HW and SW UART devices:

* Program secondary SW UART timing parameter correctly. Supposedly this
  controls the sampling position when receiving data.
* Swap around trim range/adjust parameter for HW UART devices (as
  determind by older traces)
* Use correct constant for timing calculations

This should fix some STC15x10xW series MCUs. Compatibility to other
devices might be improved as well.

Addresses #17.
2021-01-06 19:56:46 +01:00
Grigori Goronzy
0d5e8e645f Revert "protocols: fix off-by-one in payload extraction"
This reverts commit 0e020f2aa4.

Nothing needed fixing here. This strips off one byte too many.
2021-01-03 18:40:27 +01:00
Grigori Goronzy
1ec855e6a1 Revert "Add "stc89a" protocol"
This reverts commit c5d509d1fa.
2021-01-03 18:36:05 +01:00
Grigori Goronzy
c5d509d1fa Add "stc89a" protocol
This protocol variant is designed for newer STC89 series chips with BSL
version 7.x.x. The new firmware uses framing
with 16-bit checksum.

This protocol variant is currently untested because I don't have any
hardware at hand.

Addresses #50, #40.
2021-01-03 16:41:05 +01:00
Grigori Goronzy
0e020f2aa4 protocols: fix off-by-one in payload extraction
Drop all of the checksum bytes. This didn't take the end-of-frame marker
into account.
2021-01-03 16:10:05 +01:00
Grigori Goronzy
ce251f9d30 stc8: check length of status packet
Fixes a fuzzing error.
2018-08-21 13:17:14 +02:00
Grigori Goronzy
c7c4937628 stc8: remove cruft
Remove some old leftover cruft from porting and development. No
functional change intended.
2018-08-21 13:16:40 +02:00
Grigori Goronzy
ac119e180e stc8: round BRT value to nearest integer
Baud rate register (BRT) values need to be as accurate as possible, so
use nearest instead of floor. This should improve reliability at higher
programming speeds.
2018-08-21 13:16:40 +02:00
Grigori Goronzy
fcbc560ade 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.
2018-08-21 13:16:40 +02:00
Grigori Goronzy
fe60e647bf stc8: implement option handling
Implement option handling for STC8 series, based on STC8A8K64S4A12
reverse engineering.

This mostly wraps up all important parts of the STC8 implementation.

Interoperability was tested with STC-ISP V6.86O.

v2: update documentation
2018-08-21 02:01:39 +02:00
Grigori Goronzy
0ffcbd197b stc8: finish up frequency calibration
This is a collection of various changes:

* Also try divider of 5 (it's used by STC-ISP)
* Use the correct IAP delay for the 24 MHz programming frequency
* Also try a trim adjust value of 0x03 and adjust trim ranges
* Fix display of calibrated frequency
2018-08-20 23:47:42 +02:00
Grigori Goronzy
85e815366c stc8: try dividers 2 and 4 for frequency calibration 2018-06-27 01:10:46 +02:00
Grigori Goronzy
8bc9d89257 Add preliminary STC8 support
What works:
* Frequency calibration of internal RC oscillator
* Flash/EEPROM programming

What doesn't work yet:
* Everything else
2018-06-27 01:05:06 +02:00
Grigori Goronzy
d9e71a8694 Misc style and consistency fixes (NFC)
Various style, naming and other changes as recommended by pep8,
pyflakes and pylint.
2018-06-27 00:05:16 +02:00
Grigori Goronzy
4dcde5cc49 Clean up imports (NFC) 2018-06-27 00:05:16 +02:00
Jens J
c71e455f16
stc15f204ea should use protocol stc15a 2018-02-04 09:46:10 -06:00
Andrew Andrianov
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
Grigori Goronzy
030497beb0 Extract StcAutoProtocol class, fix autodetection
With the introduction of real abstract classes, it is not possible
anymore to instantiate StcBaseProtocol. Instead, extract some of the
code for autodetection into the new class StcAutoProtocol and use
that for autodetection.
2017-10-12 23:02:02 +02:00
Grigori Goronzy
1cde6da007 stc15: check that a UID has been received
Found by fuzzing. In some cases it's possible that we end up without
a valid UID. Detect and workaround.
2017-10-12 23:02:02 +02:00
Grigori Goronzy
ca30a508aa Fix various issues in frequency trimming
Found by fuzzing. The frequency trimming functions did a bad job of
checking for possible out of bounds accesses and didn't handle various
failure cases correctly. Add suitable checks to fix the issues found.

v2: fix one check, add several new ones
2017-10-12 23:01:50 +02:00
Grigori Goronzy
b9208c4772 Add length checks for status packets
Fuzzing found a number of issues when status packets are cut short.
Introduce checks on the length of status packets to fix these issues.
2017-10-11 23:20:20 +02:00
Grigori Goronzy
ad5a89297f Check length of responses
Fuzzing found lots of issues when packets are cut short. This should
rarely happen, but stcgal should be able to handle it without crashing.

This adds length checks when checking the magic of packets or when
checking checksums.
2017-10-11 23:20:20 +02:00
Grigori Goronzy
0cb56f4919 Use abc for StcBaseProtocol
Use the abc module to declare StcBaseProtocol as an abstract base
class and clean up imports while at it.
2017-10-11 23:20:20 +02:00
Andrew Andrianov
191a580469 protocols: Move device reset logic to a separate method
Signed-off-by: Andrew Andrianov <andrew@ncrmnt.org>
2017-10-08 23:20:17 +03:00
Andrew Andrianov
ba4faf9c43 Implement power-cycling via custom shell command
Sometimes instead of DTR line some custom way (e.g SoC gpio line)
may be used to reset the device. This commit implements
automated power-cycling using a a custom shell command that can
be specified via -r option

Signed-off-by: Andrew Andrianov <andrew@ncrmnt.org>
2017-10-07 22:41:49 +03:00
Grigori Goronzy
7e84b8e0fb Fix some additional code smells
No functional change intended.
2017-08-31 21:07:56 +02:00
Grigori Goronzy
f15b64f4f7 Fix some minor code smells reported by pylint
No functional change intended.
2017-06-16 10:21:43 +02:00
Grigori Goronzy
53f9544281 stc15: fix RC oscillator baudrate switch packet
It looks like it wasn't correct. The last value sent is probably
supposed to be the trim value for the chosen trim frequency.

Found while investigating grigorig/stcgal#16.
2016-06-10 12:45:58 +02:00
Grigori Goronzy
65a7759647 stc12+: drop checksum verification for flashing
It's not needed on STC12 and up. All transfers are error checked with
parity and a 16-bit modular sum already. STC15 dropped the verification
checksum on the protocol level, it's not sent with the write status
packet, which is a testament to this not being needed.

Some parts store the UID in the last bytes of flash memory and this
verification actually caused incorrect verification failures because
of the verification, which apparently read the UID on verification
readback.

Fixes grigorig/stcgal#15.
2016-05-28 11:25:44 +02:00
Grigori Goronzy
26ef34991b usb15: abort if permission denied
Don't ignore permission denied when looking for a suitable USB device.
Otherwise users don't notice what the problem is, stcgal will just
keep waiting on the prompt.
2016-05-20 02:55:23 +02:00
Grigori Goronzy
f90fe4152b Add STC12B protocol variant
This is just like STC12, but with the STC12A option packet. Used by
STC12xx52 series, STC12xx56 series and possibly others.

Fixes grigorig/stcgal#14.
2016-05-20 02:54:57 +02:00
Grigori Goronzy
d6ef028dc7 Extract mix-ins for STC12 and STC12A options
This simplifies code down the line. No functional change intended.
2016-05-20 02:44:50 +02:00
Grigori Goronzy
979d7f513f Get rid of USB bmRequestType constants
These constants were calculated on class loading, which didn't work
without pyusb. USB support is going to remain optional.
2016-05-18 16:40:09 +02:00
Grigori Goronzy
fce2f01232 Extract option classes into separate file 2016-05-18 02:38:13 +02:00
Grigori Goronzy
854f36100b stc12: fix option handling
MCS3 index was wrong. Also, write MCS3 to two possible indices in
the option set packet.

Addresses grigorig/stcgal#14.
2016-05-18 02:14:02 +02:00
Grigori Goronzy
92f4def11a stc12a: revamp option handling
Completely revamp option handling. This fixes a wrong index of MCS4,
which is now renamed to MCS3. Now programming should be exactly
similar to STC-ISP 6.85M.

This also renames the values of the low_voltage_reset option for
clarity.

Also update the documentation and clarity the meaning of the option
according to protocol family.

Addresses grigorig/stcgal#14.
2016-05-18 01:40:08 +02:00
Grigori Goronzy
61a4fa0e4f stc89: add missing output flush 2016-05-14 21:37:27 +02:00
Grigori Goronzy
da5f6678c5 usb15: sanity check on status packet reads
If a previous stcgal invocation is aborted, we can be in a bad state
upon connect. Add a simple sanity check based on packet length to
detect this and drop unsuitable packets.
2016-05-14 21:03:07 +02:00
Grigori Goronzy
81c890337e usb15: make USB support optional 2016-05-14 13:03:03 +02:00
Grigori Goronzy
9af984a191 Add USB-ISP support for STC15W4 series
The STC15W4 series have an additional USB BSL mode which kind of wraps
the serial protocol. It just uses basic USB control transfers. This
adds support for this mode, based on the existing STC15 code. pyusb is
required as an additional dependency. The new protocol mode is called
"usb15". There are some shortcomings, for instance timing of erase
and programming operations is problematic and right now only delays
are used. Also, only a single USB-ISP device is supported and the
first one found is always used.

Flash programming and setting options has been mildly tested on an
STC15W4K56S4 MCU.
2016-05-14 12:50:14 +02:00
Grigori Goronzy
2738118e8f stc15: extract build_options method
This will allow use to reuse code for upcoming USB-ISP support.
2016-05-14 12:43:15 +02:00
Grigori Goronzy
eedf9169a7 stc15: add error reporting for locked MCUs
It is possible to lock devices with a password against flashing. This
feature is not currently supported by stcgal, so at least output a
sensible message.
2016-05-13 02:54:43 +02:00
laborer
afba6c6805 Add automatic protocol detection
The model of the target MCU and its protocol is detected using model ID provided in the first packet (status packet) from the MCU.

Tested working models include,
IAP15F2K61S2 (BSL version: 7.1.4S, protocol: stc15)
STC15F104W (BSL version: 7.1.4Q, protocol: stc15)
TC11F02E (BSL version: 6.5K, protocol: stc12)
STC10F04XE (BSL version: 6.5J, protocol: stc12)
STC12C5A16S2 (BSL version: 6.2I, protocol: stc12)
STC12C5608AD (BSL version: 6.0G, protocol: stc12)
STC12C2052 (BSL version: 5.8D, protocol: stc12a)
STC90C52RC (BSL version: 4.3C, protocol: stc89)
STC89C52RC (BSL version: 4.3C, protocol: stc89)
STC89C54RD+ (BSL version: 4.3C, protocol: stc89)
STC15F104E (BSL version: 6.7Q, protocol: stc15a)

STC15F104E uses a different status packet protocol than other MCUs; it waits for an ACK packet before sending out the status packet.  Another problem is that STC15F104E shares the same model magic with STC15F104W.  Fortunately, these two models can be differentiated by their BSL version numbers.
2016-05-11 15:14:13 -04:00
Grigori Goronzy
372f854c77 Add autoreset feature via DTR control line
This is inspired by Arduino.  Directly before connect, DTR is asserted
for 0.5 seconds and then deasserted again.  With a small external
circuit, the MCU can be power-cycled with this control signal.
In case the MCU draws very little power, it may even be possible to
supply power directly from the DTR pin.
2016-04-02 23:00:07 +02:00
Grigori Goronzy
6a38127c0d Fix regression with some STC15 parts
The cpu core voltage setting is only available on newer parts, so do not
try to support it on older ones. The option packet is too short on some
parts, which resulted in an assertion hit.

There may be a nicer solution, but this works for now.

Fixes grigorig/stcgal#6.
2016-01-05 07:03:59 +01:00
Grigori Goronzy
d3911870c3 Fix initialization with CH340 UARTs
Initialization is messed up on older Linux kernels and that results
in 9600 baud being used unconditionally in some cases. Setting the
baud rate separately seems to work around this successfully.

Fixes grigorig/stcgal#5.
2015-12-15 20:22:25 +01:00
Grigori Goronzy
f13650a352 stc15: remove extra space in output 2015-12-11 01:50:55 +01:00
Grigori Goronzy
6f0ee0387d serial: flush input on connect
Sometimes there's some garbage in the input buffer and we aren't
interested in that.
2015-12-11 01:21:06 +01:00
Grigori Goronzy
76b3418f0a stc15: add core voltage option (STC15W)
This is used on STC15W4 series and has no function on earlier MCUs.
There is no good way to filter options, unfortunately.
2015-12-11 01:11:53 +01:00