Last active
September 24, 2023 19:07
-
-
Save blark/7237235 to your computer and use it in GitHub Desktop.
A quick and dirty Python script to test Raspberry Pi SPI communication with a Microchip 23LCV512 SRAM (512kbit). See comments for requirements.
This file contains hidden or 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
#!/usr/bin/python2 | |
# | |
# requires SPI kernel module (sudo modprobe spi_bcm2708) | |
# requires spi.so from https://github.com/lthiery/SPI-Py | |
# | |
from __future__ import print_function | |
import spi | |
import os | |
from random import randint | |
import time | |
print('setting up spi...', end='') | |
status = spi.openSPI(speed=20000000) | |
print('%s' % status) | |
#read sram mode register | |
mode = spi.transfer((0x05,0))[1] | |
if mode == 0: modeType = "byte" | |
elif mode == 64: modeType = "sequential" | |
elif mode == 128: modeType = "page" | |
else: modeType = "unknown" | |
print('read mode register: %s (%s)\n' % ('{0:08b}'.format(mode), modeType)) | |
#set some static variables | |
WRITE = 0x02 | |
READ = 0x03 | |
DATA = (0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD, 0xF0, 0x0D) * 256 | |
SIZE = len(DATA) | |
LAST_ADDR = float(0xFFFF) | |
BLOCKS = int(round(LAST_ADDR/SIZE)) | |
#fill the SRAM with data 2Kb at a time | |
print('filling 512kbit of memory with %s*%s bit blocks...' % (BLOCKS, SIZE)) | |
start = time.time() | |
print('#\tlocation') | |
for write in range(0,(BLOCKS)): | |
loc = ((write*SIZE)) | |
print('%s\t0x%04x' % (write ,loc)) | |
x,y = divmod(loc, 0x100) | |
spi.transfer((WRITE, x, y) + DATA) | |
#uncomment to error check each block after write | |
#if DATA != (spi.transfer((READ, x, y) + (0,) * SIZE)[3:]): print('error') | |
end = time.time() | |
#time isn't 100% accurate, but good enough to get an idea | |
print('\ndone in %s seconds\n' % (end-start)) | |
#now do some random error checking to make sure we read back the right data | |
for x in range(0,5): | |
mem = (randint(0,BLOCKS)*SIZE) | |
print('reading random %s byte memory segment 0x%04x: ' % (SIZE, mem), end='') | |
x,y = divmod(mem, 0x100) | |
#first three bytes returned are null as we're instructing the SRAM | |
#to read, loc byte 1, loc byte 2, then it spits out data | |
randomMem = (spi.transfer((READ, x, y) + (0,) * SIZE)[3:]) | |
if DATA != randomMem: print('error!') | |
else: print('data matches!') | |
print('\nfin') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I need to communicate raspberry pi 3 and SRAM "MX25L6436E" which is present in other card through spi communication.
what are the changes i need to do in this script to use it.
i am quite new to python, please tell me script changes or any other changes as well if needed.