Documentation ¶
Overview ¶
Package sdcard provides low-level primitives to handle Secure Digital Card protocol. The sdmc and sdio subpacckages provide higher level interface for respectively SD Memory and SDIO cards.
Index ¶
- Constants
- Variables
- type BusWidth
- type BusWidths
- type CID
- type CSD
- func (csd CSD) CCC() uint16
- func (csd CSD) COPY() bool
- func (csd CSD) CRC() byte
- func (csd CSD) C_SIZE() int64
- func (csd CSD) DSR_IMP() bool
- func (csd CSD) ERASE_BLK_EN() bool
- func (csd CSD) FILE_FORMAT() FileFormat
- func (csd CSD) NSAC() int
- func (csd CSD) PERM_WRITE_PROTECT() bool
- func (csd CSD) R2W_FACTOR() int
- func (csd CSD) READ_BLK_MISALIGN() bool
- func (csd CSD) READ_BL_LEN() int
- func (csd CSD) READ_BL_PARTIAL() bool
- func (csd CSD) SECTOR_SIZE() int
- func (csd CSD) TAAC() int
- func (csd CSD) TMP_WRITE_PROTECT() bool
- func (csd CSD) TRAN_SPEED() int
- func (csd CSD) Version() int
- func (csd CSD) WP_GRP_ENABLE() bool
- func (csd CSD) WP_GRP_SIZE() int
- func (csd CSD) WRITE_BLK_MISALIGN() bool
- func (csd CSD) WRITE_BL_LEN() int
- func (csd CSD) WRITE_BL_PARTIAL() bool
- type CardStatus
- type CmdSupport
- type Command
- func ACMD41(ocr OCR) (Command, uint32)
- func ACMD42(pullUp bool) (Command, uint32)
- func ACMD51() (Command, uint32)
- func ACMD6(bw BusWidth) (Command, uint32)
- func CMD0() (Command, uint32)
- func CMD12() (Command, uint32)
- func CMD13(rca uint16, reg StatusReg) (Command, uint32)
- func CMD16(blen int) (Command, uint32)
- func CMD17(addr uint) (Command, uint32)
- func CMD18(addr uint) (Command, uint32)
- func CMD2() (Command, uint32)
- func CMD24(addr uint) (Command, uint32)
- func CMD25(addr uint) (Command, uint32)
- func CMD3() (Command, uint32)
- func CMD5(ocr OCR) (Command, uint32)
- func CMD52(f, addr int, flags IORWFlags, val byte) (Command, uint32)
- func CMD53(f, addr int, flags IORWFlags, n int) (Command, uint32)
- func CMD55(rca uint16) (Command, uint32)
- func CMD6(sf SwitchFunc) (Command, uint32)
- func CMD7(rca uint16) (Command, uint32)
- func CMD8(vhs VHS, checkPattern byte) (Command, uint32)
- func CMD9(rca uint16) (Command, uint32)
- type Data
- type DataMode
- type FileFormat
- type Host
- type IORWFlags
- type IOStatus
- type OCR
- type Response
- func (r Response) R1() CardStatus
- func (r Response) R2CID() CID
- func (r Response) R2CSD() CSD
- func (r Response) R3() OCR
- func (r Response) R4() (ocr OCR, mem bool, numIO int)
- func (r Response) R5() (val byte, status IOStatus)
- func (r Response) R6() (rca uint16, status CardStatus)
- func (r Response) R7() (vhs VHS, pattern byte)
- type SCR
- func (scr SCR) CMD_SUPPORT() CmdSupport
- func (scr SCR) DATA_STAT_AFTER_ERASE() int
- func (scr SCR) EX_SECURITY() int
- func (scr SCR) SCR_STRUCTURE() int
- func (scr SCR) SD_BUS_WIDTHS() BusWidths
- func (scr SCR) SD_SECURITY() SDSecurity
- func (scr SCR) SD_SPEC() int
- func (scr SCR) SD_SPEC3() int
- func (scr SCR) SD_SPEC4() int
- func (scr SCR) SD_SPECX() int
- type SDSecurity
- type StatusReg
- type SwitchFunc
- type VHS
Constants ¶
const ( // Command fields CmdIdx Command = 63 << 0 // Command index. HasResp Command = 1 << 6 // Response expected. LongResp Command = 1 << 7 // Long response. RespIdx Command = 15 << 10 // Response index. Busy Command = 1 << 14 // Command can set D0 low to signal busy state. App Command = 1 << 15 // Application command (hint, APP_CMD required). // Response types RespType = RespIdx | HasResp | LongResp R1 = 1<<10 | HasResp R2 = 2<<10 | HasResp | LongResp R3 = 3<<10 | HasResp R4 = 4<<10 | HasResp R5 = 5<<10 | HasResp R6 = 6<<10 | HasResp R7 = 7<<10 | HasResp )
All Command constants are defined to be friendly to use with ARM PrimeCell Multimedia Card Interface (used by STM32, LPC and probably more MCUs). Do not add, delete, modify command fields without checking the stm32/hal/sdmmc and lpc/hal/sdmmc.
const ( IncAddr IORWFlags = 1 << 0 // OP Code (CMD53) RAW IORWFlags = 1 << 1 // Read after write (CMD52) Block IORWFlags = 1 << 1 // Block mode (CMD53) Read IORWFlags = 0 << 5 // Read data (CMD52, CMD53) Write IORWFlags = 1 << 5 // Write data (CMD52, CMD53) WriteRead = Write | RAW // CMD52 only. BlockRead = Read | Block // CMD53 only. BlockWrite = Write | Block // CMD53 only. )
Variables ¶
var ( // ErrCmdTimeout is returned by Host in case of command response timeout. ErrCmdTimeout = errors.New("sdio: cmd timeout") // ErrBusyTimeout is returned by Host if it waited 1s for end of card busy // state and for that reason it could not start a data transfer. ErrBusyTimeout = errors.New("sdmc: busy timeout") )
Functions ¶
This section is empty.
Types ¶
type CSD ¶
type CSD [4]uint32
CSD - Card Specific Data register
func (CSD) DSR_IMP ¶
DSR_IMP reports whether the configurable driver stage is integrated on the card and Driver Stage Register is implemented.
func (CSD) ERASE_BLK_EN ¶
ERASE_BLK_EN reports whether the memory card supports erasing of one or multiple 512 B blocks. If ERASE_BLK_EN is false it supports erasing of one or more sectors of SECTOR_SIZE * WRITE_BL_LEN bytes.
func (CSD) FILE_FORMAT ¶
func (csd CSD) FILE_FORMAT() FileFormat
FILE_FORMAT returns the file format on the card (3-bit field: the MS bit represents format group, two LS bits represents format).
func (CSD) NSAC ¶
NSAC returns the worst case for the clock-dependent factor of the data access time [clk].
func (CSD) PERM_WRITE_PROTECT ¶
PERM_WRITE_PROTECT reports whether the entrie card content is permanently protected against overwriting or erasing.
func (CSD) R2W_FACTOR ¶
R2W_FACTOR returns the typical block program time as a multiple of the read access time.
func (CSD) READ_BLK_MISALIGN ¶
READ_BLK_MISALIGN reports whether the data block to be read by one command can be spread over more than one physical block of the memory device.
func (CSD) READ_BL_LEN ¶
READ_BL_LEN returns the maximum read data block length (bytes).
func (CSD) READ_BL_PARTIAL ¶
READ_BL_PARTIAL reports whether Partial Block Read is allowed.
func (CSD) SECTOR_SIZE ¶
SECTOR_SIZE returns the size of an erasable sector as number of blocks of WRITE_BL_LEN bytes.
func (CSD) TMP_WRITE_PROTECT ¶
TMP_WRITE_PROTECT reports whether the entrie card content is temporarily protected against overwriting or erasing.
func (CSD) TRAN_SPEED ¶
TRAN_SPEED returns the maximum data transfer rate per one data line kb/s.
func (CSD) WP_GRP_ENABLE ¶
WP_GRP_ENABLE reports whether the write protection is possible
func (CSD) WP_GRP_SIZE ¶
WP_GRP_SIZE returns the size of a write protected group.
func (CSD) WRITE_BLK_MISALIGN ¶
WRITE_BLK_MISALIGN reports whether the data block to be written by one command can be spread over more than one physical block of the memory device.
func (CSD) WRITE_BL_LEN ¶
WRITE_BL_LEN returns the maximum write data block length (bytes).
func (CSD) WRITE_BL_PARTIAL ¶
WRITE_BL_PARTIAL reports whether the partial block sizes can be used in block write commands
type CardStatus ¶
type CardStatus uint32
const ( AKE_SEQ_ERROR CardStatus = 1 << 3 APP_CMD CardStatus = 1 << 5 FX_EVENT CardStatus = 1 << 6 READY_FOR_DATA CardStatus = 1 << 8 CURRENT_STATE CardStatus = 15 << 9 StateIdle CardStatus = 0 << 9 StateReady CardStatus = 1 << 9 StateIdent CardStatus = 2 << 9 StateStby CardStatus = 3 << 9 StateTran CardStatus = 4 << 9 StateData CardStatus = 5 << 9 StateRcv CardStatus = 6 << 9 StatePrg CardStatus = 7 << 9 StateDis CardStatus = 8 << 9 StateIOOnly CardStatus = 15 << 9 ERASE_RESET CardStatus = 1 << 13 CARD_ECC_DISABLED CardStatus = 1 << 14 WP_ERASE_SKIP CardStatus = 1 << 15 CSD_OVERWRITE CardStatus = 1 << 16 ERROR CardStatus = 1 << 19 CC_ERROR CardStatus = 1 << 20 CARD_ECC_FAILED CardStatus = 1 << 21 ILLEGAL_COMMAND CardStatus = 1 << 22 COM_CRC_ERROR CardStatus = 1 << 23 LOCK_UNLOCK_FAILED CardStatus = 1 << 24 CARD_IS_LOCKED CardStatus = 1 << 25 WP_VIOLATION CardStatus = 1 << 26 ERASE_PARAM CardStatus = 1 << 27 ERASE_SEQ_ERROR CardStatus = 1 << 28 BLOCK_LEN_ERROR CardStatus = 1 << 29 ADDRESS_ERROR CardStatus = 1 << 30 OUT_OF_RANGE CardStatus = 1 << 31 )
type CmdSupport ¶
type CmdSupport byte
const ( HasCMD20 CmdSupport = 1 << 0 HasCMD23 CmdSupport = 1 << 1 HasCMD48_49 CmdSupport = 1 << 2 HasCMD58_59 CmdSupport = 1 << 3 )
type Command ¶
type Command uint16
func CMD12 ¶
CMD12 (STOP_TRANSMISSION, R1b) forces the card to stop transmission in Multiple Block Read Operation.
func CMD18 ¶
CMD18 (READ_MULTIPLE_BLOCK, R1) works like CMD17 but does not stop the transmision after first data block. Instead the card continuously transfers data blocks until it receives CMD12 (STOP_TRANSMISSION) command.
func CMD25 ¶
CMD25 (WRITE_MULTIPLE_BLOCK, R1) works like CMD24 but allows to transmit multiple block to the card until. To signal the end of transfer host have to send CMD12 (STOP_TRANSMISSION) command.
func CMD3 ¶
CMD3 (SEND_RELATIVE_ADDR, R6) asks the card to publishets a new Relative Card Address (RCA) and Card Status bits 23,22,19,12:0
func CMD55 ¶
CMD55 (APP_CMD, R1) indicates to the card that the next command is an application specific command.
func CMD6 ¶
func CMD6(sf SwitchFunc) (Command, uint32)
CMD6 (SWITCH_FUNC, R1) switches or expands memory card functions.
func CMD7 ¶
CMD7 (SELECT_CARD/DESELECT_CARD, R1b) selects card with rca address (puts into Transfer State) and deselects all other (puts into Stand-by State).
type Data ¶
type Data []uint64
Data can be used to access bytes sent/received using SD card data transfers. It ensures 8-byte alignment required by Host.SetupData method. The bit order of its 64-bit elements is hardware depend. Use Bytes method to return Data as correctly ordered string of bytes. Additionally it helps to operate on typical 512 byte blocks.
func MakeDataBlocks ¶
MakeDataBlocks allocates and initializes Data object that can store n 512-byte blocks.
func MakeDataBytes ¶
MakeDataBytes allocates and initializes Data object that can store n bytes.
func (Data) BlockSlice ¶
Block returns the slice of d that contains n-m 512-byte blocks from m to n.
type DataMode ¶
type DataMode int16
DataMode describes data transfer mode.
const ( Send DataMode = 0 << 1 // Send data to a card. Recv DataMode = 1 << 1 // Receive data from a card. Stream DataMode = 1 << 2 // Stream or SDIO multibyte data transfer. Block1 DataMode = 0 << 4 // Block data transfer, block size: 1 B. Block2 DataMode = 1 << 4 // Block data transfer, block size: 2 B. Block4 DataMode = 2 << 4 // Block data transfer, block size: 4 B. Block8 DataMode = 3 << 4 // Block data transfer, block size: 8 B. Block16 DataMode = 4 << 4 // Block data transfer, block size: 16 B. Block32 DataMode = 5 << 4 // Block data transfer, block size: 32 B. Block64 DataMode = 6 << 4 // Block data transfer, block size: 64 B. Block128 DataMode = 7 << 4 // Block data transfer, block size: 128 B. Block256 DataMode = 8 << 4 // Block data transfer, block size: 256 B. Block512 DataMode = 9 << 4 // Block data transfer, block size: 512 B. Block1K DataMode = 10 << 4 // Block data transfer, block size: 1 KiB. Block2K DataMode = 11 << 4 // Block data transfer, block size: 2 KiB. Block4K DataMode = 12 << 4 // Block data transfer, block size: 4 KiB. Block8K DataMode = 13 << 4 // Block data transfer, block size: 8 KiB. Block16K DataMode = 14 << 4 // Block data transfer, block size: 16 KiB. BlockStep DataMode = 1 << 4 // To iterate over block sizes. RWaitStart DataMode = 1 << 8 // Read wait start. RWaitStop DataMode = 1 << 9 // Read wait stop. RWaitCK DataMode = 1 << 10 // Read wait control using CK instead od D2. IO DataMode = 1 << 11 // SDIO specific operation. )
Do not use DataMode(exp<<4) to specify block size, instead use BlockMode function. You can also use Block* constants directly instead of exp and iterate over block sizes this way:
for bm := sdcard.Block512; bm >= sdcard.Block1; bm -= sdcard.BlockStep { bs := bm.BlockSize() ... }
type FileFormat ¶
type FileFormat byte
const ( HardDisk FileFormat = 0 // Hard disk-like file system with partition table. DOSFloppy FileFormat = 1 // DOS FAT (floppy-like) without partition table. UFF FileFormat = 2 // Universal File Format. OtherFF FileFormat = 3 // Other/unknown. )
type Host ¶
type Host interface { // SetClock sets SD/SPI clock frequency. If pwrsave is true the host can // disable clock in case of inactive bus. SetClock(freqhz int, pwrsave bool) // SetBusWidth sets the SD bus width. Returns supported bus widths. SD host // returns combination of SDBus1 and SDBus4. SPI host returns SPIBus. SetBusWidth(width BusWidth) BusWidths // SendCmd sends the cmd to the card and receives its response, if any. // Short response is returned in r[0], long is returned in r[0:3] (r[0] // contains the least significant bits, r[3] contains the most significant // bits). If preceded by SetupData, SendCmd waits up to 1s for end of busy // state and performs the data transfer. SendCmd(cmd Command, arg uint32) (r Response) // SetupData setups the data transfer for subsequent command. SetupData(mode DataMode, buf []uint64, nbytes int) // Wait waits for deassertion of busy signal on DATA0 line (READY_FOR_DATA // state). It returns false if the deadline has passed. Wait can not be used // while transfer is in progress. Wait(deadline int64) bool // Err returns and clears the host internal error. The internal error, if // not nil, prevents any subsequent operations. Host should convert its // internal command timeout error to ErrCmdTimeout. Err(clear bool) error }
type OCR ¶
type OCR uint32
const ( DVC OCR = 1 << 7 // Dual Voltage Card (SDMC) V21 OCR = 1 << 8 // 2.0-2.1 V (SDIO) V22 OCR = 1 << 9 // 2.1-2.2 V (SDIO) V23 OCR = 1 << 10 // 2.2-2.3 V (SDIO) V24 OCR = 1 << 11 // 2.3-2.4 V (SDIO) V25 OCR = 1 << 12 // 2.4-2.5 V (SDIO) V26 OCR = 1 << 13 // 2.5-2.6 V (SDIO) V27 OCR = 1 << 14 // 2.6-2.7 V (SDIO) V28 OCR = 1 << 15 // 2.7-2.8 V V29 OCR = 1 << 16 // 2.8-2.9 V V30 OCR = 1 << 17 // 2.9-3.0 V V31 OCR = 1 << 18 // 3.0-3.1 V V32 OCR = 1 << 19 // 3.1-3.2 V V33 OCR = 1 << 20 // 3.2-3.3 V V34 OCR = 1 << 21 // 3.3-3.4 V V35 OCR = 1 << 22 // 3.4-3.5 V V36 OCR = 1 << 23 // 3.5-3.6 V S18 OCR = 1 << 24 // Switching to 1.8V XPC OCR = 1 << 18 // SDXC maximum performance UHSII OCR = 1 << 29 // UHS-II Card Status HCXC OCR = 1 << 30 // Card Capacity Status (set fot SDHC, SDXC). PWUP OCR = 1 << 31 // Card in power up state (^Busy). )
type Response ¶
type Response [4]uint32
func (Response) R1 ¶
func (r Response) R1() CardStatus
func (Response) R6 ¶
func (r Response) R6() (rca uint16, status CardStatus)
R6 contains Relative Card Address and Card Status bits 23, 22, 19, 12:0.
type SCR ¶
type SCR uint64
SCR (SD CARD Configuration Register) is 8 byte register that can be read using ACMD51 and 8-byte block data transfer (it is not returned in response).
func (SCR) CMD_SUPPORT ¶
func (scr SCR) CMD_SUPPORT() CmdSupport
CMD_SUPPORT returns the bitfield that describes support for CMD20, CMD23, CMD48, CMD49, CMD58, CMD59.
func (SCR) DATA_STAT_AFTER_ERASE ¶
DATA_STAT_AFTER_ERASE returns data status after erase: 0 or 1.
func (SCR) EX_SECURITY ¶
EX_SECURITY returns Extended Security indicator.
func (SCR) SCR_STRUCTURE ¶
SCR_STRUCTURE returns SCR structure version.
func (SCR) SD_BUS_WIDTHS ¶
SD_BUS_WIDTHS returns the bitfield that describes supported data bus widths.
func (SCR) SD_SECURITY ¶
func (scr SCR) SD_SECURITY() SDSecurity
SD_SECURITY returns CPRM Security Specification Version.
type SDSecurity ¶
type SDSecurity byte
const ( NoSecurity SDSecurity = 0 SecurityNotUsed SDSecurity = 1 SecuritySDSC SDSecurity = 2 // Version 1.01 SecuritySDHC SDSecurity = 3 // Version 2.00 SecuritySDXC SDSecurity = 4 // Version 3.xx )
type SwitchFunc ¶
type SwitchFunc uint32
const ( AccessMode SwitchFunc = 0x00000F // Access mode (keep current). DefaultSpeed SwitchFunc = 0x000000 // Default Speed or SDR12. HighSpeed SwitchFunc = 0x000001 // High Speed or SDR25. SDR50 SwitchFunc = 0x000002 // SDR50. SDR104 SwitchFunc = 0x000003 // SDR104. DDR50 SwitchFunc = 0x000004 // DDR50. CommandSystem SwitchFunc = 0x0000F0 // Command system (keep current). DefaultSystem SwitchFunc = 0x000000 // Default Command System. OTP SwitchFunc = 0x000030 ASSD SwitchFunc = 0x000040 VendorSpec SwitchFunc = 0x0000E0 Driver SwitchFunc = 0x000F00 // UHS-I driver strength (keep current). DefaultTypeB SwitchFunc = 0x000000 // Default Type B driver. TypeA SwitchFunc = 0x000100 // Type A driver. TypeC SwitchFunc = 0x000200 // Type C driver. TypeD SwitchFunc = 0x000300 // Type D driver. PowerLimit SwitchFunc = 0x00F000 // Power limit (keep current). Default720 SwitchFunc = 0x000000 // Default limit: 720 mW. Power1440 SwitchFunc = 0x001000 // Limit: 1440 mW. Power2160 SwitchFunc = 0x002000 // Limit: 2160 mW. Power2880 SwitchFunc = 0x003000 // Limit: 2880 mW. Power1800 SwitchFunc = 0x004000 // Limit: 1800 mW. ModeCheck SwitchFunc = 0 << 31 // Checks switchable function. ModeSwitch SwitchFunc = 1 << 31 // Switch card function. )