Jump to content
Forum sending old emails Read more... ×
Sara

Having problems to use SD card driver and looking for a library. CC1352R/HELP

Recommended Posts

Hi everybody!

Problem Description:

I've been programming CC1352R platform lately. And I am realizing there are no many libraries for while. I need to implement the storage of my sensor data and I started to look for a library for using with an SD card. In the TI drivers, (from the SDK - Software Development Kit), there's a library SD.h. And in the examples folder for TI drivers there's an example which name is "sdraw". The first time I run this example It couldn't open the sd. First I sent a question on TI community: https://e2e.ti.com/support/wireless-connectivity/sub-1-ghz/f/156/p/759572/2819479#2819479 and they told me to debug. But I thought strange an example code not to run. Then I simply changed the sd card and It could open, write and read. (It did not display errors). But the example code was intended to write an array and read it to compare if the written data matches with the read data. But I read only zeros (of course it did not match). When I placed the micro sd card in a computer adapter I also had an empty sd. I don't know if that was because it has no file or no file system. 

What I need:

I still didn't have answer from the TI community and I don't have much time to deliver this implementation. So, I would like to know if there's some library for interfacing sd card with a CC1352R. Or if someone has used this library I mentioned: "can you give me some suggestion for what is happening?". I am trying to get some help because I know that if I have to implement it from zero it will cost me a long time, and I don't have experience with sd cards. 

Thanks in advance, I'll appreciate any help.

best regards, 

Sara

 

 

Share this post


Link to post
Share on other sites

Hi sara,

 

I can only give you limited support, and hopefully point you in the right direction.........

Your HW does have SPI, so you should be able to write to SD cards with out issue.

Quote

2× SSI (SPI, MICROWIRE, TI)

 

Try looking here

 

the only thing I see is you have to change the code to fit your HW, I do not know if SSI is close to USCI or USI.

also if you add more info on your setup we maybe able to guide you in the right direction as well.

Share this post


Link to post
Share on other sites

Hi Jsolarski,

1. Yes, it does have SPI interface. The SD.h library that comes in the SDK (Software Development Kit) transparently uses its SPI. And the library documentation says it internally points to a hardware specific implementation also the MISO, MOSI, CLK and CS pins which the SPI uses. I am using RTOS, then I am using the generic drivers from the SDK. 

2. It seems those FAT libraries from the posts you suggested me is intended to be used with Energia, but I just checked the Energia website right now and they still do not support CC1352R family.

The example code I used has a thread to take care of the SD questions. The code is below:

/*
 * 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 <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SD.h>
#include <ti/display/Display.h>

/* 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 1

static Display_Handle display;

unsigned char textarray[BUFFSIZE];

unsigned char cpy_buff[BUFFSIZE];

/*
 *  ======== 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();
    /* 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");
    /* Initialize the array to write to the SD card */
    for (i = 0; i < BUFFSIZE; i++)
    {
        textarray[i] = i & 0xFF;
    }

    /* Mount and register the SD Card */
    sdHandle = SD_open(Board_SD0, NULL);
    if (sdHandle == NULL) {
        Display_printf(display, 0, 0, "Error starting the SD card = %d\n", sdHandle);
        while (1);
    }

    result = SD_initialize(sdHandle);
    if (result != SD_STATUS_SUCCESS) {
        Display_printf(display, 0, 0, "Error initializing the SD card\n");
        while (1);
    }

    totalSectors = SD_getNumSectors(sdHandle);
    sectorSize = SD_getSectorSize(sdHandle);
    cardCapacity = (totalSectors / BYTESPERKILOBYTE) * sectorSize;

    Display_printf(display, 0, 0,
        "There are %u total sectors on the SD card.\n", totalSectors);
    Display_printf(display, 0, 0,
        "The Read/Write sector size is %u bytes\n", sectorSize);
    Display_printf(display, 0, 0,
        "The total card capacity is %u KB\n", cardCapacity);

    /* Calculate number of sectors taken up by the array by rounding up */
    sectors = (sizeof(textarray) + sectorSize -1) / sectorSize;

#if (WRITEENABLE)
    Display_printf(display, 0, 0, "Writing the array...\n");

    result = SD_write(sdHandle, textarray, STARTINGSECTOR, sectors);

    if (result != SD_STATUS_SUCCESS) {
        Display_printf(display, 0, 0, "Error writing to the SD card\n");
        while (1);
    }
#endif

    Display_printf(display, 0, 0, "Reading the array...\n");
    result = SD_read(sdHandle, cpy_buff, STARTINGSECTOR, sectors);
    if (result != SD_STATUS_SUCCESS) {
        Display_printf(display, 0, 0, "Error reading from the SD card\n");
        while (1);
    }

    /* Compare data read from the SD card with expected values */
    for (i = 0; i < BUFFSIZE; i++)
    {
        if (cpy_buff[i] != textarray[i])
        {
            Display_printf(display, 0, 0,"Data read from SD card differed from expected value\n");
            Display_printf(display, 0, 0,"Expected value for index %d: %d, got %d\n", i, textarray[i], cpy_buff[i]);
            Display_printf(display, 0, 0, "Run the example with WRITEENABLE = 1 to write expected values to the SD card\n");
            break;
        }
    }
    if (i == BUFFSIZE) {
        Display_printf(display, 0, 0, "Data read from SD card matched expected values\n");
    }

    SD_close(sdHandle);

    return (NULL);
}

The result I had in Putty (Realize I have no errors messages and instead of this, it runs normally, but in the end, the written data doesn't match the read data). When I place the sd card in an adapter to see it on the computer it also has no data. 

 

putty.PNG

Share this post


Link to post
Share on other sites

so lets start simple,

have you tried a different sd card? or tried a different sector?    (not all SD cards are equal)

have you made sure the card lock switch is in the correct position? (im sure you have but just want to make sure.)

are you able or do you know how to use "dd" to see if you actually have written to the sd card? (dd is a command to either write or read raw data from a disk or image.) we want to verify that you are actually writing to the sd card

 

The code looks good, but i dont use RTOS, and dont have a way to test at this time, im sure i could but I dont have time to get it wall setup.

Share this post


Link to post
Share on other sites

Hi Jsolarski,

start simply is really the best idea. I got it running now: It wrote in the micro sd card and when I read it matched. This time not with "zeros". 

What was the problem: the supply voltage. Since I am using a shield for Arduino to interface the sd card, its supply was 5V, not 3.3V. What should be happening is that the writing and reading run perfectly but in 3.3V the current is not enough to write. Solved!🙂

When I placed the micro sd card in the computer adapter it could not read because there's no file system. Actually, this is still a problem I don't know how to solve... Any suggestions?

Thanks Jsolarski

Capturar.PNG

sd.PNG

Share this post


Link to post
Share on other sites

No file system is expected when writing data to the SD card like you are, you have to implement a file system, that's why i pointed to FATFS....basically you need to write to a file to read from a computer.

You have a few options on how to get the data.

1) Dump the SD card data using DD or other command that can read block devices, hex editors and file recovery software are places to start.

2) create a function that dumps it to the serial output, and have your computer put it in a file.

3) implement a file system, where you write data to a file on the SD card.

 

im researching some things for you to get you pointed out in the right direction.

Share this post


Link to post
Share on other sites

http://www.ti.com/lit/ug/spruhd4m/spruhd4m.pdf

Quote
1.7
FatFS Module: TI-RTOS File System
FatFS is an open-source FAT file system module intended for use in embedded systems. The API used
by your applications is generic to all FatFS implementations, and is described and documented at
. In order to use FatFS in TI-RTOS applications, use the
ti.mw.fatfs.FatFS module.
For more information about FatFS, see the following:
Chapter 7
, "Using the FatFs File System Drivers"

 

 

This should get you going

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×