发新帖我要提问
12
返回列表
[应用相关]

如何使用CubeMx制作一个基于SD卡的文件系统工程(转载)

[复制链接]
楼主: 观海
手机看帖
扫描二维码
随时随地手机跟帖
观海|  楼主 | 2019-6-17 12:12 | 显示全部楼层
文件测试
相比之前的代码,文件测试我们将其改在按键回调函数中进行:


void FileTest(void)
{
    FRESULT res;
    uint32_t byteswritten, bytesread;                     /* File write/read counts */
    uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
    uint8_t rtext[100];


    if(f_open(&MyFile, "0:/STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
    {
        /* 'STM32.TXT' file Open for write Error */
        Error_Handler();
    }
  else
  {
            res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
            if((byteswritten == 0) || (res != FR_OK))
            {
                /* 'STM32.TXT' file Write or EOF Error */
                Error_Handler();
            }
            else
            {
                f_close(&MyFile);


                /*##-7- Open the text file object with read access ###############*/
                if(f_open(&MyFile, "0:/STM32.TXT", FA_READ) != FR_OK)
                {
                    /* 'STM32.TXT' file Open for read Error */
                    Error_Handler();
                }
                else
                {
                    res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);


                    if((bytesread == 0) || (res != FR_OK))
                    {
                        /* 'STM32.TXT' file Read or EOF Error */
                        Error_Handler();
                    }
                    else
                    {
                        f_close(&MyFile);


                        /*##-10- Compare read data with the expected data ############*/
                        if((bytesread != byteswritten))
                        {
                            /* Read data is different from the expected data */
                            Error_Handler();
                        }
                        else
                        {
                            /* Success of the demo: no error occurrence */
                            HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
                        }
                    }
                }
            }
  }
}



使用特权

评论回复
观海|  楼主 | 2019-6-17 12:13 | 显示全部楼层
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if(GPIO_Pin ==GPIO_PIN_15)
    {
        if(BSP_SD_IsDetected() ==SD_PRESENT)
        {
            FileTest();
        }
    }
}


使用特权

评论回复
观海|  楼主 | 2019-6-17 12:13 | 显示全部楼层
2 SDIO使用DMA来操作

要增加DMA,首先得在CubeMx中为SDIO分别为Tx和RX增加DMA:

SouthEast.jpg

使用特权

评论回复
观海|  楼主 | 2019-6-17 12:13 | 显示全部楼层

其次需要注意地是,SDIO的几个中断优先级设置:

SouthEast.jpg
这里SDIO global inerrupt的中断优先级不能比DMA的优先级高,否则会出现DMA回调死等的情况。

使用特权

评论回复
观海|  楼主 | 2019-6-17 12:14 | 显示全部楼层
最后就是对接了:
在sd_diskio.c文件中,找到SD_read函数,将BSP_SD_ReadBlocks函数改为BSP_SD_ReadBlocks_DMA函数:

DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
{
  DRESULT res = RES_OK;

  if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff,
                       (uint64_t) (sector * BLOCK_SIZE),
                       BLOCK_SIZE,
                       count) != MSD_OK)
  {
    res = RES_ERROR;
  }

  return res;
}


使用特权

评论回复
观海|  楼主 | 2019-6-17 12:14 | 显示全部楼层
同样,写函数也相应地修改,使用带DMA函数:

DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
{
  DRESULT res = RES_OK;

  if(BSP_SD_WriteBlocks_DMA((uint32_t*)buff,
                        (uint64_t)(sector * BLOCK_SIZE),
                        BLOCK_SIZE, count) != MSD_OK)
  {
    res = RES_ERROR;
  }

  return res;
}

需要注意地是,sd_diskio.c这个文件每次cubeMx生成代码时都会重新覆盖它,默认是使用非DMA方式的。

使用特权

评论回复
观海|  楼主 | 2019-6-17 12:14 | 显示全部楼层
3 结论:

这里的关键是那个中断优先级,SDIO global中断(4)必须比DMA中断优先级高(5),否则程序会卡在DMA中断里。


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则