18 #ifndef MBED_SD_BLOCK_DEVICE_H 19 #define MBED_SD_BLOCK_DEVICE_H 24 #include "blockdevice/BlockDevice.h" 25 #include "drivers/SPI.h" 26 #include "drivers/Timer.h" 27 #include "drivers/MbedCRC.h" 28 #include "drivers/DigitalOut.h" 29 #include "platform/platform.h" 30 #include "platform/PlatformMutex.h" 31 #include "hal/static_pinmap.h" 33 #ifndef MBED_CONF_SD_SPI_MOSI 34 #define MBED_CONF_SD_SPI_MOSI NC 36 #ifndef MBED_CONF_SD_SPI_MISO 37 #define MBED_CONF_SD_SPI_MISO NC 39 #ifndef MBED_CONF_SD_SPI_CLK 40 #define MBED_CONF_SD_SPI_CLK NC 42 #ifndef MBED_CONF_SD_SPI_CS 43 #define MBED_CONF_SD_SPI_CS NC 45 #ifndef MBED_CONF_SD_INIT_FREQUENCY 46 #define MBED_CONF_SD_INIT_FREQUENCY 100000 48 #ifndef MBED_CONF_SD_TRX_FREQUENCY 49 #define MBED_CONF_SD_TRX_FREQUENCY 1000000 51 #ifndef MBED_CONF_SD_CRC_ENABLED 52 #define MBED_CONF_SD_CRC_ENABLED 0 71 PinName miso = MBED_CONF_SD_SPI_MISO,
72 PinName sclk = MBED_CONF_SD_SPI_CLK,
73 PinName cs = MBED_CONF_SD_SPI_CS,
74 uint64_t hz = MBED_CONF_SD_TRX_FREQUENCY,
75 bool crc_on = MBED_CONF_SD_CRC_ENABLED);
84 PinName cs = MBED_CONF_SD_SPI_CS,
85 uint64_t hz = MBED_CONF_SD_TRX_FREQUENCY,
86 bool crc_on = MBED_CONF_SD_CRC_ENABLED);
118 virtual int read(
void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t
size);
136 virtual int program(
const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
155 virtual int trim(mbed::bd_addr_t addr, mbed::bd_size_t size);
174 virtual mbed::bd_size_t
size()
const;
180 virtual void debug(
bool dbg);
193 virtual const char *
get_type()
const;
200 CMD_NOT_SUPPORTED = -1,
201 CMD0_GO_IDLE_STATE = 0,
202 CMD1_SEND_OP_COND = 1,
203 CMD6_SWITCH_FUNC = 6,
204 CMD8_SEND_IF_COND = 8,
207 CMD12_STOP_TRANSMISSION = 12,
208 CMD13_SEND_STATUS = 13,
209 CMD16_SET_BLOCKLEN = 16,
210 CMD17_READ_SINGLE_BLOCK = 17,
211 CMD18_READ_MULTIPLE_BLOCK = 18,
213 CMD24_WRITE_BLOCK = 24,
214 CMD25_WRITE_MULTIPLE_BLOCK = 25,
216 CMD27_PROGRAM_CSD = 27,
217 CMD32_ERASE_WR_BLK_START_ADDR = 32,
219 CMD33_ERASE_WR_BLK_END_ADDR = 33,
225 CMD59_CRC_ON_OFF = 59,
227 ACMD6_SET_BUS_WIDTH = 6,
228 ACMD13_SD_STATUS = 13,
229 ACMD22_SEND_NUM_WR_BLOCKS = 22,
230 ACMD23_SET_WR_BLK_ERASE_COUNT = 23,
231 ACMD41_SD_SEND_OP_COND = 41,
232 ACMD42_SET_CLR_CARD_DETECT = 42,
233 ACMD51_SEND_SCR = 51,
237 int _cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg,
bool isAcmd = 0, uint32_t *resp = NULL);
250 uint32_t _go_idle_state();
251 int _initialise_card();
253 mbed::bd_size_t _sectors;
254 mbed::bd_size_t _sd_sectors();
256 bool _is_valid_trim(mbed::bd_addr_t addr, mbed::bd_size_t size);
259 mbed::Timer _spi_timer;
261 uint32_t _transfer_sck;
266 uint8_t _cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg);
267 void _spi_wait(uint8_t count);
269 bool _wait_token(uint8_t token);
270 bool _wait_ready(std::chrono::duration<uint32_t, std::milli> timeout = std::chrono::milliseconds{300});
271 int _read(uint8_t *buffer, uint32_t length);
272 int _read_bytes(uint8_t *buffer, uint32_t length);
273 uint8_t _write(
const uint8_t *buffer, uint8_t token, uint32_t length);
275 void _preclock_then_select();
276 void _postclock_then_deselect();
283 virtual void unlock()
289 static const uint32_t _block_size;
290 uint32_t _erase_size;
291 bool _is_initialized;
293 uint32_t _init_ref_count;
295 #if MBED_CONF_SD_CRC_ENABLED virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Program blocks to a block device.
virtual void debug(bool dbg)
Enable or disable debugging.
A hardware device capable of writing and reading blocks.
virtual int init()
Initialize a block device.
virtual mbed::bd_size_t size() const
Get the total size of the underlying device.
virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Read blocks from a block device.
virtual mbed::bd_size_t get_read_size() const
Get the size of a readable block.
A SPI Master, used for communicating with SPI slave devices.
virtual mbed::bd_size_t get_program_size() const
Get the size of a programmable block.
virtual int frequency(uint64_t freq)
Set the transfer frequency.
SDBlockDevice(PinName mosi=NC, PinName miso=NC, PinName sclk=NC, PinName cs=NC, uint64_t hz=1000000, bool crc_on=0)
Creates an SDBlockDevice on a SPI bus specified by pins (using dynamic pin-map)
virtual int deinit()
Deinitialize a block device.
virtual const char * get_type() const
Get the BlockDevice class type.
virtual int trim(mbed::bd_addr_t addr, mbed::bd_size_t size)
Mark blocks as no longer in use.