-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadchip.py
146 lines (82 loc) · 2.49 KB
/
readchip.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
import board as FTDI
import busio as Serial
import digitalio as GPIO
#import time
# Variables
block_start = 0
numblocks = 256
sector_start = 0
#numsectors = 16
# Configuration
CS_pin = FTDI.D7
baudrate = 40000000
# Constants
read = 0x03
sector_length = 0x1000
block_sectors = 0x10
sector_pages = 0x10
page_start = 0x00
cell = 0x00
# instantiation, lock board for use, configure frequency, set CS pin
FT232H = Serial.SPI(FTDI.SCLK, FTDI.MOSI, FTDI.MISO)
while not FT232H.try_lock(): pass
FT232H.configure(baudrate)
CS = GPIO.DigitalInOut(CS_pin)
CS.direction = GPIO.Direction.OUTPUT
response = [0] * sector_length
payload = bytearray()
numreads = 0
# main
# handle variable range, set relative variables
block_start += sector_start // block_sectors
block_start_print = block_start
sector_start_print = sector_start = sector_start % block_sectors
if 'numsectors' not in locals():
numsectors = block_sectors
if numsectors != block_sectors:
numblocks = (sector_start + numsectors) // block_sectors + 1
# check existance
try:
check = open('response.bin', 'r+b')
check.close()
print('Careful! response.bin already exists, rename it!! \n\n')
quit()
except:
pass
# open for read
dump = open('response.bin', 'a+b')
# read cycle
for blocknum in range(numblocks):
if (numblocks > 1) & (numblocks - 1 == blocknum) & (numsectors != block_sectors) & (sector_start != 0):
numsectors = (sector_end + numsectors) % block_sectors
for sectornum in range(numsectors):
block = block_start + blocknum
sector = sector_start + sectornum
page = page_start + (sector * sector_pages)
if sector == block_sectors:
if blocknum == 0:
sector_end = sector_start
sector_start = 0
break
instruction = [read, block, page, cell]
print('reading block', block, 'sector', sector)
numreads += 1
CS.value = False
FT232H.write(instruction)
FT232H.readinto(response)
CS.value = True
#time.sleep(0.1)
# write payload to file
for i in range(sector_length):
payload.extend(bytes([response[i]]))
dump.write(payload)
dump.flush()
# clear buffers
response = [0] * sector_length
payload = bytearray()
#time.sleep(0.1)
# Close
dump.close()
FT232H.unlock()
print('DONE')
print('read', numreads, 'sectors starting at block', block_start_print, 'sector', sector_start_print)