2013-02-21 20:10:54 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2013-02-21 20:10:54 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-02-21 20:10:54 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2009-04-17 Bernard first version.
|
|
|
|
*/
|
|
|
|
|
2024-09-14 05:40:40 +08:00
|
|
|
#ifndef __DEV_SPI_MSD_H_INCLUDED__
|
|
|
|
#define __DEV_SPI_MSD_H_INCLUDED__
|
2013-02-21 20:10:54 +08:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2023-10-18 20:50:30 +08:00
|
|
|
#include <rtdevice.h>
|
2024-09-14 05:40:40 +08:00
|
|
|
#include "drivers/dev_spi.h"
|
2013-02-21 20:10:54 +08:00
|
|
|
|
|
|
|
/* SD command (SPI mode) */
|
|
|
|
#define GO_IDLE_STATE 0 /* CMD0 R1 */
|
|
|
|
#define SEND_OP_COND 1 /* CMD1 R1 */
|
|
|
|
#define SWITCH_FUNC 6 /* CMD6 R1 */
|
|
|
|
#define SEND_IF_COND 8 /* CMD8 R7 */
|
|
|
|
#define SEND_CSD 9 /* CMD9 R1 */
|
|
|
|
#define SEND_CID 10 /* CMD10 R1 */
|
|
|
|
#define STOP_TRANSMISSION 12 /* CMD12 R1B */
|
|
|
|
#define SEND_STATUS 13 /* CMD13 R2 */
|
|
|
|
#define SET_BLOCKLEN 16 /* CMD16 R1 */
|
|
|
|
#define READ_SINGLE_BLOCK 17 /* CMD17 R1 */
|
|
|
|
#define READ_MULTIPLE_BLOCK 18 /* CMD18 R1 */
|
|
|
|
#define WRITE_BLOCK 24 /* CMD24 R1 */
|
|
|
|
#define WRITE_MULTIPLE_BLOCK 25 /* CMD25 R1 */
|
|
|
|
#define PROGRAM_CSD 27 /* CMD27 R1 */
|
|
|
|
#define SET_WRITE_PROT 28 /* CMD28 R1B */
|
|
|
|
#define CLR_WRITE_PROT 29 /* CMD29 R1B */
|
|
|
|
#define SEND_WRITE_PROT 30 /* CMD30 R1 */
|
|
|
|
#define ERASE_WR_BLK_START_ADDR 32 /* CMD32 R1 */
|
|
|
|
#define ERASE_WR_BLK_END_ADDR 33 /* CMD33 R1 */
|
|
|
|
#define ERASE 38 /* CMD38 R1B */
|
|
|
|
#define LOCK_UNLOCK 42 /* CMD42 R1 */
|
|
|
|
#define APP_CMD 55 /* CMD55 R1 */
|
|
|
|
#define GEN_CMD 56 /* CMD56 R1 */
|
|
|
|
#define READ_OCR 58 /* CMD58 R3 */
|
|
|
|
#define CRC_ON_OFF 59 /* CMD59 R1 */
|
|
|
|
|
|
|
|
/* Application-Specific Command */
|
|
|
|
#define SD_STATUS 13 /* ACMD13 R2 */
|
|
|
|
#define SEND_NUM_WR_BLOCKS 22 /* ACMD22 R1 */
|
|
|
|
#define SET_WR_BLK_ERASE_COUNT 23 /* ACMD23 R1 */
|
|
|
|
#define SD_SEND_OP_COND 41 /* ACMD41 R1 */
|
|
|
|
#define SET_CLR_CARD_DETECT 42 /* ACMD42 R1 */
|
|
|
|
#define SEND_SCR 51 /* ACMD51 R1 */
|
2011-07-01 16:40:22 +08:00
|
|
|
|
|
|
|
/* Start Data tokens */
|
|
|
|
/* Tokens (necessary because at nop/idle (and CS active) only 0xff is on the data/command line) */
|
2013-02-21 20:10:54 +08:00
|
|
|
#define MSD_TOKEN_READ_START 0xFE /* Data token start byte, Start Single Block Read */
|
|
|
|
#define MSD_TOKEN_WRITE_SINGLE_START 0xFE /* Data token start byte, Start Single Block Write */
|
2011-07-01 16:40:22 +08:00
|
|
|
|
2013-02-21 20:10:54 +08:00
|
|
|
#define MSD_TOKEN_WRITE_MULTIPLE_START 0xFC /* Data token start byte, Start Multiple Block Write */
|
|
|
|
#define MSD_TOKEN_WRITE_MULTIPLE_STOP 0xFD /* Data toke stop byte, Stop Multiple Block Write */
|
2011-07-01 16:40:22 +08:00
|
|
|
|
|
|
|
/* MSD reponses and error flags */
|
2013-02-21 20:10:54 +08:00
|
|
|
#define MSD_RESPONSE_NO_ERROR 0x00
|
|
|
|
#define MSD_IN_IDLE_STATE 0x01
|
|
|
|
#define MSD_ERASE_RESET 0x02
|
|
|
|
#define MSD_ILLEGAL_COMMAND 0x04
|
|
|
|
#define MSD_COM_CRC_ERROR 0x08
|
|
|
|
#define MSD_ERASE_SEQUENCE_ERROR 0x10
|
|
|
|
#define MSD_ADDRESS_ERROR 0x20
|
|
|
|
#define MSD_PARAMETER_ERROR 0x40
|
|
|
|
#define MSD_RESPONSE_FAILURE 0xFF
|
2011-07-01 16:40:22 +08:00
|
|
|
|
|
|
|
/* Data response error */
|
2013-02-21 20:10:54 +08:00
|
|
|
#define MSD_DATA_OK 0x05
|
|
|
|
#define MSD_DATA_CRC_ERROR 0x0B
|
|
|
|
#define MSD_DATA_WRITE_ERROR 0x0D
|
|
|
|
#define MSD_DATA_OTHER_ERROR 0xFF
|
|
|
|
#define MSD_DATA_RESPONSE_MASK 0x1F
|
|
|
|
#define MSD_GET_DATA_RESPONSE(res) (res & MSD_DATA_RESPONSE_MASK)
|
|
|
|
|
|
|
|
#define MSD_CMD_LEN 6 /**< command, arg and crc. */
|
|
|
|
#define MSD_RESPONSE_MAX_LEN 5 /**< response max len */
|
|
|
|
#define MSD_CSD_LEN 16 /**< SD crad CSD register len */
|
|
|
|
#define SECTOR_SIZE 512 /**< sector size, default 512byte */
|
|
|
|
|
|
|
|
/* card try timeout, unit: ms */
|
|
|
|
#define CARD_TRY_TIMES 3000
|
|
|
|
#define CARD_TRY_TIMES_ACMD41 800
|
|
|
|
#define CARD_WAIT_TOKEN_TIMES 800
|
|
|
|
|
|
|
|
#define MSD_USE_PRE_ERASED /**< id define MSD_USE_PRE_ERASED, before CMD25, send ACMD23 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SD/MMC card type
|
|
|
|
*/
|
|
|
|
typedef enum
|
2011-07-01 16:40:22 +08:00
|
|
|
{
|
2021-03-08 18:19:04 +08:00
|
|
|
MSD_CARD_TYPE_UNKNOWN = 0, /**< unknown */
|
|
|
|
MSD_CARD_TYPE_MMC, /**< MultiMedia Card */
|
|
|
|
MSD_CARD_TYPE_SD_V1_X, /**< Ver 1.X Standard Capacity SD Memory Card */
|
|
|
|
MSD_CARD_TYPE_SD_V2_X, /**< Ver 2.00 or later Standard Capacity SD Memory Card */
|
|
|
|
MSD_CARD_TYPE_SD_SDHC, /**< High Capacity SD Memory Card */
|
|
|
|
MSD_CARD_TYPE_SD_SDXC, /**< later Extended Capacity SD Memory Card */
|
2013-02-21 20:10:54 +08:00
|
|
|
}msd_card_type;
|
|
|
|
|
|
|
|
typedef enum
|
2011-07-01 16:40:22 +08:00
|
|
|
{
|
2013-02-21 20:10:54 +08:00
|
|
|
response_type_unknown = 0,
|
|
|
|
response_r1,
|
|
|
|
response_r1b,
|
|
|
|
response_r2,
|
|
|
|
response_r3,
|
|
|
|
response_r4,
|
|
|
|
response_r5,
|
|
|
|
response_r7,
|
|
|
|
}response_type;
|
|
|
|
|
|
|
|
struct msd_device
|
|
|
|
{
|
|
|
|
struct rt_device parent; /**< RT-Thread device struct */
|
|
|
|
struct rt_device_blk_geometry geometry; /**< sector size, sector count */
|
|
|
|
struct rt_spi_device * spi_device; /**< SPI interface */
|
|
|
|
msd_card_type card_type; /**< card type: MMC SD1.x SD2.0 SDHC SDXC */
|
|
|
|
uint32_t max_clock; /**< MAX SPI clock */
|
|
|
|
};
|
|
|
|
|
|
|
|
extern rt_err_t msd_init(const char * sd_device_name, const char * spi_device_name);
|
|
|
|
|
2024-09-14 05:40:40 +08:00
|
|
|
#endif // __DEV_SPI_MSD_H_INCLUDED
|