From 1662e647a4c4ea8cf996a611662515623787037f Mon Sep 17 00:00:00 2001 From: alistairjordan <> Date: Mon, 25 Dec 2023 21:24:28 +0000 Subject: [PATCH] update systime.c --- .../board/lorawan-bridge/LoraMac/rtc-board.h | 192 ++++++++++++++++++ .../board/lorawan-bridge/LoraMac/systime.c | 28 +++ 2 files changed, 220 insertions(+) create mode 100644 sysdrv/tools/board/lorawan-bridge/LoraMac/rtc-board.h diff --git a/sysdrv/tools/board/lorawan-bridge/LoraMac/rtc-board.h b/sysdrv/tools/board/lorawan-bridge/LoraMac/rtc-board.h new file mode 100644 index 0000000000..8fa38a692b --- /dev/null +++ b/sysdrv/tools/board/lorawan-bridge/LoraMac/rtc-board.h @@ -0,0 +1,192 @@ +/*! + * \file rtc-board.h + * + * \brief Target board RTC timer and low power modes management + * + * \copyright Revised BSD License, see section \ref LICENSE. + * + * \code + * ______ _ + * / _____) _ | | + * ( (____ _____ ____ _| |_ _____ ____| |__ + * \____ \| ___ | (_ _) ___ |/ ___) _ \ + * _____) ) ____| | | || |_| ____( (___| | | | + * (______/|_____)_|_|_| \__)_____)\____)_| |_| + * (C)2013-2017 Semtech + * + * \endcode + * + * \author Miguel Luis ( Semtech ) + * + * \author Gregory Cristian ( Semtech ) + */ +#ifndef __RTC_BOARD_H__ +#define __RTC_BOARD_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include +#include +#include "timer.h" + +/*! + * \brief Temperature coefficient of the clock source + */ +#define RTC_TEMP_COEFFICIENT ( -0.035f ) + +/*! + * \brief Temperature coefficient deviation of the clock source + */ +#define RTC_TEMP_DEV_COEFFICIENT ( 0.0035f ) + +/*! + * \brief Turnover temperature of the clock source + */ +#define RTC_TEMP_TURNOVER ( 25.0f ) + +/*! + * \brief Turnover temperature deviation of the clock source + */ +#define RTC_TEMP_DEV_TURNOVER ( 5.0f ) + +/*! + * \brief Initializes the RTC timer + * + * \remark The timer is based on the RTC + */ +void RtcInit( void ); + +/*! + * \brief Returns the minimum timeout value + * + * \retval minTimeout Minimum timeout value in in ticks + */ +uint32_t RtcGetMinimumTimeout( void ); + +/*! + * \brief converts time in ms to time in ticks + * + * \param[IN] milliseconds Time in milliseconds + * \retval returns time in timer ticks + */ +uint32_t RtcMs2Tick( TimerTime_t milliseconds ); + +/*! + * \brief converts time in ticks to time in ms + * + * \param[IN] time in timer ticks + * \retval returns time in milliseconds + */ +TimerTime_t RtcTick2Ms( uint32_t tick ); + +/*! + * \brief Performs a delay of milliseconds by polling RTC + * + * \param[IN] milliseconds Delay in ms + */ +void RtcDelayMs( TimerTime_t milliseconds ); + +/*! + * \brief Sets the alarm + * + * \note The alarm is set at now (read in this funtion) + timeout + * + * \param timeout [IN] Duration of the Timer ticks + */ +void RtcSetAlarm( uint32_t timeout ); + +/*! + * \brief Stops the Alarm + */ +void RtcStopAlarm( void ); + +/*! + * \brief Starts wake up alarm + * + * \note Alarm in RtcTimerContext.Time + timeout + * + * \param [IN] timeout Timeout value in ticks + */ +void RtcStartAlarm( uint32_t timeout ); + +/*! + * \brief Sets the RTC timer reference + * + * \retval value Timer reference value in ticks + */ +uint32_t RtcSetTimerContext( void ); + +/*! + * \brief Gets the RTC timer reference + * + * \retval value Timer value in ticks + */ +uint32_t RtcGetTimerContext( void ); + +/*! + * \brief Gets the system time with the number of seconds elapsed since epoch + * + * \param [OUT] milliseconds Number of milliseconds elapsed since epoch + * \retval seconds Number of seconds elapsed since epoch + */ +uint32_t RtcGetCalendarTime( uint16_t *milliseconds ); + +/*! + * \brief Get the RTC timer value + * + * \retval RTC Timer value + */ +uint32_t RtcGetTimerValue( void ); + +/*! + * \brief Get the RTC timer elapsed time since the last Alarm was set + * + * \retval RTC Elapsed time since the last alarm in ticks. + */ +uint32_t RtcGetTimerElapsedTime( void ); + +/*! + * \brief Writes data0 and data1 to the RTC backup registers + * + * \param [IN] data0 1st Data to be written + * \param [IN] data1 2nd Data to be written + */ +void RtcBkupWrite( uint32_t data0, uint32_t data1 ); + +/*! + * \brief Reads data0 and data1 from the RTC backup registers + * + * \param [OUT] data0 1st Data to be read + * \param [OUT] data1 2nd Data to be read + */ +void RtcBkupRead( uint32_t* data0, uint32_t* data1 ); + +/*! + * \brief Processes pending timer events + */ +void RtcProcess( void ); + +/*! + * \brief Computes the temperature compensation for a period of time on a + * specific temperature. + * + * \param [IN] period Time period to compensate in milliseconds + * \param [IN] temperature Current temperature + * + * \retval Compensated time period + */ +TimerTime_t RtcTempCompensation( TimerTime_t period, float temperature ); + +/*! + * \brief Processes pending timer events + */ +void RtcProcess( void ); + +#ifdef __cplusplus +} +#endif + +#endif // __RTC_BOARD_H__ diff --git a/sysdrv/tools/board/lorawan-bridge/LoraMac/systime.c b/sysdrv/tools/board/lorawan-bridge/LoraMac/systime.c index 9f1ffaf1b7..d44d435aed 100644 --- a/sysdrv/tools/board/lorawan-bridge/LoraMac/systime.c +++ b/sysdrv/tools/board/lorawan-bridge/LoraMac/systime.c @@ -1,6 +1,25 @@ #include +#include +#include #include "systime.h" +SysTime_t SysTimeGet( void ) { + struct timespec time_local; + SysTime_t ret; + clock_gettime(CLOCK_REALTIME, &time_local); + ret.Seconds = time_local.tv_sec; + ret.SubSeconds = time_local.tv_nsec / 1000; + return ret; +} + +void SysTimeSet( SysTime_t time_local ) { +// Hmmmm, shall we really ne doing this?? + printf("Should we really be setting system time?"); + struct timespec time_out; + time_out.tv_sec = time_local.Seconds; + time_out.tv_nsec = time_local.SubSeconds * 1000; +} + SysTime_t SysTimeAdd( SysTime_t a, SysTime_t b) { SysTime_t ret; ret.Seconds = (a.Seconds + b.Seconds); @@ -13,4 +32,13 @@ SysTime_t SysTimeSub( SysTime_t a, SysTime_t b) { ret.Seconds = (a.Seconds - b.Seconds); ret.SubSeconds = (a.SubSeconds - b.SubSeconds); return ret; +} + +SysTime_t SysTimeGetMcuTime( void ) { + struct timespec time_mcu; + SysTime_t ret; + clock_gettime(CLOCK_REALTIME, &time_mcu); + ret.Seconds = time_mcu.tv_sec; + ret.SubSeconds = time_mcu.tv_nsec / 1000; + return ret; } \ No newline at end of file