i2c-microsd-expansion-circuitpython 1.0.13__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- i2c_microsd_expansion_circuitpython-1.0.13.dist-info/METADATA +564 -0
- i2c_microsd_expansion_circuitpython-1.0.13.dist-info/RECORD +9 -0
- i2c_microsd_expansion_circuitpython-1.0.13.dist-info/WHEEL +5 -0
- i2c_microsd_expansion_circuitpython-1.0.13.dist-info/licenses/LICENSE +21 -0
- i2c_microsd_expansion_circuitpython-1.0.13.dist-info/top_level.txt +1 -0
- i2c_microsd_module/__init__.py +63 -0
- i2c_microsd_module/config.py +49 -0
- i2c_microsd_module/module.py +1794 -0
- i2c_microsd_module/protocol.py +114 -0
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: i2c-microsd-expansion-circuitpython
|
|
3
|
+
Version: 1.0.13
|
|
4
|
+
Summary: CircuitPython host library for the I²C microSD Expansion.
|
|
5
|
+
Author-email: syntax1269 <syntax1269@gmail.com>
|
|
6
|
+
Maintainer-email: syntax1269 <syntax1269@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/syntax1269/I2C_microSD_Expansion_CircuitPython
|
|
9
|
+
Project-URL: Source, https://github.com/syntax1269/I2C_microSD_Expansion_CircuitPython
|
|
10
|
+
Project-URL: Documentation, https://i2c-microsd-expansion-circuitpython.readthedocs.io/
|
|
11
|
+
Project-URL: Changelog, https://github.com/syntax1269/I2C_microSD_Expansion_CircuitPython/blob/main/CHANGELOG.md
|
|
12
|
+
Project-URL: Umbrella project, https://github.com/syntax1269/I2C-microSD-Expansion
|
|
13
|
+
Project-URL: PyPI, https://pypi.org/project/i2c-microsd-expansion-circuitpython/
|
|
14
|
+
Keywords: i2c,sd,module,attiny,micropython,circuitpython
|
|
15
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
18
|
+
Classifier: Topic :: Software Development :: Embedded Systems
|
|
19
|
+
Classifier: Topic :: System :: Hardware
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: adafruit-circuitpython-busdevice>=5.2.17
|
|
25
|
+
Provides-Extra: optional
|
|
26
|
+
Requires-Dist: Adafruit-Blinka; extra == "optional"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# I²C microSD Expansion — CircuitPython Library
|
|
30
|
+
|
|
31
|
+
**Add FAT file storage to any CircuitPython board with just two wires.**
|
|
32
|
+
|
|
33
|
+
The [I²C microSD Expansion](https://github.com/syntax1269/I2C-microSD-Expansion) module owns
|
|
34
|
+
the SD card and the FAT filesystem. Your CircuitPython code talks to it over I²C:
|
|
35
|
+
open a file, write bytes, read them back, list directories — with no FAT stack, SD
|
|
36
|
+
driver, or level shifter in your code. The module also exposes spare expansion pins
|
|
37
|
+
and a bit-bang engine that can drive WS2812B strips or run timed pin scripts
|
|
38
|
+
on its own.
|
|
39
|
+
|
|
40
|
+
This is the **CircuitPython port**. The same API and the same `microSDexp`
|
|
41
|
+
instance name are available in the
|
|
42
|
+
[Arduino](https://github.com/syntax1269/I2C-microSD-Expansion_Arduino)
|
|
43
|
+
and [Qwiic Python](https://github.com/syntax1269/I2C_microSD_Expansion_Qwiic_Py)
|
|
44
|
+
ports. The [MicroPython](https://github.com/syntax1269/I2C_microSD_Expansion_MicroPython),
|
|
45
|
+
[ESP-IDF](https://github.com/syntax1269/I2C_microSD_Expansion_ESP-IDF) and
|
|
46
|
+
[Zephyr](https://github.com/syntax1269/I2C_microSD_Expansion_Zephyr) ports
|
|
47
|
+
expose the same wire protocol.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Why use this module?
|
|
52
|
+
|
|
53
|
+
| Task | Without the module | With the module |
|
|
54
|
+
|---|---|---|
|
|
55
|
+
| Write a log | FAT library + SPI + 4+ pins + shifter | `microSDexp.open("log.csv", "a")` → `write()` → `close()` |
|
|
56
|
+
| Read a file | Same, plus RAM for buffers | `microSDexp.open("data.bin", "r")` → `read()` |
|
|
57
|
+
| List a directory | FAT directory parsing | `microSDexp.list_dir("/")` |
|
|
58
|
+
| Check card health | Mount volume, parse CSD | `microSDexp.card_info()` |
|
|
59
|
+
| Long filenames | LFN code size | Works out of the box (259-char paths) |
|
|
60
|
+
| WS2812B strip | LED library + PIO/RMT + pins | `microSDexp.show_neopixels(pin, grb, count)` |
|
|
61
|
+
|
|
62
|
+
One file can be open per module at a time. `close()` and `sync()` are a
|
|
63
|
+
committed-data barrier: they return after the module reports the bytes written
|
|
64
|
+
to the card, so a clean restart sees them. They are **not** a power-failure
|
|
65
|
+
transaction guarantee — the card may still be finishing internal flash work, and
|
|
66
|
+
a cut during a directory or FAT metadata update can damage the filesystem. Use
|
|
67
|
+
the staged, checksummed pattern in `config_save_load.py` for settings that must
|
|
68
|
+
survive an unclean cut.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Requirements
|
|
73
|
+
|
|
74
|
+
- A CircuitPython board with an I²C bus (`busio.I2C`).
|
|
75
|
+
- An I²C microSD Expansion module with a microSD card (FAT16/FAT32, or an exFAT card the module can format in place — no PC needed).
|
|
76
|
+
- `microSDexp.version()` at startup confirms the module is answering.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Install
|
|
81
|
+
|
|
82
|
+
1. Copy the `i2c_microsd_module/` folder into `lib/` on your `CIRCUITPY` drive.
|
|
83
|
+
2. `adafruit_bus_device` ships with CircuitPython 6.3 and newer. If your board
|
|
84
|
+
lacks `lib/adafruit_bus_device/`, add it from the official CircuitPython
|
|
85
|
+
library bundle.
|
|
86
|
+
3. Save your program as `code.py` (or any `*.py`) on `CIRCUITPY`.
|
|
87
|
+
|
|
88
|
+
On a Linux/RPi host with Blinka, the same library installs from PyPI:
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
pip install i2c-microsd-expansion-circuitpython
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
For packaging a single zip, `pyproject.toml` and `requirements.txt` are included
|
|
95
|
+
so the module can be built with `circuitpython-build-tools`.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Wiring
|
|
100
|
+
|
|
101
|
+
| Board | Module |
|
|
102
|
+
|---|---|
|
|
103
|
+
| SDA | SDA |
|
|
104
|
+
| SCL | SCL |
|
|
105
|
+
| GND | GND |
|
|
106
|
+
| 3.3 V **or** 5 V | Power (one only, never both) |
|
|
107
|
+
|
|
108
|
+
- The module handles 3.3 V / 5 V bus logic — no external level shifter.
|
|
109
|
+
- 3.3 kΩ pull-ups are fitted on the module; remove them via the underside trace
|
|
110
|
+
cut if your host supplies its own.
|
|
111
|
+
- Power from 5 V *or* 3.3 V, never both. The expansion pins are 3.3 V logic;
|
|
112
|
+
a 5 V LED strip needs a level shifter on DIN.
|
|
113
|
+
|
|
114
|
+
See [docs/wiring.md](docs/wiring.md) for board-specific pin notes.
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## Quick start
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
import board
|
|
122
|
+
import busio
|
|
123
|
+
from i2c_microsd_module import I2CmicroSD
|
|
124
|
+
|
|
125
|
+
# Use the SCL/SDA pins for your board, e.g. board.IO1/board.IO2 on
|
|
126
|
+
# an ESP32-S3, or board.GP1/board.GP0 on RP2040.
|
|
127
|
+
i2c = busio.I2C(board.SCL, board.SDA, frequency=400000)
|
|
128
|
+
microSDexp = I2CmicroSD(i2c, address=0x6E)
|
|
129
|
+
|
|
130
|
+
if not microSDexp.begin():
|
|
131
|
+
raise SystemExit("microSD expansion not found")
|
|
132
|
+
|
|
133
|
+
with microSDexp.open("HELLO.TXT", "w") as f:
|
|
134
|
+
f.write(b"HELLO-WORLD")
|
|
135
|
+
|
|
136
|
+
with microSDexp.open("HELLO.TXT", "r") as f:
|
|
137
|
+
print(f.read(32)) # b'HELLO-WORLD'
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The default address is `0x6E`; bus speeds of 100 kHz, 400 kHz, and 1 MHz are
|
|
141
|
+
supported. The bus clock is the controller's, so the library never changes it:
|
|
142
|
+
it reads `i2c.frequency` for timeout scaling. Set the speed on `busio.I2C(...)`
|
|
143
|
+
once; every instance on that bus then scales from the same value. Pass
|
|
144
|
+
`scl_hz=` only when the bus cannot report its frequency or you deliberately
|
|
145
|
+
need to model a different rate.
|
|
146
|
+
|
|
147
|
+
`I2CmicroSD.from_bus(i2c, address=0x6E)` is a convenience builder.
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## Using the library
|
|
152
|
+
|
|
153
|
+
### Files
|
|
154
|
+
|
|
155
|
+
For a single string or buffer, use the one-shot helpers — they open, write,
|
|
156
|
+
wait for commit and close for you:
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
microSDexp.write_text("data.log", "a,b,c\n") # create/truncate
|
|
160
|
+
microSDexp.append_text("log.csv", "1,2,3\n") # per-chunk commit
|
|
161
|
+
microSDexp.append_file("raw.bin", bytes([1, 2, 3]))
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Append durability is per chunk, not per call. Each 64 B chunk is sent as its
|
|
165
|
+
own APPEND operation and is committed on the module before the next one
|
|
166
|
+
starts, so append mode needs no `close()`. A call that spans several chunks is
|
|
167
|
+
**not transactional**: if the bus, card or power fails part-way,
|
|
168
|
+
`append_file()` returns `False` and the chunks already committed stay in the
|
|
169
|
+
file. Treat the result as a valid prefix, not an all-or-nothing write. Keep one
|
|
170
|
+
record within one chunk, or add a length/checksum and validate on read (see
|
|
171
|
+
`config_save_load.py`).
|
|
172
|
+
|
|
173
|
+
For streaming, use the file object or the explicit API:
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
# Write / append / read
|
|
177
|
+
with microSDexp.open("data.log", "w") as f: # create/truncate
|
|
178
|
+
f.write(b"a,b,c\n")
|
|
179
|
+
f.sync() # optional commit
|
|
180
|
+
|
|
181
|
+
with microSDexp.open("log.csv", "a") as f: # append
|
|
182
|
+
f.write(b"1,2,3\n")
|
|
183
|
+
|
|
184
|
+
with microSDexp.open("data.log", "r") as f:
|
|
185
|
+
data = f.read(64) # short at EOF
|
|
186
|
+
|
|
187
|
+
# Explicit openers are also available
|
|
188
|
+
microSDexp.open_write("x.bin")
|
|
189
|
+
microSDexp.open_read("x.bin")
|
|
190
|
+
microSDexp.open_append("log.csv")
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
`write()` flow-paces automatically; `read()` returns `b""` at EOF. The file object
|
|
194
|
+
is a context manager, so `with` closes it (and commits writes) for you. An
|
|
195
|
+
append handle needs no `close()` — the module commits and closes each chunk.
|
|
196
|
+
The `MicroSDExpFile` instance exposes its owner as `f.microSDexp`.
|
|
197
|
+
|
|
198
|
+
### Directories and queries
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
microSDexp.mkdir("logs")
|
|
202
|
+
microSDexp.remove("old.tmp")
|
|
203
|
+
microSDexp.rmdir("empty")
|
|
204
|
+
|
|
205
|
+
if microSDexp.exists("data.bin"):
|
|
206
|
+
size = microSDexp.file_size("data.bin")
|
|
207
|
+
is_dir = microSDexp.is_directory("data.bin")
|
|
208
|
+
|
|
209
|
+
for entry in microSDexp.list_dir("/", max_entries=32):
|
|
210
|
+
print("D" if entry.is_dir else "F", entry.name, entry.size)
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Card and module information
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
print(microSDexp.version()) # full 3-line firmware banner
|
|
217
|
+
print(microSDexp.firmware_banner()) # same CMD_VERSION answer
|
|
218
|
+
print(microSDexp.module_clock_hz()) # measured CPU clock (0x1B), Hz
|
|
219
|
+
print(microSDexp.firmware_clock_hz()) # deprecated alias of module_clock_hz()
|
|
220
|
+
print(microSDexp.read_clock()) # (year, month, day, hour, minute, second)
|
|
221
|
+
print(microSDexp.pin_mode(5)) # PA5 mode 0..4, or 0xFF if absent
|
|
222
|
+
print(microSDexp.pin_modes()) # {"PA5": 0, ..., "PA4": None}
|
|
223
|
+
print(microSDexp.volume_name()) # volume label, "" when unset
|
|
224
|
+
print(microSDexp.temperature_k()) # whole kelvin; 0xFFFF on error
|
|
225
|
+
print(microSDexp.adc_pa5()) # raw ADC; 0xFFFF on error/unavailable
|
|
226
|
+
|
|
227
|
+
info = microSDexp.card_info()
|
|
228
|
+
print(info.mounted, info.fat_type, info.sector_count, info.sck_hz)
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
`card_present()` returns `True` while a card is inserted — use it as a guard
|
|
232
|
+
before writing, or to drive your own logic. The module also NACKs card commands
|
|
233
|
+
while no card is inserted.
|
|
234
|
+
|
|
235
|
+
`card_info().fat_type` uses filesystem type values: `0` unmounted, `12`/`16`/`32` for
|
|
236
|
+
FAT12/16/32, `64` for exFAT. `sck_hz` is the negotiated card SPI clock — the best
|
|
237
|
+
predictor of throughput. `MICROSDEXP_FAT_*` constants are exported for
|
|
238
|
+
comparisons. `temperature_k()` returns whole kelvin (2-byte LE, rounded to integer K),
|
|
239
|
+
not an ADC count; subtract 273.15 for °C. Both `temperature_k()` and
|
|
240
|
+
`adc_pa5()` return `0xFFFF` on any failure — bus error, short read, or
|
|
241
|
+
unavailable ADC channel — so check for it.
|
|
242
|
+
|
|
243
|
+
### Expansion pins
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
from i2c_microsd_module import protocol
|
|
247
|
+
|
|
248
|
+
microSDexp.set_pin_mode(protocol.MICROSDEXP_PIN_PB2, 0) # mode 0 = digital output
|
|
249
|
+
microSDexp.write_pin(protocol.MICROSDEXP_PIN_PB2, 1) # level 1 = high
|
|
250
|
+
microSDexp.write_pin(protocol.MICROSDEXP_PIN_PB2, 0) # level 0 = low
|
|
251
|
+
|
|
252
|
+
# PWM: select mode 1 on a PWM-capable pin, then send duty 0..255
|
|
253
|
+
microSDexp.set_pin_mode(protocol.MICROSDEXP_PIN_PA5,
|
|
254
|
+
protocol.MICROSDEXP_PIN_MODE_PWM_OUTPUT)
|
|
255
|
+
microSDexp.pwm_write(protocol.MICROSDEXP_PIN_PA5, 128) # ~50% duty
|
|
256
|
+
adc = microSDexp.adc_pa5() # PA5 ADC
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Pin IDs `MICROSDEXP_PIN_PA0` (0), `PA1` (1) and `PA4` (4) join PA5/PB2/PB3;
|
|
260
|
+
`adc_pin(pin)` reads PA5 or PA4. PA0 is digital-only, PA1 adds PWM, PA4 adds
|
|
261
|
+
ADC. ID `7` is reserved. PA5, PB2, PA1 and PA4 produce real PWM; PB3 is a
|
|
262
|
+
threshold output (0..127 LOW, 128..255 HIGH). PA1 and PA4 give true PWM
|
|
263
|
+
when they are present on your board variant.
|
|
264
|
+
|
|
265
|
+
The mode/level literals also have named constants in `protocol`:
|
|
266
|
+
`MICROSDEXP_PIN_MODE_DIGITAL_OUTPUT` (`0`) and
|
|
267
|
+
`MICROSDEXP_PIN_MODE_PWM_OUTPUT` (`1`); `write_pin(..., 0)` = low, any nonzero
|
|
268
|
+
= high (or that duty in PWM mode); `format(0)` = automatic, `format(1)` = force
|
|
269
|
+
FAT rebuild.
|
|
270
|
+
|
|
271
|
+
Pin modes apply in RAM only; persist them (and the I²C address) once when final:
|
|
272
|
+
|
|
273
|
+
```python
|
|
274
|
+
microSDexp.save_pin_mode()
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### WS2812B and bit-bang scripts
|
|
278
|
+
|
|
279
|
+
```python
|
|
280
|
+
import time
|
|
281
|
+
from i2c_microsd_module import protocol
|
|
282
|
+
|
|
283
|
+
frame = bytearray(8 * 3) # GRB, three bytes per pixel
|
|
284
|
+
frame[1] = 40 # first pixel: a little red
|
|
285
|
+
microSDexp.set_pin_mode(protocol.MICROSDEXP_PIN_PA5, 0) # mode 0 = digital output
|
|
286
|
+
microSDexp.bitbang_clear()
|
|
287
|
+
microSDexp.show_neopixels(protocol.MICROSDEXP_PIN_PA5, frame, 8)
|
|
288
|
+
time.sleep(0.003) # keep the bus quiet while it latches
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
The workspace is 256 bytes ⇒ at most 85 pixels. For custom timing, load a token
|
|
292
|
+
program with `bitbang_buffer()` and run it with
|
|
293
|
+
`bitbang_execute(protocol.MICROSDEXP_BITBANG_PROFILE_SCRIPT)`. Tokens are
|
|
294
|
+
`SET_HIGH`, `SET_LOW`, `DELAY_US`, `DELAY_MS`, and `LOOP_GOTO`; see the
|
|
295
|
+
[Bit-Bang Engine wiki](wiki/Bit-Bang-Engine.md).
|
|
296
|
+
|
|
297
|
+
### Housekeeping and errors
|
|
298
|
+
|
|
299
|
+
```python
|
|
300
|
+
microSDexp.set_clock(2026, 11, 18, 12, 0, 0) # software RTC date/time, see below
|
|
301
|
+
microSDexp.reboot()
|
|
302
|
+
microSDexp.format(0) # DESTRUCTIVE; 0 = automatic, 1 = force FAT
|
|
303
|
+
|
|
304
|
+
if microSDexp.last_error != 0:
|
|
305
|
+
...
|
|
306
|
+
if microSDexp.file_open:
|
|
307
|
+
microSDexp.close()
|
|
308
|
+
if microSDexp.sticky_error != 0: ... # fault since clear_error()
|
|
309
|
+
microSDexp.clear_error() # start a fresh error window
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
`last_error` is the result of the *most recent* call and may be overwritten by
|
|
313
|
+
internal calls (for example, a `list_dir()` drain). Use `sticky_error` for the
|
|
314
|
+
first failure since `clear_error()` when you need an error that internal traffic
|
|
315
|
+
cannot hide. `version()` returns `""` with `INVALID` when the banner response
|
|
316
|
+
is empty or incomplete; the getters (`module_clock_hz`, `pin_mode(s)`,
|
|
317
|
+
`volume_name`, `read_clock`) likewise report `INVALID` on a short frame.
|
|
318
|
+
|
|
319
|
+
| Value | `MicroSDExpError` | Meaning |
|
|
320
|
+
|---|---|---|
|
|
321
|
+
| 0 | `OK` | No error |
|
|
322
|
+
| 1 | `NO_MODULE` | Module not responding |
|
|
323
|
+
| 2 | `NO_FILE` | No file open |
|
|
324
|
+
| 3 | `OPEN_FAIL` | Open rejected |
|
|
325
|
+
| 4 | `CARD_ERR` | Card / filesystem error |
|
|
326
|
+
| 5 | `QUEUE_FULL` | Module queue full |
|
|
327
|
+
| 6 | `INVALID` | Bad argument |
|
|
328
|
+
| 7 | `OVERFLOW` | Module buffers full |
|
|
329
|
+
| 8 | `TIMEOUT` | Operation timed out |
|
|
330
|
+
|
|
331
|
+
### Software RTC date/time
|
|
332
|
+
|
|
333
|
+
The module keeps a software RTC used for FAT file timestamps (CMD `0x0E`); it
|
|
334
|
+
keeps advancing once set and returns to the boot default
|
|
335
|
+
`2026-11-01 00:00:00` after a reset. Set a realistic value once at startup if
|
|
336
|
+
you care about file timestamps:
|
|
337
|
+
|
|
338
|
+
```python
|
|
339
|
+
microSDexp.set_clock(2026, 11, 22, 14, 30, 0) # y, mo, d, h, mi, s
|
|
340
|
+
print(microSDexp.read_clock()) # (2026, 11, 22, 14, 30, 0)
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
On the wire the value is six bytes `[year-2000, month, day, hour, minute,
|
|
344
|
+
second]`; `set_clock()` converts the year for you. Fields are checked locally
|
|
345
|
+
(year 2000–2255, month 1–12, day 1–31, hour 0–23, minute/second 0–59):
|
|
346
|
+
an invalid field sets `INVALID` with no I²C traffic. `read_clock()` returns
|
|
347
|
+
the tuple, or `None` with `INVALID` on a short frame. `set_clock_time()` is
|
|
348
|
+
kept as a deprecated alias of `set_clock()`. See `examples/set_clock.py`.
|
|
349
|
+
|
|
350
|
+
---
|
|
351
|
+
|
|
352
|
+
## Performance tips
|
|
353
|
+
|
|
354
|
+
- Use 1 MHz if your board and wiring support it; 400 kHz is a safe default.
|
|
355
|
+
- The negotiated card SPI clock matters more than card capacity. Check
|
|
356
|
+
`card_info().sck_hz`: a 10 MHz card reaches ~30 KiB/s at a 1 MHz bus; a
|
|
357
|
+
1 MHz card sits near 18 KiB/s.
|
|
358
|
+
- CircuitPython is the usual limit — one Python transaction per chunk. The
|
|
359
|
+
interpreter typically reaches about half of what a compiled host can. For the
|
|
360
|
+
fastest reads, raise the read chunk:
|
|
361
|
+
|
|
362
|
+
```python
|
|
363
|
+
microSDexp.set_read_chunk_bytes(256)
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
The default is safe on all boards; 256 reduces the number of STATUS polls
|
|
367
|
+
during a bulk read.
|
|
368
|
+
- `set_poll_interval(ms)` tunes the STATUS poll pause (default 10 ms).
|
|
369
|
+
- Time files after the first one written following a module reset; the card
|
|
370
|
+
re-trains on first use.
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
## Examples
|
|
375
|
+
|
|
376
|
+
| Example | Shows |
|
|
377
|
+
|---|---|
|
|
378
|
+
| `quickstart.py` | Write a file and read it back — start here |
|
|
379
|
+
| `one_shot_files.py` | `write_text` / `append_text` without open/close |
|
|
380
|
+
| `basic_readwrite.py` | 64 KB round trip with CRC32 |
|
|
381
|
+
| `append_and_list.py` | Append writes, directory listing, queries |
|
|
382
|
+
| `simpletest.py` | Probe, version, card info, temperature |
|
|
383
|
+
| `neopixel_blit.py` | WS2812B strip via the bit-bang engine |
|
|
384
|
+
| `bitbang_script.py` | Token scripts for timed pin edges |
|
|
385
|
+
| `sweep.py` | Throughput at 100 kHz / 400 kHz / 1 MHz |
|
|
386
|
+
| `exhaustive.py` | Full command-table acceptance test (format step opt-in) |
|
|
387
|
+
| `abrupt_recovery.py` | Recover from mid-transfer aborts, plus a directory/FAT metadata check |
|
|
388
|
+
| `pwm_fade.py` | PWM dimming from an expansion pin |
|
|
389
|
+
| `diagnostics.py` | One-page health report |
|
|
390
|
+
| `set_clock.py` | Set the software RTC date/time for correct FAT file timestamps |
|
|
391
|
+
| `nonblocking_write.py` | Non-blocking submit/poll write |
|
|
392
|
+
| `sensor_ring_logger.py` | 500 Hz sampling with an SRAM ring buffer |
|
|
393
|
+
| `config_save_load.py` | Checked queries and staged settings updates |
|
|
394
|
+
| `dual_module_mirror.py` | Two modules, one bus, `sticky_error` failover |
|
|
395
|
+
| `threshold_bitbang_alert.py` | CSV logging + module-side strobe alert |
|
|
396
|
+
|
|
397
|
+
Each example is a standalone program — copy it to `CIRCUITPY` as `code.py`.
|
|
398
|
+
|
|
399
|
+
---
|
|
400
|
+
|
|
401
|
+
## Troubleshooting
|
|
402
|
+
|
|
403
|
+
| Symptom | Likely cause | Fix |
|
|
404
|
+
|---|---|---|
|
|
405
|
+
| `begin()` returns False | Wrong pins, no common GND, mixed power | Check the bus; common GND; one supply only |
|
|
406
|
+
| Card not mounted | exFAT or unformatted card | Call `format()` once — the module rebuilds the FAT in place; no PC needed |
|
|
407
|
+
| `NO_CARD` | No card inserted | Insert a card; `card_present()` reports the state |
|
|
408
|
+
| `TIMEOUT` | Bus too slow | Lower the frequency; timeouts scale with the clock |
|
|
409
|
+
| Short reads / NACKs on a Raspberry Pi (Pi 4 or earlier) | Broadcom BSC silicon bug with clock stretching | Add `dtoverlay=i2c-gpio` to `/boot/config.txt`, or use a Pi 5 |
|
|
410
|
+
| `OVERFLOW` | Writing faster than the module commits | Slow down; the library flow-paces |
|
|
411
|
+
| Short reads | Reading before completion | Use `f.read()` in a loop until it returns `b""` |
|
|
412
|
+
| Strip stays dark | Pin not an output / wrong data pin | `set_pin_mode(pin, 0)` first |
|
|
413
|
+
| Old pixels remain | Command sent mid-frame | Delay a frame time after `show_neopixels()` |
|
|
414
|
+
| Stuck in `ERROR` / `BUSY_SD` after a host bug | Malformed or out-of-order traffic latched the module state — it did not crash | **Last resort:** `reboot()` (a `0x2F` write; the module self-resets). Retrying alone will not clear it |
|
|
415
|
+
|
|
416
|
+
Deep protocol detail is in the [wiki](wiki/Home.md). For the Broadcom host bug
|
|
417
|
+
and its workaround, see
|
|
418
|
+
[Clock stretching and Raspberry Pi hosts](wiki/Protocol-Reference.md#clock-stretching-and-raspberry-pi-hosts).
|
|
419
|
+
|
|
420
|
+
---
|
|
421
|
+
|
|
422
|
+
## Blocking behavior (important)
|
|
423
|
+
|
|
424
|
+
Most high-level methods are **synchronous**: they poll the module's STATUS
|
|
425
|
+
frame in a short sleep/poll loop until the operation completes or a timeout
|
|
426
|
+
expires. They do not sleep the whole time (the I²C bus stays responsive at the
|
|
427
|
+
protocol level), but your application **is blocked** for the duration. Plan for
|
|
428
|
+
this with real-time loops, cooperative schedulers and ESP32 tasks.
|
|
429
|
+
|
|
430
|
+
Timeouts scale up on slow buses (`base × 100000 / I2C_Hz`, floor `base`, 2× below
|
|
431
|
+
50 kHz) and are **capped at 120 s** by the host. `format()` is the exception: it
|
|
432
|
+
uses an unscaled 300 s budget. The module's diagnostics report shows the
|
|
433
|
+
configured bus clock.
|
|
434
|
+
|
|
435
|
+
| Method | Typical wait | Worst case |
|
|
436
|
+
|---|---|---|
|
|
437
|
+
| `begin()` | ~1.5 s | 10 mount attempts × 150 ms + STATUS probes |
|
|
438
|
+
| `read_status()` | one I²C transaction | no loop |
|
|
439
|
+
| `wait_for_op_complete()` | 15 s | 120 s cap |
|
|
440
|
+
| `wait_for_committed()` | 30 s | 120 s cap |
|
|
441
|
+
| `open_write()` / `open_read()` | one op wait | up to 6 × (close-flush barrier + op wait + 200 ms) — potentially minutes |
|
|
442
|
+
| `open_append()` | none | local state only |
|
|
443
|
+
| `write()` | ~0.8 s per 64 B block (flow poll) | 400 attempts × 2 ms per block; append adds one op wait per chunk |
|
|
444
|
+
| `read()` | until bytes arrive | 30 s, up to 120 s cap |
|
|
445
|
+
| `sync()` | until bytes commit | 30 s, up to 120 s cap |
|
|
446
|
+
| `close()` | sync + one op wait | ~45 s typical, up to two capped waits |
|
|
447
|
+
| `format()` | destructive, long | 300 s (unscaled; not subject to the 120 s cap) |
|
|
448
|
+
| `mkdir/rmdir/remove/exists/is_directory/file_size/version/firmware_banner/card_info/list_dir` | one op wait | 15 s, up to 120 s cap |
|
|
449
|
+
| `module_clock_hz()` / `firmware_clock_hz()` / `pin_mode(s)()` / `volume_name()` / `set_clock()` / `read_clock()` | one I²C transaction | no wait loops |
|
|
450
|
+
| `bitbang_*()` / `show_neopixels()` / `save_pin_mode()` | one I²C transaction | module-side execution is not awaited |
|
|
451
|
+
| `diagnostics()` | a few I²C reads | no wait loops |
|
|
452
|
+
|
|
453
|
+
If your board has a hardware reset timer, service it around `format()` and the
|
|
454
|
+
open/write/sync/close group. If you cannot afford to block, use the
|
|
455
|
+
non-blocking API below (no feature flag needed).
|
|
456
|
+
|
|
457
|
+
## Checked queries (return semantics)
|
|
458
|
+
|
|
459
|
+
`exists()` / `file_size()` / `is_directory()` return a value even when the I²C
|
|
460
|
+
command fails, so `False` / `0` can mean either "no such file" or "communication
|
|
461
|
+
error". Use the checked variants when that distinction matters — they return
|
|
462
|
+
`(ok, value)`:
|
|
463
|
+
|
|
464
|
+
```python
|
|
465
|
+
ok, present = microSDexp.exists_checked("A.TXT")
|
|
466
|
+
ok, size = microSDexp.file_size_checked("A.TXT")
|
|
467
|
+
ok, isdir = microSDexp.is_directory_checked("DIR")
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
`ok` is `False` on I²C/card/path error; inspect `last_error` then. `last_ok` is
|
|
471
|
+
a shorthand for `last_error == MicroSDExpError.OK`. The original methods are
|
|
472
|
+
unchanged and remain convenient when a failed query can simply mean "no".
|
|
473
|
+
|
|
474
|
+
## Diagnostics
|
|
475
|
+
|
|
476
|
+
`diagnostics()` returns a one-page report for troubleshooting:
|
|
477
|
+
|
|
478
|
+
```python
|
|
479
|
+
microSDexp.begin()
|
|
480
|
+
print(microSDexp.diagnostics())
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
```text
|
|
484
|
+
I2C microSD Expansion
|
|
485
|
+
---------------------
|
|
486
|
+
Module detected: yes
|
|
487
|
+
Current Date/Time: 2026-11-01 00:00:00
|
|
488
|
+
Protocol version: 2
|
|
489
|
+
Firmware version: v<version>
|
|
490
|
+
I2C microSD Expansion
|
|
491
|
+
Copyright (C) 2026 syntax1269@gmail.com
|
|
492
|
+
SD card detected: yes
|
|
493
|
+
Filesystem: FAT32
|
|
494
|
+
Volume Name: MYCARD
|
|
495
|
+
Capacity: 3810.0 MiB / 3.72 GiB
|
|
496
|
+
Free space: 3801.9 MiB / 3.71 GiB
|
|
497
|
+
I2C address: 0x6E
|
|
498
|
+
I2C speed: 400 kHz (bus)
|
|
499
|
+
Module Clock: 20000000 Hz
|
|
500
|
+
PIN PA5: DIGITAL_OUTPUT
|
|
501
|
+
PIN PB2: DIGITAL_OUTPUT
|
|
502
|
+
PIN PB3: DIGITAL_OUTPUT
|
|
503
|
+
PIN PA0: absent
|
|
504
|
+
PIN PA1: absent
|
|
505
|
+
PIN PA4: absent
|
|
506
|
+
Communication: OK
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
It is safe to call before `begin()`; unreadable fields show as `n/a`. The call
|
|
510
|
+
does not change `last_error`.
|
|
511
|
+
|
|
512
|
+
## Non-blocking API
|
|
513
|
+
|
|
514
|
+
If your application cannot block, the submit/poll API performs no sleeps:
|
|
515
|
+
|
|
516
|
+
```python
|
|
517
|
+
from i2c_microsd_module import I2CmicroSD, AsyncOp, ASYNC_BUSY, ASYNC_OK
|
|
518
|
+
|
|
519
|
+
i2c = busio.I2C(board.SCL, board.SDA, frequency=400000)
|
|
520
|
+
microSDexp = I2CmicroSD(i2c, address=0x6E)
|
|
521
|
+
microSDexp.begin()
|
|
522
|
+
|
|
523
|
+
op = AsyncOp()
|
|
524
|
+
microSDexp.begin_open_write("ASYNC.TXT", op)
|
|
525
|
+
while microSDexp.poll_op(op) == ASYNC_BUSY:
|
|
526
|
+
pass # do other work here
|
|
527
|
+
|
|
528
|
+
sent = 0
|
|
529
|
+
while sent < len(data):
|
|
530
|
+
sent += microSDexp.write_now(data[sent:]) # 0 = try again next pass
|
|
531
|
+
|
|
532
|
+
target = microSDexp.committed_bytes()
|
|
533
|
+
while microSDexp.committed_bytes() < target:
|
|
534
|
+
pass
|
|
535
|
+
|
|
536
|
+
op = AsyncOp()
|
|
537
|
+
microSDexp.begin_close(op)
|
|
538
|
+
while microSDexp.poll_op(op) == ASYNC_BUSY:
|
|
539
|
+
pass
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
`begin_open_write`, `begin_open_read`, `begin_close`, `begin_format` and
|
|
543
|
+
`begin_query` submit a command and return immediately. `poll_op()` performs one
|
|
544
|
+
STATUS read (`ASYNC_BUSY`, `ASYNC_OK`, or a module error code). `write_now()`
|
|
545
|
+
and `read_now()` move only what fits right now — zero/empty means "try again",
|
|
546
|
+
not failure. `committed_bytes()` reports the commit counter. Only one async
|
|
547
|
+
operation may be pending at a time.
|
|
548
|
+
|
|
549
|
+
No compile-time flag is needed (unlike the Arduino port); the blocking methods
|
|
550
|
+
remain available and unchanged. See `nonblocking_write.py`.
|
|
551
|
+
|
|
552
|
+
## Documentation
|
|
553
|
+
|
|
554
|
+
Full API documentation is available at https://i2c-microsd-expansion-circuitpython.readthedocs.io/ and can be built locally with `Sphinx <https://www.sphinx-doc.org/>`_ from `docs/`.
|
|
555
|
+
|
|
556
|
+
---
|
|
557
|
+
|
|
558
|
+
## Ownership
|
|
559
|
+
|
|
560
|
+
This port is owned and maintained solely by [syntax1269](https://github.com/syntax1269).
|
|
561
|
+
|
|
562
|
+
## License
|
|
563
|
+
|
|
564
|
+
MIT. See [LICENSE](LICENSE). Not affiliated with the SD Association.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
i2c_microsd_expansion_circuitpython-1.0.13.dist-info/licenses/LICENSE,sha256=mWFWEQmATc0KgoAR-j6LuodUZxyPmYyzrpG8JJY2SnE,1091
|
|
2
|
+
i2c_microsd_module/__init__.py,sha256=AZU6P7gVmrYLsLvKEd3otCiKJqob5rX-q22qX-ZlukA,1508
|
|
3
|
+
i2c_microsd_module/config.py,sha256=a2oamR5SCcNNbLTGIMIb_qYYF_uCvAh1ztq4WB1DnYQ,1719
|
|
4
|
+
i2c_microsd_module/module.py,sha256=ApN_z8CqZjqrAuzYa17F-7Je1wgszWBftLYGjk-5WZE,68498
|
|
5
|
+
i2c_microsd_module/protocol.py,sha256=CdkVWBSwBAkgssV_PDvhtq53vuBKEwFXxfTN6n7str0,4843
|
|
6
|
+
i2c_microsd_expansion_circuitpython-1.0.13.dist-info/METADATA,sha256=AT-nRXiu-loplgIF1U3RDZRlyMhYAL4LjavHnDp7CO4,23409
|
|
7
|
+
i2c_microsd_expansion_circuitpython-1.0.13.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
8
|
+
i2c_microsd_expansion_circuitpython-1.0.13.dist-info/top_level.txt,sha256=MWQYJtTeSklO9SrIJvcyJbuJ-7B3H2snMdMkBNtPvx8,19
|
|
9
|
+
i2c_microsd_expansion_circuitpython-1.0.13.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 I2C microSD Expansion contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
i2c_microsd_module
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 I2C microSD Expansion contributors
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""i2c_microsd_module — host library for the I²C microSD Expansion.
|
|
5
|
+
|
|
6
|
+
CircuitPython port of the I²C microSD Expansion Arduino host library
|
|
7
|
+
(Arduino ``Wire`` library). Uses protocol revision 2 for the file API,
|
|
8
|
+
pin control, the bit-bang engine and buffered-frame NeoPixel output.
|
|
9
|
+
|
|
10
|
+
The sketch owns the I2C bus (like ``Wire.begin()`` on Arduino)::
|
|
11
|
+
|
|
12
|
+
import board, busio
|
|
13
|
+
from i2c_microsd_module import I2CmicroSD
|
|
14
|
+
|
|
15
|
+
i2c = busio.I2C(board.GP1, board.GP0, frequency=400000) # bus owns the clock
|
|
16
|
+
microSDexp = I2CmicroSD(i2c) # follows i2c.frequency
|
|
17
|
+
microSDexp.begin()
|
|
18
|
+
|
|
19
|
+
The library itself has no ``print()`` calls — all output is sketch-owned,
|
|
20
|
+
mirroring the Arduino library rule (zero ``Serial`` in ``src/``).
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from .module import (
|
|
24
|
+
MicroSDExpError,
|
|
25
|
+
MicroSDExpFile,
|
|
26
|
+
CardInfo,
|
|
27
|
+
Capabilities,
|
|
28
|
+
DirEntry,
|
|
29
|
+
I2CmicroSD,
|
|
30
|
+
StatusFrame,
|
|
31
|
+
open_file,
|
|
32
|
+
AsyncOp,
|
|
33
|
+
ASYNC_OK,
|
|
34
|
+
ASYNC_BUSY,
|
|
35
|
+
ASYNC_IOERR,
|
|
36
|
+
)
|
|
37
|
+
from . import protocol
|
|
38
|
+
from . import config
|
|
39
|
+
|
|
40
|
+
__version__ = "1.0.13"
|
|
41
|
+
__repo__ = "https://github.com/syntax1269/I2C_microSD_Expansion_CircuitPython"
|
|
42
|
+
|
|
43
|
+
__all__ = [
|
|
44
|
+
"MicroSDExpError",
|
|
45
|
+
"MicroSDExpFile",
|
|
46
|
+
"CardInfo",
|
|
47
|
+
"Capabilities",
|
|
48
|
+
"DirEntry",
|
|
49
|
+
"I2CmicroSD",
|
|
50
|
+
"StatusFrame",
|
|
51
|
+
"open_file",
|
|
52
|
+
"AsyncOp",
|
|
53
|
+
"ASYNC_OK",
|
|
54
|
+
"ASYNC_BUSY",
|
|
55
|
+
"ASYNC_IOERR",
|
|
56
|
+
"protocol",
|
|
57
|
+
"config",
|
|
58
|
+
"__version__",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|