annotate python/stlink.py @ 118:db8aafe00d27

Added elf file read scripts
author Windel Bouwman
date Sat, 12 Jan 2013 09:39:23 +0100
parents f2b37d78082d
children f51791638cae
rev   line source
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
1 import struct, time
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
2 from usb import UsbContext
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
3
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
4 class STLinkException(Exception):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
5 pass
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
6
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
7 ST_VID, STLINK2_PID = 0x0483, 0x3748
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
8
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
9 def checkDevice(device):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
10 return device.VendorId == ST_VID and device.ProductId == STLINK2_PID
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
11
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
12 DFU_MODE, MASS_MODE, DEBUG_MODE = 0, 1, 2
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
13
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
14 CORE_RUNNING = 0x80
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
15 CORE_HALTED = 0x81
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
16
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
17 # Commands:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
18 GET_VERSION = 0xf1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
19 DEBUG_COMMAND = 0xf2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
20 DFU_COMMAND = 0xf3
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
21 GET_CURRENT_MODE = 0xf5
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
22
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
23 # dfu commands:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
24 DFU_EXIT = 0x7
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
25
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
26 # debug commands:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
27 DEBUG_ENTER = 0x20
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
28 DEBUG_EXIT = 0x21
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
29 DEBUG_ENTER_SWD = 0xa3
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
30 DEBUG_GETSTATUS = 0x01
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
31 DEBUG_RESETSYS = 0x03
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
32 DEBUG_READREG = 0x5
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
33 DEBUG_WRITEREG = 0x6
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
34 DEBUG_READMEM_32BIT = 0x7
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
35 DEBUG_WRITEMEM_32BIT = 0x8
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
36 DEBUG_RUNCORE = 0x9
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
37 DEBUG_STEPCORE = 0xa
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
38
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
39 JTAG_WRITEDEBUG_32BIT = 0x35
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
40 JTAG_READDEBUG_32BIT = 0x36
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
41
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
42 # cortex M3
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
43 CM3_REG_CPUID = 0xE000ED00
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
44
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
45 # F4 specifics:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
46 STM32_FLASH_BASE = 0x08000000
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
47 STM32_SRAM_BASE = 0x20000000
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
48
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
49 FLASH_KEY1 = 0x45670123
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
50 FLASH_KEY2 = 0xcdef89ab
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
51 # flash registers:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
52 FLASH_F4_REGS_ADDR = 0x40023c00
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
53
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
54 FLASH_F4_KEYR = FLASH_F4_REGS_ADDR + +0x04
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
55 FLASH_F4_SR = FLASH_F4_REGS_ADDR + 0x0c
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
56 FLASH_F4_CR = FLASH_F4_REGS_ADDR + 0x10
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
57
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
58 FLASH_F4_CR_START = 16
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
59 FLASH_F4_CR_LOCK = 31
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
60 FLASH_CR_PG = 0
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
61 FLASH_F4_CR_SER = 1
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
62 FLASH_CR_MER = 2
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
63 FLASH_F4_CR_SNB = 3
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
64 FLASH_F4_CR_SNB_MASK = 0x38
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
65 FLASH_F4_SR_BSY = 16
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
66
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
67 # flashloaders/stm32f4.s
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
68 loader_code_stm32f4 = bytes([
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
69 0x07, 0x4b,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
70
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
71 0x62, 0xb1,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
72 0x04, 0x68,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
73 0x0c, 0x60,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
74
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
75 0xdc, 0x89,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
76 0x14, 0xf0, 0x01, 0x0f,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
77 0xfb, 0xd1,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
78 0x00, 0xf1, 0x04, 0x00,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
79 0x01, 0xf1, 0x04, 0x01,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
80 0xa2, 0xf1, 0x01, 0x02,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
81 0xf1, 0xe7,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
82
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
83 0x00, 0xbe,
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
84
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
85 0x00, 0x3c, 0x02, 0x40
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
86 ])
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
87
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
88 def calculate_F4_sector(address):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
89 """
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
90 from 0x8000000 to 0x80FFFFF
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
91 4 sectors of 0x4000 (16 kB)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
92 1 sector of 0x10000 (64 kB)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
93 7 of 0x20000 (128 kB)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
94 """
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
95 sectorsizes = [0x4000] * 4 + [0x10000] + [0x20000] * 7
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
96 sectorstarts = []
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
97 a = STM32_FLASH_BASE
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
98 for sectorsize in sectorsizes:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
99 sectorstarts.append(a)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
100 a += sectorsize
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
101 # linear search:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
102 sec = 0
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
103 while sec < len(sectorsizes) and address >= sectorstarts[sec]:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
104 sec += 1
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
105 sec -= 1 # one back.
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
106 return sec, sectorsizes[sec]
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
107
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
108 def calcSectors(address, size):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
109 off = 0
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
110 sectors = []
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
111 while off < size:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
112 sectornum, sectorsize = calculate_F4_sector(address + off)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
113 #print('num: {0} size: {1:X} offset: {2}'.format(sectornum, sectorsize, off))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
114 sectors.append((sectornum, sectorsize))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
115 off += sectorsize
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
116 return sectors
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
117
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
118 class STLink:
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
119 def __init__(self):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
120 self.context = UsbContext()
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
121 def open(self):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
122 context = UsbContext()
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
123 stlink2s = list(filter(checkDevice, context.DeviceList))
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
124 if not stlink2s:
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
125 raise STLinkException('Could not find an ST link')
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
126 if len(stlink2s) > 1:
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
127 print('More then one stlink2 found, picking first one')
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
128 stlink2 = stlink2s[0]
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
129 self.devHandle = stlink2.open()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
130 if self.devHandle.Configuration != 1:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
131 self.devHandle.Configuration = 1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
132 self.devHandle.claimInterface(0)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
133
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
134 # First initialization:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
135 if self.CurrentMode == DFU_MODE:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
136 self.exitDfuMode()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
137 if self.CurrentMode != DEBUG_MODE:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
138 self.enterSwdMode()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
139 self.reset()
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
140 def close(self):
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
141 # TODO
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
142 pass
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
143
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
144 # modes:
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
145 def getCurrentMode(self):
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
146 cmd = bytearray(16)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
147 cmd[0] = GET_CURRENT_MODE
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
148 reply = self.send_recv(cmd, 2) # Expect 2 bytes back
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
149 return reply[0]
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
150 CurrentMode = property(getCurrentMode)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
151 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
152 def CurrentModeString(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
153 modes = {DFU_MODE: 'dfu', MASS_MODE: 'massmode', DEBUG_MODE:'debug'}
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
154 return modes[self.CurrentMode]
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
155 def exitDfuMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
156 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
157 cmd[0:2] = DFU_COMMAND, DFU_EXIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
158 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
159 def enterSwdMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
160 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
161 cmd[0:3] = DEBUG_COMMAND, DEBUG_ENTER, DEBUG_ENTER_SWD
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
162 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
163 def exitDebugMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
164 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
165 cmd[0:2] = DEBUG_COMMAND, DEBUG_EXIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
166 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
167
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
168 def getVersion(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
169 cmd = bytearray(16)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
170 cmd[0] = GET_VERSION
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
171 data = self.send_recv(cmd, 6) # Expect 6 bytes back
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
172 # Parse 6 bytes into various versions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
173 b0, b1, b2, b3, b4, b5 = data
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
174 stlink_v = b0 >> 4
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
175 jtag_v = ((b0 & 0xf) << 2) | (b1 >> 6)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
176 swim_v = b1 & 0x3f
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
177 vid = (b3 << 8) | b2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
178 pid = (b5 << 8) | b4
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
179
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
180 return 'stlink={0} jtag={1} swim={2} vid:pid={3:04X}:{4:04X}'.format(\
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
181 stlink_v, jtag_v, swim_v, vid, pid)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
182 Version = property(getVersion)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
183
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
184 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
185 def ChipId(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
186 return self.read_debug32(0xE0042000)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
187 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
188 def CpuId(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
189 u32 = self.read_debug32(CM3_REG_CPUID)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
190 implementer_id = (u32 >> 24) & 0x7f
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
191 variant = (u32 >> 20) & 0xf
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
192 part = (u32 >> 4) & 0xfff
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
193 revision = u32 & 0xf
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
194 return implementer_id, variant, part, revision
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
195
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
196 def getStatus(self):
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
197 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
198 cmd[0:2] = DEBUG_COMMAND, DEBUG_GETSTATUS
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
199 reply = self.send_recv(cmd, 2)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
200 return reply[0]
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
201 Status = property(getStatus)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
202 @property
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
203 def StatusString(self):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
204 s = self.Status
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
205 statii = {CORE_RUNNING: 'CORE RUNNING', CORE_HALTED: 'CORE HALTED'}
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
206 if s in statii:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
207 return statii[s]
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
208
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
209 def reset(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
210 cmd = bytearray(16)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
211 cmd[0:2] = DEBUG_COMMAND, DEBUG_RESETSYS
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
212 self.send_recv(cmd, 2)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
213
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
214 # debug commands:
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
215 def step(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
216 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
217 cmd[0:2] = DEBUG_COMMAND, DEBUG_STEPCORE
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
218 self.send_recv(cmd, 2)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
219 def run(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
220 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
221 cmd[0:2] = DEBUG_COMMAND, DEBUG_RUNCORE
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
222 self.send_recv(cmd, 2)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
223
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
224 # flashing commands:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
225 def writeFlash(self, address, content):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
226 # TODO:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
227 flashsize = 0x100000 # fixed 1 MB for now..
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
228 print('WARNING: using 1 MB as flash size')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
229 pagesize = 0x4000 # fixed for now!
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
230
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
231 # Check address range:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
232 if address < STM32_FLASH_BASE:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
233 raise STLinkException('Flashing below flash start')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
234 if address + len(content) > STM32_FLASH_BASE + flashsize:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
235 raise STLinkException('Flashing above flash size')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
236 if address & 1 == 1:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
237 raise STLinkException('Unaligned flash')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
238 if len(content) & 1 == 1:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
239 print('unaligned length, padding with zero')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
240 content += bytes([0])
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
241 if address & (pagesize - 1) != 0:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
242 raise STLinkException('Address not aligned with pagesize')
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
243
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
244 # erase required space
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
245 sectors = calcSectors(address, len(content))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
246 print('erasing {0} sectors'.format(len(sectors)))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
247 for sector, secsize in sectors:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
248 print('erasing sector {0} of size {1}'.format(sector, secsize))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
249 self.eraseFlashSector(sector)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
250
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
251 # program pages:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
252 self.initFlashLoader()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
253 self.unlockFlashIf()
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
254 self.writeFlashCrPsiz(2)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
255 self.setFlashCrPg()
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
256
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
257 offset = 0
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
258 while offset < len(content):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
259 size = len(content) - offset
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
260 if size > 0x8000:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
261 size = 0x8000
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
262
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
263 self.runFlashLoader(address + offset, content[offset:offset + size])
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
264 offset += size
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
265
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
266 self.lockFlash()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
267
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
268 # verfify program:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
269 self.verifyFlash(address, content)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
270 def eraseFlashSector(self, sector):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
271 self.waitFlashBusy()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
272 self.unlockFlashIf()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
273 self.writeFlashCrSnb(sector)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
274 self.setFlashCrStart()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
275 self.waitFlashBusy()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
276 self.lockFlash()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
277 def eraseFlash(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
278 self.waitFlashBusy()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
279 self.unlockFlashIf()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
280 self.setFlashCrMer()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
281 self.setFlashCrStart()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
282 self.waitFlashBusy()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
283 self.clearFlashCrMer()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
284 self.lockFlash()
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
285 def verifyFlash(self, address, content):
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
286 device_content = self.readFlash(address, len(content))
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
287 ok = content == device_content
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
288 print('Verify:', ok)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
289 def readFlash(self, address, size):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
290 print('reading', address, size)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
291 offset = 0
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
292 tmp_size = 0x1800
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
293 t1 = time.time()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
294 image = bytes()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
295 while offset < size:
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
296 # Correct for last page:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
297 if offset + tmp_size > size:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
298 tmp_size = size - offset
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
299
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
300 # align size to 4 bytes:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
301 aligned_size = tmp_size
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
302 while aligned_size % 4 != 0:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
303 aligned_size += 1
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
304
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
305 mem = self.read_mem32(address + offset, aligned_size)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
306 image += mem[:tmp_size]
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
307
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
308 # indicate progress:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
309 print('.', end='', flush=True)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
310
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
311 # increase for next piece:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
312 offset += tmp_size
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
313 t2 = time.time()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
314 assert size == len(image)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
315 print('done! {0} bytes/second'.format(size/(t2-t1)))
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
316 return image
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
317 def initFlashLoader(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
318 # TODO: support other loader code.
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
319 self.write_mem32(STM32_SRAM_BASE, loader_code_stm32f4)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
320 def runFlashLoader(self, targetaddress, buf):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
321 bufAddress = STM32_SRAM_BASE + len(loader_code_stm32f4)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
322 print('running flash loader for {0} bytes'.format(len(buf)))
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
323 self.write_buffer_to_sram(buf)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
324 count = int(len(buf) / 4)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
325 if len(buf) % 4 != 0: count += 1
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
326 self.write_reg(0, bufAddress)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
327 self.write_reg(1, targetaddress)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
328 self.write_reg(2, count)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
329 self.write_reg(15, STM32_SRAM_BASE) # pc
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
330
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
331 self.run() # Let loose cpu!
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
332
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
333 while self.Status == CORE_RUNNING:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
334 pass
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
335
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
336 r2 = self.read_reg(2)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
337 if r2 != 0:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
338 raise STLinkException("write error! {0}".format(r2))
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
339 def write_buffer_to_sram(self, buf):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
340 bufAddress = STM32_SRAM_BASE + len(loader_code_stm32f4)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
341 chunk = len(buf) >> 2
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
342 rem = len(buf) & 0x3
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
343 if chunk > 0:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
344 self.write_mem32(bufAddress, buf[:chunk])
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
345 if rem > 0:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
346 self.write_mem8(bufAddress + chunk, buf[chunk:chunk+rem])
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
347
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
348 def readFlashSr(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
349 return self.read_debug32(FLASH_F4_SR)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
350 def readFlashCr(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
351 return self.read_debug32(FLASH_F4_CR)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
352 def writeFlashCrSnb(self, sector):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
353 x = self.readFlashCr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
354 x &= ~FLASH_F4_CR_SNB_MASK
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
355 x |= sector << FLASH_F4_CR_SNB
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
356 x |= 1 << FLASH_F4_CR_SER
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
357 self.write_debug32(FLASH_F4_CR, x)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
358 def setFlashCrMer(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
359 x = self.readFlashCr()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
360 x |= 1 << FLASH_CR_MER
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
361 self.write_debug32(FLASH_F4_CR, x)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
362 def setFlashCrPg(self):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
363 x = self.readFlashCr()
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
364 x |= 1 << FLASH_CR_PG
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
365 self.write_debug32(FLASH_F4_CR, x)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
366 def writeFlashCrPsiz(self, n):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
367 x = self.readFlashCr()
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
368 x &= (0x3 << 8)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
369 x |= n << 8
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
370 print('psiz', n)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
371 self.write_debug32(FLASH_F4_CR, x)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
372 def clearFlashCrMer(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
373 x = self.readFlashCr()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
374 x &= ~(1 << FLASH_CR_MER)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
375 self.write_debug32(FLASH_F4_CR, x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
376 def setFlashCrStart(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
377 x = self.readFlashCr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
378 x |= 1 << FLASH_F4_CR_START
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
379 self.write_debug32(FLASH_F4_CR, x)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
380 def isFlashBusy(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
381 mask = 1 << FLASH_F4_SR_BSY
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
382 sr = self.readFlashSr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
383 return sr & mask == mask
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
384 def waitFlashBusy(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
385 """ block until flash operation completes. """
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
386 while self.isFlashBusy():
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
387 pass
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
388 def isFlashLocked(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
389 cr = self.readFlashCr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
390 mask = 1 << FLASH_F4_CR_LOCK
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
391 return cr & mask == mask
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
392 def unlockFlashIf(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
393 if self.isFlashLocked():
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
394 print('unlocking')
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
395 self.write_debug32(FLASH_F4_KEYR, FLASH_KEY1)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
396 self.write_debug32(FLASH_F4_KEYR, FLASH_KEY2)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
397 if self.isFlashLocked():
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
398 raise STLinkException('Failed to unlock')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
399 def lockFlash(self):
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
400 print('locking')
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
401 x = self.readFlashCr() | (1 << FLASH_F4_CR_LOCK)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
402 self.write_debug32(FLASH_F4_CR, x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
403
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
404 # Helper 1 functions:
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
405 def write_debug32(self, address, value):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
406 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
407 cmd[0:2] = DEBUG_COMMAND, JTAG_WRITEDEBUG_32BIT
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
408 cmd[2:6] = struct.pack('<I', address)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
409 cmd[6:10] = struct.pack('<I', value)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
410 self.send_recv(cmd, 2)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
411 def read_debug32(self, address):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
412 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
413 cmd[0:2] = DEBUG_COMMAND, JTAG_READDEBUG_32BIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
414 cmd[2:6] = struct.pack('<I', address) # pack into u32 little endian
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
415 reply = self.send_recv(cmd, 8)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
416 return struct.unpack('<I', reply[4:8])[0]
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
417 def write_reg(self, reg, value):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
418 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
419 cmd[0:3] = DEBUG_COMMAND, DEBUG_WRITEREG, reg
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
420 cmd[3:7] = struct.pack('<I', value)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
421 r = self.send_recv(cmd, 2)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
422 def read_reg(self, reg):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
423 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
424 cmd[0:3] = DEBUG_COMMAND, DEBUG_READREG, reg
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
425 reply = self.send_recv(cmd, 4)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
426 return struct.unpack('<I', reply)[0]
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
427 def write_mem32(self, address, content):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
428 assert len(content) % 4 == 0
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
429 cmd = bytearray(16)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
430 cmd[0:2] = DEBUG_COMMAND, DEBUG_WRITEMEM_32BIT
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
431 cmd[2:6] = struct.pack('<I', address)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
432 cmd[6:8] = struct.pack('<H', len(content))
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
433 self.send_recv(cmd)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
434 self.send_recv(content)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
435 def read_mem32(self, address, length):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
436 assert length % 4 == 0
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
437 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
438 cmd[0:2] = DEBUG_COMMAND, DEBUG_READMEM_32BIT
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
439 cmd[2:6] = struct.pack('<I', address)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
440 cmd[6:8] = struct.pack('<H', length) # uint16
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
441 reply = self.send_recv(cmd, length) # expect memory back!
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
442 return reply
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
443
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
444 # Helper 2 functions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
445 def send_recv(self, tx, rxsize=0):
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
446 """ Helper function that transmits and receives data in bulk mode. """
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
447 # TODO: we could use here the non-blocking libusb api.
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
448 tx = bytes(tx)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
449 #assert len(tx) == 16
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
450 self.devHandle.bulkWrite(2, tx) # write to endpoint 2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
451 if rxsize > 0:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
452 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
453
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
454 knownChipIds = {0x1: 'x'}
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
455
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
456 if __name__ == '__main__':
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
457 # Test program
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
458 sl = STLink()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
459 sl.open()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
460 print('version:', sl.Version)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
461 print('mode before doing anything:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
462 if sl.CurrentMode == DFU_MODE:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
463 sl.exitDfuMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
464 sl.enterSwdMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
465 print('mode after entering swd mode:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
466
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
467 i = sl.ChipId
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
468 if i in knownChipIds:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
469 print('chip id: 0x{0:X} -> {1}'.format(i, knownChipIds[i]))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
470 else:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
471 print('chip id: 0x{0:X}'.format(i))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
472 print('cpu: {0}'.format(sl.CpuId))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
473
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
474 print('status: {0}'.format(sl.StatusString))
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
475
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
476 #time.sleep(2.2)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
477
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
478 # test registers:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
479 sl.write_reg(3, 0x1337)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
480 sl.write_reg(2, 0x1332)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
481 sl.write_reg(6, 0x12345)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
482 assert sl.read_reg(3) == 0x1337
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
483 assert sl.read_reg(2) == 0x1332
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
484 assert sl.read_reg(6) == 0x12345
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
485
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
486 sl.exitDebugMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
487 print('mode at end:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
488
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
489 sl.close()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
490