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.

CC3220MODA: MAP_SDHostSetExpClk() hangs during SD_Open() call if card is not inserted

Part Number: CC3220MODA


Hi,

I am working on quite complex project, core of this project is FreeRTOS with TI-POSIX wrapper and simplelink SDK. Our application uses SD card and Chan's FATFS for some application specific stuff. Unfortunately, card detect pin from SD slot is not connected to any GPIO, so to see if there is any memory card inserted we periodically try to mount fatfs() to see if SD card is available. In general case what is happening under the hood is that

1) SD_Open() is called.

2) f_mount() is called and from within this function FatFS glue function to initialize the storage device is called. From within this function SD_initialize() is called and based on it's result we decide whether SD card is available or not.

3) If mounting failed (for instance, because card is not available), then SD_Close() is called

4) After mounting failure wait for 60 seconds and go to step 1.

In general this approach works good with SD host driver from TI SDK version 3.something and with fixes that we've introduced previously (this and this). But in updated TI SKD (5.10) we discovered that (except everything else) following lines were added to initHwFunction 

    /* Store current clock configuration */
    clockcfg = HWREG(ARCM_BASE + APPS_RCM_O_MMCHS_CLK_GEN);
    /*
     * Set PLL CLK DIV to 8 in order to get 80KHz clock freuqnecy. The
     * requirement to wake up the card with the initialization stream
     * is 80 clock cycles in 1ms.
     */
    HWREG(ARCM_BASE + APPS_RCM_O_MMCHS_CLK_GEN) |= PLL_CLK_DIV8;
    /* Set clock frequency to 80KHz for initialization stream */
    MAP_SDHostSetExpClk(hwAttrs->baseAddr,
            MAP_PRCMPeripheralClockGet(PRCM_SDHOST), SD_INIT_FREQ_80KHZ);
    /* Enable SD initialization stream */
    HWREG(hwAttrs->baseAddr + MMCHS_O_CON) |= SD_INIT_STREAM;
    /* Dummy command to send out the 80 clock cycles */
    send_cmd(handle, DUMMY, DUMMY);
    /* End SD initialization stream */
    HWREG(hwAttrs->baseAddr + MMCHS_O_CON) &= ~SD_INIT_STREAM;
     /* Reset clock cfg to initial cfg */
    HWREG(ARCM_BASE + APPS_RCM_O_MMCHS_CLK_GEN) = clockcfg;

    /*
     * Configure the card clock to 400KHz for the card initialization and
     * idenitification process. This is done to ensure compatibility with
     * older SD cards. Once this is complete, the card clock will be
     * reconfigured to the set operating value.
     */
    MAP_SDHostSetExpClk(hwAttrs->baseAddr,
        MAP_PRCMPeripheralClockGet(PRCM_SDHOST), SD_ID_FREQ_400KHZ);

    MAP_SDHostIntClear(hwAttrs->baseAddr, SDHOST_INT_CC | SDHOST_INT_TC |
        DATAERROR | CMDERROR | SDHOST_INT_DMAWR | SDHOST_INT_DMARD |
        SDHOST_INT_BRR | SDHOST_INT_BWR);

And it works good when card is inserted, but what is happening with this code is that it hangs if we try to call SD_Open() without card in SD slot in following while loop

This is what part of the call stack looks like:

I am not sure why it is happening, but it seems to me like there is some sort of hardware issue within SD host driver silicon module.

Maybe TI experts could clarify what is going on and if we can somehow fix this state? 

  • Hi Vadym,

    That change was made in CC32xx SDK 4.40 in order to increase reliability with older SD cards, documented in the Core SDK release notes. I'm not sure why that would cause SDHostSetExpClk to hang though. Have you made any changes to the peripheral clock?

    Best regards,

    Sarah

  • Not sure, we have minimal necessary changes inside TI SDK. I do not see any changes that would affect peripheral clock. Maybe you can specify what should we look for?

  • Hi Vadym,

    I would have to test the library in the latest SDK to see if the behavior also occurs there. I do not have the hardware available at the moment, so please give me a couple days to debug.

    Best regards,

    Sarah

  • Hi Vadym,

    Unfortunately, I have not been able to debug this further. My next test would be whether the default driver and example sees the same behavior when the SD card is unavailable. I am going to be out of the office for the next week, but this may be good for you to test and compare.

    Best regards,

    Sarah

  • Hi Sarah,

    First of all sorry for late reply - I've had too much things on my plate. But now when they re more or less done I am back into issue and can help you with investigation. I've done some research with TI SDK 5.20 and "fatsdraw" example that comes with it and I can tell that it looks to me like there is still some issue within TI SDK (although might be different to what we discuss in this thread). 

    I compile mentioned example using CCS 10.4.0.00006, GCC and FreeRTOS kernel 10.4.1. Since I do not have launchpad and I run the code on our device with CC3220 MODA module, I had to change system configuration (specifically SDIO pin mapping). I've also changed code of example so it performs manipulations with SD card in the loop, most of the change lies in mainThread code:

    void *mainThread(void *arg0)
    {
        FRESULT fresult;
    
        /* Variables to keep track of the file copy progress */
        unsigned int bytesRead = 0;
    
        SDFatFS_Handle sdfatfsHandle;
    
        /* Call driver init functions */
        GPIO_init();
        Display_init();
        SDFatFS_init();
    
        /* Open the display for output */
        display = Display_open(Display_Type_UART, NULL);
        if (display == NULL) {
            /* Failed to open display driver */
            while (1);
        }
    
        /* Initialize real-time clock */
        clock_settime(CLOCK_REALTIME, &ts);
    
        bool b_error = false;
    
        while (true)
        {
            /* Mount and register the SD Card */
            sdfatfsHandle = SDFatFS_open(CONFIG_SDFatFS_0, DRIVE_NUM);
    
            if (sdfatfsHandle == NULL)
            {
                b_error = true;
                Display_printf(display, 0, 0, "Error starting the SD card\n");
            }
            else
            {
                Display_printf(display, 0, 0, "Drive %u is mounted\n", DRIVE_NUM);
            }
    
            if (!b_error)
            {
                /* Now output the outputfile[] contents onto the console */
                fresult = f_open(&dst, outputfile, FA_READ);
                if (fresult != FR_OK)
                {
                    Display_printf(display, 0, 0, "Error opening \"%s\"\n", outputfile);
                    Display_printf(display, 0, 0, "Aborting...\n");
                    b_error = true;
                }
            }
    
            if (!b_error)
            {
                /* Print file contents */
                while (true)
                {
                    /* Read from output file */
                    fresult = f_read(&dst, cpy_buff, CPY_BUFF_SIZE, &bytesRead);
                    if (fresult || bytesRead == 0)
                    {
                        break; /* Error or EOF */
                    }
                    cpy_buff[bytesRead] = '\0';
                    /* Write output */
                    Display_printf(display, 0, 0, "%s", cpy_buff);
                }
    
                if (fresult != FR_OK)
                {
                    b_error = true;
                }
            }
    
            if (!b_error)
            {
                /* Close the file */
                fresult = f_close(&dst);
    
                if (fresult != FR_OK)
                {
                    b_error = true;
                }
            }
    
            /* Stopping the SDCard */
            SDFatFS_close(sdfatfsHandle);
    
            Display_printf(display, 0, 0, "Drive %u unmounted\n", DRIVE_NUM);
    
            sleep(2);
            b_error = false;
        }
    
    
        return (NULL);
    }

    If I insert SD card and then flash the code into device, it works good. But it stops working after same card is removed and plugged in  - SDFatFS_open() always returns non-null, but f_open will return FR_DISK_ERR. I think this issue is described in this thread https://e2e.ti.com/support/wireless-connectivity/wi-fi-group/wifi/f/wi-fi-forum/973275/cc3220moda-when-sd_initialize-fails-it-goes-to-unrecoverable-state-and-always-returns-error

    Also periodically I observed code stuck inside SDFatFS_open() function when there is no card in slot (which might be issue described in this thread), but I cannot debug issue deeper because this is CCS what shows me when I pause debugging:

    I also have hard time digging deeper into issue because after I step into SDFatFS_open() function I cannot see the code:


    Will continue my experiments

    BR,

  • Hi Sarah,

    I was going to do some more investigation on this topic, but I am stumbled upon fact that CCS most likely uses simplelink sdk from object library, thus I am very limited with deeper investigation. Is there any way I can force my CCS project compile simplelink SDK from sources, so I can explore and use modified sources?

  • Hi Vadym,

    The SDK does not provide CCS projects for the libraries, but you can add the drivers source files (like source/ti/drivers/sd) into your project directly. When the project builds, the compiler will replace the library symbols with the source in your project. This will also work for the debugger.

    Best regards,

    Sarah

  • Hi Sarah. Thanks for your advice - it worked just fine. I am able to reproduce issue discussed in this topic with non-patched SDK.

    The code and overall setup is same as described in my previous message. If SD card is unplugged and code is running, then while() loop executes a couple of iterations and then hangs in this place. Maybe you can try similar setup and similar code on your side using launchpad?

    BTW we have hardware somewhat different to launchpad, SD host is connected to other pins, like this:

  • Hi Vadym,

    Sarah is out of office this week and will get back to you on monday.

  • Hi Vincent

    Are there any updates from TI side?

  • Hi Vadym,

    It looks like the driver is not equipped the handle the issue you are seeing when the SD card is not inserted and there is no hardware check. Ideally, there would be a timeout option in the driver to avoid this situation. I can make a request, but I don't know what that timeline would be.

    In the interim, I suggest adding a timestamp check to that while loop to exit and recover gracefully, or you can rely on the watchdog to reset the device.

    Best regards,

    Sarah