Skip to content
Snippets Groups Projects
Commit a7df8adc authored by Rob Hart's avatar Rob Hart
Browse files

add material

parent 2d49db52
Branches
No related tags found
No related merge requests found
...@@ -43,6 +43,12 @@ pulses cease and CS is deasserted." ...@@ -43,6 +43,12 @@ pulses cease and CS is deasserted."
##23K256 memory chip as an example of SPI ##23K256 memory chip as an example of SPI
The 23K256 is a 32KByte memory device with a limited set of commands and registers. Good exercise for doing SPI at a low-ish level, using the MicroPython machine functions and referring to eh chip's datasheet.
[Datasheet for 23K256:](https://ww1.microchip.com/downloads/en/DeviceDoc/22100F.pdf) [Datasheet for 23K256:](https://ww1.microchip.com/downloads/en/DeviceDoc/22100F.pdf)
Set Polarity equal to zero for this chip. This is the level of the idle clock. See [SPI spec for MicroPython](https://docs.micropython.org/en/latest/library/machine.SPI.html) Set Polarity equal to zero for this chip. This is the level of the idle clock. See [SPI spec for MicroPython](https://docs.micropython.org/en/latest/library/machine.SPI.html)
[Here is a code that works](./code/SPI_23K256/SPI_23K256.py) to store and retrieve numbers from the 23K256. All operations are written out. Next step would be to put them in functions and create a library file for this chip.
This is a nice example of a simple but non-trivial protocol for SPI.
...@@ -4,15 +4,23 @@ import time ...@@ -4,15 +4,23 @@ import time
############################################################################### ###############################################################################
# Constants # Constants
#commands
READ = 0x03 READ = 0x03
WRITE = 0x02 WRITE = 0x02
READ_S_R = 0x05 READ_S_R = 0x05
WRITE_S_R = 0x01 WRITE_S_R = 0x01
# Bits to determine mode
SEQ_RW = 0x01 << 6 #B6 is 1 SEQ_RW = 0x01 << 6 #B6 is 1
BYTE_RW = 0x00 << 6 #zeros BYTE_RW = 0x00 << 6 #zeros
PAGE_RW = 0x02 << 6 #B7 is 1 PAGE_RW = 0x02 << 6 #B7 is 1
#address
ADDR_HIGH = 0x00
ADDR_LOW = 0x00
#content
value_stored = 255
######################################################## ########################################################
...@@ -33,25 +41,46 @@ spi = machine.SPI(0, ...@@ -33,25 +41,46 @@ spi = machine.SPI(0,
miso=machine.Pin(4)) miso=machine.Pin(4))
#write to status register #write to status register to set mode.
cs.value(1) cs.value(1)
msg = bytearray() msg = bytearray()
msg.append(WRITE_S_R) msg.append(WRITE_S_R)
msg.append(SEQ_RW) msg.append(BYTE_RW)
print(msg) #print(msg)
cs.value(0) cs.value(0)
spi.write(msg) spi.write(msg)
cs.value(1) cs.value(1)
#read status register #read status register, to check mode.
msg = bytearray() msg = bytearray()
msg.append(READ_S_R) msg.append(READ_S_R)
cs.value(0) cs.value(0)
spi.write(msg) spi.write(msg)
data = spi.read(1) data = spi.read(1)
cs.value(1) cs.value(1)
print(data) print("mode=",data)
#form address
msg = bytearray([WRITE, ADDR_HIGH, ADDR_LOW])
#form writing array
val = bytearray([value_stored])
print("number stored =",val)
#Write to address
cs.value(0)
spi.write(msg)
spi.write(val)
cs.value(1)
time.sleep(0.01)
#Read from address
msg = bytearray([READ, ADDR_HIGH, ADDR_LOW])
cs.value(0)
spi.write(msg) #same address
data = spi.read(1)
cs.value(1)
print('number read = ',data)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment