From 5e8ea81e656170f78ed9c126ad857955d81cc414 Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Fri, 21 Jul 2017 20:29:03 +0200 Subject: [PATCH] Added jump to bootloader Added pwm to bus_pirate --- bootloader.py | 5 ++++- bus_pirate.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/bootloader.py b/bootloader.py index 04370cd..1f5707a 100755 --- a/bootloader.py +++ b/bootloader.py @@ -179,9 +179,12 @@ if __name__ == "__main__": usage = "usage: %prog [options] addr [ihex]" parser = OptionParser(usage=usage) parser.add_option("-b", "--start-bootloader", action="store_true", dest="bToBoot", default=False, help="Start Bootloader") + parser.add_option("-j", "--jump", action="store_true", dest="bJump", default=False, help="Jump to Program") (options, args) = parser.parse_args() - if len(args) > 0: + if not args: + print "Missing Address" + else: addr = int(args[0], 16) if options.bToBoot: to_bootloader(addr) diff --git a/bus_pirate.py b/bus_pirate.py index 4ef9c61..52a1c5c 100644 --- a/bus_pirate.py +++ b/bus_pirate.py @@ -64,6 +64,29 @@ class BP: v = struct.unpack(">h", s) return v[0]/1024.0*6.6 + # http://codepad.org/qtYpZmIF + def pwm(self, freq, duty_cycle): + lPrescaler = {0:1, 1:8 , 2:64, 3:256} + Fosc = 32e6 + Tcy = 2.0 / Fosc + period = 1.0 / freq + prescaler = 1 + + # find needed prescaler + for i in range(4): + prescaler = lPrescaler[i] + PRy = period * 1.0 / (Tcy * prescaler) + PRy = int(PRy - 1) + OCR = int(PRy * duty_cycle) + + if PRy < (2 ** 16 - 1): + break # valid value for PRy, keep values + + cmd = struct.pack(">BBHH", 0b00010010, prescaler, duty_cycle, period) + ret = self.command(cmd, 1) + if ord(ret) != 0x1: + raise Exception() + def spi_command(self, cmd): ret = self.command(cmd, 1) if ord(ret) != 0x1: -- 2.39.5