F427 SDIO USB_MSC下无法完成格式化

2019-07-20 12:31发布

USB插上电脑以后,读和写都是正常的,但是无法完成格式化.不知道是何缘故.
由于代码量过去庞大,不知道需要将哪部分贴出来供大侠参考,因此只贴出部分我觉得可能和读写有关的代码
[mw_shl_code=cpp,true]int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
if (BSP_SD_ReadBlocks((uint32_t *)buf, blk_addr, blk_len, SD_DATATIMEOUT) == MSD_OK)
return (USBD_OK);
return USBD_FAIL;
}

int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
if (BSP_SD_WriteBlocks((uint32_t *)buf, blk_addr, blk_len, SD_DATATIMEOUT) == MSD_OK)
return (USBD_OK);
return USBD_FAIL;
}
[/mw_shl_code]
这里是SD卡驱动的实现部分
[mw_shl_code=cpp,true]#include "sd_driver.h"

BSP_SD_CardInfo cardInfo;
SD_HandleTypeDef cardHandle;

MSD_Status BSP_SD_Init(void)
{
cardHandle.Instance = SDIO;
cardHandle.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
cardHandle.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
cardHandle.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
cardHandle.Init.BusWide = SDIO_BUS_WIDE_1B;
cardHandle.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
cardHandle.Init.ClockDiv = 0;
HAL_StatusTypeDef sd_err = HAL_SD_Init(&cardHandle);
if (sd_err != HAL_OK)
return MSD_ERR;
sd_err = HAL_SD_ConfigWideBusOperation(&cardHandle, SDIO_BUS_WIDE_4B);
if (sd_err != HAL_OK)
return MSD_ERR;
return MSD_OK;
}

void HAL_SD_MspInit(SD_HandleTypeDef* hsd)
{

GPIO_InitTypeDef GPIO_InitStruct;
if (hsd->Instance == SDIO)
{
__HAL_RCC_SDIO_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11
| GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
__HAL_RCC_GPIOC_CLK_ENABLE();
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_2;
__HAL_RCC_GPIOD_CLK_ENABLE();
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}

}

void HAL_SD_MspDeInit(SD_HandleTypeDef* hsd)
{
if (hsd->Instance == SDIO)
{
__HAL_RCC_SDIO_CLK_DISABLE();
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12);
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
__HAL_RCC_GPIOC_CLK_DISABLE();
__HAL_RCC_GPIOD_CLK_DISABLE();
}
}

MSD_Status BSP_SD_GetCardState(void)
{
return (HAL_SD_GetCardState(&cardHandle) == HAL_SD_CARD_TRANSFER) ? MSD_OK : MSD_ERR;
}

void BSP_SD_GetCardInfo(BSP_SD_CardInfo *Info)
{
HAL_SD_GetCardInfo(&cardHandle, Info);
}

MSD_Status BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
if (HAL_SD_ReadBlocks(&cardHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) != HAL_OK)
return MSD_ERR;
return MSD_OK;
}

MSD_Status BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
if (HAL_SD_WriteBlocks(&cardHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) != HAL_OK)
return MSD_ERR;
return MSD_OK;
}[/mw_shl_code]
希望大家赐教
0条回答

一周热门 更多>