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.

CC1310: Get current Epoch seconds on using USB/UART (custom hardware)

Part Number: CC1310


Good afternoon,

I am doing a project where I have a custom PCB. This PCB gets connected to a PC once, retreives the time and sets the RTC with the received seconds. Then the PCB gets disconnected from the PC and connected to a DC source. During the reconnecting of the power a small battery will keep the system running. This means that I want my code to only need to set the time once and then keep the correct time going using the RTC.

I have the following code, copied from https://e2e.ti.com/support/wireless-connectivity/sub-1-ghz-group/sub-1-ghz/f/sub-1-ghz-forum/1010813/launchxl-cc1312r1-how-to-use-rtc-to-keep-track-of-updated-current-time and and modified for my PCB. This code works, but as you can see in line 57 the seconds are hardcoded. What I want is for the software to get the time from the PC using USB/UART.

#include <unistd.h>
#include <stdint.h>
#include <stddef.h>

#include <ti/display/Display.h>
#include <ti/display/DisplayUart.h>
#include <ti/display/DisplayExt.h>
#include <ti/display/AnsiColor.h>
#include <ti/drivers/UART.h>
#include "Board.h"
#include <ti/drivers/GPIO.h>
/* Driver configuration */
//#include "ti_drivers_config.h"

#include <ti/sysbios/hal/Seconds.h>

#include <xdc/runtime/System.h>
#include <time.h>

static uint32_t t;
static time_t t1;
static struct tm *ltm;
static char *curTime1;

void *mainThread(void *arg0)
{
    GPIO_init();

    UART_Handle uart;
      UART_Params uartParams;

      /* Call driver init functions */
      GPIO_setConfig(Board_GPIO_RLED, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW );
          GPIO_toggle(Board_GPIO_RLED);
          CPUdelay(8000 * (100));
          GPIO_toggle(Board_GPIO_RLED);
          CPUdelay(8000 * (100));

          UART_init();

      /* Create a UART with data processing off. */
      UART_Params_init(&uartParams);
      uartParams.writeDataMode = UART_DATA_TEXT;
      uartParams.baudRate = 4800;

      uart = UART_open(Board_UART0, &uartParams);


      GPIO_toggle(Board_GPIO_RLED);
      CPUdelay(8000 * (100));
              GPIO_toggle(Board_GPIO_RLED);
              CPUdelay(8000 * (100));




   Seconds_set(1652875134);

    while (1) {

        CPUdelay(8000 * (10000));

        t = Seconds_get();
        t1 = time(NULL);
        ltm = localtime(&t1);
        curTime1 = asctime(ltm);


        UART_write(uart, curTime1, 50);
        GPIO_toggle(Board_GPIO_RLED);
     //   System_printf("Time(GMT): %s\n", curTime1);
    }
}

I tried using the time.h library to test with the following code:

 time_t t = time(NULL);
 struct tm tm = *localtime(&t);

 this only works on my CC1310 launchpad and not on my own hardware. (My hardware uses the CH340N usb to uart IC).

How can I get the local Epoch time from the connected PC using UART?

Thanks in advance!

Kind regards,

Mirte H

  • Hi Mirte,

    Regarding below question. I hope I am understanding your question correctly. I would assume that you need an application running on the PC. You device could send a command over UART asking for the time. The PC application should upon receiving the command from the device, send the epoch time, and the device would read the time received over UART.

    There are of course many ways to achieve this, but above is one suggestion. 

    How can I get the local Epoch time from the connected PC using UART?

    I have a question regarding below statement. Are you referring to the two lines of code above the statement or the (73 line) code in your the code block? What exactly is not working on your own hardware, and what is the error?

    this only works on my CC1310 launchpad and not on my own hardware. (My hardware uses the CH340N usb to uart IC).

    Thanks,

    Nikolaj

  • Hi Nikolaj,

    No, I don't think I need an application running on the PC, I think I stressed the UART part too much which makes the question confusing.

    I want to retrieve the system time from a PC in C, and the only way my PCB is connected to a PC is with a USB cable using a CH340N driver. 

     

    I have a question regarding below statement. Are you referring to the two lines of code above the statement or the (73 line) code in your the code block? What exactly is not working on your own hardware, and what is the error?

    The following lines of code:

     time_t t = time(NULL);
     struct tm tm = *localtime(&t);

    Don't give me an error or anything on my hardware, it runs fine, but it returns januari first 1970, so basically 0.

    The code block I posted works, because I'm setting the seconds manually. But in my system I need to receive these seconds from the system time of the connected PC.

    I don't know if i'm right, but I'm thinking perhaps the Windows.H library might be another option, do you know how I can import this in CCS?
    (Or perhaps I'm wrong/you have a better idea , either way: I'm all ears! Slight smile)

    Kind regards,

    Mirte H
  • Hi Mirte,

    I want to retrieve the system time from a PC in C

    If you want to retrieve the system time of the PC, you need an application of some sorts running on the PC. This application can be written in C, if that is what you desire. You cannot retrieve the PC system time without having any app running on PC to retrieve the system time. Once the PC application has retrieved the time, it can send it to the device on your board (via USB/UART).

    don't know if i'm right, but I'm thinking perhaps the Windows.H library might be another option, do you know how I can import this in CCS?

    You can probably use the windows.h library in the PC application to retrieve the time. This is not something you can import to the embedded project in CCS. 

    Once the application running on the CC1310 has received the time from the PC, it can set the time using Seconds_set (as you currently do on line 57, but replace the hard coded value with the value received from the PC) 

    If you want a quick and easy proof of concept I would suggest using python for the PC application. For example you can use pySerial to communicate with your device, and you can use the time library to retrieve the time: https://docs.python.org/3/library/time.html#time.time 

    Regards,
    Nikolaj

  • Hi Nikolaj,

    Loud and clear! Thanks for the great answer Slight smile

    I am now starting to try and get data from UART using realterm on my CC1310 Launchpad using the uartecho_nortos example.

    This only reads and writes chars. do you have any idea how I can change this to receive and write integers (in my case it will be the epoch time)?

    I've tried so many things but nothing works, because if i change input to anything but a char it won't work.

    I put a code snippit, (which obviously doesn't work), below so you can get an idea of what i'm trying to do. If you can't help me I will close this thread and create a new one for this question.

    /*
     * Copyright (c) 2015-2019, 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>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART.h>
    
    /* Example/Board Header files */
    #include "Board.h"
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        char        input;
        const char  echoPrompt[] = "Echoing characters:\r\n";
        UART_Handle uart;
        UART_Params uartParams;
    uint32_t received;
        /* Call driver init functions */
        GPIO_init();
        UART_init();
    
        /* Configure the LED pin */
        GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    
        /* Turn on user LED */
        GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
    
        /* 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(Board_UART0, &uartParams);
    
        if (uart == NULL) {
            /* UART_open() failed */
            while (1);
        }
    
        UART_write(uart, echoPrompt, sizeof(echoPrompt));
    
        /* Loop forever echoing */
        while (1) {
            UART_read(uart, &input, 1);
            UART_write(uart, &input, 1);
            received = &input;
            Seconds_set(received);
            // etc..
        }
    }
    

    Thanks again!

    Mirte H

  • Hi Mirte,

    I've tried so many things but nothing works, because if i change input to anything but a char it won't work.

    The buffer argument of UART_read/UART_write indeed has to be a pointer to a char. That is why you can also point to the first element of a char array.

    This only reads and writes chars. do you have any idea how I can change this to receive and write integers (in my case it will be the epoch time)?

    You will have to send/receive your epoch time through the serial line as multiple bytes/char, and turn it back into an integer once you received all of its parts.

    I invite you to look at the "size" argument of UART read in order to achieve that.

    BR,

    Arthur

  • Thanks for the nice explanation Arthur! I managed to make my code work by looking at the received value byte by byte, and then shifting those bytes back into my integer Slight smile