forked from bgamari/pirate-swd
-
Notifications
You must be signed in to change notification settings - Fork 9
/
flashEFM32.py
executable file
·60 lines (52 loc) · 1.79 KB
/
flashEFM32.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
import time
import sys
import array
from PirateSWD import *
from SWDCommon import *
from EFM32 import *
def loadFile(path):
arr = array.array('I')
try:
arr.fromfile(open(sys.argv[1], 'rb'), 1024*1024)
except EOFError:
pass
return arr.tolist()
def main():
busPirate = PirateSWD("/dev/tty.usbserial-buspirat", vreg = True)
debugPort = DebugPort(busPirate)
efm32 = EFM32(debugPort)
part_info = efm32.ahb.readWord(0x0FE081FC) # PART_NUMBER, PART_FAMILY, PROD_REV
mem_info = efm32.ahb.readWord(0x0FE081F8) # MEM_INFO_FLASH, MEM_INFO_RAM
rev = (efm32.ahb.readWord(0xE00FFFE8) & 0xF0) | ((efm32.ahb.readWord(0xE00FFFEC) & 0xF0) >> 4) # PID2 and PID3 - see section 7.3.4 in reference manual
rev = chr(rev + ord('A'))
flash_size = mem_info & 0xFFFF
if (part_info >> 16 & 0xFF) == 71:
print "Connected."
print "Part number: EFM32G%dF%d (rev %c, production ID %dd)" % (part_info & 0xFF,
flash_size, rev, part_info >> 24 & 0xFF)
else:
print "Warning: unknown part"
sys.exit()
print "Loading '%s'..." % sys.argv[1],
vals = loadFile(sys.argv[1])
size = len(vals) * 4
print "%d bytes." % size
if size / 1024.0 > flash_size:
print "Firmware will not fit into flash!"
sys.exit(1)
efm32.halt()
efm32.flashUnlock()
print "Erasing Flash...",
efm32.flashErase(flash_size)
start_time = time.time()
print "Programming Flash...",
efm32.flashProgram(vals)
time_passed = time.time() - start_time
print "Programmed %d bytes in %.2f seconds (%.2f kB/sec)." % (size,
time_passed, (size / 1024.0) / time_passed)
print "Resetting"
efm32.sysReset()
busPirate.tristatePins()
if __name__ == "__main__":
main()