sdcard

package
v0.0.0-...-0087ba1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2023 License: BSD-3-Clause Imports: 3 Imported by: 0

README

SPI sdcard/mmc driver

This package provides the driver for sdcard/mmc with SPI connection.

To use a file system on the SDcard, please see the TinyFS repo:

https://github.com/tinygo-org/tinyfs

See examples/sdcard/console for a low-level access example.

Stack size

If you use this package, you need to set default-stack-size in targets/*.json.
For example, targets/wioterminal.json has the following configuration.

{
    "inherits": ["atsamd51p19a"],
    "build-tags": ["wioterminal"],
    "flash-1200-bps-reset": "true",
    "flash-method": "msd",
    "msd-volume-name": "Arduino",
    "msd-firmware-name": "firmware.uf2",
    "default-stack-size": 2048
}

Documentation

Overview

package sdcard provides a TinyGo driver for sdcard/mmc devices using a SPI connection.

To use a file system on the SDcard, please see the TinyFS repo:

https://github.com/tinygo-org/tinyfs

Index

Constants

View Source
const (
	CMD0_GO_IDLE_STATE              = 0
	CMD1_SEND_OP_CND                = 1
	CMD2_ALL_SEND_CID               = 2
	CMD3_SEND_RELATIVE_ADDR         = 3
	CMD4_SET_DSR                    = 4
	CMD6_SWITCH_FUNC                = 6
	CMD7_SELECT_DESELECT_CARD       = 7
	CMD8_SEND_IF_COND               = 8
	CMD9_SEND_CSD                   = 9
	CMD10_SEND_CID                  = 10
	CMD12_STOP_TRANSMISSION         = 12
	CMD13_SEND_STATUS               = 13
	CMD15_GO_INACTIVE_STATE         = 15
	CMD16_SET_BLOCKLEN              = 16
	CMD17_READ_SINGLE_BLOCK         = 17
	CMD18_READ_MULTIPLE_BLOCK       = 18
	CMD24_WRITE_BLOCK               = 24
	CMD25_WRITE_MULTIPLE_BLOCK      = 25
	CMD27_PROGRAM_CSD               = 27
	CMD28_SET_WRITE_PROT            = 28
	CMD29_CLR_WRITE_PROT            = 29
	CMD30_SEND_WRITE_PROT           = 30
	CMD32_ERASE_WR_BLK_START_ADDR   = 32
	CMD33_ERASE_WR_BLK_END_ADDR     = 33
	CMD38_ERASE                     = 38
	CMD42_LOCK_UNLOCK               = 42
	CMD55_APP_CMD                   = 55
	CMD56_GEN_CMD                   = 56
	CMD58_READ_OCR                  = 58
	CMD59_CRC_ON_OFF                = 59
	ACMD6_SET_BUS_WIDTH             = 6
	ACMD13_SD_STATUS                = 13
	ACMD22_SEND_NUM_WR_BLOCKS       = 22
	ACMD23_SET_WR_BLK_ERASE_COUNT   = 23
	ACMD41_SD_APP_OP_COND           = 41
	ACMD42_SET_CLR_CARD_DETECT      = 42
	ACMD51_SEND_SCR                 = 51
	ACMD18_SECURE_READ_MULTI_BLOCK  = 18
	ACMD25_SECURE_WRITE_MULTI_BLOCK = 25
	ACMD26_SECURE_WRITE_MKB         = 26
	ACMD38_SECURE_ERASE             = 38
	ACMD43_GET_MKB                  = 43
	ACMD44_GET_MID                  = 44
	ACMD45_SET_CER_RN1              = 45
	ACMD46_SET_CER_RN2              = 46
	ACMD47_SET_CER_RES2             = 47
	ACMD48_SET_CER_RES1             = 48
	ACMD49_CHANGE_SECURE_AREA       = 49
)
View Source
const (

	// card types
	SD_CARD_TYPE_SD1  = 1 // Standard capacity V1 SD card
	SD_CARD_TYPE_SD2  = 2 // Standard capacity V2 SD card
	SD_CARD_TYPE_SDHC = 3 // High Capacity SD card
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CID

type CID struct {
	//// byte 0
	ManufacturerID byte
	//uint8_t mid;  // Manufacturer ID
	//// byte 1-2
	OEMApplicationID uint16
	//char oid[2];  // OEM/Application ID
	//// byte 3-7
	ProductName string
	//char pnm[5];  // Product name
	//// byte 8
	ProductVersion string
	//unsigned prv_m : 4;  // Product revision n.m
	//unsigned prv_n : 4;
	//// byte 9-12
	ProductSerialNumber uint32
	//uint32_t psn;  // Product serial number
	//// byte 13
	ManufacturingYear  byte
	ManufacturingMonth byte
	//unsigned mdt_year_high : 4;  // Manufacturing date
	//unsigned reserved : 4;
	//// byte 14
	//unsigned mdt_month : 4;
	//unsigned mdt_year_low : 4;
	//// byte 15
	Always1 byte
	//unsigned always1 : 1;
	CRC byte
}

func NewCID

func NewCID(buf []byte) *CID

type CSD

type CSD struct {
	CSD_STRUCTURE      byte   //  2 R  [127:126]   0x01 : CSD Structure
	TAAC               byte   //  8 R  [119:112]   0x0E : Data Read Access-Time-1
	NSAC               byte   //  8 R  [111:104]   0x00 : Data Read Access-Time-2 in CLK Cycles (NSAC*100)
	TRAN_SPEED         byte   //  8 R  [103:96]    0x5A : Max. Data Transfer Rate
	CCC                uint16 // 12 R  [95:84]    0x5B5 : Card Command Classes
	READ_BL_LEN        byte   //  4 R  [83:80]     0x09 : Max. Read Data Block Length
	READ_BL_PARTIAL    byte   //  1 R  [79:79]     0x00 : Partial Blocks for Read Allowed
	WRITE_BLK_MISALIGN byte   //  1 R  [78:78]     0x00 : Write Block Misalignment
	READ_BLK_MISALIGN  byte   //  1 R  [77:77]     0x00 : Read Block Misalignment
	DSR_IMP            byte   //  1 R  [76:76]     0x00 : DSR Implemented
	C_SIZE             uint32 // 22 R  [69:48] 0xXXXXXX : Device Size
	ERASE_BLK_EN       byte   //  1 R  [46:46]     0x01 : Erase Single Block Enable
	SECTOR_SIZE        byte   //  7 R  [45:39]     0x7F : Erase Sector Size
	WP_GRP_SIZE        byte   //  7 R  [38:32]     0x00 : Write Protect Group Size
	WP_GRP_ENABLE      byte   //  1 R  [31:31]     0x00 : Write Protect Group Enable
	R2W_FACTOR         byte   //  3 R  [28:26]     0x02 : Write Speed Factor
	WRITE_BL_LEN       byte   //  4 R  [25:22]     0x09 : Max. Write data Block Length
	WRITE_BL_PARTIAL   byte   //  1 R  [21:21]     0x00 : Partial Blocks for Write Allowed
	FILE_FORMAT_GRP    byte   //  1 R  [15:15]     0x00 : File Format Group
	COPY               byte   //  1 RW [14:14]     0x00 :Copy Flag
	PERM_WRITE_PROTECT byte   //  1 RW [13:13]     0x00 : Permanent Write Protection
	TMP_WRITE_PROTECT  byte   //  1 RW [12:12]     0x00 : Temporary Write Protection
	FILE_FORMAT        byte   //  2 R  [11:10]     0x00 : File Format
	CRC                byte   //  7 RW [7:1]       0xXX : CRC
}

func NewCSD

func NewCSD(buf []byte) *CSD

func (*CSD) Dump

func (c *CSD) Dump()

func (*CSD) Sectors

func (c *CSD) Sectors() (int64, error)

func (*CSD) Size

func (c *CSD) Size() uint64

type Device

type Device struct {
	CID *CID
	CSD *CSD
	// contains filtered or unexported fields
}

func New

func New(b *machine.SPI, sck, sdo, sdi, cs machine.Pin) Device

func (*Device) Configure

func (d *Device) Configure() error

func (*Device) EraseBlockSize

func (dev *Device) EraseBlockSize() int64

EraseBlockSize returns the smallest erasable area on this sdcard in bytes.

func (*Device) EraseBlocks

func (dev *Device) EraseBlocks(start, len int64) error

EraseBlocks erases the given number of blocks.

func (*Device) ReadAt

func (dev *Device) ReadAt(buf []byte, addr int64) (int, error)

ReadAt reads the given number of bytes from the sdcard.

func (Device) ReadCID

func (d Device) ReadCID(csd []byte) error

ReadCID reads the CID using CMD10

func (Device) ReadCSD

func (d Device) ReadCSD(csd []byte) error

ReadCSD reads the CSD using CMD9.

func (Device) ReadData

func (d Device) ReadData(block uint32, dst []byte) error

ReadData reads 512 bytes from sdcard into dst.

func (*Device) Size

func (dev *Device) Size() int64

Size returns the number of bytes in this sdcard.

func (*Device) WriteAt

func (dev *Device) WriteAt(buf []byte, addr int64) (n int, err error)

WriteAt writes the given number of bytes to sdcard.

func (*Device) WriteBlockSize

func (dev *Device) WriteBlockSize() int64

WriteBlockSize returns the block size in which data can be written to memory.

func (Device) WriteData

func (d Device) WriteData(block uint32, src []byte) error

WriteData writes 512 bytes from dst to sdcard.

func (Device) WriteMulti

func (d Device) WriteMulti(buf []byte) error

WriteMulti performs continuous writing. It is necessary to call WriteMultiStart() in prior.

func (Device) WriteMultiStart

func (d Device) WriteMultiStart(block uint32) error

WriteMultiStart starts the continuous write mode using CMD25.

func (Device) WriteMultiStop

func (d Device) WriteMultiStop() error

WriteMultiStop exits the continuous write mode.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL