-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogflash.py
executable file
·127 lines (98 loc) · 3.01 KB
/
progflash.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
#!/usr/bin/env python
# Written by Liam Jackson based on code by Limor "Ladyada" Fried for Adafruit Industries, (c) 2015
# This code is released into the public domain
import time
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
DEBUG = 1
# change these as desired
clockpin = 22
misopin = 24
mosipin = 25
cspin = 23
holdpin = 27
wppin = 26
# read SPI data from chip
# cmd hex command to send
# cmdlen number of bits in cmd
# readlen number of bits to read back
# sleeptime time to sleep between write & read
def spicmd(cmd, cmdlen, readlen, sleeptime=0):
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
if(cmdlen != 0):
commandout = cmd
for i in range(cmdlen):
if (commandout & (1 << (cmdlen - 1))):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
#end if cmdlen != 0
if(sleeptime != 0):
time.sleep(sleeptime)
chipout = 0
if(readlen != 0):
# read in bits on clk high
for i in range(readlen):
GPIO.output(clockpin, True)
chipout <<= 1
if (GPIO.input(misopin)):
chipout |= 0x1
GPIO.output(clockpin, False)
#end if readlen != 0
GPIO.output(cspin, True) # bring CS high to end
return chipout
#end function
# set up the SPI interface pins
GPIO.setup(mosipin, GPIO.OUT)
GPIO.setup(misopin, GPIO.IN)
GPIO.setup(clockpin, GPIO.OUT)
GPIO.setup(cspin, GPIO.OUT)
#Shouldn't need to set these up for now.
#GPIO.setup(SPIHOLD, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(SPIWP, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#Read the ID/wakeup register
value = spicmd(0xAB, 8, 64, 0.5)
if((value & 0xff) != ((value >> 8)& 0xff)):
print "Chip ID read didn't go well"
id = value & 0xff
print "ID ", format(id, '02x')
if((id < 0x01) or (id > 0x20)):
print "ID is suspect, perhaps comms not working?"
time.sleep(0.5)
#write enable
result = spicmd(0x06, 8, 0)
# read the status register
sreg1 = spicmd(0x05, 8, 8)
print "Status 1 ", format(sreg1, '02x')
if((sreg1 & 0x02) == 0):
print "Write enable didn't work"
sreg2 = spicmd(0x35, 8, 8)
print "Status 2 ", format(sreg2, '02x')
#verify
sreg2b = spicmd(0x35, 8, 8)
if(sreg2 != sreg2b):
print "Veritication of status2 register failed!"
print "Status 2 ", format(sreg2b, '02x')
#send cmd
#8 bits instruction
#next 8 bits sreg 1
#then 8 bits sreg2 with QE masked out
cmd = (0x01 << 16) | (sreg1 << 8) | (sreg2 & 0xFD)
result = spicmd(cmd, 24, 0)
#wait for the flash write to happen
time.sleep(0.5)
#verify the write
verify = spicmd(0x35, 8, 8)
print "Status 2 now ", format(verify, '02x')
if(verify & 0x02):
print "Readback of status2 register failed! If you have HOLD connected to CS this is normal."
# hang out and do nothing for a half second
time.sleep(0.5)
GPIO.cleanup()
print "Done!"