PER Firmware
Loading...
Searching...
No Matches
sdio.c File Reference

Secure Digital Input Output Interface driver for STM32F4. More...

#include "sdio.h"
#include "main.h"
#include "string.h"
Include dependency graph for sdio.c:

Macros

#define SD_LOG(...)
 
#define SDIO_TXDMA_CONFIG(tx_addr_, priority_)
 
#define SDIO_RXDMA_CONFIG(rx_addr_, priority_)
 

Functions

static SD_Error CmdError (void)
 Checks for error conditions for CMD0.
 
static SD_Error CmdResp1Error (uint8_t cmd)
 Checks for error conditions for R1 response.
 
static SD_Error CmdResp7Error (void)
 Checks for error conditions for R7 response.
 
static SD_Error CmdResp3Error (void)
 Checks for error conditions for R3 (OCR) response.
 
static SD_Error CmdResp2Error (void)
 Checks for error conditions for R2 (CID or CSD) response.
 
static SD_Error CmdResp6Error (uint8_t cmd, uint16_t *prca)
 Checks for error conditions for R6 (RCA) response.
 
static SD_Error SDEnWideBus (FunctionalState NewState)
 Enables or disables the SDIO wide bus mode.
 
static SD_Error FindSCR (uint16_t rca, uint32_t *pscr)
 Find the SD card SCR register value.
 
bool PHAL_SDIO_init (void)
 Configures the SDIO peripheral.
 
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.
 
void PHAL_SDIO_SendCommand (const PHAL_SD_Cmd_t *cmd)
 Send a command.
 
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_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_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_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.
 
SD_Error SD_StopTransfer (void)
 Aborts an ongoing data transfer.
 
SD_Error SD_SendStatus (uint32_t *pcardstatus)
 Returns the current card's status.
 
void SDIO_IRQHandler (void)
 Allows to process all the interrupts that are high.
 
void DMA2_Stream6_IRQHandler (void)
 
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)
 

Variables

static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1
 
static uint32_t CSD_Tab [4]
 
static uint32_t CID_Tab [4]
 
static uint32_t RCA = 0
 
static uint8_t SDSTATUS_Tab [16]
 
__IO SD_Error TransferError = SD_OK
 
__IO uint32_t TransferEnd = 0
 
__IO uint32_t DMAEndOfTransfer = 0
 
SD_CardInfo SDCardInfo
 
static uint32_t last_read_length = 0
 
static dma_init_t sdio_tx_config = SDIO_TXDMA_CONFIG(NULL, 3)
 
static dma_init_t sdio_rx_config = SDIO_RXDMA_CONFIG(NULL, 3)
 

Detailed Description

Secure Digital Input Output Interface driver for STM32F4.

Author
Tilen Majerle, modified by Luke Oxley for PER usage (lcoxl.nosp@m.ey@p.nosp@m.urdue.nosp@m..edu)
Version
0.1
Date
2023-12-31

Macro Definition Documentation

◆ SD_LOG

#define SD_LOG ( ...)

◆ SDIO_RXDMA_CONFIG

#define SDIO_RXDMA_CONFIG ( rx_addr_,
priority_ )
Value:
{ \
.periph_addr = (uint32_t)&(SDIO->FIFO), \
.mem_addr = (uint32_t)(rx_addr_), \
.tx_size = 1, \
.increment = false, \
.circular = false, \
.dir = 0b0, \
.mem_inc = true, \
.periph_inc = false, \
.mem_to_mem = false, \
.priority = (priority_), \
.mem_size = 0b10, \
.periph_size = 0b10, \
.tx_isr_en = true, \
.dma_chan_request = 0b0100, \
.stream_idx = 6, \
.periph = DMA2, \
.stream = DMA2_Stream6}

◆ SDIO_TXDMA_CONFIG

#define SDIO_TXDMA_CONFIG ( tx_addr_,
priority_ )
Value:
{ \
.periph_addr = (uint32_t)&(SDIO->FIFO), \
.mem_addr = (uint32_t)(tx_addr_), \
.tx_size = 1, \
.increment = false, \
.circular = false, \
.dir = 0b1, \
.mem_inc = true, \
.periph_inc = false, \
.mem_to_mem = false, \
.priority = (priority_), \
.mem_size = 0b10, \
.periph_size = 0b10, \
.tx_isr_en = true, \
.dma_chan_request = 0b0100, \
.stream_idx = 6, \
.periph = DMA2, \
.stream = DMA2_Stream6}

Function Documentation

◆ CmdError()

static SD_Error CmdError ( void )
static

Checks for error conditions for CMD0.

Parameters
None
Return values
SD_ErrorSD Card Error code.

< 10000

Here is the caller graph for this function:

◆ CmdResp1Error()

static SD_Error CmdResp1Error ( uint8_t cmd)
static

Checks for error conditions for R1 response.

Parameters
cmdThe sent command index.
Return values
SD_ErrorSD Card Error code.

< We have received response, retrieve it for analysis

Here is the caller graph for this function:

◆ CmdResp2Error()

static SD_Error CmdResp2Error ( void )
static

Checks for error conditions for R2 (CID or CSD) response.

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

◆ CmdResp3Error()

static SD_Error CmdResp3Error ( void )
static

Checks for error conditions for R3 (OCR) response.

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

◆ CmdResp6Error()

static SD_Error CmdResp6Error ( uint8_t cmd,
uint16_t * prca )
static

Checks for error conditions for R6 (RCA) response.

Parameters
cmdThe sent command index.
prcapointer to the variable that will contain the SD card relative address RCA.
Return values
SD_ErrorSD Card Error code.

< We have received response, retrieve it.

Here is the caller graph for this function:

◆ CmdResp7Error()

static SD_Error CmdResp7Error ( void )
static

Checks for error conditions for R7 response.

Parameters
None
Return values
SD_ErrorSD Card Error code.

< Card is not V2.0 complient or card does not support the set voltage range

< Card is SD V2.0 compliant

Here is the caller graph for this function:

◆ DMA2_Stream6_IRQHandler()

void DMA2_Stream6_IRQHandler ( void )

◆ FindSCR()

static SD_Error FindSCR ( uint16_t rca,
uint32_t * pscr )
static

Find the SD card SCR register value.

Parameters
rcaselected card address.
pscrpointer to the buffer that will contain the SCR value.
Return values
SD_ErrorSD Card Error code.

< Set Block Size To 8 Bytes

< Send CMD55 APP_CMD with argument as card's RCA

< Send ACMD51 SD_APP_SEND_SCR with argument as 0

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

◆ PHAL_SDIO_init()

bool PHAL_SDIO_init ( void )

Configures the SDIO peripheral.

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:

◆ SDEnWideBus()

static SD_Error SDEnWideBus ( FunctionalState NewState)
static

Enables or disables the SDIO wide bus mode.

Parameters
NewStatenew state of the SDIO wide bus mode. This parameter can be: ENABLE or DISABLE.
Return values
SD_ErrorSD Card Error code.

< If requested card supports wide bus operation

< Send CMD55 APP_CMD with argument as card's RCA.

< If wide bus operation to be disabled

< If requested card supports 1 bit mode operation

< Send CMD55 APP_CMD with argument as card's RCA.

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

◆ SDIO_IRQHandler()

void SDIO_IRQHandler ( void )

Allows to process all the interrupts that are high.

Variable Documentation

◆ CardType

uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1
static

◆ CID_Tab

uint32_t CID_Tab[4]
static

◆ CSD_Tab

uint32_t CSD_Tab[4]
static

◆ DMAEndOfTransfer

__IO uint32_t DMAEndOfTransfer = 0

◆ last_read_length

uint32_t last_read_length = 0
static

◆ RCA

uint32_t RCA = 0
static

◆ SDCardInfo

SD_CardInfo SDCardInfo

◆ sdio_rx_config

dma_init_t sdio_rx_config = SDIO_RXDMA_CONFIG(NULL, 3)
static

◆ sdio_tx_config

dma_init_t sdio_tx_config = SDIO_TXDMA_CONFIG(NULL, 3)
static

◆ SDSTATUS_Tab

uint8_t SDSTATUS_Tab[16]
static

◆ TransferEnd

__IO uint32_t TransferEnd = 0

◆ TransferError

__IO SD_Error TransferError = SD_OK