-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpyMicromem.py
180 lines (156 loc) · 6.44 KB
/
pyMicromem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# /* Copyright (C) 2017,2018,2019 Burns Fisher
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License along
# * with this program; if not, write to the Free Software Foundation, Inc.,
# * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# *
# */
#
# This module is to access a microprocessor with built-in loader. The class
# representing the loader is in its own module and must have a class FlashLdr.
# Currently have pyAltosFlash.py with the Altus Metrum loader, and pySerialFlash
# with the AMSAT Golf Serial Loader. It could be modified to use, say, the
# ST-Link dongle or even the STM32L built-in USB or serial loader.
#
import os
import platform
import sys
import traceback
import serial
import serial.tools.list_ports
import time
class MemoryPage(object):
'A representation of a page of memory in the microprocessor'
size=0x100 #Default assumption
loaded = False
def __init__(self,address,loader):
self.lowAddress = address & ~(self.size-1)
self.highAddress=self.lowAddress+self.size-1;
self.loaded = False
self.dirty = False
self.loader = loader
self.size = loader.GetPageSize()
def TestPage(self):
# If we have never written, don't bother. Plus there might be no
# contents.
if(not self.dirty):
return
tempContents = self.loader.ReadPage(self.lowAddress,self.size)
for i in range(0,self.size):
if(tempContents[i] != self.contents[i]):
print("\nCompare fail for address="+hex(self.lowAddress+i)+': Device='+
str(tempContents[i])+ ' File='+str(self.contents[i]))
def LoadPage(self):
self.contents = self.loader.ReadPage(self.lowAddress,self.size)
self.loaded = True
self.dirty = False
def WritePage(self,force=False):
if(force or self.dirty):
self.loader.WritePage(self.contents,self.lowAddress)
self.dirty=False
def GetSize(self):
return self.size
def PutByte(self,data,address):
if(address<self.lowAddress or address>self.highAddress):
raise ValueError('Address out of range of object:'+
hex(self.lowAddress)+' '+hex(self.highAddress))
if(not self.loaded):
self.LoadPage()
offset = address-self.lowAddress
self.contents[offset] = data
self.dirty=True
return
def GetByte(self,address):
if(address<self.lowAddress or address>self.highAddress):
raise ValueError('Address out of range of object:'+
hex(self.lowAddress)+' '+hex(self.highAddress))
if(not self.loaded):
self.LoadPage()
offset = address-self.lowAddress
return self.contents[offset]
def GetByteArray(self,address,length):
if(not self.loaded):
raise ValueError('Page not loaded')
if(address<self.lowAddress or (address+length-1)>self.highAddress):
raise ValueError('Address out of range of object:'+str(self.lowAddress)+' '+str(self.highAddress))
offset = address-self.lowAddress
return self.contents[offset:offset+length-1]
def GetRange(self):
return ([self.lowAddress],[self.highAddress])
class Device(object):
'A representation of the entire microprocessor--mainely the memory'
def __init__(self,low,high,loader):
self.loader = loader
self.pageSize = MemoryPage.size
self.lowAddress = low
self.highAddress = high
self.highIndex = int(high/self.pageSize)
self.lowIndex = int(low/self.pageSize)
self.memory = [MemoryPage(i,loader) for i in
range(low,high,self.pageSize)]
def _GetPageIndex(self,address):
return (int(address/self.pageSize))-self.lowIndex
def GetByte(self,address):
return self.memory[self._GetPageIndex(address)].GetByte(address)
def PutByte(self,data,address):
pageNum = int(address/self.pageSize)
offset = pageNum - self.lowIndex
self.memory[self._GetPageIndex(address)].PutByte(data,address)
return
def PutInt16(self,data,address):
self.PutByte(data&0xff,address)
self.PutByte((data>>8)&0xff,address+1)
def GetInt16(self,address):
lowVal=self.GetByte(address)
highVal=self.GetByte(address+1)
return (highVal<<8)|lowVal
def GetInt32(self,address):
lowVal=self.GetInt16(address)
highVal=self.GetInt16(address+2)
return (highVal<<16)|lowVal
def MemoryFlush(self):
maxIndex = int((self.highAddress - self.lowAddress)/self.pageSize)
for i in range(0,maxIndex):
self.memory[i].WritePage(False) #Write, but only if it is dirty
return
def MemoryLoad(self):
maxIndex = int((self.highAddress - self.lowAddress)/self.pageSize)
for i in range(0,maxIndex):
self.memory[i].LoadPage()
def MemoryCompare(self):
maxIndex = int((self.highAddress - self.lowAddress)/self.pageSize)
for i in range(0,maxIndex):
self.memory[i].TestPage()
if __name__ == '__main__':
if(False):
import pyAltosFlash as ldr
else:
import pySerialFlash as ldr
loader = ldr.FlashLdr(debug=False)
print(loader.GetDevice())
ihu = Device(loader.GetLowAddr(),loader.GetHighAddr(),loader)
for addr in range(0x8030000,0x8030100):
data = ihu.GetByte(addr)
print(hex(addr)+":"+hex(data))
print("Writing now")
for addr in range(0x8030000,0x8030100):
ihu.PutByte(addr&0xff,addr)
#ihu.PutByte(ord('a')+(addr&0xf),addr)
for addr in range(0x8030000,0x8030100):
data = ihu.GetByte(addr)
#print(hex(addr)+":"+hex(data))
#ihu.PutByte(11,0x8001104)
#data = ihu.GetByte(0x8001104)
#print('Modified Serial Number='+str(data))
ihu.MemoryFlush()
sys.exit()