Last active
August 29, 2015 14:25
-
-
Save rheum/8ed2e33a10f975ce5b55 to your computer and use it in GitHub Desktop.
Raspberry Pi - AT45DB081D SPI Flash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spidev | |
def ready(): | |
command = [0xD7, 0xFF] | |
recv = spi.xfer(command) | |
return (recv[1] & 0x80) > 0 | |
def read_mem(page, offset=0, num=256): | |
if page < 2**12 and offset < 2**8: | |
addrByte1 = (page & 0xF00) >> 8 | |
addrByte2 = (page & 0x0FF) | |
addrByte3 = offset | |
opcode = [0x0B] | |
address = [addrByte1, addrByte2, addrByte3] | |
dontcare = [0xFF] | |
command = opcode + address + dontcare | |
recv = spi.xfer(command + [0xFF] * num) | |
return recv[len(command):] | |
def read_buffer(num, offset=0, buffer=0): | |
opcode = 0 | |
if opcode == 0: | |
opcode = 0xD4 | |
else: | |
opcode = 0xD6 | |
command = [opcode, 0xFF, 0xFF, offset, 0xFF] | |
recv = spi.xfer(command + [0xFF]*num) | |
return recv[len(command):] | |
def write_buffer(data, offset=0, buffer=0): | |
opcode = 0 | |
if opcode == 0: | |
opcode = 0x84 | |
else: | |
opcode = 0x87 | |
command = [opcode, 0xFF, 0xFF, offset] | |
spi.xfer(command + data) | |
def buffer_to_mem(page, buffer=0): | |
opcode = 0 | |
if opcode == 0: | |
opcode = 0x83 | |
else: | |
opcode = 0x86 | |
addrByte1 = (page & 0xF00) >> 8 | |
addrByte2 = (page & 0x0FF) | |
command = [opcode, addrByte1, addrByte2, 0xFF] | |
spi.xfer(command) | |
while not ready(): | |
pass | |
print "Ready: %s" % (ready()) | |
spi = spidev.SpiDev() | |
spi.open(0,0) | |
spi.max_speed_hz = 10000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment