From 7a465e7dd9701f2f9d2caf3b1549c2649e86fc00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Thu, 9 Mar 2017 12:05:25 +0000 Subject: [PATCH] Rework HAL time functions (#160) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - see #159 Signed-off-by: José Simões --- src/CLR/Core/GarbageCollector.cpp | 4 ++-- src/CLR/Include/WireProtocol.h | 2 +- src/CLR/Include/nanoCLR_Runtime.h | 9 ++++----- src/CLR/WireProtocol/WireProtocol.cpp | 6 +++--- src/CLR/WireProtocol/WireProtocol_Message.c | 2 +- src/HAL/Include/nanoHAL_Time.h | 3 +-- targets/CMSIS-OS/ChibiOS/Include/targetHAL_Time.h | 2 +- targets/CMSIS-OS/ChibiOS/nanoCLR/targetHAL_Time.c | 13 +++++++++---- targets/os/mbed-os/nanoBooter/WireProtocol.cpp | 6 +++--- targets/os/win32/Include/targetHAL_Time.h | 2 +- targets/os/win32/nanoCLR/Time.cpp | 10 +++++----- 11 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/CLR/Core/GarbageCollector.cpp b/src/CLR/Core/GarbageCollector.cpp index f43d985f40..8f4e81a94e 100644 --- a/src/CLR/Core/GarbageCollector.cpp +++ b/src/CLR/Core/GarbageCollector.cpp @@ -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 @@ -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 ); } diff --git a/src/CLR/Include/WireProtocol.h b/src/CLR/Include/WireProtocol.h index 20d0ef2439..7cce8daf38 100644 --- a/src/CLR/Include/WireProtocol.h +++ b/src/CLR/Include/WireProtocol.h @@ -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; diff --git a/src/CLR/Include/nanoCLR_Runtime.h b/src/CLR/Include/nanoCLR_Runtime.h index 0b83cdac49..b911af901c 100644 --- a/src/CLR/Include/nanoCLR_Runtime.h +++ b/src/CLR/Include/nanoCLR_Runtime.h @@ -2939,7 +2939,7 @@ struct CLR_RT_ExecutionEngine { if(m_recursion++ == 0) { - m_start = HAL_Time_CurrentTicks(); + m_start = HAL_Time_CurrentSysTicks(); } } @@ -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 ); } }; diff --git a/src/CLR/WireProtocol/WireProtocol.cpp b/src/CLR/WireProtocol/WireProtocol.cpp index 25bb3f307a..08be360ad2 100644 --- a/src/CLR/WireProtocol/WireProtocol.cpp +++ b/src/CLR/WireProtocol/WireProtocol.cpp @@ -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; @@ -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; diff --git a/src/CLR/WireProtocol/WireProtocol_Message.c b/src/CLR/WireProtocol/WireProtocol_Message.c index aa890b391e..80f2ac58c0 100644 --- a/src/CLR/WireProtocol/WireProtocol_Message.c +++ b/src/CLR/WireProtocol/WireProtocol_Message.c @@ -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; diff --git a/src/HAL/Include/nanoHAL_Time.h b/src/HAL/Include/nanoHAL_Time.h index 69dc28eab8..a9f184669f 100644 --- a/src/HAL/Include/nanoHAL_Time.h +++ b/src/HAL/Include/nanoHAL_Time.h @@ -9,7 +9,6 @@ #include #define NANOHAL_TIME_CONVERSION_MICRO_TO_SECONDS 1000000 -#define NANOHAL_TIME_CONVERSION_MICRO_TO_HUNDREDS_NANOSECONDS 10000 ////////////////////////////////////////// // TODO delete these when working on #130 @@ -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 } diff --git a/targets/CMSIS-OS/ChibiOS/Include/targetHAL_Time.h b/targets/CMSIS-OS/ChibiOS/Include/targetHAL_Time.h index 1c2296fc0c..8808317877 100644 --- a/targets/CMSIS-OS/ChibiOS/Include/targetHAL_Time.h +++ b/targets/CMSIS-OS/ChibiOS/Include/targetHAL_Time.h @@ -8,6 +8,6 @@ #include -#define HAL_Time_CurrentTicks osKernelSysTick +#define HAL_Time_CurrentSysTicks osKernelSysTick #endif //_TARGET_HAL_TIME_H_ diff --git a/targets/CMSIS-OS/ChibiOS/nanoCLR/targetHAL_Time.c b/targets/CMSIS-OS/ChibiOS/nanoCLR/targetHAL_Time.c index b57333cf7c..732a162f8b 100644 --- a/targets/CMSIS-OS/ChibiOS/nanoCLR/targetHAL_Time.c +++ b/targets/CMSIS-OS/ChibiOS/nanoCLR/targetHAL_Time.c @@ -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; } diff --git a/targets/os/mbed-os/nanoBooter/WireProtocol.cpp b/targets/os/mbed-os/nanoBooter/WireProtocol.cpp index 1193dc293b..c3a9f96494 100644 --- a/targets/os/mbed-os/nanoBooter/WireProtocol.cpp +++ b/targets/os/mbed-os/nanoBooter/WireProtocol.cpp @@ -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; @@ -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; diff --git a/targets/os/win32/Include/targetHAL_Time.h b/targets/os/win32/Include/targetHAL_Time.h index fcf7430287..a70ec2ea4d 100644 --- a/targets/os/win32/Include/targetHAL_Time.h +++ b/targets/os/win32/Include/targetHAL_Time.h @@ -24,7 +24,7 @@ // -UINT32 HAL_Time_CurrentTicks(); +UINT32 HAL_Time_CurrentSysTicks(); diff --git a/targets/os/win32/nanoCLR/Time.cpp b/targets/os/win32/nanoCLR/Time.cpp index ff20ef5d0f..32343ed2d7 100644 --- a/targets/os/win32/nanoCLR/Time.cpp +++ b/targets/os/win32/nanoCLR/Time.cpp @@ -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()