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.

The SDSPI driver example provided by TI-RTOS doesn't work properly for MSP432p401r

Part Number: MSP432P401R

Tool/software: TI-RTOS

Fellow MSP users,

The SDSPI example provided with TI-RTOS doesn't work properly. The below error message kept displaying when the SD card was mounted and even when the driver was removed from the launchpad. I am using the latest version of TI-RTOS 2.20 along with the latest compilers (TI 15.12.3 LTS / TI 16.12.0 STS) along with the latest MSP432ware. I am using the CCS version 6.2.0 and have it installed in the default location. I imported the project from Resource Explorer and followed its instructions. 

I tried adding a time delay in the middle and tested using an oscilloscope but a clock signal was not present. Therefore I think the problem is in the library and not in the board since I got a clock signal from the designated pins on the board. I checked on the .cfg file and assured that all the configurations were present.

I am not using a SD module shown below. The error would occur even when the SD card module is removed hence I think that the board is not giving an output, but just running the programme.

The code is below.

/*
 * Copyright (c) 2015, 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.
 */

/*
 *  ======== fatsd.c ========
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/System.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Task.h>

/* TI-RTOS Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SDSPI.h>

/* Example/Board Header files */
#include "Board.h"

/* Buffer size used for the file copy process */
#ifndef CPY_BUFF_SIZE
#define CPY_BUFF_SIZE       2048
#endif

/* String conversion macro */
#define STR_(n)             #n
#define STR(n)              STR_(n)

/* Drive number used for FatFs */
#define DRIVE_NUM           0

#define TASKSTACKSIZE       768

const char  inputfile[] = "fat:"STR(DRIVE_NUM)":input.txt";
const char outputfile[] = "fat:"STR(DRIVE_NUM)":output.txt";

const char textarray[] = \
"***********************************************************************\n"
"0         1         2         3         4         5         6         7\n"
"01234567890123456789012345678901234567890123456789012345678901234567890\n"
"This is some text to be inserted into the inputfile if there isn't\n"
"already an existing file located on the media.\n"
"If an inputfile already exists, or if the file was already once\n"
"generated, then the inputfile will NOT be modified.\n"
"***********************************************************************\n";

unsigned char cpy_buff[CPY_BUFF_SIZE + 1];

Task_Struct task0Struct;
Char task0Stack[TASKSTACKSIZE];

/*
 *  ======== taskFxn ========
 *  Task to perform a file copy
 *
 *  Task tries to open an existing file inputfile[]. If the file doesn't
 *  exist, create one and write some known content into it.
 *  The contents of the inputfile[] are then copied to an output file
 *  outputfile[]. Once completed, the contents of the output file are
 *  printed onto the system console (stdout).
 *
 *  Task for this function is created statically. See the project's .cfg file.
 */
Void taskFxn(UArg arg0, UArg arg1)
{
    SDSPI_Handle sdspiHandle;
    SDSPI_Params sdspiParams;

    /* Variables for the CIO functions */
    FILE *src, *dst;

    /* Variables to keep track of the file copy progress */
    unsigned int bytesRead = 0;
    unsigned int bytesWritten = 0;
    unsigned int filesize;
    unsigned int totalBytesCopied = 0;

    /* Mount and register the SD Card */
    SDSPI_Params_init(&sdspiParams);
    sdspiHandle = SDSPI_open(Board_SDSPI0, DRIVE_NUM, &sdspiParams);
    if (sdspiHandle == NULL) {
        System_abort("Error starting the SD card\n");
    }
    else {
        System_printf("Drive %u is mounted\n", DRIVE_NUM);
    }

    /* Try to open the source file */
    src = fopen(inputfile, "r");
    if (!src) {
        System_printf("Creating a new file \"%s\"...", inputfile);

        /* Open file for both reading and writing */
        src = fopen(inputfile, "w+");
        if (!src) {
            System_printf("Error: \"%s\" could not be created.\n"
                          "Please check the Getting Started Guide "
                          "if additional jumpers are necessary.\n",
                          inputfile);
            System_abort("Aborting...\n");
        }

        fwrite(textarray, 1, strlen(textarray), src);
        fflush(src);

        /* Reset the internal file pointer */
        rewind(src);

        System_printf("done\n");
    }
    else {
        System_printf("Using existing copy of \"%s\"\n", inputfile);
    }

    /* Create a new file object for the file copy */
    dst = fopen(outputfile, "w");
    if (!dst) {
        System_printf("Error opening \"%s\"\n", outputfile);
        System_abort("Aborting...\n");
    }
    else {
        System_printf("Starting file copy\n");
    }

    /*  Copy the contents from the src to the dst */
    while (true) {
        /*  Read from source file */
        bytesRead = fread(cpy_buff, 1, CPY_BUFF_SIZE, src);
        if (bytesRead == 0) {
            break; /* Error or EOF */
        }

        /*  Write to dst file */
        bytesWritten = fwrite(cpy_buff, 1, bytesRead, dst);
        if (bytesWritten < bytesRead) {
            System_printf("Disk Full\n");
            break; /* Error or Disk Full */
        }

        /*  Update the total number of bytes copied */
        totalBytesCopied += bytesWritten;
    }

    fflush(dst);

    /* Get the filesize of the source file */
    fseek(src, 0, SEEK_END);
    filesize = ftell(src);
    rewind(src);

    /* Close both inputfile[] and outputfile[] */
    fclose(src);
    fclose(dst);

    System_printf("File \"%s\" (%u B) copied to \"%s\" (Wrote %u B)\n",
                  inputfile, filesize, outputfile, totalBytesCopied);

    /* Now output the outputfile[] contents onto the console */
    dst = fopen(outputfile, "r");
    if (!dst) {
        System_printf("Error opening \"%s\"\n", outputfile);
        System_abort("Aborting...\n");
    }

    /* Print file contents */
    while (true) {
        /* Read from output file */
        bytesRead = fread(cpy_buff, 1, CPY_BUFF_SIZE, dst);
        if (bytesRead == 0) {
            break; /* Error or EOF */
        }
        cpy_buff[bytesRead] = '\0';
        /* Write output */
        System_printf("%s", cpy_buff);
        System_flush();
    }

    /* Close the file */
    fclose(dst);
    int i;
    for(i=0;i<2000000;i++){}//added as a delay after the problem occured since the SDSPI is not closed yet.

    /* Stopping the SDCard */
    SDSPI_close(sdspiHandle);
    System_printf("Drive %u unmounted\n", DRIVE_NUM);

    BIOS_exit(0);
}

/*
 *  ======== main ========
 */
int main(void)
{
    Task_Params taskParams;

    /* Call board init functions */
    Board_initGeneral();
    Board_initGPIO();
    Board_initSDSPI();

    /* Construct file copy Task thread */
    Task_Params_init(&taskParams);
    taskParams.stackSize = TASKSTACKSIZE;
    taskParams.stack = &task0Stack;
    Task_construct(&task0Struct, (Task_FuncPtr)taskFxn, &taskParams, NULL);

    /* Turn on user LED */
    GPIO_write(Board_LED0, Board_LED_ON);

    System_printf("Starting the FatSD example\n");

    /* Start BIOS */
    BIOS_start();

    return (0);
}

Please can someone help me with this issue.

Best regards,

Inoth.

  • Hi Inoth,

    Can you share your project with us? Are you using TI board and booster pack? Or your own custom board? Can you check if your SD card is working fine and is formatted to FAT32 format?

    Vikram
  • Hello, Vikram, Thank you for your attention.


    Can you share your project with us?

    Yes, I would like to share the project , but I didn't do any changes codewise. Just the project directly exported from the CCS Resouce Explorer. It is below.

    7737.fatsd_MSP_EXP432P401R_TI_MSP432P401R.zip

    Are you using TI board and booster pack? Or your own custom board?

    No I'm not using a boosterpack, just a SD module simillar to the one shown in the link below.

    www.ebay.com/.../262136732921


     Can you check if your SD card is working fine and is formatted to FAT32 format?

    Yes, the SD card is working with Fat32 format. The problem is the above error message would appear even when the SD card module is removed.

    If you can identify the problem it would be a great help.

    Regards,

    Inoth.

  • Inoth Munisinghe said:
    Yes, I would like to share the project , but I didn't do any changes codewise. Just the project directly exported from the CCS Resouce Explorer. It is below.

    I see this change:

            bytesWritten = fwrite(cpy_buff, -1, bytesRead, dst);

    since the 2nd parameter is of type size_t which is an unsigned integral type, a -1 would be converted to MAX integer value. Is this change required?

    Inoth Munisinghe said:
    No I'm not using a boosterpack, just a SD module simillar to the one shown in the link below.

    We just support boosterpack with the example. For other boards, you may need to make sure the pins that are defined for SDSPI driver (see MSP_EXP432P401R.c and search SDSPI) are matching with the pins that you are connecting to.

    Vikram

  • Hello Vikram,
    Thank you for your reply. It solved the problem.
    After reffering the MSP432PR01R.c file I found that I was using the P1.0 pin for the chip select. The correct pin was P4.6.
    Thanks again for the help.
    Inoth.