Other Parts Discussed in Thread: MSP430FR5992
Hi TI team,
Can you provide me a code that can convert the RTC time from calendar to epoch time format. If you have any code for that can you please provide me with that resource.
Regards
Abhisek
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.
Hi TI team,
Can you provide me a code that can convert the RTC time from calendar to epoch time format. If you have any code for that can you please provide me with that resource.
Regards
Abhisek
Hi Abhisek
May I check what you need (epoch time format) is the current time for example, get the time stamp over serial?
Thanks
Hi Xiaodong,
Here IMX8 processor is the master and mspm0 is slave. So imx8 will send epoch time through Spi protocol. Our msp will take that epoch time and store it in rtc registers and when imx will request to read updated epoch data, then our msp will take value from the rtc registers, convert it to epoch format and will send the updated epoch time to imx8.
Regards
Abhisek
Hi,
Previously I have written the same logic for msp430fr5992.There mktime() and localtime() functions there. But here are there any functions?
Regards
Abhisek
Hi Abhisek
Could you please share mktime() and localtime() functions on msp430fr5992.
it is helpful for me to understand the detailed.
Thanks
Hi ,
I will attach the time.h file.
/*****************************************************************************/
/* time.h */
/* */
/* Copyright (c) 1990 Texas Instruments Incorporated */
/* http://www.ti.com/ */
/* */
/* 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. */
/* */
/*****************************************************************************/
#ifndef _TIME_H_
#define _TIME_H_
#include <_ti_config.h>
#include <abi_prefix.h>
#if __has_include(<sys/_types.h>)
#include <sys/_types.h>
#endif
#if __has_include(<sys/timespec.h>)
#include <sys/timespec.h>
#endif
#if defined(__TMS320C2000__)
#if defined(__TMS320C28XX_CLA__)
#error "Header file <time.h> not supported by CLA compiler"
#endif
#endif
#ifndef NULL
#define NULL 0
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* Use sys/_types.h for clock_t and time_t if it exists */
#if __has_include(<sys/_types.h>)
#ifndef _CLOCK_T_DECLARED
typedef __clock_t clock_t;
#define _CLOCK_T_DECLARED
#endif
#ifndef _TIME_T_DECLARED
typedef __time_t time_t;
#define _TIME_T_DECLARED
#endif
typedef __int64_t __time64_t;
typedef __uint32_t __time32_t;
#ifndef _SIZE_T_DECLARED
#define _SIZE_T_DECLARED
#ifdef __clang__
typedef __SIZE_TYPE__ size_t;
#else
typedef __size_t size_t;
#endif
#endif
#else
#if (!defined(__C6X_MIGRATION__) && (defined(_TMS320C6X) || defined(__TMS320C6X__))) || defined(__ARM_ARCH)
typedef unsigned int clock_t;
typedef unsigned int __time32_t;
#else
typedef unsigned long clock_t;
typedef unsigned long __time32_t;
#endif /* int is 32 bits */
typedef long long __time64_t;
#if defined(__C7000__) || (defined(__TMS320C2000__) && defined(__TI_EABI__))
#define _TARGET_DEFAULTS_TO_TIME64
#endif
#if defined(_TARGET_DEFAULTS_TO_TIME64) || \
(defined(__TI_TIME_USES_64) && __TI_TIME_USES_64)
typedef __time64_t time_t;
#else
typedef __time32_t time_t;
#endif
#ifndef _SIZE_T_DECLARED
#define _SIZE_T_DECLARED
#ifdef __clang__
typedef __SIZE_TYPE__ size_t;
#else
typedef __SIZE_T_TYPE__ size_t;
#endif
#endif
#endif
/*---------------------------------------------------------------------------*/
/* The ARM ABI says __aeabi_CLOCKS_PER_SEC is a const int. C99 says that */
/* CLOCKS_PER_SEC is a constant of type clock_t. Therefore on ARM we define */
/* type of __clocks_per_sec_t as int and for all other targets it is clock_t */
/*---------------------------------------------------------------------------*/
#if defined(__ARM_ARCH)
typedef int __clocks_per_sec_t;
#else
typedef clock_t __clocks_per_sec_t;
#endif
extern _DATA_ACCESS const __clocks_per_sec_t __TI_P(CLOCKS_PER_SEC);
#define CLOCKS_PER_SEC __TI_P(CLOCKS_PER_SEC)
struct tm
{
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours after the midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since Jan 1st - [0,365] */
int tm_isdst; /* Daylight Savings Time flag */
#if defined(_AEABI_PORTABILITY_LEVEL) && _AEABI_PORTABILITY_LEVEL != 0
int __extra_1, __extra_2; /* ABI-required extra fields */
#endif
};
/*************************************************************************/
/* TIME ZONE STRUCTURE DEFINITION */
/*************************************************************************/
typedef struct
{
short daylight;
long timezone; /* seconds WEST of UTC. Strange but traditional */
char tzname[4];
char dstname[4];
} TZ;
extern _DATA_ACCESS TZ _tz;
/****************************************************************************/
/* FUNCTION DECLARATIONS. (NOTE : clock AND time ARE SYSTEM SPECIFIC) */
/****************************************************************************/
_CODE_ACCESS clock_t clock(void);
_CODE_ACCESS __time32_t __time32(__time32_t *_timer);
_CODE_ACCESS __time32_t __mktime32(struct tm *_tptr);
_CODE_ACCESS double __difftime32(__time32_t _time1, __time32_t _time0);
_IDECL char *__ctime32(const __time32_t *_timer);
_CODE_ACCESS struct tm *__gmtime32(const __time32_t *_timer);
_CODE_ACCESS struct tm *__localtime32(const __time32_t *_timer);
_CODE_ACCESS __time64_t __time64(__time64_t *_timer);
_CODE_ACCESS __time64_t __mktime64(struct tm *_tptr);
_CODE_ACCESS double __difftime64(__time64_t _time1, __time64_t _time0);
_IDECL char *__ctime64(const __time64_t *_timer);
_CODE_ACCESS struct tm *__gmtime64(const __time64_t *_timer);
_CODE_ACCESS struct tm *__localtime64(const __time64_t *_timer);
_CODE_ACCESS char *asctime(const struct tm *_timeptr);
_CODE_ACCESS size_t strftime(char * __restrict _out, size_t _maxsize,
const char * __restrict _format,
const struct tm * __restrict _timeptr);
#if defined(_INLINE) || defined(_CTIME32_IMPLEMENTATION)
_IDEFN char *__ctime32(const __time32_t *timer)
{
return asctime(__localtime32(timer));
}
#endif /* _INLINE || _CTIME32_IMPLEMENTATION */
#if defined(_INLINE) || defined(_CTIME64_IMPLEMENTATION)
_IDEFN char *__ctime64(const __time64_t *timer)
{
return asctime(__localtime64(timer));
}
#endif /* _INLINE || _CTIME64_IMPLEMENTATION */
/*-----------------------------------------------------------------------*/
/* The user may define __TI_TIME_USES_64=1 to redirect all time */
/* functions to time64 functions. */
/*-----------------------------------------------------------------------*/
#if defined(_TIME_IMPLEMENTATION) && defined(__TI_TIME_USES_64) && __TI_TIME_USES_64
#error "Do not build the RTS with __TI_TIME_USES_64 set"
#elif defined(__TI_TIME_USES_64) && __TI_TIME_USES_64
static __inline time_t time(time_t *timer) { return __time64(timer); }
static __inline time_t mktime(struct tm *tptr) { return __mktime64(tptr); }
static __inline double difftime(time_t time1, time_t time0) { return __difftime64(time1, time0); }
static __inline char *ctime(const time_t *timer) { return __ctime64(timer); }
static __inline struct tm *gmtime(const time_t *timer) { return __gmtime64(timer); }
static __inline struct tm *localtime(const time_t *timer) { return __localtime64(timer); }
#else
_CODE_ACCESS time_t time(time_t *_timer);
_CODE_ACCESS time_t mktime(struct tm *_tptr);
_CODE_ACCESS double difftime(time_t _time1, time_t _time0);
_IDECL char *ctime(const time_t *_timer);
_CODE_ACCESS struct tm *gmtime(const time_t *_timer);
_CODE_ACCESS struct tm *localtime(const time_t *_timer);
#if defined(_INLINE) && !defined(_TIME_IMPLEMENTATION)
_IDEFN char *ctime(const time_t *timer)
{
return asctime(localtime(timer));
}
#endif /* _INLINE */
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
/*---------------------------------------------------------------------------*/
/* A header file conforming to ARM CLIB ABI (GENC-003539) should */
/* define _AEABI_PORTABLE when _AEABI_PORTABILITY_LEVEL is defined. */
/*---------------------------------------------------------------------------*/
#if defined(_AEABI_PORTABILITY_LEVEL) && _AEABI_PORTABILITY_LEVEL != 0 && \
!defined(_AEABI_PORTABLE)
#define _AEABI_PORTABLE
#endif
/*----------------------------------------------------------------------------*/
/* If sys/cdefs.h is available, go ahead and include it. xlocale.h assumes */
/* this file will have already included sys/cdefs.h. */
/*----------------------------------------------------------------------------*/
#if __has_include(<sys/cdefs.h>)
#include <sys/cdefs.h>
#endif
/*----------------------------------------------------------------------------*/
/* Include xlocale/_time.h if POSIX is enabled. This will expose the */
/* xlocale time interface. */
/*----------------------------------------------------------------------------*/
#if (defined(__POSIX_VISIBLE) && __POSIX_VISIBLE >= 200809) || defined(_XLOCALE_H_)
__BEGIN_DECLS
#include <xlocale/_time.h>
__END_DECLS
#endif
#endif /* _TIME_H_ */
And this how I implemented mktime() and localtime() in my code
/**
* @brief rtcEpochToReadable() - Convert the epoch time in seconds to calendar format and assigning it to RTC registers
*/
void rtcEpochToReadable()
{
uint8_t rtc_copy_index;
// Assigning the values of SPIRx array to RTCEpochData array
for(rtc_copy_index = RTC_COPY_INDEX_START ; rtc_copy_index < RTC_COPY_INDEX_END ; rtc_copy_index++)
{
RTCEpochData[rtc_copy_index] = SPIRxData[rtc_copy_index + RTC_OFFSET_INDEX];
}
// Converting the RTCEpochData[led_control_index] array to unsigned 32 bit variable i.e. epoch_time
epoch_time |= (unsigned long)RTCEpochData[RTC_EPOCH_DATA_FIRSTBYTE] << SHIFT_BY_TWENTYFOUR;
epoch_time |= (unsigned long)RTCEpochData[RTC_EPOCH_DATA_SECONDBYTE] << SHIFT_BY_SIXTEEN;
epoch_time |= (unsigned long)RTCEpochData[RTC_EPOCH_DATA_THIRDBYTE] << SHIFT_BY_EIGHT;
epoch_time |= (unsigned long)RTCEpochData[RTC_EPOCH_DATA_FOURTHBYTE];
struct tm *time_info;
time_info = localtime(&epoch_time); //epoch to structure format Calendar/
//MSP430 configuring RTC and storing time received
RTCCTL0_H = RTCKEY_H; // Unlock RTC
RTCCTL13 = RTCHOLD | RTCMODE; // RTC enable, RTC hold
// Assigning the epoch time to RTC registers
RTCSEC = time_info->tm_sec; //Seconds
RTCMIN = time_info->tm_min; //Minutes
RTCHOUR = time_info->tm_hour; //Hour
RTCDAY = time_info->tm_mday; // Day of month
RTCMON = 1+time_info->tm_mon; // months since jan
RTCYEAR = time_info->tm_year; // years since 1970
RTCCTL13 &= ~RTCHOLD; // release RTC module
}
/**
* @brief rtcReadableToEpoch() - Extract the updated value from RTC registers and return the updated current epoch time in hexadecimal format
*/
void rtcReadableToEpoch()
{
uint8_t rtc_index;
struct tm rtc_time = {
.tm_sec = RTCSEC ,
.tm_min = RTCMIN ,
.tm_hour = RTCHOUR,
.tm_mday = RTCDAY,
.tm_mon = RTCMON - 1,
.tm_year = RTCYEAR,
};
epoch_current = mktime(&rtc_time); // Getting the current epoch_time
for ( rtc_index = RTC_READ_TO_EPOCH_START_INDEX ; rtc_index < RTC_EPOCH_DATA_SIZE ; rtc_index++)
{
epoch_transmit_array[RTC_OFFSET_INDEX - rtc_index] = (epoch_current>> (SHIFT_BY_EIGHT*rtc_index)) & RTC_DUMMY; // Assigning the current epoch_time of 32 bit to epoch_transmit_array[l] for transmitting to master
}
}
Hi Abhisek
I can see there is "#if defined(__ARM_ARCH)"
#if defined(__TMS320C2000__)
#if defined(__TMS320C28XX_CLA__)
May I confirm if this code is based on msp430fr5992?
Thanks
Hi,
This is working in msp430fr5992. This time.h file is located in C:\ti\ccs1240\ccs\tools\compiler\ti-cgt-msp430_21.6.1.LTS\include\time.h.
I have tested this code also. The mktime() and localtime() function I implemented in my code is working. Please also go through how I implemented these functions in my code.
Regards
Abhisek
Hi Abhisek
I can't find the epoch converter code on mspm0 SDK
May I know if you can update your code from msp430fr5992 to mspm0?
Thanks!
Hi Xiaodong,
Thats the main thing I am asking. Do you know any logic how to convert from calendar format to epoch format and vice versa in any controller without using mktime() and localtime() functions?
Regards
Abhisek
Hi Abhisek
I checked the timer.h ccs1230\ccs\tools\compiler\ti-cgt-armllvm_2.1.3.LTS\include\c
There are mktime() and localtime() functions as well.
I am not familiar with this logic.
could you please check if these 2 functions (usage) and this timer.h is what you needed?
Thanks