annotate python/stlink.py @ 120:bb08633510bf

Added repostat script
author Windel Bouwman
date Sat, 12 Jan 2013 10:56:12 +0100
parents f51791638cae
children 347d7d8b96c0
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:
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
119 """ ST link interface code """
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
120 def __init__(self, stlink2=None):
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
121 if not stlink2:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
122 context = UsbContext()
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
123 stlink2s = list(filter(checkDevice, context.DeviceList))
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
124 if not stlink2s:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
125 raise STLinkException('Could not find an ST link')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
126 if len(stlink2s) > 1:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
127 print('More then one stlink2 found, picking first one')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
128 stlink2 = stlink2s[0]
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
129 self.stlink2 = stlink2
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
130 def open(self):
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
131 self.devHandle = self.stlink2.open()
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
132 if self.devHandle.Configuration != 1:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
133 self.devHandle.Configuration = 1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
134 self.devHandle.claimInterface(0)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
135
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
136 # First initialization:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
137 if self.CurrentMode == DFU_MODE:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
138 self.exitDfuMode()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
139 if self.CurrentMode != DEBUG_MODE:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
140 self.enterSwdMode()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
141 self.reset()
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
142 def close(self):
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
143 # TODO
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
144 pass
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
145
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
146 # modes:
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
147 def getCurrentMode(self):
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
148 cmd = bytearray(16)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
149 cmd[0] = GET_CURRENT_MODE
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
150 reply = self.send_recv(cmd, 2) # Expect 2 bytes back
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
151 return reply[0]
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
152 CurrentMode = property(getCurrentMode)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
153 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
154 def CurrentModeString(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
155 modes = {DFU_MODE: 'dfu', MASS_MODE: 'massmode', DEBUG_MODE:'debug'}
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
156 return modes[self.CurrentMode]
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
157 def exitDfuMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
158 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
159 cmd[0:2] = DFU_COMMAND, DFU_EXIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
160 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
161 def enterSwdMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
162 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
163 cmd[0:3] = DEBUG_COMMAND, DEBUG_ENTER, DEBUG_ENTER_SWD
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
164 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
165 def exitDebugMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
166 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
167 cmd[0:2] = DEBUG_COMMAND, DEBUG_EXIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
168 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
169
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
170 def getVersion(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
171 cmd = bytearray(16)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
172 cmd[0] = GET_VERSION
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
173 data = self.send_recv(cmd, 6) # Expect 6 bytes back
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
174 # Parse 6 bytes into various versions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
175 b0, b1, b2, b3, b4, b5 = data
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
176 stlink_v = b0 >> 4
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
177 jtag_v = ((b0 & 0xf) << 2) | (b1 >> 6)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
178 swim_v = b1 & 0x3f
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
179 vid = (b3 << 8) | b2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
180 pid = (b5 << 8) | b4
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
181
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
182 return 'stlink={0} jtag={1} swim={2} vid:pid={3:04X}:{4:04X}'.format(\
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
183 stlink_v, jtag_v, swim_v, vid, pid)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
184 Version = property(getVersion)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
185
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
186 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
187 def ChipId(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
188 return self.read_debug32(0xE0042000)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
189 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
190 def CpuId(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
191 u32 = self.read_debug32(CM3_REG_CPUID)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
192 implementer_id = (u32 >> 24) & 0x7f
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
193 variant = (u32 >> 20) & 0xf
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
194 part = (u32 >> 4) & 0xfff
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
195 revision = u32 & 0xf
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
196 return implementer_id, variant, part, revision
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
197
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
198 def getStatus(self):
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
199 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
200 cmd[0:2] = DEBUG_COMMAND, DEBUG_GETSTATUS
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
201 reply = self.send_recv(cmd, 2)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
202 return reply[0]
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
203 Status = property(getStatus)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
204 @property
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
205 def StatusString(self):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
206 s = self.Status
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
207 statii = {CORE_RUNNING: 'CORE RUNNING', CORE_HALTED: 'CORE HALTED'}
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
208 if s in statii:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
209 return statii[s]
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
210
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
211 def reset(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
212 cmd = bytearray(16)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
213 cmd[0:2] = DEBUG_COMMAND, DEBUG_RESETSYS
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
214 self.send_recv(cmd, 2)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
215
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
216 # debug commands:
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
217 def step(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
218 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
219 cmd[0:2] = DEBUG_COMMAND, DEBUG_STEPCORE
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
220 self.send_recv(cmd, 2)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
221 def run(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
222 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
223 cmd[0:2] = DEBUG_COMMAND, DEBUG_RUNCORE
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
224 self.send_recv(cmd, 2)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
225
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
226 # flashing commands:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
227 def writeFlash(self, address, content):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
228 # TODO:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
229 flashsize = 0x100000 # fixed 1 MB for now..
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
230 print('WARNING: using 1 MB as flash size')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
231 pagesize = 0x4000 # fixed for now!
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
232
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
233 # Check address range:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
234 if address < STM32_FLASH_BASE:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
235 raise STLinkException('Flashing below flash start')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
236 if address + len(content) > STM32_FLASH_BASE + flashsize:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
237 raise STLinkException('Flashing above flash size')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
238 if address & 1 == 1:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
239 raise STLinkException('Unaligned flash')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
240 if len(content) & 1 == 1:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
241 print('unaligned length, padding with zero')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
242 content += bytes([0])
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
243 if address & (pagesize - 1) != 0:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
244 raise STLinkException('Address not aligned with pagesize')
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
245
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
246 # erase required space
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
247 sectors = calcSectors(address, len(content))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
248 print('erasing {0} sectors'.format(len(sectors)))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
249 for sector, secsize in sectors:
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
250 print('erasing sector {0} of {1} bytes'.format(sector, secsize))
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
251 self.eraseFlashSector(sector)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
252
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
253 # program pages:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
254 self.unlockFlashIf()
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
255 self.writeFlashCrPsiz(2) # writes are 32 bits aligned
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
256 self.setFlashCrPg()
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
257
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
258 print('writing {0} bytes'.format(len(content)), end='')
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
259 offset = 0
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
260 t1 = time.time()
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
261 while offset < len(content):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
262 size = len(content) - offset
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
263 if size > 0x8000:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
264 size = 0x8000
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
265
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
266 chunk = content[offset:offset + size]
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
267 while len(chunk) % 4 != 0:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
268 print('padding chunk')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
269 chunk = chunk + bytes([0])
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
270
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
271 # Use simple mem32 writes:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
272 self.write_mem32(address + offset, chunk)
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
273
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
274 offset += size
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
275 print('.', end='', flush=True)
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
276 t2 = time.time()
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
277 print('Done!')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
278 print('Speed: {0} bytes/second'.format(len(content)/(t2-t1)))
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
279
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
280 self.lockFlash()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
281
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
282 # verfify program:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
283 self.verifyFlash(address, content)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
284 def eraseFlashSector(self, sector):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
285 self.waitFlashBusy()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
286 self.unlockFlashIf()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
287 self.writeFlashCrSnb(sector)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
288 self.setFlashCrStart()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
289 self.waitFlashBusy()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
290 self.lockFlash()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
291 def eraseFlash(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
292 self.waitFlashBusy()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
293 self.unlockFlashIf()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
294 self.setFlashCrMer()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
295 self.setFlashCrStart()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
296 self.waitFlashBusy()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
297 self.clearFlashCrMer()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
298 self.lockFlash()
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
299 def verifyFlash(self, address, content):
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
300 device_content = self.readFlash(address, len(content))
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
301 ok = content == device_content
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
302 print('Verify:', ok)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
303 def readFlash(self, address, size):
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
304 print('Reading {1} bytes from 0x{0:X}'.format(address, size), end='')
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
305 offset = 0
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
306 tmp_size = 0x1800
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
307 t1 = time.time()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
308 image = bytes()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
309 while offset < size:
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
310 # Correct for last page:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
311 if offset + tmp_size > size:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
312 tmp_size = size - offset
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
313
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
314 # align size to 4 bytes:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
315 aligned_size = tmp_size
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
316 while aligned_size % 4 != 0:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
317 aligned_size += 1
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
318
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
319 mem = self.read_mem32(address + offset, aligned_size)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
320 image += mem[:tmp_size]
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
321
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
322 # indicate progress:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
323 print('.', end='', flush=True)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
324
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
325 # increase for next piece:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
326 offset += tmp_size
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
327 t2 = time.time()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
328 assert size == len(image)
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
329 print('Done!')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
330 print('Speed: {0} bytes/second'.format(size/(t2-t1)))
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
331 return image
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
332 def readFlashSr(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
333 return self.read_debug32(FLASH_F4_SR)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
334 def readFlashCr(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
335 return self.read_debug32(FLASH_F4_CR)
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
336 def writeFlashCr(self, x):
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
337 self.write_debug32(FLASH_F4_CR, x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
338 def writeFlashCrSnb(self, sector):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
339 x = self.readFlashCr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
340 x &= ~FLASH_F4_CR_SNB_MASK
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
341 x |= sector << FLASH_F4_CR_SNB
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
342 x |= 1 << FLASH_F4_CR_SER
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
343 self.writeFlashCr(x)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
344 def setFlashCrMer(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
345 x = self.readFlashCr()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
346 x |= 1 << FLASH_CR_MER
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
347 self.writeFlashCr(x)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
348 def setFlashCrPg(self):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
349 x = self.readFlashCr()
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
350 x |= 1 << FLASH_CR_PG
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
351 self.writeFlashCr(x)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
352 def writeFlashCrPsiz(self, n):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
353 x = self.readFlashCr()
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
354 x &= (0x3 << 8)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
355 x |= n << 8
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
356 self.writeFlashCr(x)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
357 def clearFlashCrMer(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
358 x = self.readFlashCr()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
359 x &= ~(1 << FLASH_CR_MER)
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
360 self.writeFlashCr(x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
361 def setFlashCrStart(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
362 x = self.readFlashCr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
363 x |= 1 << FLASH_F4_CR_START
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
364 self.writeFlashCr(x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
365 def isFlashBusy(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
366 mask = 1 << FLASH_F4_SR_BSY
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
367 sr = self.readFlashSr()
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
368 # Check for error bits:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
369 errorbits = {}
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
370 errorbits[7] = 'Programming sequence error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
371 errorbits[6] = 'Programming parallelism error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
372 errorbits[5] = 'Programming alignment error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
373 errorbits[4] = 'Write protection error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
374 errorbits[1] = 'Operation error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
375 #errorbits[0] = 'End of operation'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
376 for bit, msg in errorbits.items():
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
377 if sr & (1 << bit) == (1 << bit):
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
378 raise STLinkException(msg)
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
379
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
380 return sr & mask == mask
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
381 def waitFlashBusy(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
382 """ block until flash operation completes. """
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
383 while self.isFlashBusy():
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
384 pass
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
385 def isFlashLocked(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
386 cr = self.readFlashCr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
387 mask = 1 << FLASH_F4_CR_LOCK
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
388 return cr & mask == mask
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
389 def unlockFlashIf(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
390 if self.isFlashLocked():
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
391 self.write_debug32(FLASH_F4_KEYR, FLASH_KEY1)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
392 self.write_debug32(FLASH_F4_KEYR, FLASH_KEY2)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
393 if self.isFlashLocked():
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
394 raise STLinkException('Failed to unlock')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
395 def lockFlash(self):
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
396 x = self.readFlashCr() | (1 << FLASH_F4_CR_LOCK)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
397 self.write_debug32(FLASH_F4_CR, x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
398
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
399 # Helper 1 functions:
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
400 def write_debug32(self, address, value):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
401 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
402 cmd[0:2] = DEBUG_COMMAND, JTAG_WRITEDEBUG_32BIT
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
403 cmd[2:10] = struct.pack('<II', address, value)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
404 self.send_recv(cmd, 2)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
405 def read_debug32(self, address):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
406 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
407 cmd[0:2] = DEBUG_COMMAND, JTAG_READDEBUG_32BIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
408 cmd[2:6] = struct.pack('<I', address) # pack into u32 little endian
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
409 reply = self.send_recv(cmd, 8)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
410 return struct.unpack('<I', reply[4:8])[0]
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
411 def write_reg(self, reg, value):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
412 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
413 cmd[0:3] = DEBUG_COMMAND, DEBUG_WRITEREG, reg
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
414 cmd[3:7] = struct.pack('<I', value)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
415 r = self.send_recv(cmd, 2)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
416 def read_reg(self, reg):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
417 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
418 cmd[0:3] = DEBUG_COMMAND, DEBUG_READREG, reg
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
419 reply = self.send_recv(cmd, 4)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
420 return struct.unpack('<I', reply)[0]
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
421 def write_mem32(self, address, content):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
422 assert len(content) % 4 == 0
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
423 cmd = bytearray(16)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
424 cmd[0:2] = DEBUG_COMMAND, DEBUG_WRITEMEM_32BIT
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
425 cmd[2:8] = struct.pack('<IH', address, len(content))
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
426 self.send_recv(cmd)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
427 self.send_recv(content)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
428 def read_mem32(self, address, length):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
429 assert length % 4 == 0
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
430 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
431 cmd[0:2] = DEBUG_COMMAND, DEBUG_READMEM_32BIT
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
432 cmd[2:8] = struct.pack('<IH', address, length)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
433 reply = self.send_recv(cmd, length) # expect memory back!
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
434 return reply
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
435
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
436 # Helper 2 functions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
437 def send_recv(self, tx, rxsize=0):
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
438 """ Helper function that transmits and receives data in bulk mode. """
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
439 # TODO: we could use here the non-blocking libusb api.
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
440 tx = bytes(tx)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
441 #assert len(tx) == 16
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
442 self.devHandle.bulkWrite(2, tx) # write to endpoint 2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
443 if rxsize > 0:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
444 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
445
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
446 knownChipIds = {0x1: 'x'}
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
447
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
448 if __name__ == '__main__':
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
449 # Test program
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
450 sl = STLink()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
451 sl.open()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
452 print('version:', sl.Version)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
453 print('mode before doing anything:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
454 if sl.CurrentMode == DFU_MODE:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
455 sl.exitDfuMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
456 sl.enterSwdMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
457 print('mode after entering swd mode:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
458
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
459 i = sl.ChipId
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
460 if i in knownChipIds:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
461 print('chip id: 0x{0:X} -> {1}'.format(i, knownChipIds[i]))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
462 else:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
463 print('chip id: 0x{0:X}'.format(i))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
464 print('cpu: {0}'.format(sl.CpuId))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
465
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
466 print('status: {0}'.format(sl.StatusString))
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
467
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
468 #time.sleep(2.2)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
469
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
470 # test registers:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
471 sl.write_reg(3, 0x1337)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
472 sl.write_reg(2, 0x1332)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
473 sl.write_reg(6, 0x12345)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
474 assert sl.read_reg(3) == 0x1337
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
475 assert sl.read_reg(2) == 0x1332
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
476 assert sl.read_reg(6) == 0x12345
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
477
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
478 sl.exitDebugMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
479 print('mode at end:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
480
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
481 sl.close()
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
482 print('Test succes!')
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
483