This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

求助,C6748 SD卡读写例程.



开发平台:LCDKC6748

IDE:CCS v7.4

第一步:

在开发SD卡文件读写过程中,在论坛看到两个相关帖子:

https://e2echina.ti.com/question_answer/dsp_arm/omap_l1x/f/54/p/177479/534427?tisearch=e2e-sitesearch

https://e2echina.ti.com/question_answer/dsp_arm/c6000_dsp/f/32/t/177478

这两个帖子的回复,都指向https://e2e.ti.com/support/processors/f/791/t/283373

我下载压缩包“2045.starterware_c6748_mmcsd.zip”,新建工程,编译并调试,实现了成像功能,可通过串口终端访问LCDKC6748上的TF卡。

  • 第二步:

    我在该工程中加入写文件的相关操作,发现无法写入!
    然后我单独测试了底层的SD驱动函数,disk_read()和disk_write()。
    发现disk_read()函数没有问题;但是disk_write()有问题。
    情况1:disk_write()单次写单个扇区时,没问题,返回RES_OK;
    情况2:disk_write()单次写多个扇区时,返回都是RES_ERROR。

    对情况2进行分析,发现虽然返回的是RES_ERROR,但是已经将数据正确写入了相应扇区。

    测试函数如下:

    #define     RING_BUF_SIZE   1024
    
    unsigned char test_Buf[RING_BUF_SIZE];
    /*填充测试扇区*/
    static void fill_buf(unsigned char *buf)
    {
        unsigned int i;
        for(i=0; i<RING_BUF_SIZE/2; i++) {
            *buf++ = 'D';
            *buf++ = 'g';
        }
    }
    /*对TF卡进行扇区写操作*/
    void SD_write_sector_test(void)
    {
        DRESULT res;
        UINT i;
        fill_buf(test_Buf);
        for(i=0;i<3;i++) {
            res = disk_write(0, test_Buf, 0x50 + i, 1);//写一个扇区,结果正确,并返回RES_OK
        }
        for(i=0;i<3;i++) {
            res = disk_write(0, test_Buf, 0x53 + i*2, 2);//写两个扇区,结果正确,但返回RES_ERROR
        }
    }
    /*对TF卡进行扇区读操作*/
    void SD_read_sector_test(void)
    {
        DRESULT res;
        fill_buf(test_Buf);
        res = disk_read(0, test_Buf, 0xA0000/512, 1);//读取一个扇区,结果正确,并返回RES_OK
        res = disk_read(0, test_Buf, 0xA0200/512, 2);//读取两个扇区,结果正确,并返回RES_OK
    }
    /*在TF卡创建test.txt并写入一些数据*/
    void SD_write_speed_test(void)
    {
        FRESULT res;
        UINT i;
        WORD bytes_writed;
        FIL rec_file_chlm;
    
        res = f_open(&rec_file_chlm, "test.txt", FA_CREATE_ALWAYS | FA_WRITE);
    
        if (FR_OK != res) {
           return;
        }
        fill_buf(test_Buf);
        i = 100;
        while(i--) {
            res = f_write(&rec_file_chlm, test_Buf, RING_BUF_SIZE, &bytes_writed); //返回FR_RW_ERROR
            f_sync(&rec_file_chlm);
        }
        f_close(&rec_file_chlm);
    }
    

    main函数for(;;)中增加对3个测试函数的调用如下():

     for(;;)
        {
            if((MMCSDCardPresent(&ctrlInfo)) == 1)
            {
    #if DEBUG_PRINT
                UARTPuts("Card is present\r\n", -1);
    #endif
                if(initFlg)
                {
                    UARTPuts("Call MMCSDFsMount\r\n", -1);
                    MMCSDFsMount(0, &sdCard);
                    UARTPuts("Back MMCSDFsMount\r\n", -1);
                    initFlg = 0;
                    UARTPuts("Call Cmd_help\r\n", -1);
                    Cmd_help(0, NULL);
                    SD_write_speed_test();// xxx 测试SD写入速度,不可屏蔽,确保SD卡被初始化。
                    SD_read_sector_test();// XXX  测试SD读扇区。
                    SD_write_sector_test();// xxx 测试SD写扇区。
                }
                MMCSDFsProcessCmdLine();
            }
            else
            {
    UARTPuts("Card is not present\r\n", -1);
    delay(1000);
    //          delay(1);
    
                i = (i + 1) & 0xFFF;
    
                if(i == 1)
                {
                     UARTPuts("Please insert the card \r\n", -1);
                }
    
                if(initFlg != 1)
                {
                     /* Reinitialize all the state variables */
                     dmaIsRunning = 0;
                     xferCompFlag = 0;
                     dataTimeout  = 0;
                     dataCRCError = 0;
                     cmdCompFlag  = 0;
                     cmdTimeout   = 0;
    
                     /* Initialize the MMCSD controller */
                     MMCSDCtrlInit(&ctrlInfo);
    
                     MMCSDIntEnable(&ctrlInfo);
                }
    
                initFlg = 1;
            }
        }
    
    

  • TI有没有比较完整并可用的SD卡初始化和扇区读写例程?

  • 为更加有效地解决您的问题,我们建议您将问题发布在E2E英文技术论坛上https://e2e.ti.com/support/processors/f/791,将由资深的工程师为您提供帮助。我们的E2E英文社区有TI专家进行回复,并得到全球各地工程师的支持,分享他们的知识和经验。