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.

RTOS/SIMPLELINK-CC26X2-SDK: add_device() for SIMPLELINK-CC26X2-SDK

Part Number: SIMPLELINK-CC26X2-SDK

Tool/software: TI-RTOS

Regarding the following link:

e2e.ti.com/.../766011

I can't generate the file on SD card, but if I  add the add_device() before running SDFatFS_open(), I can successfully generate the file.

do you have any description about add_device()???

add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
           ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);

SDFatFS_open()   

thanks.

  • Hey Trevor,

    I will consult the subject-matter expert and get back to you shortly.

    BR,
    Seong
  • Hi,

    Could you provide a sample code for interfacing SD cards on CC26x2 ?

    Best regards,

  • /*
     * Copyright (c) 2016-2017, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*
     *  ======== sdraw.c ========
     */
    
    
    #include <xdc/std.h>
    #include <xdc/runtime/System.h>
    #include <file.h>
    #include <stdio.h>
    #include <stddef.h>
    #include <stdint.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/SDFatFS.h>
    #include <ti/drivers/SD.h>
    #include <ti/display/Display.h>
    
    
    #include <third_party/fatfs/ff.h>
    #include <third_party/fatfs/diskio.h>
    #include <third_party/fatfs/ffcio.h>
    
    
    /*
    * ======== fatfs_getFatTime ========
    */
    int32_t fatfs_getFatTime(void)
    {
        time_t seconds;
        uint32_t fatTime;
        struct tm *pTime;
    
        seconds = time(NULL);
    
        pTime = localtime(&seconds);
    
        fatTime = ((uint32_t)(pTime->tm_year - 80) << 25) |
        ((uint32_t)(pTime->tm_mon) << 21) |
        ((uint32_t)(pTime->tm_mday) << 16) |
        ((uint32_t)(pTime->tm_hour) << 11) |
        ((uint32_t)(pTime->tm_min) << 5) |
        ((uint32_t)(pTime->tm_sec) >> 1);
    
        return ((int32_t)fatTime);
    }
    
    
    
    
    
    /* Example/Board Header files */
    #include "Board.h"
    
    /* Buffer size used for the file copy process */
    #define BUFFSIZE 1024
    
     /* Starting sector to write/read to */
    #define STARTINGSECTOR 0
    
    #define BYTESPERKILOBYTE 1024
    
    /*
     * Set this constant to 1 in order to write to the SD card.
     * WARNING: Running this example with WRITEENABLE set to 1 will cause
     * any filesystem present on the SD card to be corrupted!
     */
    #define WRITEENABLE 0
    
    static Display_Handle display;
    
    unsigned char textarray[BUFFSIZE];
    
    unsigned char cpy_buff[BUFFSIZE];
    
    
    
    
    
    // SD card write
    #define DRIVE_NUM       0
    #define SD_DRIVE_NUM    0
    
    // file definitions for SD write
    const char outputfile[] = "fat:0:test.txt";
    
    
    
    /* File name prefix for this filesystem for use with TI C RTS */
    char fatfsPrefix[] = "fat";
    
    
    /*
     * Note: The SDFatFS driver provides interface functions to enable FatFs
     * but relies on the SD driver to communicate with SD cards.  Opening a
     * SDFatFs driver instance will internally try to open a SD driver instance
     * reusing the same index number (opening SDFatFs driver at index 0 will try to
     * open SD driver at index 0).  This requires that all SDFatFs driver instances
     * have an accompanying SD driver instance defined with the same index.  It is
     * acceptable to have more SD driver instances than SDFatFs driver instances
     * but the opposite is not supported & the SDFatFs will fail to open.
     */
    SDFatFS_Object sdfatfsObjects[1];
    
    const SDFatFS_Config SDFatFS_config[1] = {
        {
            .object = &sdfatfsObjects[0]
        }
    };
    
    const uint_least8_t SDFatFS_count = 1;
    
    
    
    void SD_writedata(void){
    
        SDFatFS_Handle sdfatfsHandle;
        FILE *src;
        sdfatfsHandle = SDFatFS_open(0, DRIVE_NUM);
        if (sdfatfsHandle != NULL) {
            // Try to open the source file 4 times
            uint8_t timeout = 0;
            src = fopen(outputfile, "w+");
            if(src != NULL){
                // Print in chunks of 750 values to enable a higher ADXL sample speed
                uint16_t i = 0;
                char *str="test.....................";
    
                fwrite(str, 1, strlen(str), src);
                fflush(src);
                fclose(src);
    
            }
        }
        // Release SPI0!
        SDFatFS_close(sdfatfsHandle);
    }
    
    
    
    /*
     *  ======== mainThread ========
     *  Task to perform a raw write and read from the SD card.
     *  Note: Running this application will cause any filesystem on the
     *      SD card to become corrupted!
     */
    void *mainThread(void *arg0)
    {
        int_fast8_t   result;
        uint_fast32_t cardCapacity;
        uint_fast32_t totalSectors;
        uint_fast32_t sectorSize;
        uint_fast32_t sectors;
        int           i;
        SD_Handle     sdHandle;
    
        Display_init();
        GPIO_init();
     // SD_init();
    
        SDFatFS_init();
    
        /* Configure the LED pin */
        GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    
        /* Open the display for output */
        display = Display_open(Display_Type_UART, NULL);
        if (display == NULL) {
            /* Failed to open display driver */
            while (1);
        }
    
        /* Turn on user LED */
        GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
    
        Display_printf(display, 0, 0, "Starting the SD example\n");
    
    
    
        add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
               ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
    
        SD_writedata();
    
        Display_printf(display, 0, 0, "SD_writedata DONE\n");
    
    
    
    
    
    
        return (NULL);
    }
    

  • Hello,

    Do you have a complete sample project for interfacing SD cards on LAUNCHXL-CC26X2R1 or CC2650?

    Best regards,

    Sebastian
  • In which file is add_device() defined? I could not find it in the third party folder where SDFatFS is defined.
  • please refer to attached project,

    if you remove the add_device(), it can't generate file on SD card.

    Regarding add_device(), you could found from MSP430 SDK example.

    sdraw_CC26X2R1_LAUNCHXL_tirtos_ccs.zip

  • add_device should not be required, ref e2e.ti.com/.../721354

    If you don't add the add_device(), do you get any feedback when you step through line by line in SD_writedata?
  • Closing this thread since I have not gotten any new information.
  • I am sorry to reply you too late,

    I have checked the SD_writedata() function, which has no errors.

    I have upload our project above, could you help to check on your side???

    void SD_writedata(void){

        SDFatFS_Handle sdfatfsHandle;
        FILE *src;
        sdfatfsHandle = SDFatFS_open(0, DRIVE_NUM);
        if (sdfatfsHandle != NULL) {
            // Try to open the source file 4 times
            uint8_t timeout = 0;
            src = fopen(outputfile, "w+");

            if(src != NULL){
                // Print in chunks of 750 values to enable a higher ADXL sample speed
                uint16_t i = 0;
                char *str="test.....................";

                if(fwrite(str, 1, strlen(str), src)<0)
                    Display_printf(display, 0, 0, "fwrite error !!!\n");

                if(fflush(src) != 0)
                    Display_printf(display, 0, 0, "fflush error !!!\n");

                if(fclose(src) != 0)
                    Display_printf(display, 0, 0, "fclose error !!!\n");
            }
            else
                Display_printf(display, 0, 0, "fopen error !!!\n");
        }
        // Release SPI0!
        SDFatFS_close(sdfatfsHandle);
    }

    thanks

    BR

    Trevor

  • Which hardware do you together with the CC26x2 launchpad to get access to a SD card?
  • Our custom board,but we refer to TI’s booster pack board.

    I think it is not a hardware problem.

  • Meaning that if I use launchpad together with the boosterpack you linked to above I would be able to see the same issue?
  • Yes, please help to check this issue.

    You could use my project on you hardware.

  • Hello Trevor,

    Where do you configure data rate to write on the SD card ?

    Thank you,
  • Hi Sebastian21
    I just use TI's SDFatFS API directly.
  • I tried to import the project that you posted. When I look at properties->general->Products I don't see any information about the SDK used?
  • The SDK  version is 2.30.

    If you can’t import my project, you can import the SDK/example/tirtos/driver/sdraw project directly, then replace the sdraw.c.

  • Got it up and running and looked at which functions that are called.

    Do you use the correct function? I see that you use fopen, but the documentation indicate that f_open should be used?
  • Hi TER,

    I refer the following thread:

    e2e.ti.com/.../159067

    do you suggest that we use f_open() function?

    thanks.

    BR

    Trevor

  • The documentation on this is poor and I will ask the driver team if they would be able to improve it.

    I was looking at source/third_party/fatfs/documents/00index_e.html and since the SDFatFS includes the fatfs I assumed that this was correct. But re-reading the SDFatFs file this state that "the standard C runtime file I/O calls (fopen, fclose, etc...)" can be used. I have tried to find documentation on add_device and why this is needed but I have not found any good source yet. The closest I could find is form processors.wiki.ti.com/.../Tips_for_using_printf : "Call add_device() to add your functions to the stream table (i.e. in addition to stdin, stdout, stderr)."

    I will continue looking to see if I find some better documentation.
  • Hi TER,
    thanks for your help.

    BR
    Trevor