PYNQ读写SD卡

使用了这个博客的代码,效果不错:https://blog.csdn.net/love_ljq/article/details/79117738

需要添加xilffs的支持包,方法如下:

  1. 右击BSP工程包,选择BSP settings;
    PYNQ读写SD卡_第1张图片
    2.勾选xilffs,电机OK。
    PYNQ读写SD卡_第2张图片
    代码如下:
#include "platform.h"
#include "xparameters.h"
#include "xil_printf.h"
#include "ff.h"
#include "xdevcfg.h"
#include 

static FATFS fatfs;

int SD_Init()
{
    FRESULT rc;
    rc = f_mount(&fatfs,"",0);
    if(rc)
    {
        xil_printf("ERROR: f_mount returned %d\r\n",rc);
        return XST_FAILURE;
    }
    return XST_SUCCESS;
}

int SD_Transfer_read(char *FileName,u32 DestinationAddress,u32 ByteLength)
{
    FIL fil;
    FRESULT rc;
    UINT br;

    rc=f_open(&fil,FileName,FA_READ);
    if(rc)
    {
        xil_printf("ERROR:f_open returned %d\r\n",rc);
        return XST_FAILURE;
    }
    rc = f_lseek(&fil,0);
    if(rc)
    {
        xil_printf("ERROR:f_open returned %d\r\n",rc);
        return XST_FAILURE;
    }
    rc = f_read(&fil,(void*)DestinationAddress,ByteLength,&br);
    if(rc)
    {
        xil_printf("ERROR:f_open returned %d\r\n",rc);
        return XST_FAILURE;
    }
    rc = f_close(&fil);
    if(rc)
    {
        xil_printf("ERROR:f_open returned %d\r\n",rc);
        return XST_FAILURE;
    }
    return XST_SUCCESS;
}

int SD_Transfer_write(char *FileName,u32 SourceAddress,u32 ByteLength)
{
    FIL fil;
    FRESULT rc;
    UINT bw;

    rc = f_open(&fil,FileName,FA_CREATE_ALWAYS | FA_WRITE);
    if(rc)
    {
        xil_printf("ERROR : f_open returned %d\r\n",rc);
        return XST_FAILURE;
    }
    rc = f_lseek(&fil, 0);
    if(rc)
    {
        xil_printf("ERROR : f_lseek returned %d\r\n",rc);
        return XST_FAILURE;
    }
    rc = f_write(&fil,(void*) SourceAddress,ByteLength,&bw);
    if(rc)
    {
        xil_printf("ERROR : f_write returned %d\r\n", rc);
        return XST_FAILURE;
    }
    rc = f_close(&fil);
    if(rc){
        xil_printf("ERROR : f_close returned %d\r\n",rc);
        return XST_FAILURE;
    }
    return XST_SUCCESS;
}

#define FILE "test.txt"

int main()
{
    init_platform();
    int rc;

    char src_str[30] = {0};
    sprintf(src_str,"today is %d.%d.%d\r\n%.2f",2019,11,6,3.332);
    u32 len = strlen(src_str);

    rc = SD_Init();
    if(XST_SUCCESS != rc)
    {
        xil_printf("fail to open SD Card~\r\n");
    }
    else
    {
        xil_printf("success to open SD Card~\r\n");
    }

    rc = SD_Transfer_write(FILE,(u32)src_str,len/*(len/256+1)*256*/);
    xil_printf("%d",(len/256+1)*256);
    if(XST_SUCCESS != rc)
    {
        xil_printf("fail to write SD Card~\r\n");
    }
    else
    {
        xil_printf("success to write SD Card~\r\n");
    }
    char dest_str[33];
    rc = SD_Transfer_read(FILE,(u32)dest_str,(len+1));
    if(XST_SUCCESS != rc)
    {
        xil_printf("fail to read SD Card~\r\n");
    }
    else
    {
        xil_printf("success to read SD Card~\r\n");
    }
    xil_printf("%s\r\n",dest_str);
    printf("SD Card Write and Read Success~");
    cleanup_platform();
    return 0;
  }

效果如下:

PYNQ读写SD卡_第3张图片
同时我们可以使用 f_printf() 函数,类似 printf() ,在 “ff.h” 里面可以看到该函数:
PYNQ读写SD卡_第4张图片
但是不能直接使用该函数,需要在BSP里面进行设置:

  1. 右击BSP工程包,选择BSP settings;
    PYNQ读写SD卡_第5张图片
    2.将xilffs里面的use_strfunc中的Value从0改为2,这里的意思是将该函数的有效参数设置为2;
    PYNQ读写SD卡_第6张图片
    这样就可以使用 f_printf() 函数了。

你可能感兴趣的:(vivado)