PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
SD Card Interface (SDIF)

Data Structures

struct  stc_sd_cmdconfig
 SD command configure data. More...
 
struct  stc_sd_config
 SD configure data. More...
 
struct  stc_sdcard_type
 
struct  stc_csdver1
 
struct  stc_csdver2
 
struct  stc_sdcard_info
 SD command configure data. More...
 

Macros

#define SD_PCG   0x4003C120
 
#define PCG_ENABLE   (1UL<<8)
 
#define DELAYUNIT   5000
 
#define WAITTIME   50000
 
#define RETRYTIMES   100
 
#define stc_sd_t   FM4_SDIF_TypeDef
 
#define SD_TIMEOUT_DATLINE   1000
 
#define RESET_DAT   (1<<2)
 
#define RESET_CMD   (1<<1)
 
#define RESET_ALL   1
 
#define SDMA   1
 
#define ADMA   0
 
#define BIT1   0
 
#define BIT4   1
 
#define BUSWIDTH   BIT4
 
#define SD_DLY_1UNIT   8000
 
#define E_SD_OK   0
 
#define E_SD_NO_CARD   -1
 
#define E_SD_INIT   -2
 
#define E_SD_PARAMETER   -3
 
#define E_SD_READ   -4
 
#define E_SD_PROTECT   -5
 
#define E_SD_WRITE   -6
 
#define E_SD_DEVICE_TYPE   -7
 
#define E_SD_ACTIVE   -8
 
#define E_SD_CMD_ERR   -11
 
#define E_SD_CMD_TIMEOUT   -13
 
#define E_SD_CMD_CRC_ERR   -14
 
#define E_SD_CMD_WR_ERR   -15
 
#define E_SD_CMD_IDLE   -16
 
#define E_SD_CMD_CARD_ERR   -20
 
#define BLOCK_SIZE   512
 
#define CMD8_WAIT   (0x00001000)
 
#define ARG_CMD8   0x000001AA
 
#define ARG_ACMD41_HCS_OFF   0x003C0000
 
#define ARG_ACMD41_HCS_ON   0x403C0000
 
#define ARG_ACMD41_BUSY   0x80000000
 
#define CSD_VERSION_MASK   3
 
#define CSD_STRUCT_OFFSET   14
 
#define OCR_CAPACITY_OFFSET   30
 
#define OCR_CAPACITY_MASK   0x40000000
 
#define SD_VERSION_1_0   0
 
#define SD_VERSION_2_0   1
 
#define SD_CAPACITY_STANDARD   0
 
#define SD_CAPACITY_HIGH   1
 
#define SD_ACMD_OFFSET   0x40
 
#define SD_CMD0   (0)
 
#define SD_CMD2   (2)
 
#define SD_CMD3   (3)
 
#define SD_CMD7   (7)
 
#define SD_CMD8   (8)
 
#define SD_CMD9   (9)
 
#define SD_CMD10   (10)
 
#define SD_CMD12   (12)
 
#define SD_CMD13   (13)
 
#define SD_CMD16   (16)
 
#define SD_CMD17   (17)
 
#define SD_CMD18   (18)
 
#define SD_CMD24   (24)
 
#define SD_CMD25   (25)
 
#define SD_CMD28   (28)
 
#define SD_CMD29   (29)
 
#define SD_CMD30   (30)
 
#define SD_CMD55   (55)
 
#define SD_ACMD6   (SD_ACMD_OFFSET + 6)
 
#define SD_ACMD13   (SD_ACMD_OFFSET + 13)
 
#define SD_ACMD41   (SD_ACMD_OFFSET + 41)
 
#define SD_ACMD51   (SD_ACMD_OFFSET + 51)
 
#define CRC7_INIT   0
 
#define INIT_CCITT_CRC16   0x0000
 

Typedefs

typedef void(* func_ptr_sd_arg32_t )(volatile FM4_SDIF_TypeDef *)
 
typedef enum en_sd_clk en_sd_clk_t
 SD clock SDCLK frequency. More...
 
typedef enum en_sd_transaction en_sd_transaction_t
 SD transaction cases (with or without data line) More...
 
typedef enum en_sd_dir en_sd_dir_t
 SD transfer direction. More...
 
typedef enum en_sd_cmd_type en_sd_scmd_type_t
 SD special command. More...
 
typedef enum en_sd_existing en_sd_existing_t
 SD card exist status. More...
 
typedef enum en_sd_buswidth en_sd_buswidth_t
 SD bus width. More...
 
typedef enum en_sd_busvoltage en_sd_busvoltage_t
 SD bus voltage level. More...
 
typedef enum en_sd_response en_sd_response_t
 SD response. More...
 
typedef enum en_sd_auto_cmd en_sd_auto_cmd_t
 SD auto command functions. More...
 
typedef enum en_sd_boundary en_sd_boundary_t
 
typedef struct stc_sd_cmdconfig stc_sd_comdconfig_t
 SD command configure data. More...
 
typedef struct stc_sd_config stc_sd_config_t
 SD configure data. More...
 
typedef struct stc_sdcard_type stc_sdcard_type_t
 
typedef struct stc_csdver1 stc_csdver1_t
 
typedef struct stc_csdver2 stc_csdver2_t
 
typedef struct stc_sdcard_info stc_sdcard_info_t
 SD command configure data. More...
 

Enumerations

enum  en_sd_clk {
  CLK_25M = 25,
  CLK_50M = 50
}
 SD clock SDCLK frequency. More...
 
enum  en_sd_transaction {
  NO_RESPONSE = 0,
  NO_DATA,
  SINGLE_BLOCK,
  MULTI_BLOCK
}
 SD transaction cases (with or without data line) More...
 
enum  en_sd_dir {
  SD_WRITE = 0,
  SD_READ = 1
}
 SD transfer direction. More...
 
enum  en_sd_cmd_type {
  NORMAL = 0,
  SUSPEND = 1,
  RESUME = 2,
  ABORT = 3
}
 SD special command. More...
 
enum  en_sd_existing {
  DEBOUNCING = 0,
  INSERTED = 1,
  REMOVAL = 2
}
 SD card exist status. More...
 
enum  en_sd_buswidth {
  BIT_1 = 0,
  BIT_4 = 1
}
 SD bus width. More...
 
enum  en_sd_busvoltage {
  V1_8 = 0x05,
  V3_0 = 0x06,
  V3_3 = 0x07
}
 SD bus voltage level. More...
 
enum  en_sd_response {
  NO_RSP = 0,
  R1NORMAL_R5_R6_R7,
  R1AUTO,
  R1B_NORMAL,
  R1B_AUTO,
  R2,
  R3_R4,
  R5B
}
 SD response. More...
 
enum  en_sd_auto_cmd {
  AUTO_DISABLE = 0,
  AUTO_CMD12 = 1,
  AUTO_CMD23 =2
}
 SD auto command functions. More...
 
enum  en_sd_boundary {
  BOUND_4K = 0,
  BOUND_8K = 1,
  BOUND_16K = 2,
  BOUND_32K = 3,
  BOUND_64K = 4,
  BOUND_128K = 5,
  BOUND_256K = 6,
  BOUND_512K = 7
}
 

Functions

static void dly_1unit (uint32_t u32Delay)
 Delay according to specified number. More...
 
static void SdInitPins (void)
 Init the IO configuration for SDIF. More...
 
static boolean_t SdEnablePCG (void)
 Enable SD I/F peripheral clock. More...
 
static void SdDisablePCG (void)
 Disable SD I/F peripheral clock. More...
 
static void SdInitHostInternalData (volatile stc_sd_config_t *pstcSdCfg, stc_sd_comdconfig_t *pstcCmdCfg)
 Initial the SDIF internal data. More...
 
static boolean_t SdGetCmdComplete (void)
 Check whether the command complete. More...
 
static void SdClearCmdComplete (void)
 Clear command complete flag. More...
 
static boolean_t SdGetTrsComplete (void)
 Check whether the transfer complete. More...
 
static void SdGetCardStatus (volatile FM4_SDIF_TypeDef *pstcSd, volatile stc_sd_config_t *pstcCfg)
 Wait for card status stable and set the card status flag. More...
 
static void SdIrqHandler (void)
 SD interrupt handler. More...
 
static void SdGetResponse (volatile FM4_SDIF_TypeDef *pstcSd, en_sd_response_t enSdresponse, uint32_t *pu32Buf)
 get response More...
 
static boolean_t SdSendCmdOnly (volatile FM4_SDIF_TypeDef *pstcSd, stc_sd_comdconfig_t *pstcCmdCfg)
 Send command without data transfer using DAT line. More...
 
static boolean_t SdSendCmdDatLine (volatile FM4_SDIF_TypeDef *pstcSd, stc_sd_comdconfig_t *pstcCmdCfg)
 Send command with data transfer using DAT line. More...
 
static boolean_t SdTxData (volatile FM4_SDIF_TypeDef *pstcSd, stc_sd_comdconfig_t *pstcCmdCfg, uint32_t *pu32Buf)
 Send data segment using DAT line. More...
 
static boolean_t SdRxRsp (volatile FM4_SDIF_TypeDef *pstcSd, stc_sd_comdconfig_t *pstcCmdCfg, uint32_t *pu32Buf)
 Get response data after command. More...
 
static boolean_t SdRxData (volatile FM4_SDIF_TypeDef *pstcSd, stc_sd_comdconfig_t *pstcCmdCfg, uint32_t *pu32Buf)
 Receive data segment using DAT line. More...
 
en_result_t Sd_EnableInt (volatile FM4_SDIF_TypeDef *pstcSd)
 Enable the interrupt flag of SD. More...
 
en_result_t Sd_DisableInt (volatile FM4_SDIF_TypeDef *pstcSd)
 Disable the interrupt flag of SD. More...
 
en_sd_existing_t Sd_CardDetect (volatile stc_sd_config_t *pstcCmdCfg)
 Detect the SD card insertion status. More...
 
en_result_t Sd_ClockSupply (volatile FM4_SDIF_TypeDef *pstcSd, en_sd_clk_t enClk)
 Supply clock to an SD card. More...
 
en_result_t Sd_ClockStop (volatile FM4_SDIF_TypeDef *pstcSd)
 stop clock More...
 
en_result_t Sd_BusPowerControl (volatile FM4_SDIF_TypeDef *pstcSd, en_sd_busvoltage_t enVoltage)
 Provide power for SD card. More...
 
boolean_t Sd_SendCmd (en_sd_transaction_t enTran, volatile FM4_SDIF_TypeDef *pstcSd, stc_sd_comdconfig_t *pstcCmdCfg, uint32_t *pu32Buf)
 Transmit commands and check the corresponding response. More...
 
boolean_t Sd_TxData (volatile FM4_SDIF_TypeDef *pstcSd, stc_sd_comdconfig_t *pstcCmdCfg, uint32_t *pu32Buf)
 Transmit data segment for write commands only. More...
 
boolean_t Sd_RxData (en_sd_transaction_t enTran, volatile FM4_SDIF_TypeDef *pstcSd, stc_sd_comdconfig_t *pstcCmdCfg, uint32_t *pu32Buf)
 Receive the data of read command. More...
 
en_result_t Sd_SoftwareReset (volatile FM4_SDIF_TypeDef *pstcSd, uint8_t u8reset)
 software reset for command and data line More...
 
void Sd_Handler (void)
 SD interrupt entry. More...
 
en_result_t Sd_HostInit (void)
 Initialize SD host. More...
 
en_result_t Sd_HostDeInit (void)
 Deinitializes SD. More...
 
static void SdDly_1unit (uint32_t u32Delay)
 Sd card delay function. More...
 
boolean_t Sdcard_Detect (void)
 detect SD More...
 
int32_t Sdcard_Init (stc_sdcard_info_t *pstcSdcardReg)
 SD card initialization. More...
 
int32_t Sdcard_ReadSector (stc_sdcard_info_t *pstcSdcardInfo, uint32_t u32Start, uint16_t u16Count, uint8_t *pu8Buf)
 read one or multiple sectors of data More...
 
int32_t Sdcard_WriteSector (stc_sdcard_info_t *pstcSdcardInfo, uint32_t u32Start, uint16_t u16Count, const uint8_t *pu8Buf)
 write data into the SD card More...
 
boolean_t Sdcmd_RxData (uint32_t *pu32Buf)
 get data of SD card More...
 
boolean_t Sdcmd_TxData (uint32_t *pu32Buf)
 send data to SD card More...
 
boolean_t Sdcmd_SendCmd (uint8_t u8Cmd, uint32_t u32Arg, uint32_t *pu32Buf)
 send command to SD card More...
 

Variables

volatile stc_sd_config_t Sd_stcCfg
 
stc_sd_comdconfig_t Sd_stcCmdCfg
 
volatile FM4_SDIF_TypeDef * Sd_stcCtrl = (volatile FM4_SDIF_TypeDef *)FM4_SDIF_BASE
 
volatile stc_sd_config_t Sd_stcCfg
 
stc_sd_comdconfig_t Sd_stcCmdCfg
 
volatile FM4_SDIF_TypeDef * Sd_stcCtrl
 
static uint32_t m_u32NumSector
 

Detailed Description

Macro Definition Documentation

#define ADMA   0

Definition at line 77 of file sd_cfg.h.

#define ARG_ACMD41_BUSY   0x80000000

Definition at line 101 of file sdcard.h.

Referenced by Sdcard_Init().

#define ARG_ACMD41_HCS_OFF   0x003C0000

Definition at line 99 of file sdcard.h.

Referenced by Sdcard_Init().

#define ARG_ACMD41_HCS_ON   0x403C0000

Definition at line 100 of file sdcard.h.

Referenced by Sdcard_Init().

#define ARG_CMD8   0x000001AA

Definition at line 97 of file sdcard.h.

Referenced by Sdcard_Init().

#define BIT1   0

Definition at line 80 of file sd_cfg.h.

#define BIT4   1

Definition at line 81 of file sd_cfg.h.

#define BLOCK_SIZE   512

Definition at line 94 of file sdcard.h.

Referenced by Sdcard_ReadSector(), and Sdcard_WriteSector().

#define BUSWIDTH   BIT4

Definition at line 82 of file sd_cfg.h.

#define CMD8_WAIT   (0x00001000)

Definition at line 95 of file sdcard.h.

Referenced by Sdcard_Init().

#define CRC7_INIT   0

Definition at line 112 of file sdcmd.h.

#define CSD_STRUCT_OFFSET   14

Definition at line 104 of file sdcard.h.

#define CSD_VERSION_MASK   3

Definition at line 103 of file sdcard.h.

#define DELAYUNIT   5000

Definition at line 70 of file sd.c.

Referenced by dly_1unit().

#define E_SD_ACTIVE   -8

Definition at line 85 of file sdcard.h.

#define E_SD_CMD_CARD_ERR   -20

Definition at line 92 of file sdcard.h.

#define E_SD_CMD_CRC_ERR   -14

Definition at line 89 of file sdcard.h.

#define E_SD_CMD_ERR   -11

Definition at line 87 of file sdcard.h.

#define E_SD_CMD_IDLE   -16

Definition at line 91 of file sdcard.h.

#define E_SD_CMD_TIMEOUT   -13

Definition at line 88 of file sdcard.h.

#define E_SD_CMD_WR_ERR   -15

Definition at line 90 of file sdcard.h.

#define E_SD_DEVICE_TYPE   -7

Definition at line 84 of file sdcard.h.

#define E_SD_INIT   -2

Definition at line 79 of file sdcard.h.

Referenced by Sdcard_Init().

#define E_SD_NO_CARD   -1

Definition at line 78 of file sdcard.h.

Referenced by Sdcard_Init(), Sdcard_ReadSector(), and Sdcard_WriteSector().

#define E_SD_OK   0

Definition at line 77 of file sdcard.h.

Referenced by Sdcard_Init(), Sdcard_ReadSector(), and Sdcard_WriteSector().

#define E_SD_PARAMETER   -3

Definition at line 80 of file sdcard.h.

Referenced by Sdcard_Init(), Sdcard_ReadSector(), and Sdcard_WriteSector().

#define E_SD_PROTECT   -5

Definition at line 82 of file sdcard.h.

#define E_SD_READ   -4

Definition at line 81 of file sdcard.h.

Referenced by Sdcard_ReadSector().

#define E_SD_WRITE   -6

Definition at line 83 of file sdcard.h.

Referenced by Sdcard_WriteSector().

#define INIT_CCITT_CRC16   0x0000

Definition at line 113 of file sdcmd.h.

#define OCR_CAPACITY_MASK   0x40000000

Definition at line 106 of file sdcard.h.

Referenced by Sdcard_Init().

#define OCR_CAPACITY_OFFSET   30

Definition at line 105 of file sdcard.h.

#define PCG_ENABLE   (1UL<<8)

Definition at line 69 of file sd.c.

Referenced by SdEnablePCG().

#define RESET_ALL   1

Definition at line 92 of file sd.h.

Referenced by Sd_HostDeInit().

#define RESET_CMD   (1<<1)

Definition at line 91 of file sd.h.

#define RESET_DAT   (1<<2)

Definition at line 90 of file sd.h.

#define RETRYTIMES   100

Definition at line 72 of file sd.c.

Referenced by Sd_RxData(), and SdSendCmdOnly().

#define SD_ACMD13   (SD_ACMD_OFFSET + 13)

Definition at line 108 of file sdcmd.h.

Referenced by Sdcmd_SendCmd().

#define SD_ACMD41   (SD_ACMD_OFFSET + 41)

Definition at line 109 of file sdcmd.h.

Referenced by Sdcard_Init(), and Sdcmd_SendCmd().

#define SD_ACMD51   (SD_ACMD_OFFSET + 51)

Definition at line 110 of file sdcmd.h.

#define SD_ACMD6   (SD_ACMD_OFFSET + 6)

Definition at line 107 of file sdcmd.h.

Referenced by Sdcmd_SendCmd().

#define SD_ACMD_OFFSET   0x40

Definition at line 78 of file sdcmd.h.

#define SD_CAPACITY_HIGH   1

Definition at line 110 of file sdcard.h.

Referenced by Sdcard_Init().

#define SD_CAPACITY_STANDARD   0

Definition at line 109 of file sdcard.h.

Referenced by Sdcard_Init(), Sdcard_ReadSector(), and Sdcard_WriteSector().

#define SD_CMD0   (0)

Definition at line 81 of file sdcmd.h.

Referenced by Sdcard_Init(), and Sdcmd_SendCmd().

#define SD_CMD10   (10)

Definition at line 88 of file sdcmd.h.

Referenced by Sdcmd_SendCmd().

#define SD_CMD12   (12)

Definition at line 90 of file sdcmd.h.

Referenced by Sdcmd_SendCmd().

#define SD_CMD13   (13)

Definition at line 91 of file sdcmd.h.

Referenced by Sdcmd_SendCmd().

#define SD_CMD16   (16)

Definition at line 93 of file sdcmd.h.

Referenced by Sdcmd_SendCmd().

#define SD_CMD17   (17)

Definition at line 94 of file sdcmd.h.

Referenced by Sdcard_ReadSector(), and Sdcmd_SendCmd().

#define SD_CMD18   (18)

Definition at line 95 of file sdcmd.h.

#define SD_CMD2   (2)

Definition at line 82 of file sdcmd.h.

Referenced by Sdcard_Init(), and Sdcmd_SendCmd().

#define SD_CMD24   (24)

Definition at line 97 of file sdcmd.h.

Referenced by Sdcard_WriteSector(), and Sdcmd_SendCmd().

#define SD_CMD25   (25)

Definition at line 98 of file sdcmd.h.

#define SD_CMD28   (28)

Definition at line 100 of file sdcmd.h.

#define SD_CMD29   (29)

Definition at line 101 of file sdcmd.h.

#define SD_CMD3   (3)

Definition at line 83 of file sdcmd.h.

Referenced by Sdcard_Init(), and Sdcmd_SendCmd().

#define SD_CMD30   (30)

Definition at line 102 of file sdcmd.h.

#define SD_CMD55   (55)

Definition at line 104 of file sdcmd.h.

Referenced by Sdcard_Init(), and Sdcmd_SendCmd().

#define SD_CMD7   (7)

Definition at line 85 of file sdcmd.h.

Referenced by Sdcard_Init(), and Sdcmd_SendCmd().

#define SD_CMD8   (8)

Definition at line 86 of file sdcmd.h.

Referenced by Sdcard_Init(), and Sdcmd_SendCmd().

#define SD_CMD9   (9)

Definition at line 87 of file sdcmd.h.

Referenced by Sdcard_Init(), and Sdcmd_SendCmd().

#define SD_DLY_1UNIT   8000

Definition at line 68 of file sdcard.c.

Referenced by SdDly_1unit().

#define SD_PCG   0x4003C120

Definition at line 68 of file sd.c.

Referenced by SdDisablePCG(), and SdEnablePCG().

#define SD_TIMEOUT_DATLINE   1000

Definition at line 87 of file sd.h.

#define SD_VERSION_1_0   0

Definition at line 107 of file sdcard.h.

#define SD_VERSION_2_0   1

Definition at line 108 of file sdcard.h.

#define SDMA   1

Definition at line 76 of file sd_cfg.h.

#define stc_sd_t   FM4_SDIF_TypeDef

Definition at line 82 of file sd.h.

#define WAITTIME   50000

Definition at line 71 of file sd.c.

Referenced by SdGetCardStatus().

Typedef Documentation

SD auto command functions.

SD bus voltage level.

SD bus width.

typedef enum en_sd_clk en_sd_clk_t

SD clock SDCLK frequency.

typedef enum en_sd_dir en_sd_dir_t

SD transfer direction.

SD card exist status.

SD response.

SD special command.

SD transaction cases (with or without data line)

typedef void(* func_ptr_sd_arg32_t)(volatile FM4_SDIF_TypeDef *)

Definition at line 96 of file sd.h.

typedef struct stc_csdver1 stc_csdver1_t
typedef struct stc_csdver2 stc_csdver2_t

SD command configure data.

SD configure data.

SD command configure data.

Enumeration Type Documentation

SD auto command functions.

Enumerator
AUTO_DISABLE 
AUTO_CMD12 
AUTO_CMD23 

Definition at line 198 of file sd.h.

Enumerator
BOUND_4K 
BOUND_8K 
BOUND_16K 
BOUND_32K 
BOUND_64K 
BOUND_128K 
BOUND_256K 
BOUND_512K 

Definition at line 205 of file sd.h.

SD bus voltage level.

Enumerator
V1_8 
V3_0 
V3_3 

Definition at line 170 of file sd.h.

SD bus width.

Enumerator
BIT_1 

1 bit

BIT_4 

4 bits

Definition at line 160 of file sd.h.

enum en_sd_clk

SD clock SDCLK frequency.

Enumerator
CLK_25M 

25 Mhz

CLK_50M 

50 Mhz

Definition at line 103 of file sd.h.

SD special command.

Enumerator
NORMAL 

Non special command.

SUSPEND 

CMD52 for writing "Bus Suspend" in CCCR.

RESUME 

CMD52 for wirting "Function Select" in CCCR.

ABORT 

CMD12,CMD52 for writing "I/O abort" in CCCR.

Definition at line 137 of file sd.h.

enum en_sd_dir

SD transfer direction.

Enumerator
SD_WRITE 

host to card

SD_READ 

card to host

Definition at line 126 of file sd.h.

SD card exist status.

Enumerator
DEBOUNCING 

debouncing

INSERTED 

sd card is inserted

REMOVAL 

sd card is removed

Definition at line 149 of file sd.h.

SD response.

Enumerator
NO_RSP 
R1NORMAL_R5_R6_R7 
R1AUTO 
R1B_NORMAL 
R1B_AUTO 
R2 
R3_R4 
R5B 

Definition at line 182 of file sd.h.

SD transaction cases (with or without data line)

Enumerator
NO_RESPONSE 

command without response

NO_DATA 

no data in response or command

SINGLE_BLOCK 

read/write single block

MULTI_BLOCK 

read/write multiple blocks

Definition at line 113 of file sd.h.

Function Documentation

static void dly_1unit ( uint32_t  u32Delay)
static

Delay according to specified number.

Parameters
[in]u32DelayDelay number

Definition at line 108 of file sd.c.

References DELAYUNIT.

Referenced by Sd_RxData(), SdGetCardStatus(), and SdSendCmdOnly().

Here is the caller graph for this function:

en_result_t Sd_BusPowerControl ( volatile FM4_SDIF_TypeDef *  pstcSd,
en_sd_busvoltage_t  enVoltage 
)

Provide power for SD card.

Parameters
[in]pstcSdPointer of SDIF instance
[in]enVoltageSD card voltage setting
Return values
Ok

Definition at line 765 of file sd.c.

References ErrorInvalidParameter, Ok, V1_8, and V3_3.

Referenced by Sd_HostInit().

Here is the caller graph for this function:

en_sd_existing_t Sd_CardDetect ( volatile stc_sd_config_t *  pstcCmdCfg)

Detect the SD card insertion status.

Parameters
[in]pstcCmdCfgCommand parameters
Return values
DEBOUNCINGSD card is debouncing
INSERTEDSD card is inserted
REMOVALSD card is removed

Definition at line 680 of file sd.c.

References DEBOUNCING, and stc_sd_config::enExist.

Referenced by Sd_HostInit(), and Sdcard_Detect().

Here is the caller graph for this function:

en_result_t Sd_ClockStop ( volatile FM4_SDIF_TypeDef *  pstcSd)

stop clock

Parameters
[in]pstcSdPointer of SDIF instance
Return values
Ok

Definition at line 745 of file sd.c.

References ErrorInvalidParameter, and Ok.

en_result_t Sd_ClockSupply ( volatile FM4_SDIF_TypeDef *  pstcSd,
en_sd_clk_t  enClk 
)

Supply clock to an SD card.

Parameters
[in]pstcSdPointer of SDIF instance
[in]enClkSD Clock frequency
Return values
Ok

base clock

Definition at line 698 of file sd.c.

References ErrorInvalidParameter, and Ok.

Referenced by Sd_HostInit().

Here is the caller graph for this function:

en_result_t Sd_DisableInt ( volatile FM4_SDIF_TypeDef *  pstcSd)

Disable the interrupt flag of SD.

Parameters
[in]pstcSdPointer of SDIF instance
Return values
Ok

Definition at line 651 of file sd.c.

References ErrorInvalidParameter, and Ok.

Referenced by Sd_HostDeInit().

Here is the caller graph for this function:

en_result_t Sd_EnableInt ( volatile FM4_SDIF_TypeDef *  pstcSd)

Enable the interrupt flag of SD.

Parameters
[in]pstcSdPointer of SDIF instance
Return values
Ok

Definition at line 624 of file sd.c.

References ErrorInvalidParameter, and Ok.

Referenced by Sd_HostInit().

Here is the caller graph for this function:

void Sd_Handler ( void  )

SD interrupt entry.

Definition at line 911 of file sd.c.

References SdIrqHandler().

Here is the call graph for this function:

en_result_t Sd_HostDeInit ( void  )

Deinitializes SD.

Return values
OkInternal data has been setup

Definition at line 958 of file sd.c.

References Ok, RESET_ALL, Sd_DisableInt(), Sd_SoftwareReset(), Sd_stcCtrl, and SdDisablePCG().

Referenced by Sdcard_Init().

Here is the call graph for this function:

Here is the caller graph for this function:

en_result_t Sd_HostInit ( void  )

Initialize SD host.

Return values
OkInitialization successful
ErrorInitialization failed

Definition at line 924 of file sd.c.

References CLK_25M, Error, INSERTED, Ok, Sd_BusPowerControl(), Sd_CardDetect(), Sd_ClockSupply(), Sd_EnableInt(), Sd_stcCtrl, SdEnablePCG(), SdGetCardStatus(), SdInitHostInternalData(), SdInitPins(), TRUE, and V3_3.

Here is the call graph for this function:

boolean_t Sd_RxData ( en_sd_transaction_t  enTran,
volatile FM4_SDIF_TypeDef *  pstcSd,
stc_sd_comdconfig_t *  pstcCmdCfg,
uint32_t *  pu32Buf 
)

Receive the data of read command.

Parameters
[in]enTranTransmission type depends on the CMD and expected response
[in]pstcSdPointer of SDIF instance
[in]pstcCmdCfgCommand parameters
[out]pu32BufBuffer pointer storing tje recieved data
Return values
TRUEData is received
FAlSEError occurs during reception

Definition at line 854 of file sd.c.

References dly_1unit(), FALSE, MULTI_BLOCK, RETRYTIMES, SdRxData(), and SINGLE_BLOCK.

Referenced by Sdcmd_RxData().

Here is the call graph for this function:

Here is the caller graph for this function:

boolean_t Sd_SendCmd ( en_sd_transaction_t  enTran,
volatile FM4_SDIF_TypeDef *  pstcSd,
stc_sd_comdconfig_t *  pstcCmdCfg,
uint32_t *  pu32Buf 
)

Transmit commands and check the corresponding response.

Parameters
[in]enTranTransmission type depends on the CMD and expected response
[in]pstcSdPointer of SDIF instance
[in]pstcCmdCfgCommand parameters
[in]pu32BufBuffer pointer storing the recieved data
Return values
TRUEData is transmitted
FAlSEError occurs during transmission

Definition at line 790 of file sd.c.

References FALSE, MULTI_BLOCK, NO_DATA, NO_RESPONSE, SdRxRsp(), SdSendCmdDatLine(), SdSendCmdOnly(), SINGLE_BLOCK, and TRUE.

Referenced by Sdcmd_SendCmd().

Here is the call graph for this function:

Here is the caller graph for this function:

en_result_t Sd_SoftwareReset ( volatile FM4_SDIF_TypeDef *  pstcSd,
uint8_t  u8reset 
)

software reset for command and data line

Parameters
[in]pstcSdPointer of SDIF instance
[in]u8resetOptions of reset setting,such as command ,data line and all
Return values
Ok

Definition at line 890 of file sd.c.

References ErrorInvalidParameter, and Ok.

Referenced by Sd_HostDeInit().

Here is the caller graph for this function:

boolean_t Sd_TxData ( volatile FM4_SDIF_TypeDef *  pstcSd,
stc_sd_comdconfig_t *  pstcCmdCfg,
uint32_t *  pu32Buf 
)

Transmit data segment for write commands only.

Parameters
[in]pstcSdPointer of SDIF instance
[in]pstcCmdCfgCommand parameters
[in]pu32BufBuffer pointer storing tje recieved data
Return values
TRUEData is transmitted
FAlSEError occurs during transmission

Definition at line 832 of file sd.c.

References FALSE, and SdTxData().

Referenced by Sdcmd_TxData().

Here is the call graph for this function:

Here is the caller graph for this function:

boolean_t Sdcard_Detect ( void  )

detect SD

Return values
TRUEThe SD card is inserted
FALSEThe SD card is non-existent

Definition at line 121 of file sdcard.c.

References FALSE, INSERTED, Sd_CardDetect(), Sd_stcCfg, and TRUE.

Referenced by Sdcard_Init(), Sdcard_ReadSector(), and Sdcard_WriteSector().

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t Sdcard_ReadSector ( stc_sdcard_info_t *  pstcSdcardInfo,
uint32_t  u32Start,
uint16_t  u16Count,
uint8_t *  pu8Buf 
)

read one or multiple sectors of data

This function read the data and store into buffer

Parameters
[in]pstcSdcardInfoSD card information
[in]u32StartData address to read
[in]u16CountSector number to read
[in]pu8BufData buffer
Return values
E_SD_NO_CARDSD card is removed
E_SD_PARAMETERParameters are not valid
E_SD_READRead error
E_SD_OKSD card accessing is OK

<SDSC card uses byte unit address,multiply by 512

<SDHC and SDXC card use block unit address (512 Bytes)

Definition at line 313 of file sdcard.c.

References BLOCK_SIZE, E_SD_NO_CARD, E_SD_OK, E_SD_PARAMETER, E_SD_READ, FALSE, SD_CAPACITY_STANDARD, SD_CMD17, Sdcard_Detect(), Sdcmd_RxData(), Sdcmd_SendCmd(), stc_sdcard_info::stcCardType, TRUE, stc_sdcard_info::u32MaxSectorNum, and stc_sdcard_type::u8Capacity.

Here is the call graph for this function:

int32_t Sdcard_WriteSector ( stc_sdcard_info_t *  pstcSdcardInfo,
uint32_t  u32Start,
uint16_t  u16Count,
const uint8_t *  pu8Buf 
)

write data into the SD card

This function write one or multiple sectors of data into the SD card

Parameters
[in]pstcSdcardInfoSD card information
[in]u32StartData address to write
[in]u16CountSector number to write
[in]pu8BufData buffer
Return values
E_SD_NO_CARDSD card is removed
E_SD_PARAMETERParameters are not valid
E_SD_OKSD card accessing is OK

<SDSC card uses byte unit address,multiply by 512

<SDHC and SDXC card use block unit address (512 Bytes)

Definition at line 374 of file sdcard.c.

References BLOCK_SIZE, E_SD_NO_CARD, E_SD_OK, E_SD_PARAMETER, E_SD_WRITE, FALSE, SD_CAPACITY_STANDARD, SD_CMD24, Sdcard_Detect(), Sdcmd_SendCmd(), Sdcmd_TxData(), stc_sdcard_info::stcCardType, TRUE, stc_sdcard_info::u32MaxSectorNum, and stc_sdcard_type::u8Capacity.

Here is the call graph for this function:

static void SdClearCmdComplete ( void  )
static

Clear command complete flag.

Definition at line 248 of file sd.c.

References stc_sd_config::bCmdComplete, and FALSE.

Referenced by SdSendCmdOnly().

Here is the caller graph for this function:

boolean_t Sdcmd_RxData ( uint32_t *  pu32Buf)

get data of SD card

This function retrieve the data of SD card

Parameters
[out]pu32BufBuffer to store the received data
Return values
TRUEReceive data success
FALSEReceive data failure

Definition at line 108 of file sdcmd.c.

References Sd_RxData(), Sd_stcCmdCfg, Sd_stcCtrl, and SINGLE_BLOCK.

Referenced by Sdcard_ReadSector().

Here is the call graph for this function:

Here is the caller graph for this function:

boolean_t Sdcmd_SendCmd ( uint8_t  u8Cmd,
uint32_t  u32Arg,
uint32_t *  pu32Buf 
)

send command to SD card

This function will send out the command based on the SD speification.

Parameters
[in]u8CmdSD command index
[in]u32ArgSD command argument
[in]pu32BufBuffer to store the response data
Return values
OkSend success
ErrorSend failure

Definition at line 144 of file sdcmd.c.

References ABORT, stc_sd_cmdconfig::bDataPresent, stc_sd_cmdconfig::enCmdType, stc_sd_cmdconfig::enReadWrite, stc_sd_cmdconfig::enResponse, FALSE, NO_DATA, NO_RESPONSE, NO_RSP, NORMAL, R1B_AUTO, R1B_NORMAL, R1NORMAL_R5_R6_R7, R2, R3_R4, SD_ACMD13, SD_ACMD41, SD_ACMD6, SD_CMD0, SD_CMD10, SD_CMD12, SD_CMD13, SD_CMD16, SD_CMD17, SD_CMD2, SD_CMD24, SD_CMD3, SD_CMD55, SD_CMD7, SD_CMD8, SD_CMD9, SD_READ, Sd_SendCmd(), Sd_stcCmdCfg, Sd_stcCtrl, SD_WRITE, SINGLE_BLOCK, TRUE, stc_sd_cmdconfig::u16BlockCount, stc_sd_cmdconfig::u16BlockSize, stc_sd_cmdconfig::u32Argument1, and stc_sd_cmdconfig::u8Index.

Referenced by Sdcard_Init(), Sdcard_ReadSector(), and Sdcard_WriteSector().

Here is the call graph for this function:

Here is the caller graph for this function:

boolean_t Sdcmd_TxData ( uint32_t *  pu32Buf)

send data to SD card

This function send ata to SD card

Parameters
[in]pu32BufData buffer to be sent
Return values
TRUESend data success
FALSESend data failure

Definition at line 125 of file sdcmd.c.

References Sd_stcCmdCfg, Sd_stcCtrl, and Sd_TxData().

Referenced by Sdcard_WriteSector().

Here is the call graph for this function:

Here is the caller graph for this function:

static void SdDisablePCG ( void  )
static

Disable SD I/F peripheral clock.

Definition at line 175 of file sd.c.

References SD_PCG.

Referenced by Sd_HostDeInit().

Here is the caller graph for this function:

static void SdDly_1unit ( uint32_t  u32Delay)
static

Sd card delay function.

Parameters
[in]u32DelayDelay interval

Definition at line 101 of file sdcard.c.

References SD_DLY_1UNIT.

Referenced by Sdcard_Init().

Here is the caller graph for this function:

static boolean_t SdEnablePCG ( void  )
static

Enable SD I/F peripheral clock.

Definition at line 153 of file sd.c.

References FALSE, PCG_ENABLE, SD_PCG, and TRUE.

Referenced by Sd_HostInit().

Here is the caller graph for this function:

static void SdGetCardStatus ( volatile FM4_SDIF_TypeDef *  pstcSd,
volatile stc_sd_config_t *  pstcCfg 
)
static

Wait for card status stable and set the card status flag.

Parameters
[in]pstcSdPointer of SDIF instance
[in]pstcCfgPointer of SDIF configuration data

Definition at line 283 of file sd.c.

References dly_1unit(), stc_sd_config::enExist, INSERTED, REMOVAL, and WAITTIME.

Referenced by Sd_HostInit().

Here is the call graph for this function:

Here is the caller graph for this function:

static boolean_t SdGetCmdComplete ( void  )
static

Check whether the command complete.

Returns
TRUE Completed
FALSE Imcompleted

Definition at line 229 of file sd.c.

References stc_sd_config::bCmdComplete, FALSE, Sd_stcCtrl, and TRUE.

Referenced by SdSendCmdOnly().

Here is the caller graph for this function:

static void SdGetResponse ( volatile FM4_SDIF_TypeDef *  pstcSd,
en_sd_response_t  enSdresponse,
uint32_t *  pu32Buf 
)
static

get response

This function will get the response data.

Parameters
[in]pstcSdPointer of SDIF instance
[in]enSdresponseSD response type
[out]pu32BufBuffer of received response data.

Definition at line 324 of file sd.c.

References R1AUTO, R1B_AUTO, R1B_NORMAL, R1NORMAL_R5_R6_R7, R2, R3_R4, and R5B.

Referenced by SdRxRsp().

Here is the caller graph for this function:

static boolean_t SdGetTrsComplete ( void  )
static

Check whether the transfer complete.

Returns
TRUE Completed
FALSE Imcompleted

Definition at line 261 of file sd.c.

References stc_sd_config::bSendComplete, FALSE, Sd_stcCtrl, and TRUE.

Referenced by SdTxData().

Here is the caller graph for this function:

static void SdInitPins ( void  )
static

Init the IO configuration for SDIF.

SCLK :P0E CMD :P0D DATA1 :P0C DATA0 :P0B DATA3 :P0A DATA2 :P09 (AN19) CD :P63 WP :P62

< AN19 = 0 select none analog function

< P09~P0E =1

< P62~P63 =1

< PCR of P09~P0C =1 ,pull up the 4 data lines

< enable peripheral function

Definition at line 132 of file sd.c.

Referenced by Sd_HostInit().

Here is the caller graph for this function:

static void SdIrqHandler ( void  )
static

SD interrupt handler.

Definition at line 308 of file sd.c.

Referenced by Sd_Handler().

Here is the caller graph for this function:

static boolean_t SdRxData ( volatile FM4_SDIF_TypeDef *  pstcSd,
stc_sd_comdconfig_t *  pstcCmdCfg,
uint32_t *  pu32Buf 
)
static

Receive data segment using DAT line.

Parameters
[in]pstcSdPointer of SDIF instance
[in]pstcCmdCfgSD command configuration
[in]pu32BufPointer to reception buffer
Return values
TURECommand is sent
FALSECommand send error

Definition at line 580 of file sd.c.

References FALSE, TRUE, and stc_sd_cmdconfig::u16BlockSize.

Referenced by Sd_RxData().

Here is the caller graph for this function:

static boolean_t SdRxRsp ( volatile FM4_SDIF_TypeDef *  pstcSd,
stc_sd_comdconfig_t *  pstcCmdCfg,
uint32_t *  pu32Buf 
)
static

Get response data after command.

Parameters
[in]pstcSdPointer of SDIF instance
[in]pstcCmdCfgSD Command configuration
[out]pu32BufBuffer of received response data.
Return values
TUREThe correct response is received
FALSEError response is received

Definition at line 542 of file sd.c.

References stc_sd_cmdconfig::enResponse, FALSE, stc_sd_cmdconfig::pfnErrorResponseCallback, SdGetResponse(), and TRUE.

Referenced by Sd_SendCmd().

Here is the call graph for this function:

Here is the caller graph for this function:

static boolean_t SdSendCmdDatLine ( volatile FM4_SDIF_TypeDef *  pstcSd,
stc_sd_comdconfig_t *  pstcCmdCfg 
)
static

Send command with data transfer using DAT line.

Parameters
[in]pstcSdPointer of SDIF instance
[in]pstcCmdCfgPointer of SD command configuration
Return values
TURECommand is sent
FALSECommand sent error

Definition at line 457 of file sd.c.

References stc_sd_cmdconfig::enAutoCmd, FALSE, SdSendCmdOnly(), stc_sd_cmdconfig::u16BlockCount, and stc_sd_cmdconfig::u16BlockSize.

Referenced by Sd_SendCmd().

Here is the call graph for this function:

Here is the caller graph for this function:

static boolean_t SdSendCmdOnly ( volatile FM4_SDIF_TypeDef *  pstcSd,
stc_sd_comdconfig_t *  pstcCmdCfg 
)
static

Send command without data transfer using DAT line.

Parameters
[in]pstcSdPointer of SDIF instance
[in]pstcCmdCfgPointer of SD command configuration
Return values
TURECommand is sent
FALSECommand sent error

Definition at line 366 of file sd.c.

References stc_sd_cmdconfig::bDataPresent, dly_1unit(), stc_sd_cmdconfig::enReadWrite, stc_sd_cmdconfig::enResponse, FALSE, R1AUTO, R1B_AUTO, R1B_NORMAL, R1NORMAL_R5_R6_R7, R2, R3_R4, R5B, RETRYTIMES, SdClearCmdComplete(), SdGetCmdComplete(), TRUE, stc_sd_cmdconfig::u16BlockSize, stc_sd_cmdconfig::u32Argument1, and stc_sd_cmdconfig::u8Index.

Referenced by Sd_SendCmd(), and SdSendCmdDatLine().

Here is the call graph for this function:

Here is the caller graph for this function:

static boolean_t SdTxData ( volatile FM4_SDIF_TypeDef *  pstcSd,
stc_sd_comdconfig_t *  pstcCmdCfg,
uint32_t *  pu32Buf 
)
static

Send data segment using DAT line.

Parameters
[in]pstcSdPointer of SDIF instance
[in]pstcCmdCfgSD command configuration
[in]pu32BufData to be send
Return values
TUREData is sent
FALSEError occurs during transmission

Definition at line 494 of file sd.c.

References FALSE, SdGetTrsComplete(), TRUE, and stc_sd_cmdconfig::u16BlockSize.

Referenced by Sd_TxData().

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

uint32_t m_u32NumSector
static

Definition at line 85 of file sdcard.c.

Referenced by Sdcard_Init().

volatile stc_sd_config_t Sd_stcCfg

Definition at line 77 of file sd.c.

Referenced by Sdcard_Detect().

volatile stc_sd_config_t Sd_stcCfg

Definition at line 77 of file sd.c.

Referenced by Sdcard_Detect().

stc_sd_comdconfig_t Sd_stcCmdCfg

Definition at line 78 of file sd.c.

Referenced by Sdcmd_RxData(), Sdcmd_SendCmd(), and Sdcmd_TxData().

stc_sd_comdconfig_t Sd_stcCmdCfg

Definition at line 78 of file sd.c.

Referenced by Sdcmd_RxData(), Sdcmd_SendCmd(), and Sdcmd_TxData().

volatile FM4_SDIF_TypeDef* Sd_stcCtrl = (volatile FM4_SDIF_TypeDef *)FM4_SDIF_BASE
volatile FM4_SDIF_TypeDef* Sd_stcCtrl