Skip to content

Commit babbad0

Browse files
committed
spi: Flash programming
1 parent d3747ad commit babbad0

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/main/scala/spi/SerialSpiTest.scala

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,41 @@ object SerialSpiTest extends App {
119119
v
120120
}
121121

122+
def programFlash(id: Int, addr: Int, data: Array[Byte]) = {
123+
124+
readStatusRegister(id)
125+
126+
writeRead(setCmd(id, 0)) // CS low
127+
writeByte(id, 0x01) // write status register
128+
writeByte(id, 0x00) // write status register
129+
writeRead(setCmd(id, 4)) // CS high
130+
131+
readStatusRegister(id)
132+
133+
writeRead(setCmd(id, 0)) // CS low
134+
writeByte(id, 0x06) // write enable
135+
writeRead(setCmd(id, 4)) // CS high
136+
137+
readStatusRegister(id)
138+
139+
writeRead(setCmd(id, 0)) // CS low
140+
writeByte(id, 0x02)
141+
writeByte(id, (addr >> 16) & 0xff)
142+
writeByte(id, (addr >> 8) & 0xff)
143+
writeByte(id, addr & 0xff)
144+
for (d <- data) {
145+
println("Writing 0x" + d.toHexString)
146+
writeByte(id, d)
147+
}
148+
// writeByte(id, 0x04) // write disable
149+
writeRead(setCmd(id, 4)) // CS high
150+
151+
readStatusRegister(id)
152+
Thread.sleep(300)
153+
readStatusRegister(id)
154+
155+
}
156+
122157

123158
// TODO: fix this hard coded thing
124159
val port = SerialPort.getCommPort("/dev/tty.usbserial-210292B408601")
@@ -137,6 +172,17 @@ object SerialSpiTest extends App {
137172
readStatusRegister(2) // SRAM
138173
readMemory(1, 0)
139174
readSram(2, 0)
175+
176+
val s = "Hello, World!\n"
177+
val data = s.getBytes
178+
// programFlash(1, 0, data)
179+
Thread.sleep(1000)
180+
181+
for (i <- 0 until 8) {
182+
print(readMemory(1, i).toChar)
183+
}
184+
println()
185+
140186
print(writeRead(setCmd(0, 4))) // all CS high
141187
out.close()
142188
port.closePort()

0 commit comments

Comments
 (0)