PER Firmware
Loading...
Searching...
No Matches
sdio.h File Reference
#include <stddef.h>
#include "common/phal_F4/dma/dma.h"
#include "common/phal_F4/gpio/gpio.h"
#include "common/phal_F4/rcc/rcc.h"
#include "stm32f4xx.h"
Include dependency graph for sdio.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  PHAL_SD_Cmd_t
 
struct  SD_CSD
 Card Specific Data: CSD Register. More...
 
struct  SD_CID
 Card Identification Data: CID Register. More...
 
struct  SD_CardInfo
 SD Card information. More...
 

Macros

#define SDIO_STD_CAPACITY_SD_CARD_V1_1   ((uint32_t)0x00000000)
 Supported SD Memory Cards.
 
#define SDIO_STD_CAPACITY_SD_CARD_V2_0   ((uint32_t)0x00000001)
 
#define SDIO_HIGH_CAPACITY_SD_CARD   ((uint32_t)0x00000002)
 
#define SDIO_MULTIMEDIA_CARD   ((uint32_t)0x00000003)
 
#define SDIO_SECURE_DIGITAL_IO_CARD   ((uint32_t)0x00000004)
 
#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD   ((uint32_t)0x00000005)
 
#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD   ((uint32_t)0x00000006)
 
#define SDIO_HIGH_CAPACITY_MMC_CARD   ((uint32_t)0x00000007)
 
#define SDIO_CMD0TIMEOUT   ((uint32_t)0x00010000)
 
#define SDIO_STATIC_FLAGS   ((uint32_t)0x000005FF)
 
#define SD_CHECK_PATTERN   ((uint32_t)0x000001AA)
 
#define SD_CMD_GO_IDLE_STATE   ((uint8_t)0)
 SDIO Commands Index.
 
#define SD_CMD_SEND_OP_COND   ((uint8_t)1)
 
#define SD_CMD_ALL_SEND_CID   ((uint8_t)2)
 
#define SD_CMD_SET_REL_ADDR   ((uint8_t)3)
 
#define SD_CMD_SET_DSR   ((uint8_t)4)
 
#define SD_CMD_SDIO_SEN_OP_COND   ((uint8_t)5)
 
#define SD_CMD_HS_SWITCH   ((uint8_t)6)
 
#define SD_CMD_SEL_DESEL_CARD   ((uint8_t)7)
 
#define SD_CMD_HS_SEND_EXT_CSD   ((uint8_t)8)
 
#define SD_CMD_SEND_CSD   ((uint8_t)9)
 
#define SD_CMD_SEND_CID   ((uint8_t)10)
 
#define SD_CMD_READ_DAT_UNTIL_STOP   ((uint8_t)11)
 
#define SD_CMD_STOP_TRANSMISSION   ((uint8_t)12)
 
#define SD_CMD_SEND_STATUS   ((uint8_t)13)
 
#define SD_CMD_HS_BUSTEST_READ   ((uint8_t)14)
 
#define SD_CMD_GO_INACTIVE_STATE   ((uint8_t)15)
 
#define SD_CMD_SET_BLOCKLEN   ((uint8_t)16)
 
#define SD_CMD_READ_SINGLE_BLOCK   ((uint8_t)17)
 
#define SD_CMD_READ_MULT_BLOCK   ((uint8_t)18)
 
#define SD_CMD_HS_BUSTEST_WRITE   ((uint8_t)19)
 
#define SD_CMD_WRITE_DAT_UNTIL_STOP   ((uint8_t)20)
 
#define SD_CMD_SET_BLOCK_COUNT   ((uint8_t)23)
 
#define SD_CMD_WRITE_SINGLE_BLOCK   ((uint8_t)24)
 
#define SD_CMD_WRITE_MULT_BLOCK   ((uint8_t)25)
 
#define SD_CMD_PROG_CID   ((uint8_t)26)
 
#define SD_CMD_PROG_CSD   ((uint8_t)27)
 
#define SD_CMD_SET_WRITE_PROT   ((uint8_t)28)
 
#define SD_CMD_CLR_WRITE_PROT   ((uint8_t)29)
 
#define SD_CMD_SEND_WRITE_PROT   ((uint8_t)30)
 
#define SD_CMD_SD_ERASE_GRP_START   ((uint8_t)32)
 
#define SD_CMD_SD_ERASE_GRP_END   ((uint8_t)33)
 
#define SD_CMD_ERASE_GRP_START   ((uint8_t)35)
 
#define SD_CMD_ERASE_GRP_END   ((uint8_t)36)
 
#define SD_CMD_ERASE   ((uint8_t)38)
 
#define SD_CMD_FAST_IO   ((uint8_t)39)
 
#define SD_CMD_GO_IRQ_STATE   ((uint8_t)40)
 
#define SD_CMD_LOCK_UNLOCK   ((uint8_t)42)
 
#define SD_CMD_APP_CMD   ((uint8_t)55)
 
#define SD_CMD_GEN_CMD   ((uint8_t)56)
 
#define SD_CMD_NO_CMD   ((uint8_t)64)
 
#define SD_CMD_APP_SD_SET_BUSWIDTH   ((uint8_t)6)
 Following commands are SD Card Specific commands. SDIO_APP_CMD should be sent before sending these commands.
 
#define SD_CMD_SD_APP_STAUS   ((uint8_t)13)
 
#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS   ((uint8_t)22)
 
#define SD_CMD_SD_APP_OP_COND   ((uint8_t)41)
 
#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT   ((uint8_t)42)
 
#define SD_CMD_SD_APP_SEND_SCR   ((uint8_t)51)
 
#define SD_CMD_SDIO_RW_DIRECT   ((uint8_t)52)
 
#define SD_CMD_SDIO_RW_EXTENDED   ((uint8_t)53)
 
#define SD_CMD_SD_APP_GET_MKB   ((uint8_t)43)
 Following commands are SD Card Specific security commands. SDIO_APP_CMD should be sent before sending these commands.
 
#define SD_CMD_SD_APP_GET_MID   ((uint8_t)44)
 
#define SD_CMD_SD_APP_SET_CER_RN1   ((uint8_t)45)
 
#define SD_CMD_SD_APP_GET_CER_RN2   ((uint8_t)46)
 
#define SD_CMD_SD_APP_SET_CER_RES2   ((uint8_t)47)
 
#define SD_CMD_SD_APP_GET_CER_RES1   ((uint8_t)48)
 
#define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK   ((uint8_t)18)
 
#define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK   ((uint8_t)25)
 
#define SD_CMD_SD_APP_SECURE_ERASE   ((uint8_t)38)
 
#define SD_CMD_SD_APP_CHANGE_SECURE_AREA   ((uint8_t)49)
 
#define SD_CMD_SD_APP_SECURE_WRITE_MKB   ((uint8_t)48)
 
#define SD_OCR_ADDR_OUT_OF_RANGE   ((uint32_t)0x80000000)
 Mask for errors Card Status R1 (OCR Register)
 
#define SD_OCR_ADDR_MISALIGNED   ((uint32_t)0x40000000)
 
#define SD_OCR_BLOCK_LEN_ERR   ((uint32_t)0x20000000)
 
#define SD_OCR_ERASE_SEQ_ERR   ((uint32_t)0x10000000)
 
#define SD_OCR_BAD_ERASE_PARAM   ((uint32_t)0x08000000)
 
#define SD_OCR_WRITE_PROT_VIOLATION   ((uint32_t)0x04000000)
 
#define SD_OCR_LOCK_UNLOCK_FAILED   ((uint32_t)0x01000000)
 
#define SD_OCR_COM_CRC_FAILED   ((uint32_t)0x00800000)
 
#define SD_OCR_ILLEGAL_CMD   ((uint32_t)0x00400000)
 
#define SD_OCR_CARD_ECC_FAILED   ((uint32_t)0x00200000)
 
#define SD_OCR_CC_ERROR   ((uint32_t)0x00100000)
 
#define SD_OCR_GENERAL_UNKNOWN_ERROR   ((uint32_t)0x00080000)
 
#define SD_OCR_STREAM_READ_UNDERRUN   ((uint32_t)0x00040000)
 
#define SD_OCR_STREAM_WRITE_OVERRUN   ((uint32_t)0x00020000)
 
#define SD_OCR_CID_CSD_OVERWRIETE   ((uint32_t)0x00010000)
 
#define SD_OCR_WP_ERASE_SKIP   ((uint32_t)0x00008000)
 
#define SD_OCR_CARD_ECC_DISABLED   ((uint32_t)0x00004000)
 
#define SD_OCR_ERASE_RESET   ((uint32_t)0x00002000)
 
#define SD_OCR_AKE_SEQ_ERROR   ((uint32_t)0x00000008)
 
#define SD_OCR_ERRORBITS   ((uint32_t)0xFDFFE008)
 
#define SD_R6_GENERAL_UNKNOWN_ERROR   ((uint32_t)0x00002000)
 Masks for R6 Response.
 
#define SD_R6_ILLEGAL_CMD   ((uint32_t)0x00004000)
 
#define SD_R6_COM_CRC_FAILED   ((uint32_t)0x00008000)
 
#define SD_VOLTAGE_WINDOW_SD   ((uint32_t)0x80100000)
 
#define SD_HIGH_CAPACITY   ((uint32_t)0x40000000)
 
#define SD_STD_CAPACITY   ((uint32_t)0x00000000)
 
#define SD_CHECK_PATTERN   ((uint32_t)0x000001AA)
 
#define SD_MAX_VOLT_TRIAL   ((uint32_t)0x0000FFFF)
 
#define SD_ALLZERO   ((uint32_t)0x00000000)
 
#define SD_WIDE_BUS_SUPPORT   ((uint32_t)0x00040000)
 
#define SD_SINGLE_BUS_SUPPORT   ((uint32_t)0x00010000)
 
#define SD_CARD_LOCKED   ((uint32_t)0x02000000)
 
#define SD_DATATIMEOUT   ((uint32_t)0xFFFFFFFF)
 
#define SD_0TO7BITS   ((uint32_t)0x000000FF)
 
#define SD_8TO15BITS   ((uint32_t)0x0000FF00)
 
#define SD_16TO23BITS   ((uint32_t)0x00FF0000)
 
#define SD_24TO31BITS   ((uint32_t)0xFF000000)
 
#define SD_MAX_DATA_LENGTH   ((uint32_t)0x01FFFFFF)
 
#define SD_HALFFIFO   ((uint32_t)0x00000008)
 
#define SD_HALFFIFOBYTES   ((uint32_t)0x00000020)
 
#define SD_CCCC_LOCK_UNLOCK   ((uint32_t)0x00000080)
 Command Class Supported.
 
#define SD_CCCC_WRITE_PROT   ((uint32_t)0x00000040)
 
#define SD_CCCC_ERASE   ((uint32_t)0x00000020)
 
#define SD_PRESENT   ((uint8_t)0x01)
 SD detection on its memory slot.
 
#define SD_NOT_PRESENT   ((uint8_t)0x00)
 
#define SDIO_BusWide_1b   (1)
 
#define SDIO_BusWide_4b   (4)
 
#define SDIO_BusWide_8b   (8)
 
#define FATFS_SDIO_4BIT   1
 
#define SDIO_SEND_IF_COND   ((uint32_t)0x00000008)
 Following commands are SD Card Specific commands. SDIO_APP_CMD should be sent before sending these commands.
 
#define SDIO_DATABLOCKSIZE   ((uint32_t)((9 << 4)))
 
#define SDIO_INIT_CLK_DIV   ((uint8_t)118)
 SDIO Intialization Frequency (400KHz max)
 
#define SDIO_TRANSFER_CLK_DIV   ((uint8_t)0)
 SDIO Data Transfer Frequency (25MHz max)
 
#define SD_DMA_MODE   ((uint32_t)0x00000000)
 

Enumerations

enum  PHAL_SD_CmdResp_t { SD_RESP_NONE = 0b00 , SD_RESP_SHORT = 0b01 , SD_RESP_LONG = 0b11 }
 
enum  SD_Error {
  SD_CMD_CRC_FAIL = (1) , SD_DATA_CRC_FAIL = (2) , SD_CMD_RSP_TIMEOUT = (3) , SD_DATA_TIMEOUT = (4) ,
  SD_TX_UNDERRUN = (5) , SD_RX_OVERRUN = (6) , SD_START_BIT_ERR = (7) , SD_CMD_OUT_OF_RANGE = (8) ,
  SD_ADDR_MISALIGNED = (9) , SD_BLOCK_LEN_ERR = (10) , SD_ERASE_SEQ_ERR = (11) , SD_BAD_ERASE_PARAM = (12) ,
  SD_WRITE_PROT_VIOLATION = (13) , SD_LOCK_UNLOCK_FAILED = (14) , SD_COM_CRC_FAILED = (15) , SD_ILLEGAL_CMD = (16) ,
  SD_CARD_ECC_FAILED = (17) , SD_CC_ERROR = (18) , SD_GENERAL_UNKNOWN_ERROR = (19) , SD_STREAM_READ_UNDERRUN = (20) ,
  SD_STREAM_WRITE_OVERRUN = (21) , SD_CID_CSD_OVERWRITE = (22) , SD_WP_ERASE_SKIP = (23) , SD_CARD_ECC_DISABLED = (24) ,
  SD_ERASE_RESET = (25) , SD_AKE_SEQ_ERROR = (26) , SD_INVALID_VOLTRANGE = (27) , SD_ADDR_OUT_OF_RANGE = (28) ,
  SD_SWITCH_ERROR = (29) , SD_SDIO_DISABLED = (30) , SD_SDIO_FUNCTION_BUSY = (31) , SD_SDIO_FUNCTION_FAILED = (32) ,
  SD_SDIO_UNKNOWN_FUNCTION = (33) , SD_INTERNAL_ERROR , SD_NOT_CONFIGURED , SD_REQUEST_PENDING ,
  SD_REQUEST_NOT_APPLICABLE , SD_INVALID_PARAMETER , SD_UNSUPPORTED_FEATURE , SD_UNSUPPORTED_HW ,
  SD_ERROR , SD_OK = 0
}
 
enum  SDTransferState { SD_TRANSFER_OK = 0 , SD_TRANSFER_BUSY = 1 , SD_TRANSFER_ERROR }
 SDIO Transfer state. More...
 
enum  SDCardState {
  SD_CARD_READY = ((uint32_t)0x00000001) , SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002) , SD_CARD_STANDBY = ((uint32_t)0x00000003) , SD_CARD_TRANSFER = ((uint32_t)0x00000004) ,
  SD_CARD_SENDING = ((uint32_t)0x00000005) , SD_CARD_RECEIVING = ((uint32_t)0x00000006) , SD_CARD_PROGRAMMING = ((uint32_t)0x00000007) , SD_CARD_DISCONNECTED = ((uint32_t)0x00000008) ,
  SD_CARD_ERROR = ((uint32_t)0x000000FF)
}
 SD Card States. More...
 

Functions

bool PHAL_SDIO_init (void)
 Initialize SDIO peripheral with the configuired structure.
 
void SD_LowLevel_DMA_TxConfig (const uint32_t *BufferSRC, uint32_t BufferSize)
 Configures the DMA 2 Channel 6 for SDIO Tx request.
 
void SD_LowLevel_DMA_RxConfig (uint32_t *BufferDST, uint32_t BufferSize)
 Configures the DMA2 Channel4 for SDIO Rx request.
 
void SD_DeInit (void)
 
void PHAL_SDIO_SendCommand (const PHAL_SD_Cmd_t *cmd)
 Send a command.
 
SD_Error SD_Init (void)
 Initializes the SD Card and put it into StandBy State (Ready for data transfer).
 
SDTransferState SD_GetStatus (void)
 Gets the cuurent sd card data transfer status.
 
SDCardState SD_GetState (void)
 Returns the current card's state.
 
uint8_t SD_Detect (void)
 Detect if SD card is correctly plugged in the memory slot.
 
SD_Error SD_PowerON (void)
 Enquires cards about their operating voltage and configures clock controls.
 
SD_Error SD_InitializeCards (void)
 Intialises all cards or single card as the case may be Card(s) come into standby state.
 
SD_Error SD_GetCardInfo (SD_CardInfo *cardinfo)
 Returns information about specific card.
 
SD_Error SD_EnableWideBusOperation (uint32_t WideMode)
 Enables wide bus opeartion for the requeseted card if supported by card.
 
SD_Error SD_SelectDeselect (uint64_t addr)
 Selects od Deselects the corresponding card.
 
SD_Error SD_ReadMultiBlocks (uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
 Allows to read blocks from a specified address in a card. The Data transfer can be managed by DMA mode or Polling mode.
 
SD_Error SD_WriteMultiBlocks (uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
 Allows to write blocks starting from a specified address in a card. The Data transfer can be managed by DMA mode only.
 
SD_Error SD_StopTransfer (void)
 Aborts an ongoing data transfer.
 
SD_Error SD_SendStatus (uint32_t *pcardstatus)
 Returns the current card's status.
 
SD_Error SD_WaitReadOperation (void)
 This function waits until the SDIO DMA data transfer is finished. This function should be called after SDIO_ReadMultiBlocks() function to insure that all data sent by the card are already transferred by the DMA controller.
 
SD_Error SD_WaitWriteOperation (void)
 This function waits until the SDIO DMA data transfer is finished. This function should be called after SDIO_WriteBlock() and SDIO_WriteMultiBlocks() function to insure that all data sent by the card are already transferred by the DMA controller.
 

Macro Definition Documentation

◆ FATFS_SDIO_4BIT

#define FATFS_SDIO_4BIT   1

◆ SD_0TO7BITS

#define SD_0TO7BITS   ((uint32_t)0x000000FF)

◆ SD_16TO23BITS

#define SD_16TO23BITS   ((uint32_t)0x00FF0000)

◆ SD_24TO31BITS

#define SD_24TO31BITS   ((uint32_t)0xFF000000)

◆ SD_8TO15BITS

#define SD_8TO15BITS   ((uint32_t)0x0000FF00)

◆ SD_ALLZERO

#define SD_ALLZERO   ((uint32_t)0x00000000)

◆ SD_CARD_LOCKED

#define SD_CARD_LOCKED   ((uint32_t)0x02000000)

◆ SD_CCCC_ERASE

#define SD_CCCC_ERASE   ((uint32_t)0x00000020)

◆ SD_CCCC_LOCK_UNLOCK

#define SD_CCCC_LOCK_UNLOCK   ((uint32_t)0x00000080)

Command Class Supported.

◆ SD_CCCC_WRITE_PROT

#define SD_CCCC_WRITE_PROT   ((uint32_t)0x00000040)

◆ SD_CHECK_PATTERN [1/2]

#define SD_CHECK_PATTERN   ((uint32_t)0x000001AA)

◆ SD_CHECK_PATTERN [2/2]

#define SD_CHECK_PATTERN   ((uint32_t)0x000001AA)

◆ SD_CMD_ALL_SEND_CID

#define SD_CMD_ALL_SEND_CID   ((uint8_t)2)

◆ SD_CMD_APP_CMD

#define SD_CMD_APP_CMD   ((uint8_t)55)

◆ SD_CMD_APP_SD_SET_BUSWIDTH

#define SD_CMD_APP_SD_SET_BUSWIDTH   ((uint8_t)6)

Following commands are SD Card Specific commands. SDIO_APP_CMD should be sent before sending these commands.

For SD Card only

◆ SD_CMD_CLR_WRITE_PROT

#define SD_CMD_CLR_WRITE_PROT   ((uint8_t)29)

◆ SD_CMD_ERASE

#define SD_CMD_ERASE   ((uint8_t)38)

◆ SD_CMD_ERASE_GRP_END

#define SD_CMD_ERASE_GRP_END   ((uint8_t)36)

To set the address of the last write block of the continuous range to be erased. (For MMC card only spec 3.31)

◆ SD_CMD_ERASE_GRP_START

#define SD_CMD_ERASE_GRP_START   ((uint8_t)35)

To set the address of the first write block to be erased. (For MMC card only spec 3.31)

◆ SD_CMD_FAST_IO

#define SD_CMD_FAST_IO   ((uint8_t)39)

SD Card doesn't support it

◆ SD_CMD_GEN_CMD

#define SD_CMD_GEN_CMD   ((uint8_t)56)

◆ SD_CMD_GO_IDLE_STATE

#define SD_CMD_GO_IDLE_STATE   ((uint8_t)0)

SDIO Commands Index.

◆ SD_CMD_GO_INACTIVE_STATE

#define SD_CMD_GO_INACTIVE_STATE   ((uint8_t)15)

◆ SD_CMD_GO_IRQ_STATE

#define SD_CMD_GO_IRQ_STATE   ((uint8_t)40)

SD Card doesn't support it

◆ SD_CMD_HS_BUSTEST_READ

#define SD_CMD_HS_BUSTEST_READ   ((uint8_t)14)

◆ SD_CMD_HS_BUSTEST_WRITE

#define SD_CMD_HS_BUSTEST_WRITE   ((uint8_t)19)

◆ SD_CMD_HS_SEND_EXT_CSD

#define SD_CMD_HS_SEND_EXT_CSD   ((uint8_t)8)

◆ SD_CMD_HS_SWITCH

#define SD_CMD_HS_SWITCH   ((uint8_t)6)

◆ SD_CMD_LOCK_UNLOCK

#define SD_CMD_LOCK_UNLOCK   ((uint8_t)42)

◆ SD_CMD_NO_CMD

#define SD_CMD_NO_CMD   ((uint8_t)64)

◆ SD_CMD_PROG_CID

#define SD_CMD_PROG_CID   ((uint8_t)26)

reserved for manufacturers

◆ SD_CMD_PROG_CSD

#define SD_CMD_PROG_CSD   ((uint8_t)27)

◆ SD_CMD_READ_DAT_UNTIL_STOP

#define SD_CMD_READ_DAT_UNTIL_STOP   ((uint8_t)11)

SD Card doesn't support it

◆ SD_CMD_READ_MULT_BLOCK

#define SD_CMD_READ_MULT_BLOCK   ((uint8_t)18)

◆ SD_CMD_READ_SINGLE_BLOCK

#define SD_CMD_READ_SINGLE_BLOCK   ((uint8_t)17)

◆ SD_CMD_SD_APP_CHANGE_SECURE_AREA

#define SD_CMD_SD_APP_CHANGE_SECURE_AREA   ((uint8_t)49)

For SD Card only

◆ SD_CMD_SD_APP_GET_CER_RES1

#define SD_CMD_SD_APP_GET_CER_RES1   ((uint8_t)48)

For SD Card only

◆ SD_CMD_SD_APP_GET_CER_RN2

#define SD_CMD_SD_APP_GET_CER_RN2   ((uint8_t)46)

For SD Card only

◆ SD_CMD_SD_APP_GET_MID

#define SD_CMD_SD_APP_GET_MID   ((uint8_t)44)

For SD Card only

◆ SD_CMD_SD_APP_GET_MKB

#define SD_CMD_SD_APP_GET_MKB   ((uint8_t)43)

Following commands are SD Card Specific security commands. SDIO_APP_CMD should be sent before sending these commands.

For SD Card only

◆ SD_CMD_SD_APP_OP_COND

#define SD_CMD_SD_APP_OP_COND   ((uint8_t)41)

For SD Card only

◆ SD_CMD_SD_APP_SECURE_ERASE

#define SD_CMD_SD_APP_SECURE_ERASE   ((uint8_t)38)

For SD Card only

◆ SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK

#define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK   ((uint8_t)18)

For SD Card only

◆ SD_CMD_SD_APP_SECURE_WRITE_MKB

#define SD_CMD_SD_APP_SECURE_WRITE_MKB   ((uint8_t)48)

For SD Card only

◆ SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK

#define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK   ((uint8_t)25)

For SD Card only

◆ SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS

#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS   ((uint8_t)22)

For SD Card only

◆ SD_CMD_SD_APP_SEND_SCR

#define SD_CMD_SD_APP_SEND_SCR   ((uint8_t)51)

For SD Card only

◆ SD_CMD_SD_APP_SET_CER_RES2

#define SD_CMD_SD_APP_SET_CER_RES2   ((uint8_t)47)

For SD Card only

◆ SD_CMD_SD_APP_SET_CER_RN1

#define SD_CMD_SD_APP_SET_CER_RN1   ((uint8_t)45)

For SD Card only

◆ SD_CMD_SD_APP_SET_CLR_CARD_DETECT

#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT   ((uint8_t)42)

For SD Card only

◆ SD_CMD_SD_APP_STAUS

#define SD_CMD_SD_APP_STAUS   ((uint8_t)13)

For SD Card only

◆ SD_CMD_SD_ERASE_GRP_END

#define SD_CMD_SD_ERASE_GRP_END   ((uint8_t)33)

To set the address of the last write block of the continuous range to be erased. (For SD card only)

◆ SD_CMD_SD_ERASE_GRP_START

#define SD_CMD_SD_ERASE_GRP_START   ((uint8_t)32)

To set the address of the first write block to be erased. (For SD card only)

◆ SD_CMD_SDIO_RW_DIRECT

#define SD_CMD_SDIO_RW_DIRECT   ((uint8_t)52)

For SD I/O Card only

◆ SD_CMD_SDIO_RW_EXTENDED

#define SD_CMD_SDIO_RW_EXTENDED   ((uint8_t)53)

For SD I/O Card only

◆ SD_CMD_SDIO_SEN_OP_COND

#define SD_CMD_SDIO_SEN_OP_COND   ((uint8_t)5)

◆ SD_CMD_SEL_DESEL_CARD

#define SD_CMD_SEL_DESEL_CARD   ((uint8_t)7)

◆ SD_CMD_SEND_CID

#define SD_CMD_SEND_CID   ((uint8_t)10)

◆ SD_CMD_SEND_CSD

#define SD_CMD_SEND_CSD   ((uint8_t)9)

◆ SD_CMD_SEND_OP_COND

#define SD_CMD_SEND_OP_COND   ((uint8_t)1)

◆ SD_CMD_SEND_STATUS

#define SD_CMD_SEND_STATUS   ((uint8_t)13)

◆ SD_CMD_SEND_WRITE_PROT

#define SD_CMD_SEND_WRITE_PROT   ((uint8_t)30)

◆ SD_CMD_SET_BLOCK_COUNT

#define SD_CMD_SET_BLOCK_COUNT   ((uint8_t)23)

SD Card doesn't support it

◆ SD_CMD_SET_BLOCKLEN

#define SD_CMD_SET_BLOCKLEN   ((uint8_t)16)

◆ SD_CMD_SET_DSR

#define SD_CMD_SET_DSR   ((uint8_t)4)

◆ SD_CMD_SET_REL_ADDR

#define SD_CMD_SET_REL_ADDR   ((uint8_t)3)

SDIO_SEND_REL_ADDR for SD Card

◆ SD_CMD_SET_WRITE_PROT

#define SD_CMD_SET_WRITE_PROT   ((uint8_t)28)

◆ SD_CMD_STOP_TRANSMISSION

#define SD_CMD_STOP_TRANSMISSION   ((uint8_t)12)

◆ SD_CMD_WRITE_DAT_UNTIL_STOP

#define SD_CMD_WRITE_DAT_UNTIL_STOP   ((uint8_t)20)

SD Card doesn't support it

◆ SD_CMD_WRITE_MULT_BLOCK

#define SD_CMD_WRITE_MULT_BLOCK   ((uint8_t)25)

◆ SD_CMD_WRITE_SINGLE_BLOCK

#define SD_CMD_WRITE_SINGLE_BLOCK   ((uint8_t)24)

◆ SD_DATATIMEOUT

#define SD_DATATIMEOUT   ((uint32_t)0xFFFFFFFF)

◆ SD_DMA_MODE

#define SD_DMA_MODE   ((uint32_t)0x00000000)

◆ SD_HALFFIFO

#define SD_HALFFIFO   ((uint32_t)0x00000008)

◆ SD_HALFFIFOBYTES

#define SD_HALFFIFOBYTES   ((uint32_t)0x00000020)

◆ SD_HIGH_CAPACITY

#define SD_HIGH_CAPACITY   ((uint32_t)0x40000000)

◆ SD_MAX_DATA_LENGTH

#define SD_MAX_DATA_LENGTH   ((uint32_t)0x01FFFFFF)

◆ SD_MAX_VOLT_TRIAL

#define SD_MAX_VOLT_TRIAL   ((uint32_t)0x0000FFFF)

◆ SD_NOT_PRESENT

#define SD_NOT_PRESENT   ((uint8_t)0x00)

◆ SD_OCR_ADDR_MISALIGNED

#define SD_OCR_ADDR_MISALIGNED   ((uint32_t)0x40000000)

◆ SD_OCR_ADDR_OUT_OF_RANGE

#define SD_OCR_ADDR_OUT_OF_RANGE   ((uint32_t)0x80000000)

Mask for errors Card Status R1 (OCR Register)

◆ SD_OCR_AKE_SEQ_ERROR

#define SD_OCR_AKE_SEQ_ERROR   ((uint32_t)0x00000008)

◆ SD_OCR_BAD_ERASE_PARAM

#define SD_OCR_BAD_ERASE_PARAM   ((uint32_t)0x08000000)

◆ SD_OCR_BLOCK_LEN_ERR

#define SD_OCR_BLOCK_LEN_ERR   ((uint32_t)0x20000000)

◆ SD_OCR_CARD_ECC_DISABLED

#define SD_OCR_CARD_ECC_DISABLED   ((uint32_t)0x00004000)

◆ SD_OCR_CARD_ECC_FAILED

#define SD_OCR_CARD_ECC_FAILED   ((uint32_t)0x00200000)

◆ SD_OCR_CC_ERROR

#define SD_OCR_CC_ERROR   ((uint32_t)0x00100000)

◆ SD_OCR_CID_CSD_OVERWRIETE

#define SD_OCR_CID_CSD_OVERWRIETE   ((uint32_t)0x00010000)

◆ SD_OCR_COM_CRC_FAILED

#define SD_OCR_COM_CRC_FAILED   ((uint32_t)0x00800000)

◆ SD_OCR_ERASE_RESET

#define SD_OCR_ERASE_RESET   ((uint32_t)0x00002000)

◆ SD_OCR_ERASE_SEQ_ERR

#define SD_OCR_ERASE_SEQ_ERR   ((uint32_t)0x10000000)

◆ SD_OCR_ERRORBITS

#define SD_OCR_ERRORBITS   ((uint32_t)0xFDFFE008)

◆ SD_OCR_GENERAL_UNKNOWN_ERROR

#define SD_OCR_GENERAL_UNKNOWN_ERROR   ((uint32_t)0x00080000)

◆ SD_OCR_ILLEGAL_CMD

#define SD_OCR_ILLEGAL_CMD   ((uint32_t)0x00400000)

◆ SD_OCR_LOCK_UNLOCK_FAILED

#define SD_OCR_LOCK_UNLOCK_FAILED   ((uint32_t)0x01000000)

◆ SD_OCR_STREAM_READ_UNDERRUN

#define SD_OCR_STREAM_READ_UNDERRUN   ((uint32_t)0x00040000)

◆ SD_OCR_STREAM_WRITE_OVERRUN

#define SD_OCR_STREAM_WRITE_OVERRUN   ((uint32_t)0x00020000)

◆ SD_OCR_WP_ERASE_SKIP

#define SD_OCR_WP_ERASE_SKIP   ((uint32_t)0x00008000)

◆ SD_OCR_WRITE_PROT_VIOLATION

#define SD_OCR_WRITE_PROT_VIOLATION   ((uint32_t)0x04000000)

◆ SD_PRESENT

#define SD_PRESENT   ((uint8_t)0x01)

SD detection on its memory slot.

◆ SD_R6_COM_CRC_FAILED

#define SD_R6_COM_CRC_FAILED   ((uint32_t)0x00008000)

◆ SD_R6_GENERAL_UNKNOWN_ERROR

#define SD_R6_GENERAL_UNKNOWN_ERROR   ((uint32_t)0x00002000)

Masks for R6 Response.

◆ SD_R6_ILLEGAL_CMD

#define SD_R6_ILLEGAL_CMD   ((uint32_t)0x00004000)

◆ SD_SINGLE_BUS_SUPPORT

#define SD_SINGLE_BUS_SUPPORT   ((uint32_t)0x00010000)

◆ SD_STD_CAPACITY

#define SD_STD_CAPACITY   ((uint32_t)0x00000000)

◆ SD_VOLTAGE_WINDOW_SD

#define SD_VOLTAGE_WINDOW_SD   ((uint32_t)0x80100000)

◆ SD_WIDE_BUS_SUPPORT

#define SD_WIDE_BUS_SUPPORT   ((uint32_t)0x00040000)

◆ SDIO_BusWide_1b

#define SDIO_BusWide_1b   (1)

◆ SDIO_BusWide_4b

#define SDIO_BusWide_4b   (4)

◆ SDIO_BusWide_8b

#define SDIO_BusWide_8b   (8)

◆ SDIO_CMD0TIMEOUT

#define SDIO_CMD0TIMEOUT   ((uint32_t)0x00010000)

◆ SDIO_DATABLOCKSIZE

#define SDIO_DATABLOCKSIZE   ((uint32_t)((9 << 4)))

◆ SDIO_HIGH_CAPACITY_MMC_CARD

#define SDIO_HIGH_CAPACITY_MMC_CARD   ((uint32_t)0x00000007)

◆ SDIO_HIGH_CAPACITY_SD_CARD

#define SDIO_HIGH_CAPACITY_SD_CARD   ((uint32_t)0x00000002)

◆ SDIO_HIGH_SPEED_MULTIMEDIA_CARD

#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD   ((uint32_t)0x00000005)

◆ SDIO_INIT_CLK_DIV

#define SDIO_INIT_CLK_DIV   ((uint8_t)118)

SDIO Intialization Frequency (400KHz max)

◆ SDIO_MULTIMEDIA_CARD

#define SDIO_MULTIMEDIA_CARD   ((uint32_t)0x00000003)

◆ SDIO_SECURE_DIGITAL_IO_CARD

#define SDIO_SECURE_DIGITAL_IO_CARD   ((uint32_t)0x00000004)

◆ SDIO_SECURE_DIGITAL_IO_COMBO_CARD

#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD   ((uint32_t)0x00000006)

◆ SDIO_SEND_IF_COND

#define SDIO_SEND_IF_COND   ((uint32_t)0x00000008)

Following commands are SD Card Specific commands. SDIO_APP_CMD should be sent before sending these commands.

◆ SDIO_STATIC_FLAGS

#define SDIO_STATIC_FLAGS   ((uint32_t)0x000005FF)

◆ SDIO_STD_CAPACITY_SD_CARD_V1_1

#define SDIO_STD_CAPACITY_SD_CARD_V1_1   ((uint32_t)0x00000000)

Supported SD Memory Cards.

◆ SDIO_STD_CAPACITY_SD_CARD_V2_0

#define SDIO_STD_CAPACITY_SD_CARD_V2_0   ((uint32_t)0x00000001)

◆ SDIO_TRANSFER_CLK_DIV

#define SDIO_TRANSFER_CLK_DIV   ((uint8_t)0)

SDIO Data Transfer Frequency (25MHz max)

Enumeration Type Documentation

◆ PHAL_SD_CmdResp_t

Enumerator
SD_RESP_NONE 

No response, expect CMDSENT flag.

SD_RESP_SHORT 

Short response, expect CMDREND / CCRCFAIL flag.

SD_RESP_LONG 

Long response, expect CMDREND / CCRCFAIL flag.

◆ SD_Error

enum SD_Error
Enumerator
SD_CMD_CRC_FAIL 

SDIO specific error defines.

Command response received (but CRC check failed)

SD_DATA_CRC_FAIL 

Data bock sent/received (CRC check Failed)

SD_CMD_RSP_TIMEOUT 

Command response timeout

SD_DATA_TIMEOUT 

Data time out

SD_TX_UNDERRUN 

Transmit FIFO under-run

SD_RX_OVERRUN 

Receive FIFO over-run

SD_START_BIT_ERR 

Start bit not detected on all data signals in widE bus mode

SD_CMD_OUT_OF_RANGE 

CMD's argument was out of range.

SD_ADDR_MISALIGNED 

Misaligned address

SD_BLOCK_LEN_ERR 

Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length

SD_ERASE_SEQ_ERR 

An error in the sequence of erase command occurs.

SD_BAD_ERASE_PARAM 

An Invalid selection for erase groups

SD_WRITE_PROT_VIOLATION 

Attempt to program a write protect block

SD_LOCK_UNLOCK_FAILED 

Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card

SD_COM_CRC_FAILED 

CRC check of the previous command failed

SD_ILLEGAL_CMD 

Command is not legal for the card state

SD_CARD_ECC_FAILED 

Card internal ECC was applied but failed to correct the data

SD_CC_ERROR 

Internal card controller error

SD_GENERAL_UNKNOWN_ERROR 

General or Unknown error

SD_STREAM_READ_UNDERRUN 

The card could not sustain data transfer in stream read operation.

SD_STREAM_WRITE_OVERRUN 

The card could not sustain data programming in stream mode

SD_CID_CSD_OVERWRITE 

CID/CSD overwrite error

SD_WP_ERASE_SKIP 

only partial address space was erased

SD_CARD_ECC_DISABLED 

Command has been executed without using internal ECC

SD_ERASE_RESET 

Erase sequence was cleared before executing because an out of erase sequence command was received

SD_AKE_SEQ_ERROR 

Error in sequence of authentication.

SD_INVALID_VOLTRANGE 
SD_ADDR_OUT_OF_RANGE 
SD_SWITCH_ERROR 
SD_SDIO_DISABLED 
SD_SDIO_FUNCTION_BUSY 
SD_SDIO_FUNCTION_FAILED 
SD_SDIO_UNKNOWN_FUNCTION 
SD_INTERNAL_ERROR 

Standard error defines.

SD_NOT_CONFIGURED 
SD_REQUEST_PENDING 
SD_REQUEST_NOT_APPLICABLE 
SD_INVALID_PARAMETER 
SD_UNSUPPORTED_FEATURE 
SD_UNSUPPORTED_HW 
SD_ERROR 
SD_OK 

◆ SDCardState

SD Card States.

Enumerator
SD_CARD_READY 
SD_CARD_IDENTIFICATION 
SD_CARD_STANDBY 
SD_CARD_TRANSFER 
SD_CARD_SENDING 
SD_CARD_RECEIVING 
SD_CARD_PROGRAMMING 
SD_CARD_DISCONNECTED 
SD_CARD_ERROR 

◆ SDTransferState

SDIO Transfer state.

Enumerator
SD_TRANSFER_OK 
SD_TRANSFER_BUSY 
SD_TRANSFER_ERROR 

Function Documentation

◆ PHAL_SDIO_init()

bool PHAL_SDIO_init ( void )

Initialize SDIO peripheral with the configuired structure.

Parameters
configSDIO initial configuration
Returns
true
false

Initialize SDIO peripheral with the configuired structure.

Parameters
cfg
Returns
true Success
false Fail
Here is the call graph for this function:

◆ PHAL_SDIO_SendCommand()

void PHAL_SDIO_SendCommand ( const PHAL_SD_Cmd_t * cmd)

Send a command.

Parameters
cmd
Here is the caller graph for this function:

◆ SD_DeInit()

void SD_DeInit ( void )

< Disable SDIO Clock

< Set Power State to OFF

< DeInitializes the SDIO peripheral

◆ SD_Detect()

uint8_t SD_Detect ( void )

Detect if SD card is correctly plugged in the memory slot.

Parameters
None
Return values
Returnif SD is detected or not
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_EnableWideBusOperation()

SD_Error SD_EnableWideBusOperation ( uint32_t WideMode)

Enables wide bus opeartion for the requeseted card if supported by card.

Parameters
WideModeSpecifies the SD card wide bus mode. This parameter can be one of the following values:
  • SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
  • SDIO_BusWide_4b: 4-bit data transfer
  • SDIO_BusWide_1b: 1-bit data transfer
Return values
SD_ErrorSD Card Error code.

< MMC Card doesn't support this feature

< Configure the SDIO peripheral

< Configure the SDIO peripheral

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_GetCardInfo()

SD_Error SD_GetCardInfo ( SD_CardInfo * cardinfo)

Returns information about specific card.

Parameters
cardinfopointer to a SD_CardInfo structure that contains all SD card information.
Return values
SD_ErrorSD Card Error code.

< Byte 0

< Byte 1

< Byte 2

< Byte 3

< Byte 4

< Byte 5

< Byte 6

< Reserved

< Byte 7

< Byte 8

< Byte 9

< Byte 10

< Byte 7

< Byte 8

< Byte 9

< Byte 10

< Byte 11

< Byte 12

< Byte 13

< Byte 14

< Byte 15

< Byte 0

< Byte 1

< Byte 2

< Byte 3

< Byte 4

< Byte 5

< Byte 6

< Byte 7

< Byte 8

< Byte 9

< Byte 10

< Byte 11

< Byte 12

< Byte 13

< Byte 14

< Byte 15

Here is the caller graph for this function:

◆ SD_GetState()

SDCardState SD_GetState ( void )

Returns the current card's state.

Parameters
None
Return values
SDCardStateSD Card Error or SD Card Current State.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_GetStatus()

SDTransferState SD_GetStatus ( void )

Gets the cuurent sd card data transfer status.

Parameters
None
Return values
SDTransferStateData Transfer state. This value can be:
  • SD_TRANSFER_OK: No data transfer is acting
  • SD_TRANSFER_BUSY: Data transfer is acting
Here is the call graph for this function:

◆ SD_Init()

SD_Error SD_Init ( void )

Initializes the SD Card and put it into StandBy State (Ready for data transfer).

Parameters
None
Return values
SD_ErrorSD Card Error code.

< CMD Response TimeOut (wait for CMDSENT flag)

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_InitializeCards()

SD_Error SD_InitializeCards ( void )

Intialises all cards or single card as the case may be Card(s) come into standby state.

Parameters
None
Return values
SD_ErrorSD Card Error code.

< Send CMD2 ALL_SEND_CID

< Send CMD3 SET_REL_ADDR with argument 0

< SD Card publishes its RCA.

< Send CMD9 SEND_CSD with argument as card's RCA

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_LowLevel_DMA_RxConfig()

void SD_LowLevel_DMA_RxConfig ( uint32_t * BufferDST,
uint32_t BufferSize )

Configures the DMA2 Channel4 for SDIO Rx request.

Parameters
BufferDSTpointer to the destination buffer
BufferSizebuffer size
Return values
None
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_LowLevel_DMA_TxConfig()

void SD_LowLevel_DMA_TxConfig ( const uint32_t * BufferSRC,
uint32_t BufferSize )

Configures the DMA 2 Channel 6 for SDIO Tx request.

Parameters
BufferSRCpointer to the source buffer
BufferSizebuffer size
Return values
None
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_PowerON()

SD_Error SD_PowerON ( void )

Enquires cards about their operating voltage and configures clock controls.

Parameters
None
Return values
SD_ErrorSD Card Error code.

< CMD0: GO_IDLE_STATE ------------------------------------------------—

< No CMD response required

< CMD Response TimeOut (wait for CMDSENT flag)

< CMD8: SEND_IF_COND -------------------------------------------------—

< Send CMD8 to verify SD card interface operating condition

< Argument: - [31:12]: Reserved (shall be set to '0')

  • [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
  • [7:0]: Check Pattern (recommended 0xAA)

< CMD Response: R7

< SD Card 2.0

< CMD55

< If errorstatus is Command TimeOut, it is a MMC card

< If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch) or SD card 1.x

< SD CARD

< Send ACMD41 SD_APP_OP_COND with Argument 0x80100000

< SEND CMD55 APP_CMD with RCA as 0

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_ReadMultiBlocks()

SD_Error SD_ReadMultiBlocks ( uint8_t * readbuff,
uint64_t ReadAddr,
uint16_t BlockSize,
uint32_t NumberOfBlocks )

Allows to read blocks from a specified address in a card. The Data transfer can be managed by DMA mode or Polling mode.

Note
This operation should be followed by two functions to check if the DMA Controller and SD Card status.
  • SD_ReadWaitOperation(): this function insure that the DMA controller has finished all data transfer.
  • SD_GetStatus(): to check that the SD Card has finished the data transfer and it is ready for data.
Parameters
readbuffpointer to the buffer that will contain the received data.
ReadAddrAddress from where data are to be read.
BlockSizethe SD card Data block size. The Block size should be 512.
NumberOfBlocksnumber of blocks to be read.
Return values
SD_ErrorSD Card Error code.

< Send CMD18 READ_MULT_BLOCK with argument data address

< Send CMD17 READ_SINGLE_BLOCK with argument data address

Here is the call graph for this function:

◆ SD_SelectDeselect()

SD_Error SD_SelectDeselect ( uint64_t addr)

Selects od Deselects the corresponding card.

Parameters
addrAddress of the Card to be selected.
Return values
SD_ErrorSD Card Error code.

< Send CMD7 SDIO_SEL_DESEL_CARD

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_SendStatus()

SD_Error SD_SendStatus ( uint32_t * pcardstatus)

Returns the current card's status.

Parameters
pcardstatuspointer to the buffer that will contain the SD card status (Card Status register).
Return values
SD_ErrorSD Card Error code.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_StopTransfer()

SD_Error SD_StopTransfer ( void )

Aborts an ongoing data transfer.

Parameters
None
Return values
SD_ErrorSD Card Error code.

< Send CMD12 STOP_TRANSMISSION

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_WaitReadOperation()

SD_Error SD_WaitReadOperation ( void )

This function waits until the SDIO DMA data transfer is finished. This function should be called after SDIO_ReadMultiBlocks() function to insure that all data sent by the card are already transferred by the DMA controller.

Parameters
None.
Return values
SD_ErrorSD Card Error code.
Here is the call graph for this function:

◆ SD_WaitWriteOperation()

SD_Error SD_WaitWriteOperation ( void )

This function waits until the SDIO DMA data transfer is finished. This function should be called after SDIO_WriteBlock() and SDIO_WriteMultiBlocks() function to insure that all data sent by the card are already transferred by the DMA controller.

Parameters
None.
Return values
SD_ErrorSD Card Error code.
Here is the call graph for this function:

◆ SD_WriteMultiBlocks()

SD_Error SD_WriteMultiBlocks ( uint8_t * writebuff,
uint64_t WriteAddr,
uint16_t BlockSize,
uint32_t NumberOfBlocks )

Allows to write blocks starting from a specified address in a card. The Data transfer can be managed by DMA mode only.

Note
This operation should be followed by two functions to check if the DMA Controller and SD Card status.
  • SD_ReadWaitOperation(): this function insure that the DMA controller has finished all data transfer.
  • SD_GetStatus(): to check that the SD Card has finished the data transfer and it is ready for data.
Parameters
WriteAddrAddress from where data are to be read.
writebuffpointer to the buffer that contain the data to be transferred.
BlockSizethe SD card Data block size. The Block size should be 512.
NumberOfBlocksnumber of blocks to be written.
Return values
SD_ErrorSD Card Error code.
Here is the call graph for this function: