mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-12 19:03:42 +00:00
rename EraseBlocks to EraseSectors
This commit is contained in:
+28
-11
@@ -13,13 +13,19 @@ var (
|
|||||||
var _ Card = (*SPICard)(nil)
|
var _ Card = (*SPICard)(nil)
|
||||||
|
|
||||||
type Card interface {
|
type Card interface {
|
||||||
|
// WriteBlocks writes the given data to the card, starting at the given block index.
|
||||||
|
// The data must be a multiple of the block size.
|
||||||
WriteBlocks(data []byte, startBlockIdx int64) error
|
WriteBlocks(data []byte, startBlockIdx int64) error
|
||||||
|
// ReadBlocks reads the given number of blocks from the card, starting at the given block index.
|
||||||
|
// The dst buffer must be a multiple of the block size.
|
||||||
ReadBlocks(dst []byte, startBlockIdx int64) error
|
ReadBlocks(dst []byte, startBlockIdx int64) error
|
||||||
EraseBlocks(start, len int64) error
|
// EraseBlocks erases
|
||||||
|
EraseSectors(startBlockIdx, numBlocks int64) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBlockDevice(card Card, blockSize int, numBlocks, eraseBlockSize int64) (*BlockDevice, error) {
|
// NewBlockDevice creates a new BlockDevice from a Card.
|
||||||
if card == nil || blockSize <= 0 || eraseBlockSize <= 0 || numBlocks <= 0 {
|
func NewBlockDevice(card Card, blockSize int, numBlocks, eraseBlockSizeInBytes int64) (*BlockDevice, error) {
|
||||||
|
if card == nil || blockSize <= 0 || eraseBlockSizeInBytes <= 0 || numBlocks <= 0 {
|
||||||
return nil, errors.New("invalid argument(s)")
|
return nil, errors.New("invalid argument(s)")
|
||||||
}
|
}
|
||||||
tz := bits.TrailingZeros(uint(blockSize))
|
tz := bits.TrailingZeros(uint(blockSize))
|
||||||
@@ -27,16 +33,17 @@ func NewBlockDevice(card Card, blockSize int, numBlocks, eraseBlockSize int64) (
|
|||||||
return nil, errors.New("blockSize must be a power of 2")
|
return nil, errors.New("blockSize must be a power of 2")
|
||||||
}
|
}
|
||||||
bd := &BlockDevice{
|
bd := &BlockDevice{
|
||||||
card: card,
|
card: card,
|
||||||
blockbuf: make([]byte, blockSize),
|
blockbuf: make([]byte, blockSize),
|
||||||
blockshift: tz,
|
blockshift: tz,
|
||||||
blockmask: (1 << tz) - 1,
|
blockmask: (1 << tz) - 1,
|
||||||
numblocks: numBlocks,
|
numblocks: int64(numBlocks),
|
||||||
|
eraseBlockSize: eraseBlockSizeInBytes,
|
||||||
}
|
}
|
||||||
return bd, nil
|
return bd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlockDevice implements tinyfs.BlockDevice interface.
|
// BlockDevice implements tinyfs.BlockDevice interface for an [sd.Card] type.
|
||||||
type BlockDevice struct {
|
type BlockDevice struct {
|
||||||
card Card
|
card Card
|
||||||
blockbuf []byte
|
blockbuf []byte
|
||||||
@@ -54,6 +61,7 @@ func (bd *BlockDevice) divideBlockSize(n int64) int64 {
|
|||||||
return n >> bd.blockshift
|
return n >> bd.blockshift
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadAt implements [io.ReadAt] interface for an SD card.
|
||||||
func (bd *BlockDevice) ReadAt(p []byte, off int64) (n int, err error) {
|
func (bd *BlockDevice) ReadAt(p []byte, off int64) (n int, err error) {
|
||||||
if off < 0 {
|
if off < 0 {
|
||||||
return 0, errNegativeOffset
|
return 0, errNegativeOffset
|
||||||
@@ -94,6 +102,7 @@ func (bd *BlockDevice) ReadAt(p []byte, off int64) (n int, err error) {
|
|||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteAt implements [io.WriterAt] interface for an SD card.
|
||||||
func (bd *BlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
|
func (bd *BlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
|
||||||
if off < 0 {
|
if off < 0 {
|
||||||
return 0, errNegativeOffset
|
return 0, errNegativeOffset
|
||||||
@@ -140,14 +149,22 @@ func (bd *BlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
|
|||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size returns the number of bytes in this block device.
|
||||||
func (bd *BlockDevice) Size() int64 {
|
func (bd *BlockDevice) Size() int64 {
|
||||||
return int64(len(bd.blockbuf)) * bd.numblocks
|
return int64(len(bd.blockbuf)) * bd.numblocks
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bd *BlockDevice) EraseBlocks(start, len int64) error {
|
// EraseBlocks erases the given number of blocks. An implementation may
|
||||||
return bd.card.EraseBlocks(start, len)
|
// transparently coalesce ranges of blocks into larger bundles if the chip
|
||||||
|
// supports this. The start and len parameters are in block numbers, use
|
||||||
|
// EraseBlockSize to map addresses to blocks.
|
||||||
|
func (bd *BlockDevice) EraseBlocks(startEraseBlockIdx, len int64) error {
|
||||||
|
return bd.card.EraseSectors(startEraseBlockIdx, len)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EraseBlockSize returns the smallest erasable area on this particular chip
|
||||||
|
// in bytes. This is used for the block size in EraseBlocks.
|
||||||
|
// It must be a power of two, and may be as small as 1. A typical size is 4096.
|
||||||
func (bd *BlockDevice) EraseBlockSize() int64 {
|
func (bd *BlockDevice) EraseBlockSize() int64 {
|
||||||
return bd.eraseBlockSize
|
return bd.eraseBlockSize
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-22
@@ -13,10 +13,6 @@ var (
|
|||||||
errBadCSDCID = errors.New("sd:bad CSD/CID in CRC or always1")
|
errBadCSDCID = errors.New("sd:bad CSD/CID in CRC or always1")
|
||||||
errNoSDCard = errors.New("sd:no card")
|
errNoSDCard = errors.New("sd:no card")
|
||||||
errCardNotSupported = errors.New("sd:card not supported")
|
errCardNotSupported = errors.New("sd:card not supported")
|
||||||
errCmd8 = errors.New("sd:cmd8")
|
|
||||||
errCmdOCR = errors.New("sd:cmd_ocr")
|
|
||||||
errCmdBlkLen = errors.New("sd:cmd_blklen")
|
|
||||||
errAcmdAppCond = errors.New("sd:acmd_appOrCond")
|
|
||||||
errWaitStartBlock = errors.New("sd:did not find start block token")
|
errWaitStartBlock = errors.New("sd:did not find start block token")
|
||||||
errNeedBlockLenMultiple = errors.New("sd:need blocksize multiple for I/O")
|
errNeedBlockLenMultiple = errors.New("sd:need blocksize multiple for I/O")
|
||||||
errWrite = errors.New("sd:write")
|
errWrite = errors.New("sd:write")
|
||||||
@@ -25,28 +21,27 @@ var (
|
|||||||
errBusyTimeout = errors.New("sd:busy card timeout")
|
errBusyTimeout = errors.New("sd:busy card timeout")
|
||||||
errOOB = errors.New("sd:oob block access")
|
errOOB = errors.New("sd:oob block access")
|
||||||
errNoblocks = errors.New("sd:no readable blocks")
|
errNoblocks = errors.New("sd:no readable blocks")
|
||||||
errCmdGeneric = errors.New("sd:command error")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type digitalPinout func(b bool)
|
type digitalPinout = func(b bool)
|
||||||
|
|
||||||
type SPICard struct {
|
type SPICard struct {
|
||||||
bus drivers.SPI
|
bus drivers.SPI
|
||||||
cs digitalPinout
|
cs digitalPinout
|
||||||
bufcmd [6]byte
|
|
||||||
kind CardKind
|
timers [2]timer
|
||||||
cid CID
|
numblocks int64
|
||||||
csd CSD
|
timeout time.Duration
|
||||||
lastCRC uint16
|
wait time.Duration
|
||||||
|
// Card Identification Register.
|
||||||
|
cid CID
|
||||||
|
// Card Specific Register.
|
||||||
|
csd CSD
|
||||||
|
bufcmd [6]byte
|
||||||
|
kind CardKind
|
||||||
// shift to calculate blocksize, taken from CSD.
|
// shift to calculate blocksize, taken from CSD.
|
||||||
blockshift uint8
|
blockshift uint8
|
||||||
timers [2]timer
|
lastCRC uint16
|
||||||
numblocks int64
|
|
||||||
timeout time.Duration
|
|
||||||
wait time.Duration
|
|
||||||
// relative card address.
|
|
||||||
rca uint32
|
|
||||||
lastr1 r1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSPICard(spi drivers.SPI, cs digitalPinout) *SPICard {
|
func NewSPICard(spi drivers.SPI, cs digitalPinout) *SPICard {
|
||||||
@@ -93,8 +88,6 @@ func (d *SPICard) CSD() CSD { return d.csd }
|
|||||||
|
|
||||||
func (d *SPICard) yield() { time.Sleep(d.wait) }
|
func (d *SPICard) yield() { time.Sleep(d.wait) }
|
||||||
|
|
||||||
var timeoutTimer [2]timer
|
|
||||||
|
|
||||||
type timer struct {
|
type timer struct {
|
||||||
deadline time.Time
|
deadline time.Time
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -189,7 +189,7 @@ func (d *SPICard) ReadBlocks(dst []byte, startBlockIdx int64) error {
|
|||||||
panic("unreachable numblocks<=0")
|
panic("unreachable numblocks<=0")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *SPICard) EraseBlocks(startBlock, endBlock int64) error {
|
func (d *SPICard) EraseSectors(startSector, numberSectors int64) error {
|
||||||
return errors.New("sd:erase not implemented")
|
return errors.New("sd:erase not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user