Repository navigation
Expand file tree
/
Copy pathmb_PSRAM_64Mb_SPI_CP.py
More file actions
134 lines (102 loc) · 4.91 KB
/
Copy pathmb_PSRAM_64Mb_SPI_CP.py
File metadata and controls
134 lines (102 loc) · 4.91 KB
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
128
129
130
131
132
133
134
"""
mb_PSRAM_64Mb_SPI_CP.py
Very Simple CircuitPython module/driver for Espressif 64Mbit SPI PSRAM
(Adafruit prdoduct ID: 4677 or similar)
Author: mark@marksbench.com
Version: 0.1, 2021-06-11
**NOTE(1): There is no guarantee that this software will work in the way you expect (or at all).
**Use at your own risk.
**NOTE(2): Writing and reading 64Mib over SPI is _not_ fast. If you're concerned about
** whether your program has crashed out over a long read or write loop, set up some sort
** of heartbeat - a periodically flashing LED, print statment, etc.
**NOTE(3): As this is PSRAM and not SRAM, do not exceed the maximum operating frequency
** or access the PSRAM outside its timing parameters as shown in the datasheet, or the
** PSRAM may not have enough time to do an internal refresh, causing a loss of data.
Prerequisites:
- RP2040 silicon (tested with Raspberry Pi Pico), should work with other MCUs with SPI
- Tested with CircuitPython 6.2, 6.3, and 7.0.0-alpha.3
- PSRAM connected to hardware SPI port0 or port1 pins, should also work with SW SPI
- Dedicated /CS pin (can be any GP pin that's not already being used for SPI). Do not tie /CS to
GND - the device requires state changes on /CS to function properly.
Usage:
- Set up hardware or software SPI. Polarity and phase are both 0.
- specify /CS pin (can be any GP pin that's not already being used for SPI):
cs = GP#
- Create constructor:
thisMemoryChipDeviceName = mb_PSRAM_64Mb_SPI_CP.mb_PSRAM_64Mb_CP(spi, cs)
- To write a single byte to an address:
thisMemoryChipDeviceName.write_byte(address, value)
- To read a single byte from an address:
thisMemoryChipDeviceName.read_byte(address)
- See mb_PSRAM_64Mb_SPI_CP_example.py
For more information, consult the Raspberry Pi Pico MicroPython SDK documentation at:
https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-python-sdk.pdf
and the CircuitPython documentation at:
https://circuitpython.org
and the Adafruit 64Mbit PSRAM page and datasheet at:
https://www.adafruit.com/product/4677
"""
import bitbangio
import busio
import digitalio
from board import *
import time
# Command set for this device. Only using regular speed reads with normal SPI.
_RESET_EN = const(0x66) # Reset enable
_RESET = const(0x99) # Reset
_WRITE = const(0x02) # Write
_READ = const(0x03) # Read
_MAX_ADDRESS = const(8388607) # 8MiB address size for this device (0 to 8388607)
class mb_PSRAM_64Mb_SPI_CP:
"""Driver for generic 64Mib SPI PSRAM module from Adafruit"""
def __init__(self, spi, cs):
# Init with SPI settings
self.spi = spi
# Init /CS pin
self.cs = digitalio.DigitalInOut(cs)
self.cs.direction = digitalio.Direction.OUTPUT
self.cs.value = True # According to the datasheet, the device needs /CE high first
time.sleep(0.0002) # Device needs 150us and a user-issued reset operation to complete its self-init
self.cs.value = False
self.spi.write(bytearray([_RESET_EN])) # Reset enable
self.cs.value = True
time.sleep(0.00005)
self.cs.value = False
self.spi.write(bytearray([_RESET])) # With Reset enabled, do a reset
self.cs.value = True
time.sleep(0.0001)
# Done init, ready to go
self.address_byte=[0,0,0]
def write_byte(self, address, value):
# Check to see if the address is valid
if((address > _MAX_ADDRESS) or (address < 0)):
raise ValueError("Address is outside of device address range: 0 to",_MAX_ADDRESS)
return()
# Now check to make sure the data is within 0 and 255
if((value > 255) or (value < 0)):
raise ValueError("You can only pass an 8-bit data value (0-255) to this function")
return()
# Break the address into three bytes to send (we're using 8-bit SPI)
self.address_byte[2] = address & 0x0000ff
self.address_byte[1] = (address & 0x00ff00) >> 8
self.address_byte[0] = (address >> 16)
self.cs.value = False
self.spi.write(bytearray([_WRITE, self.address_byte[0], self.address_byte[1], self.address_byte[2], value]))
self.cs.value = True
return()
def read_byte(self, address):
# Check to see if the address is valid
if((address > _MAX_ADDRESS) or (address < 0)):
raise ValueError("Address is outside of device address range: 0 to",_MAX_ADDRESS)
return()
# Break the address into three bytes and send read command
self.address_byte[2] = address & 0x0000ff
self.address_byte[1] = (address & 0x00ff00) >> 8
self.address_byte[0] = (address >> 16)
self.cs.value = False
self.spi.write(bytearray([_READ, self.address_byte[0], self.address_byte[1], self.address_byte[2]]))
self.value_read=bytearray(1)
self.spi.readinto(self.value_read)
self.cs.value = True
self.value_read = int.from_bytes(self.value_read, "big")
return(self.value_read)