Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions sdcard/cid.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package sdcard

import "fmt"

type CID struct {
//// byte 0
ManufacturerID byte
Expand Down Expand Up @@ -39,7 +37,7 @@ func NewCID(buf []byte) *CID {
ManufacturerID: buf[0],
OEMApplicationID: (uint16(buf[0]) << 8) | uint16(buf[1]),
ProductName: string(buf[3:8]),
ProductVersion: fmt.Sprintf("%d.%d", (buf[8]&0xF0)>>4, buf[8]&0x0F),
ProductVersion: string([]byte{'0' + (buf[8]&0xF0)>>4, '.', '0' + buf[8]&0x0F}),
ProductSerialNumber: (uint32(buf[9]) << 24) | (uint32(buf[10]) << 16) | (uint32(buf[11]) << 8) | uint32(buf[12]),
ManufacturingYear: (buf[13] & 0xF0) | (buf[14] & 0x0F),
ManufacturingMonth: (buf[14] & 0xF0) >> 4,
Expand Down
71 changes: 41 additions & 30 deletions sdcard/csd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package sdcard

import (
"fmt"
)

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
Expand Down Expand Up @@ -61,30 +57,30 @@ func NewCSD(buf []byte) *CSD {
}

func (c *CSD) Dump() {
fmt.Printf("CSD_STRUCTURE: %X\r\n", c.CSD_STRUCTURE)
fmt.Printf("TAAC: %X\r\n", c.TAAC)
fmt.Printf("NSAC: %X\r\n", c.NSAC)
fmt.Printf("TRAN_SPEED: %X\r\n", c.TRAN_SPEED)
fmt.Printf("CCC: %X\r\n", c.CCC)
fmt.Printf("READ_BL_LEN: %X\r\n", c.READ_BL_LEN)
fmt.Printf("READ_BL_PARTIAL: %X\r\n", c.READ_BL_PARTIAL)
fmt.Printf("WRITE_BLK_MISALIGN: %X\r\n", c.WRITE_BLK_MISALIGN)
fmt.Printf("READ_BLK_MISALIGN: %X\r\n", c.READ_BLK_MISALIGN)
fmt.Printf("DSR_IMP: %X\r\n", c.DSR_IMP)
fmt.Printf("C_SIZE: %X\r\n", c.C_SIZE)
fmt.Printf("ERASE_BLK_EN: %X\r\n", c.ERASE_BLK_EN)
fmt.Printf("SECTOR_SIZE: %X\r\n", c.SECTOR_SIZE)
fmt.Printf("WP_GRP_SIZE: %X\r\n", c.WP_GRP_SIZE)
fmt.Printf("WP_GRP_ENABLE: %X\r\n", c.WP_GRP_ENABLE)
fmt.Printf("R2W_FACTOR: %X\r\n", c.R2W_FACTOR)
fmt.Printf("WRITE_BL_LEN: %X\r\n", c.WRITE_BL_LEN)
fmt.Printf("WRITE_BL_PARTIAL: %X\r\n", c.WRITE_BL_PARTIAL)
fmt.Printf("FILE_FORMAT_GRP: %X\r\n", c.FILE_FORMAT_GRP)
fmt.Printf("COPY: %X\r\n", c.COPY)
fmt.Printf("PERM_WRITE_PROTECT: %X\r\n", c.PERM_WRITE_PROTECT)
fmt.Printf("TMP_WRITE_PROTECT: %X\r\n", c.TMP_WRITE_PROTECT)
fmt.Printf("FILE_FORMAT: %X\r\n", c.FILE_FORMAT)
fmt.Printf("CRC: %X\r\n", c.CRC)
dumpField("CSD_STRUCTURE: ", uint32(c.CSD_STRUCTURE))
dumpField("TAAC: ", uint32(c.TAAC))
dumpField("NSAC: ", uint32(c.NSAC))
dumpField("TRAN_SPEED: ", uint32(c.TRAN_SPEED))
dumpField("CCC: ", uint32(c.CCC))
dumpField("READ_BL_LEN: ", uint32(c.READ_BL_LEN))
dumpField("READ_BL_PARTIAL: ", uint32(c.READ_BL_PARTIAL))
dumpField("WRITE_BLK_MISALIGN: ", uint32(c.WRITE_BLK_MISALIGN))
dumpField("READ_BLK_MISALIGN: ", uint32(c.READ_BLK_MISALIGN))
dumpField("DSR_IMP: ", uint32(c.DSR_IMP))
dumpField("C_SIZE: ", uint32(c.C_SIZE))
dumpField("ERASE_BLK_EN: ", uint32(c.ERASE_BLK_EN))
dumpField("SECTOR_SIZE: ", uint32(c.SECTOR_SIZE))
dumpField("WP_GRP_SIZE: ", uint32(c.WP_GRP_SIZE))
dumpField("WP_GRP_ENABLE: ", uint32(c.WP_GRP_ENABLE))
dumpField("R2W_FACTOR: ", uint32(c.R2W_FACTOR))
dumpField("WRITE_BL_LEN: ", uint32(c.WRITE_BL_LEN))
dumpField("WRITE_BL_PARTIAL: ", uint32(c.WRITE_BL_PARTIAL))
dumpField("FILE_FORMAT_GRP: ", uint32(c.FILE_FORMAT_GRP))
dumpField("COPY: ", uint32(c.COPY))
dumpField("PERM_WRITE_PROTECT: ", uint32(c.PERM_WRITE_PROTECT))
dumpField("TMP_WRITE_PROTECT: ", uint32(c.TMP_WRITE_PROTECT))
dumpField("FILE_FORMAT: ", uint32(c.FILE_FORMAT))
dumpField("CRC: ", uint32(c.CRC))
}

func (c *CSD) Sectors() (int64, error) {
Expand All @@ -94,13 +90,28 @@ func (c *CSD) Sectors() (int64, error) {
sectors = (int64(c.C_SIZE) + 1) * 1024
} else if c.CSD_STRUCTURE == 0x00 {
// CSD version 1.0 (old, <=2GB)
return 0, fmt.Errorf("CSD format version 1.0 is not supported")
return 0, errCSDv1Unsupported
} else {
return 0, fmt.Errorf("unknown CSD format")
return 0, errUnknownCSD
}
return sectors, nil
}

func (c *CSD) Size() uint64 {
return uint64(c.C_SIZE) * 512 * 1024
}

func dumpField(name string, v uint32) {
const digits = "0123456789ABCDEF"
var buf [8]byte
i := len(buf)
for {
i--
buf[i] = digits[v&0xF]
v >>= 4
if v == 0 {
break
}
}
print(name, string(buf[i:]), "\r\n")
}
23 changes: 23 additions & 0 deletions sdcard/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sdcard

import "errors"

var (
errNoCard = errors.New("sdcard: no card")
errCardV1Unsupported = errors.New("sdcard: v1 cards not supported")
errCMD8Voltage = errors.New("sdcard: CMD8 bad voltage range")
errCMD8Pattern = errors.New("sdcard: CMD8 bad check pattern")
errACMD41 = errors.New("sdcard: ACMD41 failed")
errCMD58 = errors.New("sdcard: CMD58 failed")
errCMD16 = errors.New("sdcard: CMD16 failed")
errStartBlock = errors.New("sdcard: start block token not received")
errReadReg = errors.New("sdcard: register read failed")
errBufferTooSmall = errors.New("sdcard: buffer must be at least 512 bytes")
errCMD17 = errors.New("sdcard: CMD17 failed")
errCMD24 = errors.New("sdcard: CMD24 failed")
errCMD25 = errors.New("sdcard: CMD25 failed")
errWrite = errors.New("sdcard: write rejected")
errWriteTimeout = errors.New("sdcard: write timeout")
errCSDv1Unsupported = errors.New("sdcard: CSD version 1.0 not supported")
errUnknownCSD = errors.New("sdcard: unknown CSD format")
)
39 changes: 19 additions & 20 deletions sdcard/sdcard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package sdcard

import (
"fmt"
"machine"
"time"
)
Expand Down Expand Up @@ -100,14 +99,14 @@ func (d *Device) initCard() error {
}
}
if !ok {
return fmt.Errorf("no SD card")
return errNoCard
}

// CMD8: determine card version
r := d.cmd(CMD8_SEND_IF_COND, 0x01AA, 0x87)
if (r & _R1_ILLEGAL_COMMAND) == _R1_ILLEGAL_COMMAND {
d.sdCardType = SD_CARD_TYPE_SD1
return fmt.Errorf("init_card_v1 not impl\r\n")
return errCardV1Unsupported
} else {
// r7 response
status := byte(0)
Expand All @@ -119,7 +118,7 @@ func (d *Device) initCard() error {
}
}
if (status & 0x0F) != 0x01 {
return fmt.Errorf("SD_CARD_ERROR_CMD8 %02X", status)
return errCMD8Voltage
}

for i := 3; i < 4; i++ {
Expand All @@ -130,7 +129,7 @@ func (d *Device) initCard() error {
}
}
if status != 0xAA {
return fmt.Errorf("SD_CARD_ERROR_CMD8 %02X", status)
return errCMD8Pattern
}
d.sdCardType = SD_CARD_TYPE_SD2
}
Expand All @@ -152,13 +151,13 @@ func (d *Device) initCard() error {
}

if !ok {
return fmt.Errorf("SD_CARD_ERROR_ACMD41")
return errACMD41
}

// if SD2 read OCR register to check for SDHC card
if d.sdCardType == SD_CARD_TYPE_SD2 {
if d.cmd(CMD58_READ_OCR, 0, 0xFF) != 0 {
return fmt.Errorf("SD_CARD_ERROR_CMD58")
return errCMD58
}

status, err := d.bus.Transfer(byte(0xFF))
Expand All @@ -175,7 +174,7 @@ func (d *Device) initCard() error {
}

if d.cmd(CMD16_SET_BLOCKLEN, 0x0200, 0xFF) != 0 {
return fmt.Errorf("SD_CARD_ERROR_CMD16")
return errCMD16
}

var buf [16]byte
Expand Down Expand Up @@ -283,7 +282,7 @@ func (d Device) waitStartBlock() error {

if status != 254 {
d.cs.High()
return fmt.Errorf("SD_CARD_START_BLOCK")
return errStartBlock
}

return nil
Expand All @@ -301,7 +300,7 @@ func (d Device) ReadCID(csd []byte) error {

func (d Device) readRegister(cmd uint8, dst []byte) error {
if d.cmd(cmd, 0, 0xFF) != 0 {
return fmt.Errorf("SD_CARD_ERROR_READ_REG")
return errReadReg
}
if err := d.waitStartBlock(); err != nil {
return err
Expand All @@ -324,18 +323,18 @@ func (d Device) readRegister(cmd uint8, dst []byte) error {
// ReadData reads 512 bytes from sdcard into dst.
func (d Device) ReadData(block uint32, dst []byte) error {
if len(dst) < 512 {
return fmt.Errorf("len(dst) must be greater than or equal to 512")
return errBufferTooSmall
}

// use address if not SDHC card
if d.sdCardType != SD_CARD_TYPE_SDHC {
block <<= 9
}
if d.cmd(CMD17_READ_SINGLE_BLOCK, block, 0xFF) != 0 {
return fmt.Errorf("CMD17 error")
return errCMD17
}
if err := d.waitStartBlock(); err != nil {
return fmt.Errorf("waitStartBlock()")
return err
}

err := d.bus.Tx(dummy[:512], dst)
Expand All @@ -360,7 +359,7 @@ func (d Device) WriteMultiStart(block uint32) error {
block <<= 9
}
if d.cmd(CMD25_WRITE_MULTIPLE_BLOCK, block, 0xFF) != 0 {
return fmt.Errorf("CMD25 error")
return errCMD25
}

// skip 1 byte
Expand Down Expand Up @@ -392,13 +391,13 @@ func (d Device) WriteMulti(buf []byte) error {
return err
}
if (r & 0x1F) != 0x05 {
return fmt.Errorf("SD_CARD_ERROR_WRITE")
return errWrite
}

// wait no busy
err = d.waitNotBusy(600 * time.Millisecond)
if err != nil {
return fmt.Errorf("SD_CARD_ERROR_WRITE_TIMEOUT")
return errWriteTimeout
}

return nil
Expand All @@ -425,15 +424,15 @@ func (d Device) WriteMultiStop() error {
// WriteData writes 512 bytes from dst to sdcard.
func (d Device) WriteData(block uint32, src []byte) error {
if len(src) < 512 {
return fmt.Errorf("len(src) must be greater than or equal to 512")
return errBufferTooSmall
}

// use address if not SDHC card
if d.sdCardType != SD_CARD_TYPE_SDHC {
block <<= 9
}
if d.cmd(CMD24_WRITE_BLOCK, block, 0xFF) != 0 {
return fmt.Errorf("CMD24 error")
return errCMD24
}

// wait 1 byte?
Expand All @@ -455,13 +454,13 @@ func (d Device) WriteData(block uint32, src []byte) error {
return err
}
if (r & 0x1F) != 0x05 {
return fmt.Errorf("SD_CARD_ERROR_WRITE")
return errWrite
}

// wait no busy
err = d.waitNotBusy(600 * time.Millisecond)
if err != nil {
return fmt.Errorf("SD_CARD_ERROR_WRITE_TIMEOUT")
return errWriteTimeout
}

// TODO: probably not necessary
Expand Down