@@ -18,9 +18,12 @@ The driver has the following attributes:
1818 7 . Alternatively it can support byte-level access using Python slice syntax.
1919 8 . RAM allocations are minimised. Buffer sizes are tiny.
2020
21- ##### [ Main readme ] ( ../../README.md )
21+ ## 1.1 Notes
2222
23- ## 1.1 This document
23+ As of Jan 2024 this driver has been updated to fix a bug where the device page
24+ size was less than 256. A further aim was to make the driver more generic, with
25+ a better chance of working with other SPI EEPROM chips. The constructor has
26+ additional optional args to support this.
2427
2528Code samples assume one or more Microchip devices. If using the STM chip the
2629SPI baudrate should be 5MHz and the chip size must be specified to the ` EEPROM `
@@ -29,6 +32,8 @@ constructor, e.g.:
2932eep = EEPROM(SPI(2 , baudrate = 5_000_000 ), cspins, 256 )
3033```
3134
35+ ##### [ Main readme] ( ../../README.md )
36+
3237# 2. Connections
3338
3439Any SPI interface may be used. The table below assumes a Pyboard running SPI(2)
@@ -91,7 +96,7 @@ import os
9196from machine import SPI , Pin
9297from eeprom_spi import EEPROM
9398cspins = (Pin(Pin.board.Y5, Pin.OUT , value = 1 ), Pin(Pin.board.Y4, Pin.OUT , value = 1 ))
94- eep = EEPROM(SPI(2 , baudrate = 20_000_000 ), cspins)
99+ eep = EEPROM(SPI(2 , baudrate = 20_000_000 ), cspins, 128 ) # 128KiB chips
95100# Format the filesystem
96101os.VfsLfs2.mkfs(eep) # Omit this to mount an existing filesystem
97102os.mount(eep,' /eeprom' )
@@ -103,14 +108,15 @@ Note that, at the outset, you need to decide whether to use the array as a
103108mounted filesystem or as a byte array. The filesystem is relatively small but
104109has high integrity owing to the hardware longevity. Typical use-cases involve
105110files which are frequently updated. These include files used for storing Python
106- objects serialised using Pickle/ujson or files holding a btree database.
111+ objects serialised using Pickle/json or files holding a btree database.
107112
108113The SPI bus must be instantiated using the ` machine ` module.
109114
110115## 4.1 The EEPROM class
111116
112117An ` EEPROM ` instance represents a logical EEPROM: this may consist of multiple
113- physical devices on a common SPI bus.
118+ physical devices on a common SPI bus. Alternatively multiple EEPROM instances
119+ may share the bus, differentiated by their CS pins.
114120
115121### 4.1.1 Constructor
116122
@@ -119,14 +125,21 @@ each chip select line an EEPROM array is instantiated. A `RuntimeError` will be
119125raised if a device is not detected on a CS line.
120126
121127Arguments:
122- 1 . ` spi ` Mandatory. An initialised SPI bus created by ` machine ` .
128+ 1 . ` spi ` An initialised SPI bus created by ` machine ` .
123129 2 . ` cspins ` A list or tuple of ` Pin ` instances. Each ` Pin ` must be initialised
124130 as an output (` Pin.OUT ` ) and with ` value=1 ` and be created by ` machine ` .
125- 3 . ` size=128 ` Chip size in KiB. Set to 256 for the STM chip.
126- 4 . ` verbose=True ` If ` True ` , the constructor issues information on the EEPROM
127- devices it has detected.
131+ 3 . ` size ` Chip size in KiB. Set to 256 for the STM chip, 128 for the Microchip.
132+ 4 . ` verbose=True ` If ` True ` , the constructor performs a presence check for an
133+ EEPROM on each chip select pin and reports devices it has detected. See
134+ [ 4.1.5 Auto detection] ( ./SPI.md#415-auto-detection ) for observations on
135+ production code.
128136 5 . ` block_size=9 ` The block size reported to the filesystem. The size in bytes
129137 is ` 2**block_size ` so is 512 bytes by default.
138+ 6 . ` page_size=None ` EEPROM devices have a RAM buffer enabling fast writes. The
139+ driver determines this automatically by default. It is possible to override
140+ this by passing an integer being the page size in bytes: 16, 32, 64, 128 or 256.
141+ See [ 4.1.5 Auto detection] ( ./SPI.md#415-auto-detection ) for reasons why this
142+ is advised in production code.
130143
131144SPI baudrate: The 25LC1024 supports baudrates of upto 20MHz. If this value is
132145specified the platform will produce the highest available frequency not
@@ -152,7 +165,7 @@ of single byte access:
152165from machine import SPI , Pin
153166from eeprom_spi import EEPROM
154167cspins = (Pin(Pin.board.Y5, Pin.OUT , value = 1 ), Pin(Pin.board.Y4, Pin.OUT , value = 1 ))
155- eep = EEPROM(SPI(2 , baudrate = 20_000_000 ), cspins)
168+ eep = EEPROM(SPI(2 , baudrate = 20_000_000 ), cspins, 128 )
156169eep[2000 ] = 42
157170print (eep[2000 ]) # Return an integer
158171```
@@ -162,7 +175,7 @@ writing, the size of the slice must match the length of the buffer:
162175from machine import SPI , Pin
163176from eeprom_spi import EEPROM
164177cspins = (Pin(Pin.board.Y5, Pin.OUT , value = 1 ), Pin(Pin.board.Y4, Pin.OUT , value = 1 ))
165- eep = EEPROM(SPI(2 , baudrate = 20_000_000 ), cspins)
178+ eep = EEPROM(SPI(2 , baudrate = 20_000_000 ), cspins, 128 )
166179eep[2000 :2002 ] = bytearray ((42 , 43 ))
167180print (eep[2000 :2002 ]) # Returns a bytearray
168181```
@@ -187,7 +200,7 @@ advantage when reading of using a pre-allocated buffer. Arguments:
187200#### The len operator
188201
189202The size of the EEPROM array in bytes may be retrieved by issuing ` len(eep) `
190- where ` eep ` is the ` EEPROM ` instance.
203+ where ` eep ` is an ` EEPROM ` instance.
191204
192205#### scan
193206
@@ -201,7 +214,11 @@ identify the chip.
201214
202215#### erase
203216
204- Erases the entire array. Available only on the Microchip device.
217+ Zero the entire array. Can take several seconds.
218+
219+ #### get_page_size
220+
221+ Return the page size in bytes.
205222
206223### 4.1.4 Methods providing the block protocol
207224
@@ -216,6 +233,19 @@ their use in application code is not recommended.
216233` writeblocks() `
217234` ioctl() `
218235
236+ ### 4.1.5 Auto detection
237+
238+ The driver constructor uses auto-detection in two circumstances:
239+ * If ` verbose ` is specified, it checks each chip select for chip presence.
240+ * If ` page_size ` is set to ` None ` the value is determined by measurement.
241+
242+ In both cases data is written to the chips, then restored from RAM. If a power
243+ outage were to occur while either process was in progress, corruption could
244+ occur. It is therefore recommended that, in production code, ` verbose ` is
245+ ` False ` and ` page_size ` is set to an integer. The page size may be determined
246+ from the chip datasheet. It is also printed on instantiation if ` verbose ` is
247+ set: running any of the test scripts will do this.
248+
219249## 4.2 Byte addressing usage example
220250
221251A sample application: saving a configuration dict (which might be large and
@@ -225,7 +255,7 @@ import ujson
225255from machine import SPI , Pin
226256from eeprom_spi import EEPROM
227257cspins = (Pin(Pin.board.Y5, Pin.OUT , value = 1 ), Pin(Pin.board.Y4, Pin.OUT , value = 1 ))
228- eep = EEPROM(SPI(2 , baudrate = 20_000_000 ), cspins)
258+ eep = EEPROM(SPI(2 , baudrate = 20_000_000 ), cspins, 128 )
229259d = {1 :' one' , 2 :' two' } # Some kind of large object
230260wdata = ujson.dumps(d).encode(' utf8' )
231261sl = ' {:10d } ' .format(len (wdata)).encode(' utf8' )
@@ -252,18 +282,20 @@ possible to use JSON/pickle to store objects in a filesystem.
252282This assumes a Pyboard 1.x or Pyboard D with two EEPROMs wired to SPI(2) as
253283above with chip selects connected to pins ` Y4 ` and ` Y5 ` . It provides the
254284following. In all cases the stm arg should be ` True ` if using the STM chips.
285+ On other hardware, adapt ` cspins ` and ` get_eep ` at the start of the script.
255286
256287## 5.1 test(stm=False)
257288
258289This performs a basic test of single and multi-byte access to chip 0. The test
259- reports how many chips can be accessed. Existing array data will be lost. This
260- primarily tests the driver: as a hardware test it is not exhaustive.
290+ reports how many chips can be accessed. The current page size is printed and its
291+ validity is tested. Existing array data will be lost. This primarily tests the
292+ driver: as a hardware test it is not exhaustive.
261293
262294## 5.2 full_test(stm=False)
263295
264- This is a hardware test. Tests the entire array. Fills each 256 byte page with
265- random data, reads it back, and checks the outcome. Existing array data will be
266- lost.
296+ This is a hardware test. Tests the entire array. Fills the array with random
297+ data in blocks of 256 byes. After each block is written, it is read back and the
298+ contents compared to the data written. Existing array data will be lost.
267299
268300## 5.3 fstest(format=False, stm=False)
269301
0 commit comments