forked from audiohacked/python-pyBusPirate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPIFlash.py
118 lines (99 loc) · 3.04 KB
/
SPIFlash.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
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Sean Nelson on 2009-09-20.
Copyright 2009-2013 Sean Nelson <audiohacked@gmail.com>
This file is part of pyBusPirate.
pyBusPirate 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 3 of the License, or
(at your option) any later version.
pyBusPirate 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 pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
import string
import struct
from array import array
from pyBusPirate.Bus.SPI import SPI
class SPIFlash(SPI):
wren_cmd = 0x6
wrdi_cmd = 0x4
rdid_cmd = 0x9f
rdsr_cmd = 0x5
read_cmd = 0x3
fast_read_cmd = 0xb
pw_cmd = 0xa
pp_cmd = 0x2
pe_cmd = 0xdb
se_cmd = 0xd8
dp_cmd = 0xb9
rdp_cmd = 0xab
in_data = None
in_sdata = None
IN = None
OUT = None
def b2s(self, data):
in_sdata = array('H', data)
def s2b(self, data):
in_data = array('B', data)
def __init__(self, sp="/dev/tty.usbserial-A7004qlY", ipf=None, opf=None):
SPI.__init__(self, sp)
self.IN = ipf
self.OUT = opf
self.spi = self
self.spi.init_spi()
self.spi.enable_spi_flash("AW")
def set_cmd_codes(self, wren=0x6, wrdi=0x4, rdid=0x9f, rdsr=0x5, read=0x3,
fast_read=0xb, pw=0xa, pp=0x2, pe=0xdb, se=0xd8, dp=0xb8, rdp=0xab ):
self.wren_cmd = wren
self.wrdi_cmd = wrdi
self.rdid_cmd = rdid
self.rdsr_cmd = rdsr
self.read_cmd = read
self.fast_read_cmd = fast_read
self.pw_cmd = pw
self.pp_cmd = pp
self.pe_cmd = pe
self.se_cmd = se
self.dp_cmd = dp
self.rdp_cmd = rdp
def chip_id(self):
s = "[0x%X r:3]\r"%self.rdid_cmd
self.spi.spi_send(s)
def flash_status(self):
s = "[0x%X r]\r"%self.rdsr_cmd
def flash_write(self, start=0, size=256, data=in_data):
if data is None: pass
for i in range(size/256):
self.page_write(256*i, size, data[256*i:256*(i+1)])
def flash_read(self, start=0, size=512):
for i in range(size/256):
d = self.page_read(start=256*i)
self.to_file(d[7].strip('\r'))
def page_read(self, start=0, size=256):
s = "[0x%X 0x%X 0x%X 0x%X r:0x%X ]\r" % (self.read_cmd, start>>16&0xFF, start>>8&0xFF, start&0xFF, size) # addr
return self.spi.spi_send(s)
def page_write(self, start, size, data):
if data is None: pass
s = "[6][0x%X 0x%X 0x%X 0x%X " % (self.pw_cmd, start>>16&0xFF, start>>8&0xFF, start&0xFF)
for byte in data: s += "0x%X "%byte
s +="]\r"
#print s
self.spi.spi_send(s)
def to_file(self, data, debug=False):
for byte in data.split(' '):
if debug: print byte
try:
h = string.atoi(byte, base=16)
x = struct.pack("<B", h)
self.OUT.write(x)
except:
continue
def from_file(self, ipf=IN, size=512 debug=False):
if ipf is not self.IN: self.IN = ipf
self.in_data = array('B')
self.in_data.fromfile(ipf, size)