Skip to content

Commit e1a05c6

Browse files
committed
updated info docstring; sphinx formatting
1 parent 076d48e commit e1a05c6

File tree

1 file changed

+93
-49
lines changed

1 file changed

+93
-49
lines changed

adafruit_sdcard.py

Lines changed: 93 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,30 @@
2929
Requires an SPI bus and a CS pin. Provides readblocks and writeblocks
3030
methods so the device can be mounted as a filesystem.
3131
32-
Example usage:
32+
* Author(s): Scott Shawcroft
3333
34-
.. code-block:: python
34+
Implementation Notes
35+
--------------------
3536
36-
import busio
37-
import storage
38-
import adafruit_sdcard
39-
import os
40-
import board
37+
**Hardware:**
4138
42-
spi = busio.SPI(SCK, MOSI, MISO)
43-
sd = adafruit_sdcard.SDCard(spi, board.SD_CS)
44-
vfs = storage.VfsFat(sdcard)
45-
storage.mount(vfs, '/sd')
46-
os.listdir('/')
39+
* Adafruit `MicroSD card breakout board+
40+
<https://www.adafruit.com/product/254>`_ (Product ID: 254)
4741
48-
* Author(s): Scott Shawcroft
42+
* Adafruit `Assembled Data Logging shield for Arduino
43+
<https://www.adafruit.com/product/1141>`_ (Product ID: 1141)
44+
45+
* Adafruit `Feather M0 Adalogger
46+
<https://www.adafruit.com/product/2796>`_ (Product ID: 2796)
47+
48+
* Adalogger `FeatherWing - RTC + SD Add-on For All Feather Boards
49+
<https://www.adafruit.com/product/2922>`_ (Product ID: 2922)
50+
51+
**Software and Dependencies:**
52+
53+
* Adafruit CircuitPython firmware for the ESP8622 and M0-based boards:
54+
https://github.com/adafruit/circuitpython/releases
55+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4956
"""
5057

5158
import time
@@ -73,7 +80,25 @@ class SDCard:
7380
"""Controls an SD card over SPI.
7481
7582
:param ~busio.SPI spi: The SPI bus
76-
:param ~digitalio.DigitalInOut cs: The chip select connected to the card"""
83+
:param ~digitalio.DigitalInOut cs: The chip select connected to the card
84+
85+
Example usage:
86+
87+
.. code-block:: python
88+
89+
import busio
90+
import storage
91+
import adafruit_sdcard
92+
import os
93+
import board
94+
95+
spi = busio.SPI(SCK, MOSI, MISO)
96+
sd = adafruit_sdcard.SDCard(spi, board.SD_CS)
97+
vfs = storage.VfsFat(sdcard)
98+
storage.mount(vfs, '/sd')
99+
os.listdir('/')
100+
101+
"""
77102
def __init__(self, spi, cs):
78103
# This is the init baudrate. We create a second device for high speed.
79104
self._spi = spi_device.SPIDevice(spi, cs, baudrate=250000, extra_clocks=8)
@@ -88,9 +113,10 @@ def __init__(self, spi, cs):
88113
self._init_card()
89114

90115
def _clock_card(self, cycles=8):
91-
"""Clock the bus a minimum of `cycles` with the chip select high.
116+
"""
117+
Clock the bus a minimum of `cycles` with the chip select high.
92118
93-
:param int cycles: The minimum number of clock cycles to cycle the bus.
119+
:param int cycles: The minimum number of clock cycles to cycle the bus.
94120
"""
95121
while not self._spi.spi.try_lock():
96122
pass
@@ -177,24 +203,28 @@ def _init_card_v2(self):
177203
raise OSError("timeout waiting for v2 card")
178204

179205
def _wait_for_ready(self, spi, timeout=0.3):
180-
"""Wait for the card to clock out 0xff to indicate its ready.
206+
"""
207+
Wait for the card to clock out 0xff to indicate its ready.
181208
182-
:param busio.SPI spi: The locked SPI bus.
183-
:param float timeout: Maximum time to wait in seconds."""
209+
:param busio.SPI spi: The locked SPI bus.
210+
:param float timeout: Maximum time to wait in seconds.
211+
"""
184212
start_time = time.monotonic()
185213
self._single_byte[0] = 0x00
186214
while time.monotonic() - start_time < timeout and self._single_byte[0] != 0xff:
187215
spi.readinto(self._single_byte, write_value=0xff)
188216

189217
#pylint: disable-msg=too-many-arguments
190218
def _cmd(self, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait=True):
191-
"""Issue a command to the card and read an optional data response.
219+
"""
220+
Issue a command to the card and read an optional data response.
192221
193-
:param int cmd: The command number.
194-
:param int arg: The command argument.
195-
:param int crc: The crc to allow the card to verify the command and argument.
196-
:param bytearray response_buf: Buffer to read a data block response into.
197-
:param bool data_block: True if the response data is in a data block."""
222+
:param int cmd: The command number.
223+
:param int arg: The command argument.
224+
:param int crc: The crc to allow the card to verify the command and argument.
225+
:param bytearray response_buf: Buffer to read a data block response into.
226+
:param bool data_block: True if the response data is in a data block.
227+
"""
198228
# create and send the command
199229
buf = self._cmdbuf
200230
buf[0] = 0x40 | cmd
@@ -229,11 +259,13 @@ def _cmd(self, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait=True)
229259
#pylint: enable-msg=too-many-arguments
230260

231261
def _block_cmd(self, cmd, block, crc, response_buf=None):
232-
"""Issue a command to the card with a block argument.
262+
"""
263+
Issue a command to the card with a block argument.
233264
234-
:param int cmd: The command number.
235-
:param int block: The relevant block.
236-
:param int crc: The crc to allow the card to verify the command and argument."""
265+
:param int cmd: The command number.
266+
:param int block: The relevant block.
267+
:param int crc: The crc to allow the card to verify the command and argument.
268+
"""
237269
if self._cdv == 1:
238270
return self._cmd(cmd, block, crc, response_buf=response_buf)
239271

@@ -268,9 +300,11 @@ def _block_cmd(self, cmd, block, crc, response_buf=None):
268300
return result
269301

270302
def _cmd_nodata(self, cmd, response=0xff):
271-
"""Issue a command to the card with no argument.
303+
"""
304+
Issue a command to the card with no argument.
272305
273-
:param int cmd: The command number."""
306+
:param int cmd: The command number.
307+
"""
274308
buf = self._cmdbuf
275309
buf[0] = cmd
276310
buf[1] = 0xff
@@ -284,11 +318,13 @@ def _cmd_nodata(self, cmd, response=0xff):
284318
return 1 # timeout
285319

286320
def _readinto(self, buf, start=0, end=None):
287-
"""Read a data block into buf.
321+
"""
322+
Read a data block into buf.
288323
289-
:param bytearray buf: The buffer to write into
290-
:param int start: The first index to write data at
291-
:param int end: The index after the last byte to write to"""
324+
:param bytearray buf: The buffer to write into
325+
:param int start: The first index to write data at
326+
:param int end: The index after the last byte to write to.
327+
"""
292328
if end is None:
293329
end = len(buf)
294330
with self._spi as spi:
@@ -303,12 +339,14 @@ def _readinto(self, buf, start=0, end=None):
303339
spi.readinto(self._cmdbuf, end=2, write_value=0xff)
304340

305341
def _write(self, token, buf, start=0, end=None):
306-
"""Write a data block to the card.
342+
"""
343+
Write a data block to the card.
307344
308-
:param int token: The start token
309-
:param bytearray buf: The buffer to write from
310-
:param int start: The first index to read data from
311-
:param int end: The index after the last byte to read from"""
345+
:param int token: The start token
346+
:param bytearray buf: The buffer to write from
347+
:param int start: The first index to read data from
348+
:param int end: The index after the last byte to read from.
349+
"""
312350
cmd = self._cmdbuf
313351
if end is None:
314352
end = len(buf)
@@ -340,17 +378,21 @@ def _write(self, token, buf, start=0, end=None):
340378
return 0 # worked
341379

342380
def count(self):
343-
"""Returns the total number of sectors.
381+
"""
382+
Returns the total number of sectors.
344383
345-
:return: The number of 512-byte blocks
346-
:rtype: int"""
384+
:return: The number of 512-byte blocks
385+
:rtype: int
386+
"""
347387
return self._sectors
348388

349389
def readblocks(self, start_block, buf):
350-
"""Read one or more blocks from the card
390+
"""
391+
Read one or more blocks from the card
351392
352-
:param int start_block: The block to start reading from
353-
:param bytearray buf: The buffer to write into. Length must be multiple of 512."""
393+
:param int start_block: The block to start reading from
394+
:param bytearray buf: The buffer to write into. Length must be multiple of 512.
395+
"""
354396
nblocks, err = divmod(len(buf), 512)
355397
assert nblocks and not err, 'Buffer length is invalid'
356398
if nblocks == 1:
@@ -372,10 +414,12 @@ def readblocks(self, start_block, buf):
372414
return 0
373415

374416
def writeblocks(self, start_block, buf):
375-
"""Write one or more blocks to the card
417+
"""
418+
Write one or more blocks to the card
376419
377-
:param int start_block: The block to start writing to
378-
:param bytearray buf: The buffer to write into. Length must be multiple of 512."""
420+
:param int start_block: The block to start writing to
421+
:param bytearray buf: The buffer to write into. Length must be multiple of 512.
422+
"""
379423
nblocks, err = divmod(len(buf), 512)
380424
assert nblocks and not err, 'Buffer length is invalid'
381425
if nblocks == 1:

0 commit comments

Comments
 (0)