<commit_before>/*
 * rtc-board.c
 *
 * Description: RTC callouts for generic hardware, required by timer.c
 * License: Revised BSD License, see LICENSE.TXT file include in the project
 *  Created on: Jun 25, 2016
 *      Author: <NAME> <<EMAIL>>
 *
*/


#include "board.h"
#include "timer.h"
#include <string.h>


#if BOARD_RTC_USE_POSIX_TIMER
#include "posix/interrupt-simulate-posix.c"
#include "posix/rtc-board-posix.c"
#else
#error "Please define BOARD_RTC_ macro based on contents in rtc-board.c or define a new one"
#endif

struct RtcAlarm
{
	bool	isSet;
	TimerTime_t	value;
	func_ptr_void interruptPtr;
};

struct RtcAlarm RtcAlarm;

void RTC_Alarm_IRQHandler( int unused )
{
	(*RtcAlarm.interruptPtr)();
}
void RTC_Alarm_IRQHandlerVoid( void )
{
	TimerIrqHandler();
}

void RtcInit(void)
{
	RtcResetStartupTimeReference();
	memset(&RtcAlarm,0,sizeof(RtcAlarm));
	RtcAlarm.interruptPtr=interrupt_simulate_map_next_irq(RTC_Alarm_IRQHandlerVoid);
}


TimerTime_t RtcGetTimerValue( void )
{
    return RtcGetTimeInMillisecondsSinceStartup();
}


/**
 * @param Delay time in milliseconds to wait inline
 */
void HAL_Delay(uint32_t Delay)
{
	TimerTime_t startTime=RtcGetTimerValue();
	bool exit=false;
	do
	{
		TimerTime_t currentTime=RtcGetTimerValue();
		if( currentTime - startTime < Delay )
		{
			BoardDelay();
		}
		else
		{
			exit=true;
		}
	}while( !exit );
}

void DelayMs(uint32_t Delay)
{
	HAL_Delay(Delay);
}

TimerTime_t RtcGetElapsedAlarmTime( void )
{
	TimerTime_t elapsedTime=0;
	if( RtcAlarm.isSet )
	{
		elapsedTime=RtcGetTimerValue()-RtcAlarm.value;
	}
	else
	{
		LOG(Error,"RtcGetElapsedAlarmTime called before RtcSetTimeout()");
	}
	return elapsedTime;
}

TimerTime_t RtcComputeFutureEventTime( TimerTime_t futureEventInTime )
{
    return( RtcGetTimerValue( ) + futureEventInTime );
}

TimerTime_t RtcComputeElapsedTime( TimerTime_t eventInTime )
{
	return RtcGetTimerValue()-eventInTime;
}




void RtcSetTimeout( uint32_t timeout )
{
	RtcAlarm.isSet=true;
	RtcAlarm.value=RtcComputeFutureEventTime(timeout);
	RtcStartWakeUpAlarm(timeout,RTC_Alarm_IRQHandler);
}

void RtcEnterLowPowerStopMode( void )
{

}

TimerTime_t RtcGetAdjustedTimeoutValue( uint32_t timeout )<commit_msg>Fix TimerGetValue()<commit_after>/*
 * rtc-board.c
 *
 * Description: RTC callouts for generic hardware, required by timer.c
 * License: Revised BSD License, see LICENSE.TXT file include in the project
 *  Created on: Jun 25, 2016
 *      Author: <NAME> <<EMAIL>>
 *
*/


#include "board.h"
#include "timer.h"
#include <string.h>


#if BOARD_RTC_USE_POSIX_TIMER
#include "posix/interrupt-simulate-posix.c"
#include "posix/rtc-board-posix.c"
#else
#error "Please define BOARD_RTC_ macro based on contents in rtc-board.c or define a new one"
#endif

struct RtcAlarm
{
	bool	isSet;
	TimerTime_t	setTimestamp;
	TimerTime_t	alarmTimestamp;
	func_ptr_void interruptPtr;
};

struct RtcAlarm RtcAlarm;

void RTC_Alarm_IRQHandler( int unused )
{
	(*RtcAlarm.interruptPtr)();
}
void RTC_Alarm_IRQHandlerVoid( void )
{
	TimerIrqHandler();
}

void RtcInit(void)
{
	RtcResetStartupTimeReference();
	memset(&RtcAlarm,0,sizeof(RtcAlarm));
	RtcAlarm.interruptPtr=interrupt_simulate_map_next_irq(RTC_Alarm_IRQHandlerVoid);
}


TimerTime_t RtcGetTimerValue( void )
{
    return RtcGetTimeInMillisecondsSinceStartup();
}


/**
 * @param Delay time in milliseconds to wait inline
 */
void HAL_Delay(uint32_t Delay)
{
	TimerTime_t startTime=RtcGetTimerValue();
	bool exit=false;
	do
	{
		TimerTime_t currentTime=RtcGetTimerValue();
		if( currentTime - startTime < Delay )
		{
			BoardDelay();
		}
		else
		{
			exit=true;
		}
	}while( !exit );
}

void DelayMs(uint32_t Delay)
{
	HAL_Delay(Delay);
}

TimerTime_t RtcGetElapsedAlarmTime( void )
{
	TimerTime_t elapsedTime=0;
	if( RtcAlarm.isSet )
	{
		TimerTime_t lastTime=RtcGetTimerValue();
		elapsedTime=lastTime-RtcAlarm.setTimestamp;
	}
	else
	{
		LOG(Error,"RtcGetElapsedAlarmTime called before RtcSetTimeout()");
	}
	return elapsedTime;
}

TimerTime_t RtcComputeFutureEventTime( TimerTime_t futureEventInTime )
{
    return( RtcGetTimerValue( ) + futureEventInTime );
}

TimerTime_t RtcComputeElapsedTime( TimerTime_t eventInTime )
{
	return RtcGetTimerValue()-eventInTime;
}




void RtcSetTimeout( uint32_t timeout )
{
	RtcAlarm.setTimestamp=RtcGetTimerValue();
	RtcAlarm.alarmTimestamp=RtcAlarm.setTimestamp+timeout;
	RtcAlarm.isSet=true;
	RtcStartWakeUpAlarm(timeout,RTC_Alarm_IRQHandler);
}

void RtcEnterLowPowerStopMode( void )
{

}

TimerTime_t RtcGetAdjustedTimeoutValue( uint32_t timeout )
{'fraction_non_alphanumeric': 0.10447432223936522, 'fraction_numerical': 0.007053118800969804, 'mean_word_length': 5.4005641748942175, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1660872', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1728, 'n_words': 330}