Store accelerometer data in SD card
Antwort
15.08.19 12:09

Î am trying to save the accelerometer data in the sd card. For this i found the foloowinmg code. I placed it in the XDK workbench SDcard example. I replaced the appcontroller.c file with the following code but it shows the following error. I am attaching both the code and the error screenshot for your kind considerations.

Thank you


/* module includes ********************************************************** */
#include "XDKAppInfo.h"
#undef BCDS_MODULE_ID  /* Module ID define before including Basics package*/
#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER


/* system header files */
#include <stdio.h>

/* additional interface header files */
#include "BCDS_SDCard_Driver.h"
#include "BCDS_CmdProcessor.h"
#include "BSP_BoardType.h"
#include "BCDS_BSP_LED.h"
#include "ff.h"
#include "BCDS_Assert.h"
#include <FreeRTOS.h>
#include <timers.h>
#include "XdkSensorHandle.h"
#include "BCDS_BSP_Mic_AKU340.h"


/* constant definitions ***************************************************** */
#define BUFFER_SIZE                 UINT16_C(512)
#define SINGLE_BLOCK                UINT8_C(1)      /**< SD- Card Single block write or read */
#define DRIVE_ZERO                  UINT8_C(0)      /**< SD Card Drive 0 location */
#define SECTOR_VALUE                UINT8_C(6)      /**< SDC Disk sector value */
#define SINGLE_SECTOR_LEN           UINT32_C(512)   /**< Single sector size in SDcard

/* local variables ********************************************************** */
static xTimerHandle sdCardWriteReadHandle; /**< variable to store timer handle*/
static CmdProcessor_T *AppCmdProcessor; /**< Application Command Processor Instance */
static SDCardDriver_DiskStatus_T diskInitStatus = SDCARD_NOT_INITIALIZED;
#if FAT_FILE_SYSTEM==1
static FATFS FatFileSystemObject; /* File system specific objects */
#endif
/* global variables ********************************************************* */

/* local module global variable declarations */

uint8_t writeBuffer[SINGLE_SECTOR_LEN] = "XDK ASAMPLE TEST FOR MMC"; /**< Write Buffer for SD-Card */
uint8_t readBuffer[SINGLE_SECTOR_LEN]; /**< read buffer for SD-Card */

static void initAccelSensor(void){

    Retcode_T accelerometerInitReturnValue = RETCODE_FAILURE;
    Retcode_T returnBandwidthValue = RETCODE_FAILURE;
    Retcode_T returnRangeValue = RETCODE_FAILURE;

accelerometerInitReturnValue =
Accelerometer_init(xdkAccelerometers_BMA280_Handle);

if (RETCODE_OK != accelerometerInitReturnValue) {
    printf("Acce Sensor initialization Failed\n\r");
}

returnBandwidthValue =
Accelerometer_setBandwidth(xdkAccelerometers_BMA280_Handle,
ACCELEROMETER_BMA280_BANDWIDTH_500HZ);

if (RETCODE_OK != returnBandwidthValue) {
    printf("Acce Sensor bandwidth setting Failed\n\r");
}

returnRangeValue =
Accelerometer_setRange(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_RANGE_16G);

if (RETCODE_OK != returnRangeValue) {
    printf("Acce Sensor range setting Failed\n\r");
}
}


#if FAT_FILE_SYSTEM==1
/**
 * @brief
 *      The sdCardFatFileSystemWriteRead API uses the FAT file system library calls.
 *      This API will mount and create the file system, then it will open, seek write,
 *      read and close the files. This API will compare the contents which has been
 *      written and read.
 *
 * @retval
 *      SDCARD_APP_NO_ERROR - All the file operations are success
 *
 * @retval
 *      SDCARD_APP_ERROR - one of the file operation failed
 *
 * @retval
 *      SDCARD_INIT_FAILED - disk has not been initialized
 */
static Retcode_T sdCardFatFileSystemWriteRead(void)
{
    Retcode_T returnvalue = RETCODE_FAILURE;
    Accelerometer_XyzData_T bma280 = {INT32_C(0), INT32_C(0), INT32_C(0)};
    memset(&bma280, 0, sizeof(Accelerometer_XyzData_T));
    returnvalue = Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);


    Retcode_T returnValue = RETCODE_FAILURE;
    returnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle);
    if ( RETCODE_OK != returnValue)
        {
             printf("return AccelSensor Data failed\n\r");
        }

    FIL fileObject; /* File objects */
    int8_t ramBufferWrite[BUFFER_SIZE]; /* Temporary buffer for write file */
    int8_t ramBufferRead[BUFFER_SIZE]; /* Temporary buffer for read file */
    int8_t stringTestBuffer[100];
    uint16_t fileSize;
    UINT bytesWritten;
    UINT bytesRead;
    uint8_t fileStatus = FILE_EQUAL;

    Retcode_T result;
    FRESULT fileSystemResult;

    /*Step1 - Initialization of file buffer write */
    fileSize = 0;
    if (RETCODE_OK == returnvalue)
        {
            fileSize+= snprintf(stringTestBuffer + fileSize, sizeof(stringTestBuffer) - fileSize,
                            "BMA280 X-data: %ld", (long int) bma280.xAxisData);
            fileSize+= snprintf(stringTestBuffer + fileSize, sizeof(stringTestBuffer) - fileSize,
                            "BMA280 Y-data: %ld", (long int) bma280.yAxisData);
            fileSize+= snprintf(stringTestBuffer + fileSize, sizeof(stringTestBuffer) - fileSize,
                            "BMA280 Z-data: %ld", (long int) bma280.zAxisData);
                    }

    /* step2 - Initialization of SD card */
    if (diskInitStatus != SDCARD_DISK_INITIALIZED) /*SD-Card Disk Not Initialized */
    {
        diskInitStatus = (SDCardDriver_DiskStatus_T)SDCardDriver_DiskInitialize(DRIVE_ZERO); /* Initialize SD card */
    }

    if (SDCARD_DISK_INITIALIZED == diskInitStatus)
    {
        /*Step1 - open the file to write */
        /*  If file does not exist create it*/
        fileSystemResult = f_open(&fileObject, TEST_FILENAME,
        FA_OPEN_ALWAYS | FA_WRITE);
        if (fileSystemResult != FR_OK)
        {
            /* Error. Cannot create the file */
            return RETCODE(RETCODE_SEVERITY_ERROR, FILE_OPEN_ERROR);
        }

        /*Step2 - Set the file write pointer to first location */
        fileSystemResult = f_lseek(&fileObject, f_size(&fileObject));
        if (fileSystemResult != FR_OK)
        {
            /* Error. Cannot set the file write pointer */
            return RETCODE(RETCODE_SEVERITY_ERROR, FILE_LSEEK_ERROR);
        }

        /*Step3 - Write a buffer to file*/
        fileSystemResult = f_write(&fileObject, stringTestBuffer, fileSize,
                &bytesWritten);
        if ((fileSystemResult != FR_OK) || (fileSize != bytesWritten))
        {
            /* Error. Cannot write the file */
            return RETCODE(RETCODE_SEVERITY_ERROR, FILE_WRITE_ERROR);
        }

        /*Step4 - Close the file */
        fileSystemResult = f_close(&fileObject);
        if (fileSystemResult != FR_OK)
        {
            /* Error. Cannot close the file */
            return RETCODE(RETCODE_SEVERITY_ERROR, FILE_CLOSE_ERROR);
        }

        /*Step5 - Open the file for read */
        fileSystemResult = f_open(&fileObject, TEST_FILENAME, FA_READ);
        if (fileSystemResult != FR_OK)
        {
            /* Error. Cannot create the file */
            return RETCODE(RETCODE_SEVERITY_ERROR, FILE_OPEN_ERROR);
        }

        /*Step6 - Set the file read pointer to first location */
        fileSystemResult = f_lseek(&fileObject, (f_size(&fileObject) - fileSize));
        if (fileSystemResult != FR_OK)
        {
            /* Error. Cannot set the file pointer */
            return RETCODE(RETCODE_SEVERITY_ERROR, FILE_LSEEK_ERROR);
        }

        /*Step7 - Read some data from file */
        fileSystemResult = f_read(&fileObject, ramBufferRead, fileSize,
                &bytesRead);
        if ((fileSystemResult != FR_OK) || (fileSize != bytesRead))
        {
            /* Error. Cannot read the file */
            return RETCODE(RETCODE_SEVERITY_ERROR, SDCARD_APP_ERROR);
        }

        /*Step8 - Close the file */
        fileSystemResult = f_close(&fileObject);
        if (fileSystemResult != FR_OK)
        {
            /* Error. Cannot close the file */
            return RETCODE(RETCODE_SEVERITY_ERROR, FILE_CLOSE_ERROR);
        }

        /*Step9 - Compare ramBufferWrite and ramBufferRead */
        for (uint8_t index = 0; index < fileSize; index++)
        {
            if ((ramBufferWrite[index]) != (ramBufferRead[index]))
            {
                /* Error compare buffers*/
                fileStatus = FILE_NOT_EQUAL;
            }
        }

        if (FILE_EQUAL == fileStatus)
        {
            return RETCODE_OK;
        }
        else
        {
            return RETCODE(RETCODE_SEVERITY_ERROR, SDCARD_APP_ERROR);
        }
    }
    else
    {
        result = RETCODE(RETCODE_SEVERITY_ERROR, SDCARD_INIT_FAILED);
    }
    return (result);
}
#else

/**
 * @brief
 *      The sdCardSingleBlockWriteRead API is used to write some string and read the string.
 *      then validate the written and read string.
 *
 * @param[in] sector
 *        SDC Disk sector value for read/Write process.
 *
 * @retval
 *      SDCARD_INIT_FAILED - disk has not been initialized
 */
static Retcode_T sdCardSingleBlockWriteRead(uint32_t sector)
{

    Retcode_T sdcWriteReadStatus; /* Default return value is in Error state */

    if (diskInitStatus != SDCARD_DISK_INITIALIZED)
    { /*SD-Card Disk Not Initialized */
        diskInitStatus = SDCardDriver_DiskInitialize(DRIVE_ZERO);/* Initialize SD card */
    }

    if (diskInitStatus == SDCARD_DISK_INITIALIZED)
    {
        if (SDCardDriver_DiskWrite(DRIVE_ZERO, writeBuffer, sector, SINGLE_BLOCK) == RETCODE_OK)
        {/* Write Data into SD-Card */
            if (SDCardDriver_DiskRead(DRIVE_ZERO, readBuffer, sector, SINGLE_BLOCK) == RETCODE_OK)
            {/* Read Data's from the SD-CARD */
                int8_t sdCardWriteReadCheck; /* Validate the SD_card Write/read Buffer */
                sdCardWriteReadCheck = strncmp((const char*) writeBuffer, (const char*) readBuffer, sizeof(writeBuffer));
                if (INT8_C(0) == sdCardWriteReadCheck )
                {
                    sdcWriteReadStatus = RETCODE_OK;
                }
                else
                {
                    sdcWriteReadStatus = RETCODE(RETCODE_SEVERITY_ERROR, SDCARD_APP_ERROR);
                }
            }
            else
            { /* SDC Read failure */
                printf("\r\nSD_CARD Read failure\n\r");
                sdcWriteReadStatus = RETCODE(RETCODE_SEVERITY_ERROR, FILE_READ_ERROR);
            }
        }
        else
        { /* SDC Write failure */
            printf("\r\nSD_CARD Write failure\n\r");
            sdcWriteReadStatus = RETCODE(RETCODE_SEVERITY_ERROR, FILE_WRITE_ERROR);
        }
    }
    else
    {
        printf("\r\nSD_CARD Initialize failure\n\r");
        sdcWriteReadStatus = RETCODE(RETCODE_SEVERITY_ERROR, SDCARD_INIT_FAILED);
    }

    return (sdcWriteReadStatus);
}

#endif

/** @brief
 *      The function used to write and read depending on the SD card Detect status .
 *      The status can be SD card inserted or not inserted.
 *
 *  @param[in] void *pvParameters
 *      Rtos task should be defined with the type void *(as argument).
 */
static void SDCardWriteRead(void *pvParameters)
{
    Retcode_T returnVal = RETCODE_OK;
    BCDS_UNUSED(pvParameters);
#if FAT_FILE_SYSTEM==1
    FRESULT FileSystemResult = FR_OK;
#endif
    static uint8_t sdcardeject = FALSE;
    static uint8_t sdcardInsert = FALSE;

    Retcode_T sdCardWriteReadReturn = RETCODE(RETCODE_SEVERITY_ERROR, SDCARD_APP_ERROR); /* variable for SD-Card ReadWrite status check */

    /* Get the SD Card status as inserted in XDK or not */
    if (SDCARD_INSERTED == SDCardDriver_GetDetectStatus())
    {
        sdcardInsert = TRUE;
        printf("SD card is inserted in XDK\n\r");

        returnVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_ON); /**< Switch ON LED1 */

        if (returnVal != RETCODE_OK)
        {
            printf("SD card is inserted LED indication failure XDK\n\r");
        }
        if (sdcardeject == TRUE)
        {
            if (RETCODE_OK == returnVal)
            {
                returnVal = SDCardDriver_DiskInitialize(DRIVE_ZERO);
                if (RETCODE_OK == returnVal)
                {
                    diskInitStatus = SDCARD_DISK_INITIALIZED;
                }
            }

#if FAT_FILE_SYSTEM==1
            if (RETCODE_OK == returnVal)
            {
                FileSystemResult = f_mount(&FatFileSystemObject, DEFAULT_LOGICAL_DRIVE, FORCE_MOUNT);
                if (FR_OK != FileSystemResult)
                {
                    printf("f_mount failed hence read and write in Sdcard using FATfs is not possible \n\r");
                    assert(0);
                    return;
                }
            }
#endif
            sdcardeject = FALSE;
        }
#if FAT_FILE_SYSTEM==1 /* Already restrict this value to be 0 or 1. */
        /* SDC Disk FAT file system Write/Read functionality */
        sdCardWriteReadReturn = sdCardFatFileSystemWriteRead();
        if (sdCardWriteReadReturn == RETCODE_OK)
        {
            printf("Write and read using FAT file system success \n\r");
        }
        else
        {
            printf("Write and read using FAT file system failed\n\r");
        }
#else /* FAT_FILE_SYSTEM == 0 */
        /* SDC Disk  single block write and read without FAT file system  */
        sdCardWriteReadReturn = sdCardSingleBlockWriteRead(SECTOR_VALUE);
        if (sdCardWriteReadReturn == RETCODE_OK)
        {
            printf("single block write and read without FAT file system success \n\r");
        }
        else
        {
            printf("single block write and read without FAT file system failed \n\r");
        }
#endif /* FAT_FILE_SYSTEM == 1 */
    }
    else
    {
        if (TRUE == sdcardInsert)
        {
            sdcardeject = TRUE;
        }
        sdcardInsert = FALSE;
        printf("\r\nSD card is not inserted in XDK\n\r");

        returnVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_OFF); /**< Switch OFF LED1 */
        if (returnVal != RETCODE_OK)
        {
            printf("SD card is not inserted LED indication failure XDK\n\r");
        }
    }
}

/*
 * Defines the prototype to which timer callback functions must conform.
 */
/**
 * @brief        This is a application timer callback function used to enqueue SDCardwriteread function
 *               to the command processor.
 *
 * @param[in]    pvParameters unused parameter.
 */
/*static void EnqueueSDCardWriteread(void *pvParameters)
{
    BCDS_UNUSED(pvParameters);

    Retcode_T retVal = CmdProcessor_enqueue(AppCmdProcessor, (CmdProcessor_Func_T) SDCardWriteRead, NULL, UINT32_C(0));
    if (RETCODE_OK != retVal)
    {
        printf("Failed to Enqueue SDCard write read to Application Command Processor \r\n");
    }
}*/
/**
 * @brief
 *      The SDC_sdCardWriteReadInit API creates a Write/read task and starts the task.
 */
static void Init(void)
{
    Retcode_T retVal;
#if FAT_FILE_SYSTEM==1
    FRESULT FileSystemResult = FR_OK;
    FILINFO FileInfo;
#endif
    static_assert((portTICK_RATE_MS != 0), "Tick rate MS is zero");

    retVal = SDCardDriver_Initialize();
    if (RETCODE_OK == retVal)
    {
        retVal = SDCardDriver_DiskInitialize(DRIVE_ZERO);
        if (RETCODE_OK == retVal)
        {
            diskInitStatus = SDCARD_DISK_INITIALIZED;
        }
    }

#if FAT_FILE_SYSTEM==1
    if (RETCODE_OK == retVal)
    {
        FileSystemResult = f_mount(&FatFileSystemObject, DEFAULT_LOGICAL_DRIVE, FORCE_MOUNT);
        if (FR_OK == FileSystemResult)
        {
            FileSystemResult = f_stat(TEST_FILENAME, &FileInfo);
            if (FR_OK == FileSystemResult)
            {
                FileSystemResult = f_unlink(TEST_FILENAME);
            }
            if (FR_NO_FILE == FileSystemResult)
            {
                printf("File does not exist. \n\r");
            }
        }
        else
        {
            printf("f_mount failed hence read and write in Sdcard using FATfs is not possible \n\r");
            assert(0);
            return;
        }
    }
#endif
}

/**
 * @brief This is a template function where the user can write his custom application.
 *
 */
void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
{
    if (CmdProcessorHandle == NULL)
     {
     printf("Command processor handle is null \n\r");
     assert(false);
     }
     /*AppCmdProcessor = (CmdProcessor_T *)CmdProcessorHandle;*/
     BCDS_UNUSED(param2);

       uint32_t timerBlockTime = UINT32_MAX;
       uint32_t threemiliSecondDelay = UINT32_C(3);
       uint32_t timerAutoReloadOn = pdTRUE;

       xTimerHandle SensorHandle = NULL;
       SensorHandle = xTimerCreate((const char *) "sdCardFatFileSystemWriteRead",
     threemiliSecondDelay,timerAutoReloadOn, NULL, sdCardFatFileSystemWriteRead);

     initAccelSensor();
     Init();
     xTimerStart(SensorHandle,timerBlockTime);
     vTaskDelay(5000);
     xTimerStop(SensorHandle,timerBlockTime);

}

0 (0 Stimmen)
RE: Store accelerometer data in SD card
Antwort
16.08.19 06:56 als Antwort auf Md Zakaria RAhman.

Hello Rahman,

sdCardFatFileSystemWriteRead function is declared and defined under protected macro FAT_FILE_SYSTEM. Please enable this macro to fix the compilation issue.

#define FAT_FILE_SYSTEM 1

 

0 (0 Stimmen)
RE: Store accelerometer data in SD card
Antwort
20.08.19 10:43 als Antwort auf Padmapriya G.
Thank you for your reply. I added #define FAT_FILE_SYSTEM 1 in the code but now a new error message is being shown. 
0 (0 Stimmen)
RE: Store accelerometer data in SD card
Antwort
21.08.19 08:25 als Antwort auf Md Zakaria RAhman.
Hi Zakaria,
the errors You are getting are resolved by adding the corresponding #defines.
Please look into the documentation of this FatFs implementation: http://elm-chan.org/fsw/ff/00index_e.html for the concrete options. Else the API is very similar to the standard C-File API found in file.h

Best Regards,

Francisco
0 (0 Stimmen)