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