SDIO Library

The SD/SDIO MMC card host interface (SDIO) provides an interface between the APB2 peripheral bus and MultiMediaCards (MMCs), SD memory cards, SDIO cards and CE-ATA devices. It is available only for the Cortex M4 devices.
The MultiMediaCard/SD bus connects cards to the controller.

The SDIO features include the following :

Library Routines

SDIO_Reset

Prototype

void SDIO_Reset();

Description

Deinitializes the SD I/O peripheral registers to their default reset values.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_Init

Prototype

void SDIO_Init(unsigned long cfg, char clockDiv, const Module_Struct *module);

Description

Initializes the SD I/O peripheral according to the specified parameters.

Parameters
  • cfg : Configuration paramerer. Valid values :
    Value Description
    _SDIO_CFG_POWER_SAVE_DISABLE SD I/O Clock output is disabled when the bus is idle.
    _SDIO_CFG_POWER_SAVE_ENABLE SD I/O Clock output is enabled when the bus is idle.
    _SDIO_CFG_1_WIDE_BUS_MODE SD I/O bus width.
    _SDIO_CFG_4_WIDE_BUS_MODE SD I/O bus width.
    _SDIO_CFG_8_WIDE_BUS_MODE SD I/O bus width.
    _SDIO_CFG_CLOCK_BYPASS_DISABLE SD I/O Clock divider bypass is disabled.
    _SDIO_CFG_CLOCK_BYPASS_ENABLE SD I/O Clock divider bypass is enabled.
    _SDIO_CFG_CLOCK_RISING_EDGE Bit capture is made on rising edge.
    _SDIO_CFG_CLOCK_FALING_EDGE Bit capture is made on falling edge.
    _SDIO_CFG_HW_FLOW_DISABLE SD I/O hardware flow control is disabled.
    _SDIO_CFG_HW_FLOW_ENABLE SD I/O hardware flow control is enabled.
  • clockDiv: Specifies the divide factor between the input clock (SDIOCLK) and the output clock (SDIO_CK) (SDIO_CK frequency = SDIOCLK / [clockDiv + 2]).
  • module: Appropriate SD I/O module pinout. Valid values :
    SD I/O Module Pinouts
    _GPIO_MODULE_SDIO_D0_D7 _GPIO_MODULE_SDIO_D0_D3 _GPIO_MODULE_SDIO_D0
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_ClockCmd

Prototype

void SDIO_ClockCmd(char newState);

Description

Enables or disables the SD I/O Clock.

Parameters
  • cfg : New state of the SD I/O Clock. Valid values :
    Value Description
    _SDIO_DISABLE SD I/O Clock is disabled.
    _SDIO_ENABLE SD I/O Clock is enabled.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_SetPowerState

Prototype

void SDIO_SetPowerState(char powerState);

Description

Sets the power status of the controller.

Parameters
  • cfg : New power state of the SD I/O controller. Valid values :
    Value Description
    _SDIO_POWER_STATE_OFF The clock to card is stopped.
    _SDIO_POWER_STATE_ON The card is clocked.
    _SDIO_POWER_STATE_UP Reserved power-up.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_GetPowerState

Prototype

unsigned long SDIO_GetPowerState();

Description

Returns the power status of the controller.

Parameters

None.

Returns

Power status of the controller. The returned value can be one of the following values :

Value Symbolic Name Description
0 _SDIO_POWER_STATE_OFF The clock to card is stopped.
%11 _SDIO_POWER_STATE_ON The card is clocked.
%10 _SDIO_POWER_STATE_UP Reserved power-up.
Requires

Nothing.

Notes

None.

SDIO_SendCommand

Prototype

void SDIO_SendCommand(SDIO_CmdInitTypeDef *SDIO_CmdInitStruct);

Description

Initializes the SD I/O Command according to the specified parameters in the SDIO_CmdInitStruct and sends the command.

Parameters
  • SDIO_CmdInitStruct : record consisted of the following fields :
    typedef struct {
      unsigned long argument;  // Specifies the SD I/O command argument which is sent
                               // to a card as part of a command message. If a command
                               // contains an argument, it must be loaded into this register
                               // before writing the command to the command register.     
      unsigned long cmdIndex;  // Specifies the SD I/O command index. It must be lower than 0x40.
      unsigned long response;  // Specifies the SD I/O response type.
      unsigned long wait;      // Specifies whether SD I/O wait-for-interrupt request is enabled or disabled.
      unsigned long CPSM;      // Specifies whether SD I/O Command path state machine (CPSM) is enabled or disabled.
    } SDIO_CmdInitTypeDef;
    
SDIO_CmdInitTypeDef Record Field Values
response _SDIO_RESPONSE_NO _SDIO_RESPONSE_SHORT _SDIO_RESPONSE_LONG
wait _SDIO_WAIT_NO _SDIO_WAIT_IT _SDIO_WAIT_PEND
CPSM _SDIO_CPSM_DISABLE _SDIO_CPSM_ENABLE
Returns

None.

Requires

Nothing.

Notes

None.

SDIO_GetCommandResponse

Prototype

char SDIO_GetCommandResponse();

Description

Returns command index of last command for which response received.

Parameters

Returns

Command index of the last command response received.

Requires

Nothing.

Notes

None.

SDIO_DataConfig

Prototype

void SDIO_DataConfig(SDIO_DataInitTypeDef* SDIO_DataInitStruct);

Description

Initializes the SD I/O data path according to the specified parameters in the SDIO_DataInitStruct parameter.

Parameters
  • SDIO_DataInitStruct : record consisted of the following fields :
    typedef struct
      unsigned long dataTimeOut    // Specifies the data timeout period in card bus clock periods.
      unsigned long dataLength     // Specifies the number of data bytes to be transferred.
      unsigned long dataBlockSize  // Specifies the data block size for block transfer.
      unsigned long transferDir    // Specifies the data transfer direction, whether the transfer is a read or write.
      unsigned long transferMode   // Specifies whether data transfer is in stream or block mode.
      unsigned long DPSM           // Specifies whether SD I/O Data path state machine (DPSM)  is enabled or disabled.
    } SDIO_DataInitTypeDef;
    
SDIO_DataInitTypeDef Record Field Values
dataBlockSize _SDIO_DATA_BLOCK_SIZE_1 _SDIO_DATA_BLOCK_SIZE_2 _SDIO_DATA_BLOCK_SIZE_4 _SDIO_DATA_BLOCK_SIZE_8 _SDIO_DATA_BLOCK_SIZE_16
_SDIO_DATA_BLOCK_SIZE_32 _SDIO_DATA_BLOCK_SIZE_64 _SDIO_DATA_BLOCK_SIZE_128 _SDIO_DATA_BLOCK_SIZE_256 _SDIO_DATA_BLOCK_SIZE_512
_SDIO_DATA_BLOCK_SIZE_32 _SDIO_DATA_BLOCK_SIZE_64 _SDIO_DATA_BLOCK_SIZE_128 _SDIO_DATA_BLOCK_SIZE_256 _SDIO_DATA_BLOCK_SIZE_512
_SDIO_DATA_BLOCK_SIZE_1024 _SDIO_DATA_BLOCK_SIZE_2048 _SDIO_DATA_BLOCK_SIZE_4096 _SDIO_DATA_BLOCK_SIZE_8192 _SDIO_DATA_BLOCK_SIZE_16384
transferDir _SDIO_TRANSFER_DIR_TO_CARD _SDIO_TRANSFER_DIR_TO_SDIO
transferMode _SDIO_TRANSFER_MODE_BLOCK _SDIO_TRANSFER_MODE_STREAM
DPSM _SDIO_DPSM_DISABLE _SDIO_DPSM_ENABLE
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_GetDataCounter

Prototype

unsigned long SDIO_GetDataCounter();

Description

Returns number of remaining data bytes to be transferred.

Parameters

None.

Returns

Number of remaining data bytes to be transferred.

Requires

Nothing.

Notes

None.

SDIO_ReadData

Prototype

unsigned long SDIO_ReadData();

Description

Read one data word from Rx FIFO.

Parameters

None.

Returns

One data word from Rx FIFO.

Requires

Nothing.

Notes

None.

SDIO_WriteData

Prototype

void SDIO_WriteData(unsigned long dat);

Description

Write one data word to Tx FIFO.

Parameters
  • dat: 32-bit data word to write.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_GetFIFOCount

Prototype

unsigned long SDIO_GetFIFOCount();

Description

Returns the number of words left to be written to or read from FIFO.

Parameters

None.

Returns

Nothing.

Requires

Remaining number of words.

Notes

None.

SDIO_StartSDIOReadWait

Prototype

void SDIO_StartSDIOReadWait(char newState);

Description

Starts the SD I/O Read Wait operation.

Parameters
  • newState: new state of the Start SD I/O Read Wait operation. Valid values :
    Value Description
    _SDIO_DISABLE SD I/O Clock is disabled.
    _SDIO_ENABLE SD I/O Clock is enabled.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_StopSDIOReadWait

Prototype

void SDIO_StopSDIOReadWait(char newState);

Description

Stops the SD I/O Read Wait operation.

Parameters
  • newState: new state of the Stop SD I/O Read Wait operation. Valid values :
    Value Description
    _SDIO_DISABLE SD I/O Clock is disabled.
    _SDIO_ENABLE SD I/O Clock is enabled.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_SetSDIOReadWaitMode

Prototype

void SDIO_SetSDIOReadWaitMode(char readWaitMode);

Description

Sets one of the two options of inserting read wait interval.

Parameters
  • readWaitMode: SD I/O Read Wait operation mode. Valid values :
    Value Description
    _SDIO_READ_WAIT_MODE_CLK Read Wait control by stopping SDIOCLK.
    _SDIO_READ_WAIT_MODE_DATA2 Read Wait control using SDIO_DATA2.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_SetSDIOOperation

Prototype

void SDIO_SetSDIOOperation(char newState);

Description

Enables or disables the SD I/O Mode Operation.

Parameters
  • NewState: new state of SD I/O specific operation. Valid values :
    Value Description
    _SDIO_DISABLE Disable SD I/O operation.
    _SDIO_ENABLE Enable SD I/O operation.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_SendSDIOSuspendCmd

Prototype

void SDIO_SendSDIOSuspendCmd(char newState);

Description

Enables or disables the SD I/O Mode suspend command sending.

Parameters
  • NewState: new state of the SD I/O Mode suspend command. Valid values :
    Value Description
    _SDIO_DISABLE Disable SD I/O command sending.
    _SDIO_ENABLE Enable SD I/O command sending.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_CommandCompletionCmd

Prototype

void SDIO_CommandCompletionCmd(char newState);

Description

Enables or disables the command completion signal.

Parameters
  • NewState: new state of command completion signal. Valid values :
    Value Description
    _SDIO_DISABLE Disable SD I/O command completion signal.
    _SDIO_ENABLE Enable SD I/O command completion signal.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_CEATAITCmd

Prototype

void SDIO_CEATAITCmd(char newState);

Description

Enables or disables the CE-ATA interrupt.

Parameters
  • NewState: new state of CE-ATA interrupt. Valid values :
    Value Description
    _SDIO_DISABLE Disable SD I/O CE-ATA interrupt.
    _SDIO_ENABLE Enable SD I/O CE-ATA interrupt.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_SendCEATACmd

Prototype

void SDIO_SendCEATACmd(char newState);

Description

Enables or disables CE-ATA (CMD61) command sending.

Parameters
  • NewState: new state of CE-ATA command. Valid values :
    Value Description
    _SDIO_DISABLE Disable CE-ATA (CMD61) command sending.
    _SDIO_ENABLE Enable CE-ATA (CMD61) command sending.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_DMACmd

Prototype

void SDIO_DMACmd(char newState);

Description

Enables or disables the SD I/O DMA request.

Parameters
  • NewState: new state of the selected SD I/O DMA request. Valid values :
    Value Description
    _SDIO_DISABLE Disable SD I/O DMA request.
    _SDIO_ENABLE Enable SD I/O DMA request.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_ITConfig

Prototype

void SDIO_ITConfig(unsigned long it, char newState);

Description

Enables or disables the SD I/O interrupts.

Parameters
  • it: specifies the SD I/O interrupt sources to be enabled or disabled.
    This parameter can be one or a OR-ed combination of the following values:
    Value Description
    _SDIO_IT_CCRCFAIL Command response received (CRC check failed) interrupt.
    _SDIO_IT_DCRCFAIL Data block sent/received (CRC check failed) interrupt.
    _SDIO_IT_CTIMEOUT Command response timeout interrupt.
    _SDIO_IT_DTIMEOUT Data timeout interrupt.
    _SDIO_IT_TXUNDERR Transmit FIFO underrun error interrupt.
    _SDIO_IT_RXOVERR Received FIFO overrun error interrupt.
    _SDIO_IT_CMDREND Command response received (CRC check passed) interrupt.
    _SDIO_IT_CMDSENT Command sent (no response required) interrupt.
    _SDIO_IT_DATAEND Data end (data counter, SDIDCOUNT, is zero) interrupt.
    _SDIO_IT_STBITERR Start bit not detected on all data signals in wide bus mode interrupt.
    _SDIO_IT_DBCKEND Data block sent/received (CRC check passed) interrupt.
    _SDIO_IT_CMDACT Command transfer in progress interrupt.
    _SDIO_IT_TXACT Data transmit in progress interrupt.
    _SDIO_IT_RXACT Data receive in progress interrupt.
    _SDIO_IT_TXFIFOHE Transmit FIFO Half Empty interrupt.
    _SDIO_IT_RXFIFOHF Receive FIFO Half Full interrupt.
    _SDIO_IT_TXFIFOF Transmit FIFO full interrupt.
    _SDIO_IT_RXFIFOF Receive FIFO full interrupt.
    _SDIO_IT_TXFIFOE Transmit FIFO empty interrupt.
    _SDIO_IT_RXFIFOE Receive FIFO empty interrupt.
    _SDIO_IT_TXDAVL Data available in transmit FIFO interrupt.
    _SDIO_IT_RXDAVL Data available in receive FIFO interrupt.
    _SDIO_IT_SDIOIT SD I/O interrupt received interrupt.
    _SDIO_IT_CEATAEND CE-ATA command completion signal received for CMD61 interrupt.
  • NewState: new state of the specified SD I/O interrupts. Valid values :
    Value Description
    _SDIO_DISABLE Disable SD I/O interrupts.
    _SDIO_ENABLE Enable SD I/O interrupts.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_GetFlagStatus

Prototype

char SDIO_GetFlagStatus(unsigned long flag);

Description

Checks whether the specified SD I/O flag is set or not.

Parameters
  • it: specifies the flag to check. This parameter can be one of the following values :
    Value Description
    _SDIO_IT_CCRCFAIL Command response received (CRC check failed).
    _SDIO_IT_DCRCFAIL Data block sent/received (CRC check failed).
    _SDIO_IT_CTIMEOUT Command response timeout.
    _SDIO_IT_DTIMEOUT Data timeout.
    _SDIO_IT_TXUNDERR Transmit FIFO underrun error.
    _SDIO_IT_RXOVERR Received FIFO overrun error.
    _SDIO_IT_CMDREND Command response received (CRC check passed).
    _SDIO_IT_CMDSENT Command sent (no response required).
    _SDIO_IT_DATAEND Data end (data counter, SDIDCOUNT, is zero).
    _SDIO_IT_STBITERR Start bit not detected on all data signals in wide bus mode.
    _SDIO_IT_DBCKEND Data block sent/received (CRC check passed).
    _SDIO_IT_CMDACT Command transfer in progress.
    _SDIO_IT_TXACT Data transmit in progress.
    _SDIO_IT_RXACT Data receive in progress.
    _SDIO_IT_TXFIFOHE Transmit FIFO Half Empty.
    _SDIO_IT_RXFIFOHF Receive FIFO Half Full.
    _SDIO_IT_TXFIFOF Transmit FIFO full.
    _SDIO_IT_RXFIFOF Receive FIFO full.
    _SDIO_IT_TXFIFOE Transmit FIFO empty.
    _SDIO_IT_RXFIFOE Receive FIFO empty.
    _SDIO_IT_TXDAVL Data available in transmit FIFO.
    _SDIO_IT_RXDAVL Data available in receive FIFO.
    _SDIO_IT_SDIOIT SD I/O interrupt received.
    _SDIO_IT_CEATAEND CE-ATA command completion signal received for CMD61.
Returns

The new state of SDIO_FLAG.

Requires

Nothing.

Notes

None.

SDIO_ClearFlag

Prototype

void SDIO_ClearFlag(unsigned long flags);

Description

Clears the SD I/O's pending flags.

Parameters
  • it: specifies the flag to clear. This parameter can be one or a OR-ed combination of the following values :
    Value Description
    _SDIO_IT_CCRCFAIL Command response received (CRC check failed).
    _SDIO_IT_DCRCFAIL Data block sent/received (CRC check failed).
    _SDIO_IT_CTIMEOUT Command response timeout.
    _SDIO_IT_DTIMEOUT Data timeout.
    _SDIO_IT_TXUNDERR Transmit FIFO underrun error.
    _SDIO_IT_RXOVERR Received FIFO overrun error.
    _SDIO_IT_CMDREND Command response received (CRC check passed).
    _SDIO_IT_CMDSENT Command sent (no response required).
    _SDIO_IT_DATAEND Data end (data counter, SDIDCOUNT, is zero).
    _SDIO_IT_STBITERR Start bit not detected on all data signals in wide bus mode.
    _SDIO_IT_DBCKEND Data block sent/received (CRC check passed).
    _SDIO_IT_SDIOIT SD I/O interrupt received.
    _SDIO_IT_CEATAEND CE-ATA command completion signal received for CMD61.
Returns

Nothing.

Requires

Nothing.

Notes

None.

SDIO_GetITStatus

Prototype

char SDIO_GetITStatus(unsigned long i);

Description

Checks whether the specified SD I/O interrupt has occurred or not.

Parameters
  • it: specifies the SD I/O interrupt source to check. This parameter can be one of the following values :
    Value Description
    _SDIO_IT_CCRCFAIL Command response received (CRC check failed) interrupt.
    _SDIO_IT_DCRCFAIL Data block sent/received (CRC check failed) interrupt.
    _SDIO_IT_CTIMEOUT Command response timeout interrupt.
    _SDIO_IT_DTIMEOUT Data timeout interrupt.
    _SDIO_IT_TXUNDERR Transmit FIFO underrun error interrupt.
    _SDIO_IT_RXOVERR Received FIFO overrun error interrupt.
    _SDIO_IT_CMDREND Command response received (CRC check passed) interrupt.
    _SDIO_IT_CMDSENT Command sent (no response required) interrupt.
    _SDIO_IT_DATAEND Data end (data counter, SDIDCOUNT, is zero) interrupt.
    _SDIO_IT_STBITERR Start bit not detected on all data signals in wide bus mode interrupt.
    _SDIO_IT_DBCKEND Data block sent/received (CRC check passed) interrupt.
    _SDIO_IT_CMDACT Command transfer in progress interrupt.
    _SDIO_IT_TXACT Data transmit in progress interrupt.
    _SDIO_IT_RXACT Data receive in progress interrupt.
    _SDIO_IT_TXFIFOHE Transmit FIFO Half Empty interrupt.
    _SDIO_IT_RXFIFOHF Receive FIFO Half Full interrupt.
    _SDIO_IT_TXFIFOF Transmit FIFO full interrupt.
    _SDIO_IT_RXFIFOF Receive FIFO full interrupt.
    _SDIO_IT_TXFIFOE Transmit FIFO empty interrupt.
    _SDIO_IT_RXFIFOE Receive FIFO empty interrupt.
    _SDIO_IT_TXDAVL Data available in transmit FIFO interrupt.
    _SDIO_IT_RXDAVL Data available in receive FIFO interrupt.
    _SDIO_IT_SDIOIT SD I/O interrupt received interrupt.
    _SDIO_IT_CEATAEND CE-ATA command completion signal received for CMD61 interrupt.
Returns

The new state of SDIO_IT.

Requires

Nothing.

Notes

None.

SDIO_ClearITPendingBit

Prototype

void SDIO_ClearITPendingBit(unsigned long it);

Description

Clears the SD I/O's interrupt pending bits.

Parameters
  • it: specifies the interrupt pending bit to clear. This parameter can be one or a OR-ed combination of the following values :
    Value Description
    _SDIO_IT_CCRCFAIL Command response received (CRC check failed) interrupt.
    _SDIO_IT_DCRCFAIL Data block sent/received (CRC check failed) interrupt.
    _SDIO_IT_CTIMEOUT Command response timeout interrupt.
    _SDIO_IT_DTIMEOUT Data timeout interrupt.
    _SDIO_IT_TXUNDERR Transmit FIFO underrun error interrupt.
    _SDIO_IT_RXOVERR Received FIFO overrun error interrupt.
    _SDIO_IT_CMDREND Command response received (CRC check passed) interrupt.
    _SDIO_IT_CMDSENT Command sent (no response required) interrupt.
    _SDIO_IT_DATAEND Data end (data counter, SDIDCOUNT, is zero) interrupt.
    _SDIO_IT_STBITERR Start bit not detected on all data signals in wide bus mode interrupt.
    _SDIO_IT_DBCKEND Data block sent/received (CRC check passed) interrupt.
    _SDIO_IT_SDIOIT SD I/O interrupt received interrupt.
    _SDIO_IT_CEATAEND CE-ATA command completion signal received for CMD61 interrupt.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code