MG32F157 Standard Peripherals Firmware Library
Macros | Functions | Variables
bsp_sdio_sdcard.c File Reference

This file provides all the RCC firmware functions. More...

#include "bsp_sdio_sdcard.h"
#include "mg32f157.h"

Macros

#define NULL   0
 SDIO Static flags, TimeOut, FIFO Address.
 
#define SDIO_CMD0TIMEOUT   ((uint32_t)0x00010000)
 
#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 SDIO_SEND_IF_COND   ((uint32_t)0x00000008)
 Following commands are SD Card Specific commands. SDIO_APP_CMD should be sent before sending these commands.
 

Functions

void SD_DeInit (void)
 
SD_Error SD_Init (void)
 Initializes the SD according to the specified parameters in the SD_HandleTypeDef and create the associated handle. More...
 
SDCardState SD_GetState (void)
 Returns the current card's state. More...
 
SDTransferState SD_GetStatus (void)
 Gets the cuurent sd card data transfer status. More...
 
SD_Error SD_PowerON (void)
 Enquires cards about their operating voltage and configures clock controls and stores SD information that will be needed in future in the SD handle. More...
 
SD_Error SD_PowerOFF (void)
 Disable the SDIO card clock output. More...
 
SD_Error SD_InitializeCards (void)
 Initializes the SD Card. More...
 
SD_Error SD_GetCardInfo (SD_CardInfo *cardinfo)
 
SD_Error SD_GetCardStatus (SD_CardStatus *cardstatus)
 Enables wide bus opeartion for the requeseted card if supported by card. More...
 
SD_Error SD_EnableWideBusOperation (uint32_t WideMode)
 
SD_Error SD_SelectDeselect (uint32_t Addr)
 Send the Select Deselect command and check the response. More...
 
SD_Error SD_ReadBlock (uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize)
 Allows to read one block from a specified address in a card. The Data transfer can be managed by DMA mode or Polling mode. More...
 
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. More...
 
SD_Error SD_WriteBlock (uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize)
 Allows to write one block starting from a specified address in a card. The Data transfer can be managed by DMA mode or Polling mode. More...
 
SD_Error SD_WriteMultiBlocks (uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
 
SD_Error SD_StopTransfer (void)
 Aborts an ongoing data transfer. More...
 
SD_Error SD_Erase (uint32_t startaddr, uint32_t endaddr)
 Allows to erase memory area specified for the given card. More...
 
SD_Error SD_SendSDStatus (uint32_t *psdstatus)
 Returns the current SD card's status. More...
 

Variables

__IO uint32_t StopCondition = 0
 
__IO uint32_t TransferEnd = 0
 
__IO SD_Error TransferError = SD_OK
 
SD_CardInfo SDCardInfo
 
SDIO_InitTypeDef SDIO_InitStructure
 
SDIO_CmdInitTypeDef SDIO_CmdInitStructure
 
SDIO_DataInitTypeDef SDIO_DataInitStructure
 

Detailed Description

This file provides all the RCC firmware functions.

Author
MegawinTech Application Team
Version
V0.0.4
Date
16-June-2023

Function Documentation

◆ SD_DeInit()

void SD_DeInit ( void  )

< Disable SDIO Clock

< Set Power State to OFF

< Disable the SDIO AHB Clock

< Configure PC.08, PC.09, PC.10, PC.11, PC.12 pin: D0, D1, D2, D3, CLK pin

< Configure PD.02 CMD line

◆ SD_EnableWideBusOperation()

SD_Error SD_EnableWideBusOperation ( uint32_t  WideMode)

< MMC Card doesn't support this feature

◆ SD_Erase()

SD_Error SD_Erase ( uint32_t  startaddr,
uint32_t  endaddr 
)

Allows to erase memory area specified for the given card.

Parameters
startaddrthe start address.
endaddrthe end address.
Return values
SD_ErrorSD Card Error code.

< Check if the card coomnd class supports erase command

< Send CMD32 SD_ERASE_GRP_START with argument as addr

◆ SD_GetCardInfo()

SD_Error SD_GetCardInfo ( SD_CardInfo cardinfo)

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

SD_Error SD_GetCardStatus ( SD_CardStatus cardstatus)

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.

< Byte 0

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

◆ 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 according to the specified parameters in the SD_HandleTypeDef and create the associated handle.

Parameters
None
Return values
HALstatus

< Enable the SDIO AHB Clock

< CMD Response TimeOut (wait for CMDSENT flag)

◆ SD_InitializeCards()

SD_Error SD_InitializeCards ( void  )

Initializes the SD Card.

Parameters
None.
Note
This function initializes the SD card. It could be used when a card re-initialization is needed.
Return values
HALstatus

◆ SD_PowerOFF()

SD_Error SD_PowerOFF ( void  )

Disable the SDIO card clock output.

Parameters
None
Return values
errorstate

◆ SD_PowerON()

SD_Error SD_PowerON ( void  )

Enquires cards about their operating voltage and configures clock controls and stores SD information that will be needed in future in the SD handle.

Parameters
None
Return values
errorstate

◆ SD_ReadBlock()

SD_Error SD_ReadBlock ( uint8_t *  readbuff,
uint64_t  ReadAddr,
uint16_t  BlockSize 
)

Allows to read one block 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.
Return values
SD_ErrorSD Card Error code.

< Send CMD17 READ_SINGLE_BLOCK

< Polling mode

◆ 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

< Polling mode

◆ SD_SelectDeselect()

SD_Error SD_SelectDeselect ( uint32_t  Addr)

Send the Select Deselect command and check the response.

Parameters
SDIOxPointer to SDIO register base
addrAddress of the card to be selected
Return values
HALstatus

◆ SD_SendSDStatus()

SD_Error SD_SendSDStatus ( uint32_t *  psdstatus)

Returns the current SD card's status.

Parameters
psdstatuspointer to the buffer that will contain the SD card status (SD Status register).
Return values
SD_ErrorSD Card Error code.

< Send ACMD13 SD_APP_STAUS with argument as card's RCA.

◆ SD_StopTransfer()

SD_Error SD_StopTransfer ( void  )

Aborts an ongoing data transfer.

Parameters
None
Return values
SD_ErrorSD Card Error code.

◆ SD_WriteBlock()

SD_Error SD_WriteBlock ( uint8_t *  writebuff,
uint64_t  WriteAddr,
uint16_t  BlockSize 
)

Allows to write one block starting 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
writebuffpointer to the buffer that contain the data to be transferred.
WriteAddrAddress from where data are to be read.
BlockSizethe SD card Data block size. The Block size should be 512.
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 
)

< To improve performance