-
-
Notifications
You must be signed in to change notification settings - Fork 178
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
Conversation
- rename function to HAL_Time_TicksToTimeMicroSec to clarify output - changed the output from 'ticks' (100ns intervals) to miliseconds to follow CMSIS API and because having a time base granularity of 1 milisecond is more than enough (100ns intervals is really too much for an embedded system) this also allows an extra efficiency of using 32bits vars and math for this instead of 64bits - update code accordingly where required - add ChibiOS implementation Signed-off-by: José Simões <jose.simoes@eclo.solutions>
Hi @josesimoes, I'm nanoFramework bot. Everything seems to be in order. |
Sorry, but I don't understand the reason for change and what is the difference. 100ns ticks has nothing to do with system timer granularity. |
Time base (native target) is provided by CMSIS osKernelSysTick() and returns an uint32_t. If we are using this as the time base and the value is 32 bits I don't see a point on artificially 'extending' this to 64 bits just because that was used in NETMF (and in NETMF it's there with 64bits probably because that's what is used in FILETIME ). My point being:
|
Ok, for timeouts (which is the actual use), milliseconds should be enough. Use osKernelSysTick() directly
or wrap the timeout call similarly to ChibiOS' |
Nothing against that. And I do understand it perfectly. |
This you showing an usage example or you are suggesting that this code block should be replacing something? |
Only usage example (how to implement a timeout check). |
@@ -273,7 +273,7 @@ bool WP_Message::Process() | |||
// 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_TicksToTime(curTicks - m_payloadTicks) < (UINT64)c_PayloadTimeout) | |||
// FIXME: if(HAL_Time_TicksToTimeMicroSec(curTicks - m_payloadTicks) < (UINT64)c_PayloadTimeout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see where c_PayloadTimeout was updated (scaled) to retain the behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! And I've took the opportunity to uncomment the code lines handling the timeout.
src/CLR/Core/GarbageCollector.cpp
Outdated
@@ -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_TicksToTime( HAL_Time_CurrentTicks() - stats_start ) + TIME_CONVERSION__TICKUNITS - 1) / TIME_CONVERSION__TICKUNITS; | |||
int milliSec = (int)::HAL_Time_TicksToTimeMicroSec( HAL_Time_CurrentTicks() - stats_start ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
milliSec = microSec?? There is missing scaling (division).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... or the variable name is wrong (misleading).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected calculation to show milliseconds. Should remain that way in order to show a meaningful value.
targets/os/win32/nanoCLR/Time.cpp
Outdated
@@ -22,12 +22,13 @@ UINT64 HAL_Time_CurrentTicks() | |||
return 0; // UNDONE: FIXME: EmulatorNative::GetITimeDriver()->CurrentTicks(); | |||
} | |||
|
|||
INT64 HAL_Time_TicksToTime( UINT64 Ticks ) | |||
INT32 HAL_Time_TicksToTimeMicroSec( UINT32 Ticks ) | |||
{ | |||
_ASSERTE(Ticks <= 0x7FFFFFFFFFFFFFFF); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the value has not been updated to UINT32? This assert is now always true, which was not the original behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected by scaling the value.
//INT64 HAL_Time_TicksToTime(UINT64 Ticks); | ||
//UINT64 HAL_Time_TicksToTime(); | ||
|
||
|
||
UINT64 HAL_Time_CurrentTicks(); | ||
#define HAL_Time_CurrentTicks HAL_Time_CurrentTicks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#define symbol the same? (was already there, not a new change)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
targets/os/win32/nanoCLR/Time.cpp
Outdated
{ | ||
_ASSERTE(Ticks <= 0x7FFFFFFFFFFFFFFF); | ||
|
||
//No need to go to managed code just to return Time. | ||
|
||
// TODO need to convert from ticks to miliseconds | ||
return Ticks; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it does not really work?
This calls for a another adjustment. HAL_Time_CurrentTicks() is a proxy to osKernelSysTick which is uint32_t, so it should be scaled to uint32_t. |
No, use osKernelSysTick() instead (either rename directly, or remove the function declaration and |
But don't forget to find all occurrences and fix possible calculations... |
Not seeing why the return type should remain as UINT64... I know that when using |
??? Just leave it to what osKernelSysTick() has (that is exactly what #define will do). After it is changed to 64 bits, we will decide what to do - but better fix compiler errors later, than introduce nasty casting/wrapping bugs now. |
- adjust return type of HAL_Time_CurrentTicks to uint32_t - update code accordingly - uncomment timeout code in WireProtocol - minor reworks in code per reviewer request Signed-off-by: José Simões <jose.simoes@eclo.solutions>
src/CLR/Core/GarbageCollector.cpp
Outdated
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; |
There was a problem hiding this comment.
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 :)
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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
- as the system clock is in milliseconds it doesn't make sense to be performing the calculations in microseconds - update code accordingly - adjust target ChibiOS config to use 1ms as sys time base - adjust target ChibiOS config to use classic periodic tick (was set to use tick-less) Signed-off-by: José Simões <jose.simoes@eclo.solutions>
// Converts ticks (CMSIS sysTicks) to milliseconds | ||
INT32 HAL_Time_TicksToTimeMilliSec(UINT32 ticks) { | ||
|
||
return ticks * (NANOHAL_TIME_CONVERSION_MICRO_TO_SECONDS / osKernelSysTickFrequency); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about ChibiOS' macro ST2MS()
?
http://chibios.sourceforge.net/docs3/rt/group__time.html#ga38545158d20aed90293e73a970d66b94
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Nice finding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it won't work... There are calls that pass an argument which makes this not suitable. If they were all from sysTick it would, like this it won't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand - ST2MS macro has an argument, return ST2MS(ticks);
(? )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I checked it on a forum question and it looked like there was no argument... So: it fits the purpose. Let's go with this one.
Although I still have the concern that it can "overflow".
- ChibiOS macro STMS(n) will overflow when doing the math with uint32_t Signed-off-by: José Simões <jose.simoes@eclo.solutions>
Signed-off-by: José Simões jose.simoes@eclo.solutions