Skip to content

Commit

Permalink
Rework HAL time functions (#160)
Browse files Browse the repository at this point in the history
- see #159

Signed-off-by: José Simões <[email protected]>
  • Loading branch information
josesimoes authored Mar 9, 2017
1 parent ae1375b commit 7a465e7
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/CLR/Core/GarbageCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ CLR_UINT32 CLR_RT_GarbageCollector::ExecuteGarbageCollection()

if(s_CLR_RT_fTrace_MemoryStats >= c_CLR_RT_Trace_Info)
{
stats_start = HAL_Time_CurrentTicks();
stats_start = HAL_Time_CurrentSysTicks();
}
#endif

Expand All @@ -171,7 +171,7 @@ CLR_UINT32 CLR_RT_GarbageCollector::ExecuteGarbageCollection()
#if defined(NANOCLR_TRACE_MEMORY_STATS)
if(s_CLR_RT_fTrace_MemoryStats >= c_CLR_RT_Trace_Info)
{
int milliSec = (int)::HAL_Time_TicksToTimeMilliSec( HAL_Time_CurrentTicks() - stats_start );
int milliSec = ((int)::HAL_Time_SysTicksToTime( HAL_Time_CurrentSysTicks() - stats_start ) + TIME_CONVERSION__TICKUNITS - 1) / TIME_CONVERSION__TICKUNITS;

CLR_Debug::Printf( "GC: %dmsec %d bytes used, %d bytes available\r\n", milliSec, m_totalBytes - m_freeBytes, m_freeBytes );
}
Expand Down
2 changes: 1 addition & 1 deletion src/CLR/Include/WireProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct WP_Message
static const int CompletePayload = 6;
};

static const UINT64 c_PayloadTimeout = 6 * 1000; // 6 secs (from milliseconds time)
static const UINT32 c_PayloadTimeout = 60000000; // 6 secs (100 nsecs units)

WP_Controller* m_parent;
WP_Packet m_header;
Expand Down
9 changes: 4 additions & 5 deletions src/CLR/Include/nanoCLR_Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -2939,7 +2939,7 @@ struct CLR_RT_ExecutionEngine
{
if(m_recursion++ == 0)
{
m_start = HAL_Time_CurrentTicks();
m_start = HAL_Time_CurrentSysTicks();
}
}

Expand All @@ -2949,16 +2949,15 @@ struct CLR_RT_ExecutionEngine
{
if(--m_recursion == 0)
{
m_cumulative += (HAL_Time_CurrentTicks() - m_start);
m_cumulative += (HAL_Time_CurrentSysTicks() - m_start);
}
}
}

CLR_INT64 Adjust( CLR_INT64 time ) const
{
// need to 'convert' this from milliseconds to 100ns ticks so it won't break the code calling Adjust()
// FIXME: evaluate if the caller code can be adjusted to drop this workaround conversion
return time + ::HAL_Time_TicksToTimeMilliSec( m_cumulative ) / NANOHAL_TIME_CONVERSION_MICRO_TO_HUNDREDS_NANOSECONDS;
// FIXME: evaluate if the caller code can be adjusted to use SysTicks instead of 100ns ticks
return time + ::HAL_Time_SysTicksToTime( m_cumulative );
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/CLR/WireProtocol/WireProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ bool WP_Message::Process()
}
else
{
m_payloadTicks = HAL_Time_CurrentTicks();
m_payloadTicks = HAL_Time_CurrentSysTicks();
m_rxState = ReceiveState::ReadingPayload;
m_pos = (UINT8*)m_payload;
m_size = m_header.m_size;
Expand Down Expand Up @@ -268,12 +268,12 @@ bool WP_Message::Process()
{
TRACE0(TRACE_STATE, "RxState=ReadingPayload\n");

UINT32 curTicks = HAL_Time_CurrentTicks();
UINT64 curTicks = HAL_Time_CurrentSysTicks();

// If the time between consecutive payload bytes exceeds the timeout threshold then assume that
// the rest of the payload is not coming. Reinitialize to synch on the next header.

if(HAL_Time_TicksToTimeMilliSec(curTicks - m_payloadTicks) < c_PayloadTimeout)
if(HAL_Time_SysTicksToTime(curTicks - m_payloadTicks) < (UINT64)c_PayloadTimeout)
{
m_payloadTicks = curTicks;

Expand Down
2 changes: 1 addition & 1 deletion src/CLR/WireProtocol/WireProtocol_Message.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ bool WP_Message_Process(WP_Message* message)
}
else
{
m_payloadTicks = HAL_Time_CurrentTicks();
m_payloadTicks = HAL_Time_CurrentSysTicks();
message->m_rxState = ReceiveState_ReadingPayload;
message->m_pos = message->m_payload;
message->m_size = message->m_header.m_size;
Expand Down
3 changes: 1 addition & 2 deletions src/HAL/Include/nanoHAL_Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <targetHAL_Time.h>

#define NANOHAL_TIME_CONVERSION_MICRO_TO_SECONDS 1000000
#define NANOHAL_TIME_CONVERSION_MICRO_TO_HUNDREDS_NANOSECONDS 10000

//////////////////////////////////////////
// TODO delete these when working on #130
Expand All @@ -21,7 +20,7 @@ typedef unsigned int UINT32;
extern "C" {
#endif

UINT64 HAL_Time_TicksToTimeMilliSec(UINT32 Ticks);
UINT64 HAL_Time_SysTicksToTime(UINT32 sysTicks);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion targets/CMSIS-OS/ChibiOS/Include/targetHAL_Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

#include <cmsis_os.h>

#define HAL_Time_CurrentTicks osKernelSysTick
#define HAL_Time_CurrentSysTicks osKernelSysTick

#endif //_TARGET_HAL_TIME_H_
13 changes: 9 additions & 4 deletions targets/CMSIS-OS/ChibiOS/nanoCLR/targetHAL_Time.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ typedef unsigned int UINT32;
typedef signed int INT32;
//////////////////////////////////////////

// Converts ticks (CMSIS sysTicks) to milliseconds
UINT64 HAL_Time_TicksToTimeMilliSec(UINT32 ticks) {
// Converts CMSIS sysTicks to .NET ticks (100ns)
UINT64 HAL_Time_SysTicksToTime(UINT32 sysTicks) {

// this is a rewrite of ChibiOS ST2MS(n) macro because it will overflow if doing the math using uint32_t
return (((ticks) * 1000ULL + (uint64_t)CH_CFG_ST_FREQUENCY - 1ULL) / (uint64_t)CH_CFG_ST_FREQUENCY);
// this is a rewrite of ChibiOS ST2US(n) macro because it will overflow if doing the math using uint32_t

// convert to microseconds from CMSIS SysTicks
uint64_t microsecondsFromSysTicks = (((sysTicks) * 1000000ULL + (uint64_t)CH_CFG_ST_FREQUENCY - 1ULL) / (uint64_t)CH_CFG_ST_FREQUENCY);

// need to convert from microseconds to 100 nanoseconds
return microsecondsFromSysTicks * 10;
}
6 changes: 3 additions & 3 deletions targets/os/mbed-os/nanoBooter/WireProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ bool WP_Message::Process()
}
else
{
// FIXME: m_payloadTicks = HAL_Time_CurrentTicks();
// FIXME: m_payloadTicks = HAL_Time_CurrentSysTicks();
m_rxState = ReceiveState::ReadingPayload;
m_pos = (UINT8*)m_payload;
m_size = m_header.m_size;
Expand Down Expand Up @@ -255,12 +255,12 @@ bool WP_Message::Process()
{
TRACE0(TRACE_STATE, "RxState=ReadingPayload\n");

// FIXME: UINT64 curTicks = HAL_Time_CurrentTicks();
// FIXME: UINT64 curTicks = HAL_Time_CurrentSysTicks();

// If the time between consecutive payload bytes exceeds the timeout threshold then assume that
// the rest of the payload is not coming. Reinitialize to synch on the next header.

// FIXME: if(HAL_Time_TicksToTimeMilliSec(curTicks - m_payloadTicks) < (UINT64)c_PayloadTimeout)
// FIXME: if(HAL_Time_SysTicksToTime(curTicks - m_payloadTicks) < (UINT64)c_PayloadTimeout)
{
// FIXME: m_payloadTicks = curTicks;

Expand Down
2 changes: 1 addition & 1 deletion targets/os/win32/Include/targetHAL_Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//


UINT32 HAL_Time_CurrentTicks();
UINT32 HAL_Time_CurrentSysTicks();



Expand Down
10 changes: 5 additions & 5 deletions targets/os/win32/nanoCLR/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ void HAL_Time_Sleep_MicroSeconds_InterruptEnabled( UINT32 uSec )
// UNDONE: FIXME: return EmulatorNative::GetITimeDriver()->Sleep_MicroSecondsInterruptsEnabled( uSec );
}

UINT32 HAL_Time_CurrentTicks()
UINT32 HAL_Time_CurrentSysTicks()
{
// TODO need to check if using the Win32 100ns ticks works
return 0; // UNDONE: FIXME: EmulatorNative::GetITimeDriver()->CurrentTicks();
}

UINT64 HAL_Time_TicksToTimeMilliSec( UINT32 Ticks )
UINT64 HAL_Time_SysTicksToTime( UINT32 sysTicks )
{
_ASSERTE(Ticks <= 0x7FFFFFFF);
_ASSERTE(sysTicks <= 0x7FFFFFFF);

//No need to go to managed code just to return Time.

// TODO need to convert from whathever ticks are these to milliseconds
return Ticks;
// TODO need to convert from whatever ticks are these to Win32 100ns ticks
return sysTicks;
}

INT64 HAL_Time_CurrentTime()
Expand Down

0 comments on commit 7a465e7

Please sign in to comment.