SD卡手动挂载ELM文件系统出错

发布于 2021-04-06 16:39:09

自己写了个简单的sd卡驱动 显示注册成功 也能正常读到容量什么的 然后想着挂载上文件系统 dfs和elm初始化都通过了 但是挂载的时候显示format error然后又打印了一个illegal stdio input 这个时候控制台不能输入了
这种可能是自己的sd驱动有问题 还是我挂载的方式有问题?
sd注册初始化.jpg
11b3b7d39f0ea50c504b279c970e0ab9.jpg

struct rt_device my_sdcard;

rt_err_t rt_sdcard_init(rt_device_t dev)
{
    /* DMA controller clock enable */
  __HAL_RCC_DMA2_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA2_Channel4_5_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA2_Channel4_5_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn);
    
  hsd.Instance                                     = SDIO;
  hsd.Init.ClockEdge                         = SDIO_CLOCK_EDGE_RISING;
  hsd.Init.ClockBypass                     = SDIO_CLOCK_BYPASS_DISABLE;
  hsd.Init.ClockPowerSave             = SDIO_CLOCK_POWER_SAVE_DISABLE;
  hsd.Init.BusWide                             = SDIO_BUS_WIDE_1B;
  hsd.Init.HardwareFlowControl     = SDIO_HARDWARE_FLOW_CONTROL_ENABLE;
  hsd.Init.ClockDiv                         = SDIO_TRANSFER_CLK_DIV;
    
  if (HAL_SD_Init(&hsd) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK)
  {
    Error_Handler();
  }        
    
    return RT_EOK;
}

rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
{
    return rt_sdcard_init(dev);
}

rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t sector, void *buffer,  rt_size_t size)
{
    rt_base_t level = 0, status = 0, timeout = 0xff;
    level = rt_hw_interrupt_disable();
    
    status = HAL_SD_ReadBlocks_DMA(&hsd, buffer, sector, size);

    while(HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER)
    {
        if(timeout-- == 0)
        {
            status = RT_ERROR;
            
            rt_hw_interrupt_enable(level);
            return status;
        }
    }
    
    rt_hw_interrupt_enable(level);
    
    return RT_EOK;
}

rt_size_t rt_sdcard_write(rt_device_t dev, rt_off_t sector, const void *buffer,  rt_size_t size)
{
    rt_base_t level = 0, status = 0, timeout = 0xff;
    level = rt_hw_interrupt_disable();

    status = HAL_SD_WriteBlocks_IT(&hsd, (uint8_t *)buffer, sector, size);

    while(HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER)
    {
        if(timeout-- == 0)
        {
            status = RT_ERROR;
            
            rt_hw_interrupt_enable(level);
            return status;
        }
    }
    
    rt_hw_interrupt_enable(level);    
    return RT_EOK;
}

void rt_hw_sdcard_init(void)
{
    my_sdcard.type = RT_Device_Class_Block;
    
    my_sdcard.init             = rt_sdcard_init;
    my_sdcard.open             = rt_sdcard_open;
    my_sdcard.read             = rt_sdcard_read;
    my_sdcard.write         = rt_sdcard_write;

    
    rt_device_register(&my_sdcard, "sd0",
                                         RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE | RT_DEVICE_FLAG_DMA_RX);
    
    rt_device_open(&my_sdcard, RT_DEVICE_FLAG_RDWR);
}
INIT_APP_EXPORT(rt_hw_sdcard_init);

查看更多

关注者
0
被浏览
103
2 个回答
mii
mii 2021-04-06

在命令行输入执行过mkfs -t elm sd0 没有?
再对比一下与应用文档的差别:https://www.rt-thread.org/document/site/application-note/components/dfs/an0012-dfs/#_7

flashman2002
flashman2002 2021-04-06

单步调试过没有?卡在哪步了?你初始化过文件系统了吗?

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览