Merge pull request #62 from grigorig/fix-stc15f104w

stc15: fix baudrate switching
This commit is contained in:
Grigori Goronzy 2021-01-09 02:01:29 +01:00 committed by GitHub
commit 482e6b139f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View File

@ -20,7 +20,7 @@ So far, stcgal was tested with the following MCU models:
* STC15F104E (BSL version: 6.7Q) * STC15F104E (BSL version: 6.7Q)
* STC15F204EA (BSL version: 6.7R) * STC15F204EA (BSL version: 6.7R)
* STC15L104W (BSL version: 7.1.4Q) * STC15L104W (BSL version: 7.1.4Q)
* STC15F104W (BSL version: 7.1.4Q) * STC15F104W (BSL version: 7.1.4Q and 7.2.5Q)
* IAP15F2K61S2 (BSL version: 7.1.4S) * IAP15F2K61S2 (BSL version: 7.1.4S)
* STC15L2K16S2 (BSL version: 7.2.4S) * STC15L2K16S2 (BSL version: 7.2.4S)
* IAP15L2K61S2 (BSL version: 7.2.5S) * IAP15L2K61S2 (BSL version: 7.2.5S)

View File

@ -1394,16 +1394,19 @@ class Stc15Protocol(Stc15AProtocol):
# hardware UART. Only one family of models seems to lack a hardware # hardware UART. Only one family of models seems to lack a hardware
# UART, and we can isolate those with a check on the magic. # UART, and we can isolate those with a check on the magic.
# This is a bit of a hack, but it works. # This is a bit of a hack, but it works.
bauds = self.baud_transfer if (self.mcu_magic >> 8) == 0xf2 else self.baud_transfer * 4 if (self.mcu_magic >> 8) == 0xf2:
packet += struct.pack(">H", int(65535 - program_speed / bauds)) packet += struct.pack(">H", int(65536 - program_speed / self.baud_transfer))
packet += bytes(user_trim) packet += struct.pack(">H", int(65536 - program_speed / 2 * 3 / self.baud_transfer))
else:
packet += struct.pack(">H", int(65536 - program_speed / (self.baud_transfer * 4)))
packet += bytes(reversed(user_trim))
iap_wait = self.get_iap_delay(program_speed) iap_wait = self.get_iap_delay(program_speed)
packet += bytes([iap_wait]) packet += bytes([iap_wait])
self.write_packet(packet) self.write_packet(packet)
response = self.read_packet() response = self.read_packet()
if len(response) < 1 or response[0] != 0x01: if len(response) < 1 or response[0] != 0x01:
raise StcProtocolException("incorrect magic in handshake packet") raise StcProtocolException("incorrect magic in handshake packet")
time.sleep(0.2) time.sleep(0.1)
self.ser.baudrate = self.baud_transfer self.ser.baudrate = self.baud_transfer
def switch_baud_ext(self): def switch_baud_ext(self):
@ -1413,14 +1416,17 @@ class Stc15Protocol(Stc15AProtocol):
sys.stdout.flush() sys.stdout.flush()
packet = bytes([0x01]) packet = bytes([0x01])
packet += bytes([self.freq_count_24, 0x40]) packet += bytes([self.freq_count_24, 0x40])
packet += struct.pack(">H", int(65535 - self.mcu_clock_hz / self.baud_transfer / 4)) bauds = int(65536 - self.mcu_clock_hz / self.baud_transfer / 4)
if bauds >= 65536:
raise StcProtocolException("baudrate adjustment failed")
packet += struct.pack(">H", bauds)
iap_wait = self.get_iap_delay(self.mcu_clock_hz) iap_wait = self.get_iap_delay(self.mcu_clock_hz)
packet += bytes([0x00, 0x00, iap_wait]) packet += bytes([0x00, 0x00, iap_wait])
self.write_packet(packet) self.write_packet(packet)
response = self.read_packet() response = self.read_packet()
if len(response) < 1 or response[0] != 0x01: if len(response) < 1 or response[0] != 0x01:
raise StcProtocolException("incorrect magic in handshake packet") raise StcProtocolException("incorrect magic in handshake packet")
time.sleep(0.2) time.sleep(0.1)
self.ser.baudrate = self.baud_transfer self.ser.baudrate = self.baud_transfer
# for switching back to RC, program factory values # for switching back to RC, program factory values
@ -1664,7 +1670,7 @@ class Stc8Protocol(Stc15Protocol):
sys.stdout.flush() sys.stdout.flush()
packet = bytes([0x01, 0x00, 0x00]) packet = bytes([0x01, 0x00, 0x00])
bauds = self.baud_transfer * 4 bauds = self.baud_transfer * 4
packet += struct.pack(">H", round(65535 - 24E6 / bauds)) packet += struct.pack(">H", round(65536 - 24E6 / bauds))
packet += bytes([user_trim[1], user_trim[0]]) packet += bytes([user_trim[1], user_trim[0]])
iap_wait = self.get_iap_delay(24E6) iap_wait = self.get_iap_delay(24E6)
packet += bytes([iap_wait]) packet += bytes([iap_wait])