annotate python/stlink.py @ 123:9f1094b1587a

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