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.

CC3220: Checking for SD card is inserted when not using card detection pin

Part Number: CC3220

I utilized the example fatsd in the CC3220 SDK to interface with a FAT formatted SD card. I did not see an example of determining that the SD card is inserted.

Is there a way to determine that an SD card is inserted while only using the 3 pins provided for SDHost in the TI Pin Mux Tool?

  • Hi Ryan,

    You will need a GPIO to monitor the card detection pin, unless you wanted to poll the sd card interface continuously.

    The physical sd card socket should have a mechanical switch operated by the insertion/removal of the sd card that allows for a simple GPIO interrupt to monitor the state of that pin to see whether a card has been inserted. I advise you to look at your sd card socket documentation, or simply hook up a scope/logic analyzer to see how the SD card detection pin changes state as you insert/remove your sd card.

    If you need help with adding that GPIO or have any other questions, feel free to let me know.

    Regards,
    Michael
  • Hi Michael,

    Thanks for that information! I'm well aware of the SD card detection pin, unfortunately in the project I am working on we are very much pin constrained. I was asking what would be the best means of polling the SD card interface in the case of only having the SD host pins (i.e. no SD card detection GPIO)?

    Looking at the source code in the fatsd example I did not see a means for detecting the SD card is available without the detection pin.

    --Ryan

  • Hi Ryan,

    If you are not using a GPIO to check the host detect pin, then the other main option would be to repeatedly try to mount the SD card using SDFatFS_open() until you get a non-NULL return handle.

    The SDFatFS_open() function not only sets up the fatfs driver but also attempts to mount the sd card. If there is no SD card, then f_mount() should return a FR_NOT_READY==3 result. This is the mounting function used internally by the SDFatFS() driver, but without modification that driver will not give you the underlying error produced. Either you can take any NULL handle returned by SDFatFS_open() as an indication that the SD card is missing, or you can modify SDFatFS_open() in SDFatFS.c so that fresult is returned instead of NULL.

    Regards,
    Michael
  • Thanks Michael,

    I originally assumed that functionality. Unfortunately I do not see that with the SDFatFS_open() function. When running my driver with the following code:

    static int32_t THRD_MountSD(void)
    {
        if (true == gbIsMounted)
        {
            return ERROR_SD_CARD_ALREADY_MOUNTED;
        }
    
        gFatFSHandle = SDFatFS_open(Board_SD_CARD,
                                    FAT_FS_DRIVE_NUM);
        if (NULL == gFatFSHandle)
        {
            return ERROR_SD_CARD_FAILED_TO_MOUNT;
        }
    
        gbIsMounted = true;
    
        return ERROR_OK;
    }

    gFatFSHandle is non-NULL when there is no SD card present in the socket. Why do you think that the driver is not returning NULL when there is no SD card present?

    I am using:

    • TI v18.1.3.LTS Compiler
    • XDCtools v3.50.8.24
    • CC32XX SDK v2.10.00.04
    • Code Composer Studio v8.2.0.00007
    • Service Pack v3.9.0.6_2.0.0.0_2.2.0.6
    • CC3220SF

  • I think I may have found why the function does not return NULL. The call of f_mount() in SDFatFS.c looks like this:

    SDFatFS_Handle SDFatFS_open(uint_least8_t idx, uint_least8_t drive)
    {
        uintptr_t       key;
        DRESULT         dresult;
        FRESULT         fresult;
        TCHAR           path[3];
        SDFatFS_Handle  handle = NULL;
        SDFatFS_Object *obj;
    
        /* Verify driver index and state */
        if (isInitialized && (idx < SDFatFS_count)) {
            /* Get handle for this driver instance */
            handle = (SDFatFS_Handle)&(SDFatFS_config[idx]);
            obj = handle->object;
    
            /* Determine if the device was already opened */
            key = HwiP_disable();
            if (obj->driveNum != DRIVE_NOT_MOUNTED) {
                HwiP_restore(key);
                DebugP_log1("SDFatFS Drive %d already in use!", obj->driveNum);
                handle = NULL;
            }
            else {
                obj->driveNum = drive;
    
                /* Open SD Driver */
                obj->sdHandle = SD_open(idx, NULL);
    
                HwiP_restore(key);
    
                if (obj->sdHandle == NULL) {
                    obj->driveNum = DRIVE_NOT_MOUNTED;
                    /* Error occurred in lower level driver */
                    handle = NULL;
                }
                else {
    
                    /* Register FATFS Functions */
                    dresult = disk_register(obj->driveNum,
                        SDFatFS_diskInitialize,
                        SDFatFS_diskStatus,
                        SDFatFS_diskRead,
                        SDFatFS_diskWrite,
                        SDFatFS_diskIOctrl);
    
                    /* Check for drive errors */
                    if (dresult != RES_OK) {
                        DebugP_log0("SDFatFS: Disk functions not registered");
                        SDFatFS_close(handle);
                        handle = NULL;
                    }
                    else {
    
                        /* Construct base directory path */
                        path[0] = (TCHAR)'0' + obj->driveNum;
                        path[1] = (TCHAR)':';
                        path[2] = (TCHAR)'\0';
    
                        /*
                         * Register the filesystem with FatFs. This operation does
                         * not access the SDCard yet.
                         */
                        fresult = f_mount(&(obj->filesystem), path, 0);
                        if (fresult != FR_OK) {
                            DebugP_log1("SDFatFS: Drive %d not mounted",
                                        obj->driveNum);
    
                            SDFatFS_close(handle);
                            handle = NULL;
                        }
                        else {
    
                            /*
                             * Store the new sdfatfs handle for the input drive
                             * number
                             */
                            sdFatFSHandles[obj->driveNum] = handle;
    
                            DebugP_log0("SDFatFS: opened");
                        }
                    }
                }
            }
        }
    
        return (handle);
    }

    The opt parameter being passed into f_mount() is 0. This specifies to not mount the SD card immediately. Is this the expected outcome?

    --Ryan

  • Hi Ryan,

    Good catch with the SD card driver. It looks like on the CC3220 the driver will not attempt to mount the SD card immediately. In order to get the desired behavior I described in my previous post, you'll need to make some slight tweaks to the SDFatFs driver. Specifically, you'll want to change the code around the f_mount() call in SdFatFS_open() so that it not only attempts to mount the card immediately, but also speculatively saves the current SD card handle to the global sfFatFSHandles object so that the fatfs library has the correct pointers to the hardware peripheral functions needed. Please modify SDFatFs.c like so:

    sdFatFSHandles[obj->driveNum] = handle;
                        fresult = f_mount(&(obj->filesystem), path, 1);
                        if (fresult != FR_OK) {
                            DebugP_log1("SDFatFS: Drive %d not mounted",
                                        obj->driveNum);
    
                            sdFatFSHandles[obj->driveNum] = NULL;
                            SDFatFS_close(handle);
                            handle = NULL;
                        }

    That should get the behavior you want when you poll with SDFatFS_open(). Let me know if that doesn't work or you need more help.

    Regards,

    Michael

  • Michael,

    Will I have to recompile the simplelink library for these changes to take effect?

    --Ryan
  • Hi Ryan,

    You do not need to recompile the simplelink library. This SD card driver change does not impact the SimpleLink Wi-Fi host driver. While there is a separate TI driver library used by the linker that you could rebuild (drivers_cc32xx.aem4), that step is not needed since you can just copy the modified SDFatFS.c into your project workspace and let it be built along with the rest of your code. During the build process, the linker will prioritize just-built objects files over linked libraries, so the modified SDFatFS_open() function from the copied file will be used instead of the version from the drivers_cc32xx.aem4 library.

    Regards,
    Michael