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.

CCS/LP-CC2652RB: Unable to write data in file using message queue

Part Number: LP-CC2652RB


Tool/software: Code Composer Studio

Hi, I am trying to implement message queue to receive data from another microcontroller to CC2652 Launchpad. I want to receive and write data to sd card. For this i am following lab exercise given in the link below.

This lab exercise i have modified according to my application. I am turning an LED on after receiving '\n' character. 

Next i want to write an array of character received from the Uart into the SD card. But i am getting errors. One error is that i am not able to access the incoming character variable in the alarm.c file. Where do i declare it so that i use it across all files. I am attaching all the three edited files i.e. Uartecho.c , main_tirtos & alarm.c .

Please help me, i am really stuck in it.

Thanks,
shivam

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

/*
 *  ======== uartecho.c ========
 */
#include <stdint.h>
#include <stddef.h>
#include <mqueue.h>

/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/UART.h>

/* Driver configuration */
#include "ti_drivers_config.h"
char        input;

/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{

    const char  echoPrompt[] = "Echoing characters:\r\n";
    UART_Handle uart;
    UART_Params uartParams;
    mqd_t      *mqdes = arg0;
    int         msg;

    /* Call driver init functions */
    //GPIO_init();
    UART_init();

    /* Configure the LED pin */
   // GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

    /* Create a UART with data processing off. */
    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readReturnMode = UART_RETURN_FULL;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.baudRate = 115200;

    uart = UART_open(CONFIG_UART_0, &uartParams);

    if (uart == NULL) {
        /* UART_open() failed */
        while (1);
    }

    /* Turn on user LED to indicate successful initialization */
    //GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);

    UART_write(uart, echoPrompt, sizeof(echoPrompt));

    /* Loop forever echoing */
    while (1) {
        UART_read(uart, &input, 1);
        if (input == '\n')
        {
          msg = 5;
          mq_send(*mqdes , (char *)&msg, sizeof(msg), 0);
        }
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_OFF);
        UART_write(uart, &input, 1);
    }
}
/*
 * Copyright (c) 2016-2020, 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.
 */

/*
 *  ======== main_tirtos.c ========
 */
#include <stdint.h>

/* POSIX Header files */
#include <pthread.h>
#include <mqueue.h>

/* RTOS header files */
#include <ti/sysbios/BIOS.h>

#include <ti/drivers/Board.h>


#include <ti/drivers/GPIO.h>
#include "ti_drivers_config.h"

extern void *mainThread(void *arg0);
extern void *alarmThread(void *arg0);

/* Stack size in bytes */
#define THREADSTACKSIZE    1024
#define MSG_SIZE sizeof(int)
#define MSG_NUM  4

mqd_t mqdes;

/*
 *  ======== main ========
 */
int main(void)
{
    pthread_t           thread;
    pthread_t           alarm;
    pthread_attr_t      attrs;
    struct sched_param  priParam;
    int                 retc;
    struct mq_attr      mqAttrs;



    Board_init();
    GPIO_init();

    GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

    GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_OFF);

    mqAttrs.mq_maxmsg = MSG_NUM;
    mqAttrs.mq_msgsize = MSG_SIZE;
    mqAttrs.mq_flags = 0;
    mqdes = mq_open ("alarm", O_RDWR | O_CREAT,
                    0664, &mqAttrs);
    if (mqdes == (mqd_t)-1) {
      /* mq_open() failed */
      while (1);
    }

    /* Initialize the attributes structure with default values */
    pthread_attr_init(&attrs);

    /* Set priority, detach state, and stack size attributes */
    priParam.sched_priority = 1;
    retc = pthread_attr_setschedparam(&attrs, &priParam);
    retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
    retc |= pthread_attr_setstacksize(&attrs, THREADSTACKSIZE);
    if (retc != 0) {
        /* failed to set attributes */
        while (1) {}
    }

    retc = pthread_create(&thread, &attrs, mainThread, (void *)&mqdes);
    if (retc != 0) {
        /* pthread_create() failed */
        while (1) {}
    }

    /*
    *  Make the alarm thread a higher priority.
    */
    priParam.sched_priority = 2;
    pthread_attr_setschedparam(&attrs, &priParam);

    retc = pthread_create(&alarm, &attrs, alarmThread, (void *)&mqdes);
    if (retc != 0) {
      /* pthread_create() failed */
      while (1);
    }

    BIOS_start();

    return (0);
}
/*
 *  ======== alarm.c ========
 */
#include <file.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <mqueue.h>
#include <third_party/fatfs/ffcio.h>
#include <ti/drivers/SDFatFS.h>

/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Clock.h>

/* Driver configuration */
#include "ti_drivers_config.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
const char inputfile[] = "fat:"STR(DRIVE_NUM)":input.txt";
const char outputfile[] = "fat:"STR(DRIVE_NUM)":output.txt";
//const char textarray[] ="Hello from shivam";
//static Display_Handle display;
/* Set this to the current UNIX time in seconds */
const struct timespec ts = {
    .tv_sec = 1469647026,
    .tv_nsec = 0
};
/* File name prefix for this filesystem for use with TI C RTS */
char fatfsPrefix[] = "fat";
unsigned char cpy_buff[CPY_BUFF_SIZE];

/*
 *  ======== alarmThread ========
 */
void *alarmThread(void *arg0)
{
    mqd_t *mqdes = arg0;
    int msg;
    UInt32 sleepTickCount;
    sleepTickCount = 100000 / Clock_tickPeriod;

    while (mq_receive(*mqdes, (char *)&msg, sizeof(msg), NULL) != -1) {
        /* Turn on user LED */
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
        Task_sleep(sleepTickCount);
        SDFatFS_Handle sdfatfsHandle;
                            FILE *src;
                            SDFatFS_init();
                            add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
                                             ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
                            /* Initialize real-time clock */
                                         clock_settime(CLOCK_REALTIME, &ts);
                            sdfatfsHandle = SDFatFS_open(CONFIG_SDFatFS_0, DRIVE_NUM);
                                             /* Open file for both reading and writing */
                                             src = fopen(inputfile, "w+");
                                             fseek(src,0,SEEK_END);
                                             fwrite(&input, 1, 1, src);
                                         /* Close  inputfile[]  */
                                         fclose(src);
                                         /* Stopping the SDCard */
                                         SDFatFS_close(sdfatfsHandle);
    }

    return(0);
}

/*
 *  ======== fatfs_getFatTime ========
 */
int32_t fatfs_getFatTime(void)
{
    time_t seconds;
    uint32_t fatTime;
    struct tm *pTime;

    /*
     *  TI time() returns seconds elapsed since 1900, while other tools
     *  return seconds from 1970.  However, both TI and GNU localtime()
     *  sets tm tm_year to number of years since 1900.
     */
    seconds = time(NULL);

    pTime = localtime(&seconds);

    /*
     *  localtime() sets pTime->tm_year to number of years
     *  since 1900, so subtract 80 from tm_year to get FAT time
     *  offset from 1980.
     */
    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);
}

  • Hi,

    SHIVAM TRIVEDI said:
    Where do i declare it so that i use it across all files.

    This is a C language question, for what you can find several references around on the internet. A search yields several results such as:

    http://www.cplusplus.com/forum/general/28060/

    http://www.cplusplus.com/forum/beginner/93361/

    Hope this helps,

    Rafael

  • Hi desouza, Thanks for your response.

    I have declared the char input variable as extern global. However, now a problem is coming that as the program receives '\n'  . The code gets stuck as shown in the image below. That is the code is not executing the file write portion in alarm.c file whereas it is easily turning on the LED when i am only switching on the LED.

    Please refer the image and attached code.

      while (mq_receive(*mqdes, (char *)&msg, sizeof(msg), NULL) != -1) {
            /* Turn on user LED */
            GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
            Task_sleep(sleepTickCount);
            SDFatFS_Handle sdfatfsHandle;
                                FILE *src;
                                SDFatFS_init();
                                add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
                                                 ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
                                /* Initialize real-time clock */
                                             clock_settime(CLOCK_REALTIME, &ts);
                                sdfatfsHandle = SDFatFS_open(CONFIG_SDFatFS_0, DRIVE_NUM);
                                                 /* Open file for both reading and writing */
                                                 src = fopen(inputfile, "w+");
                                                 fseek(src,0,SEEK_END);
                                                 fwrite(&input, 1, 1, src);
                                             /* Close  inputfile[]  */
                                             fclose(src);
                                             /* Stopping the SDCard */
                                             SDFatFS_close(sdfatfsHandle);
        }

    Also i am getting these warnings as attached, what does these warnings mean?

  • The above problem is solved and i am able to receive continous data on serial. But only problem is data is not getting written in the file.

    Only file is created with no contents into it. Is it possible that contents are not getting written due to the input variable, the same input variable as is in the uart incoming character is not accessible into the alarm.c file.

    I am also able to write a dummy text into the sd file while continously receiving uart data on terminal. I am just one step away from my end goal. Please please help me how i can write the uart data into the sd card file. Please refer the attached code.

        while (mq_receive(*mqdes, (char *)&msg, sizeof(msg), NULL) != -1) {
            /* Turn on user LED */
            GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
            Task_sleep(sleepTickCount);
            SDFatFS_Handle sdfatfsHandle;
                                FILE *src;
                                SDFatFS_init();
                                add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
                                                 ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
                                /* Initialize real-time clock */
                                             clock_settime(CLOCK_REALTIME, &ts);
                                sdfatfsHandle = SDFatFS_open(CONFIG_SDFatFS_0, DRIVE_NUM);
                                                 /* Open file for both reading and writing */
                                                 src = fopen(inputfile, "w+");
                                                 //fseek(src,0,SEEK_END);
                                                 fwrite(&input, 1, 1, src);
                                                 fwrite(textarray, 1, strlen(textarray), src);
                                             /* Close  inputfile[]  */
                                             fclose(src);
                                             /* Stopping the SDCard */
                                             SDFatFS_close(sdfatfsHandle);
        }
    
        return(0);

    Thanks,

    shivam

  • Please reply on below query, else shall i open a separate thread for it?