Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HAL_Time_TicksToTimeMilliSec #145

Merged
merged 4 commits into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/CLR/Core/GarbageCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ CLR_UINT32 CLR_RT_GarbageCollector::ExecuteGarbageCollection()
CLR_RT_ExecutionEngine::ExecutionConstraint_Suspend();

#if defined(NANOCLR_TRACE_MEMORY_STATS)
CLR_UINT64 stats_start = 0;
CLR_UINT32 stats_start = 0;

if(s_CLR_RT_fTrace_MemoryStats >= c_CLR_RT_Trace_Info)
{
Expand All @@ -171,7 +171,9 @@ 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_TicksToTimeMicroSec( HAL_Time_CurrentTicks() - stats_start );
// convert to milliseconds to show a more meaningfull value
// (because of the typical operation time showing it in microseconds could lead to a huge number...)
int milliSec = (::HAL_Time_TicksToTimeMicroSec( HAL_Time_CurrentTicks() - stats_start )) * 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a named constant or conversion macro instead of a magic value (*1000). Much easier to track down next time, when the time unit changes :)


CLR_Debug::Printf( "GC: %dmsec %d bytes used, %d bytes available\r\n", milliSec, m_totalBytes - m_freeBytes, m_freeBytes );
}
Expand Down
4 changes: 2 additions & 2 deletions 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 UINT32 c_PayloadTimeout = 60000000; // 6 secs (100 nsecs units)
static const UINT32 c_PayloadTimeout = 6 * 1000; // 6 secs (from miliseconds time)

WP_Controller* m_parent;
WP_Packet m_header;
Expand All @@ -125,7 +125,7 @@ struct WP_Message
private:
UINT8* m_pos;
UINT32 m_size;
UINT64 m_payloadTicks;
UINT32 m_payloadTicks;
int m_rxState;

public:
Expand Down
8 changes: 5 additions & 3 deletions src/CLR/Include/nanoCLR_Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -2943,8 +2943,8 @@ struct CLR_RT_ExecutionEngine
struct ExecutionConstraintCompensation
{
CLR_INT32 m_recursion;
CLR_INT64 m_start;
CLR_INT64 m_cumulative;
CLR_INT32 m_start;
CLR_INT32 m_cumulative;

void Suspend()
{
Expand All @@ -2967,7 +2967,9 @@ struct CLR_RT_ExecutionEngine

CLR_INT64 Adjust( CLR_INT64 time ) const
{
return time + ::HAL_Time_TicksToTimeMicroSec( m_cumulative );
// need to 'convert' this from microseconds 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_TicksToTimeMicroSec( m_cumulative ) / 10;
}
};

Expand Down
18 changes: 9 additions & 9 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
{
// FIXME: m_payloadTicks = HAL_Time_CurrentTicks();
m_payloadTicks = HAL_Time_CurrentTicks();
m_rxState = ReceiveState::ReadingPayload;
m_pos = (UINT8*)m_payload;
m_size = m_header.m_size;
Expand Down Expand Up @@ -268,14 +268,14 @@ bool WP_Message::Process()
{
TRACE0(TRACE_STATE, "RxState=ReadingPayload\n");

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

// 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_TicksToTimeMicroSec(curTicks - m_payloadTicks) < (UINT64)c_PayloadTimeout)
if(HAL_Time_TicksToTimeMicroSec(curTicks - m_payloadTicks) < c_PayloadTimeout)
{
// FIXME: m_payloadTicks = curTicks;
m_payloadTicks = curTicks;

if(m_parent->m_phy->ReceiveBytes(m_parent->m_state, m_pos, m_size) == false)
{
Expand All @@ -288,11 +288,11 @@ bool WP_Message::Process()
m_rxState = ReceiveState::CompletePayload;
}
}
// FIXME: else
//{
// TRACE0(TRACE_ERRORS, "RxError: Payload InterCharacterTimeout exceeded\n");
// m_rxState = ReceiveState::Initialize;
//}
else
{
TRACE0(TRACE_ERRORS, "RxError: Payload InterCharacterTimeout exceeded\n");
m_rxState = ReceiveState::Initialize;
}
}
break;

Expand Down
3 changes: 2 additions & 1 deletion src/CLR/WireProtocol/WireProtocol_Message.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

uint8_t receptionBuffer[2048];
static uint16_t lastOutboundMessage;
uint32_t m_payloadTicks;
static uint8_t* marker;

//////////////////////////////////////////
Expand Down Expand Up @@ -241,7 +242,7 @@ bool WP_Message_Process(WP_Message* message)
}
else
{
// FIXME: m_payloadTicks = HAL_Time_CurrentTicks();
m_payloadTicks = HAL_Time_CurrentTicks();
message->m_rxState = ReceiveState_ReadingPayload;
message->m_pos = message->m_payload;
message->m_size = message->m_header.m_size;
Expand Down
2 changes: 1 addition & 1 deletion src/HAL/Include/nanoHAL_Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <targetHAL_Time.h>

#define NANOHAL_TIME_CONVERSION_MILISECONDS 1000000
#define NANOHAL_TIME_CONVERSION_MICROSECONDS 1000000

//////////////////////////////////////////
// TODO delete these when working on #130
Expand Down
6 changes: 2 additions & 4 deletions targets/CMSIS-OS/ChibiOS/nanoCLR/targetHAL_Time.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// See LICENSE file in the project root for full license information.
//

//#include <stdint.h>
//#include <cmsis_os.h>
#include <nanoHAL_Time.h>

//////////////////////////////////////////
Expand All @@ -13,8 +11,8 @@ typedef unsigned int UINT32;
typedef signed int INT32;
//////////////////////////////////////////

// Converts ticks (CMSIS sysTicks) in miliseconds
// Converts ticks (CMSIS sysTicks) to microseconds
INT32 HAL_Time_TicksToTimeMicroSec(UINT32 ticks) {
// T[s] = 1 / f[Hz]
return (ticks / (osKernelSysTickFrequency * NANOHAL_TIME_CONVERSION_MILISECONDS));
return (ticks / (osKernelSysTickFrequency * NANOHAL_TIME_CONVERSION_MICROSECONDS));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this tested? For STM32F4-Discovery

 #define CH_CFG_ST_FREQUENCY                 10000

the multiplication overflows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad you spot this... This made me realize that we don't need micro seconds calculation for timeouts because the sys clock is... milliseconds!

}
4 changes: 2 additions & 2 deletions targets/os/win32/Include/targetHAL_Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
//


UINT64 HAL_Time_CurrentTicks();
#define HAL_Time_CurrentTicks HAL_Time_CurrentTicks
UINT32 HAL_Time_CurrentTicks();




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

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

INT32 HAL_Time_TicksToTimeMicroSec( UINT32 Ticks )
{
_ASSERTE(Ticks <= 0x7FFFFFFFFFFFFFFF);
_ASSERTE(Ticks <= 0x7FFFFFFF);

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

Expand Down