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"

Macros

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

Functions

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 (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 (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

__IO SD_Error TransferError = SD_OK
 
__IO uint32_t TransferEnd = 0
 
__IO uint32_t DMAEndOfTransfer = 0
 
SD_CardInfo SDCardInfo
 

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

◆ 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

◆ 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

◆ PHAL_SDIO_SendCommand()

void PHAL_SDIO_SendCommand ( PHAL_SD_Cmd_t * cmd)

Send a command.

Parameters
cmd

◆ 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

◆ 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

◆ 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

◆ SD_GetState()

SDCardState SD_GetState ( void )

Returns the current card's state.

Parameters
None
Return values
SDCardStateSD Card Error or SD Card Current State.

◆ 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

◆ 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)

◆ 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

◆ 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

◆ SD_LowLevel_DMA_TxConfig()

void SD_LowLevel_DMA_TxConfig ( 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

◆ 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

◆ 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

◆ 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

◆ 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.

◆ 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

◆ 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.

◆ 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.

◆ 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.