79 sdHandle.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
80 sdHandle.Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
81 sdHandle.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
82 sdHandle.Init.BusWide = SDMMC_BUS_WIDE_1B;
83 sdHandle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE;
89 if (HAL_OK != HAL_SD_Init(&
sdHandle))
97 HAL_SD_ConfigWideBusOperation(&
sdHandle, SDMMC_BUS_WIDE_4B))
107 HAL_NVIC_EnableIRQ(SDMMC1_IRQn);
120 HAL_NVIC_DisableIRQ(SDMMC1_IRQn);
121 HAL_NVIC_DisableIRQ(DMA2_Channel5_IRQn);
129 return (HAL_OK == HAL_SD_GetCardInfo(&
sdHandle, cardInfo));
134 return (HAL_SD_CARD_TRANSFER == HAL_SD_GetCardState(&
sdHandle));
138 const uint32_t blockAddress,
139 const uint32_t numOfBlocks,
140 const uint32_t timeout)
142 return (HAL_OK == HAL_SD_ReadBlocks(&
sdHandle, data, blockAddress,
143 numOfBlocks, timeout));
147 const uint32_t blockAddress,
148 const uint32_t numOfBlocks,
149 const uint32_t timeout)
168 return (HAL_OK == HAL_SD_WriteBlocks(&
sdHandle, (uint8_t*)data,
169 blockAddress, numOfBlocks, timeout));
173 const uint32_t blockAddress,
174 const uint32_t numOfBlocks)
184 result = (HAL_OK == HAL_SD_ReadBlocks_DMA(&
sdHandle, data, blockAddress,
192 const uint32_t blockAddress,
193 const uint32_t numOfBlocks)
221 result = (HAL_OK == HAL_SD_WriteBlocks_DMA(&
sdHandle, (uint8_t*)data,
222 blockAddress, numOfBlocks));
230 static DMA_HandleTypeDef dmaRx = {0};
241 dmaRx.Instance = DMA2_Channel5;
242 dmaRx.Init.Request = DMA_REQUEST_7;
243 dmaRx.Init.Direction = DMA_PERIPH_TO_MEMORY;
244 dmaRx.Init.PeriphInc = DMA_PINC_DISABLE;
245 dmaRx.Init.MemInc = DMA_MINC_ENABLE;
246 dmaRx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
247 dmaRx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
248 dmaRx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
251 __HAL_LINKDMA(&
sdHandle, hdmarx, dmaRx);
254 (void)HAL_DMA_Abort(&dmaRx);
257 (void)HAL_DMA_DeInit(&dmaRx);
260 const bool result = (HAL_OK == HAL_DMA_Init(&dmaRx));
265 HAL_NVIC_EnableIRQ(DMA2_Channel5_IRQn);
273 static DMA_HandleTypeDef dmaTx = {0};
284 dmaTx.Instance = DMA2_Channel5;
285 dmaTx.Init.Request = DMA_REQUEST_7;
286 dmaTx.Init.Direction = DMA_MEMORY_TO_PERIPH;
287 dmaTx.Init.PeriphInc = DMA_PINC_DISABLE;
288 dmaTx.Init.MemInc = DMA_MINC_ENABLE;
289 dmaTx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
290 dmaTx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
291 dmaTx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
294 __HAL_LINKDMA(&
sdHandle, hdmatx, dmaTx);
297 (void)HAL_DMA_Abort(&dmaTx);
300 (void)HAL_DMA_DeInit(&dmaTx);
303 const bool result = (HAL_OK == HAL_DMA_Init(&dmaTx));
308 HAL_NVIC_EnableIRQ(DMA2_Channel5_IRQn);
326 sdHandle.State = HAL_SD_STATE_RESET;
362 if ((
sdHandle.Context == (SD_CONTEXT_DMA | SD_CONTEXT_READ_SINGLE_BLOCK)) ||
363 (
sdHandle.Context == (SD_CONTEXT_DMA | SD_CONTEXT_READ_MULTIPLE_BLOCK)))
365 HAL_DMA_IRQHandler(
sdHandle.hdmarx);
368 (SD_CONTEXT_DMA | SD_CONTEXT_WRITE_SINGLE_BLOCK)) ||
370 (SD_CONTEXT_DMA | SD_CONTEXT_WRITE_MULTIPLE_BLOCK)))
372 HAL_DMA_IRQHandler(
sdHandle.hdmatx);
This file contains the board-specific GPIO pin definitions, peripheral configurations,...
This file contains the error handling function prototypes.
void ErrorHandler(void)
This function is executed in case of error occurrence.
#define SDMMC_INIT_MAX_TRIES
#define SDMMC_DMA_IRQ_PRIORITY
#define SDMMC_IRQ_PRIORITY
void RccDisableDma2(void)
Disable the DMA2 peripheral clock.
void RccEnableSdmmc1(void)
Enable the SDMMC1 peripheral clock.
void RccEnableDma2(void)
Enable the DMA2 peripheral clock.
void RccDisableSdmmc1(void)
Disable the SDMMC1 peripheral clock.
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
SD card receive complete callback.
static bool SdmmcConfigureDmaTx(void)
Configure the DMA channel for SD card transmit transfers.
static void SdmmcResetPeripheralHandleState(void)
Reset the SD peripheral handle state and card information.
bool SdmmcInit(void)
Initialize the SDMMC peripheral and the SD card.
bool SdmmcGetCardInfo(SdCardInfo *const cardInfo)
Get the SD card information (capacity, block size, etc.).
void SdmmcRegisterRxCompleteCallback(Callback callback)
Register a callback for the DMA receive complete event.
static Callback txCompleteCallback
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
SD card abort callback.
void DMA2_Channel5_IRQHandler(void)
DMA2 Channel 5 interrupt handler for SDMMC DMA transfers.
bool SdmmcWriteBlocks(const uint8_t *const data, const uint32_t blockAddress, const uint32_t numOfBlocks, const uint32_t timeout)
Write blocks to the SD card in blocking mode.
bool SdmmcReadBlocks(uint8_t *const data, const uint32_t blockAddress, const uint32_t numOfBlocks, const uint32_t timeout)
Read blocks from the SD card in blocking mode.
static bool SdmmcConfigureDmaRx(void)
Configure the DMA channel for SD card receive transfers.
bool SdmmcReadBlocksDma(uint8_t *const data, const uint32_t blockAddress, const uint32_t numOfBlocks)
Read blocks from the SD card using DMA.
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
SD card transmit complete callback.
static SD_HandleTypeDef sdHandle
void SdmmcDeInit(void)
De-initialize the SDMMC peripheral.
void SDMMC1_IRQHandler(void)
SDMMC1 interrupt handler.
void SdmmcRegisterTxCompleteCallback(Callback callback)
Register a callback for the DMA transmit complete event.
static Callback rxCompleteCallback
bool SdmmcWriteBlocksDma(const uint8_t *const data, const uint32_t blockAddress, const uint32_t numOfBlocks)
Write blocks to the SD card using DMA.
HAL_SD_CardInfoTypeDef SdCardInfo
bool SdmmcIsCardInTransferState(void)
Check if the SD card is in the transfer state.
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
SD card error callback.
This file includes the microcontroller-specific HAL header files.
This file contains the RCC (Reset and Clock Control) driver function prototypes for enabling and disa...
This file contains the SDMMC peripheral driver function prototypes for SD card initialization,...